bartvanremortele
3/1/2016 - 9:46 AM

Arigato Gozaimas Kato

Arigato Gozaimas Kato

  

//Load auction data from Firebase (FB) 

thisAuction.on('value',  function(snapshot) {  
var setTime = snapshot.val().times; 
var setIncrement = snapshot.val().increment;     
var setItemImage = snapshot.val().image;     
var setItem = snapshot.val().item;     
var setItemDescription = snapshot.val().description;     
var setBidders = snapshot.val().bidders;     
var setBestBid = snapshot.val().bestBid; 

$('#timer').text(setTime); //countdown defined by creator 

// if (($('#timer').text())*1 ===0){ alert("Auction is not active yet");}                   
$('#itemIncrement').text(setIncrement); 
$('#itemImage').attr("src",setItemImage); 
$('#itemDescription').text(setItemDescription); 
$('#itemName').text(setItem); 
$('#itemBidders').text(setBidders); 
$('#currentBestBid').text(setBestBid); 
                                     
  
});   


//Each time user clicks beats current amount by a constant increment defined by auction creator
 ui.on('click','#doBid',   function(event) {

var currentBestBid = ($('#currentBestBid').text())*1;
var increment = ($('#itemIncrement').text())*1;
var currentBid = currentBestBid+increment;  //What if 'n' users are pushing at the same time ? Will data be up to date?                            
var pushAvatar = $('#playerAvatarProfile').find("img").attr('src');   

var myBid = {
			bid: currentBid,
	me: pushAvatar,
           moment:Firebase.ServerValue.TIMESTAMP
		 
		};

		thisAuctionBids.push(myBid);  //public
  	myBids.push(myBid); //private 

//update the bestBid value of the auction so that cycle can start all over again 
// should we wrap this call in a transaction to ensure the bestBid value is updated to all clients properly? 
		thisAuction.update({bestBid:currentBid})
       

 });   //push offer/bid    





 // CREATE AUCTION
      ui.on('click','#doCreate', function(event) {
  	var itemName = $('#itemName').val();
     	var itemDescription = $('#itemDescription').val();
  	var itemUrl = $('#itemUrl').val();

  	var bidders = $('#biddersInput').val();
		var time = $('#timeInput').val();
		var maxBid = $('#maxBidInput').val();
	   	var increment = $('#incrementInput').val();
       	var passprotected = $('#passwordInput').val();  



 // This 3 next lines crashes the browser * check comment below 
var generateUrl = auctions.push();
var url = generateUrl.name();
var generatedUrl= $("#urlInput").val(url);

         
	var newAuction = {   
			admin: user.id, // push my user.id as admin
			item: itemName,
			 description:itemDescription,
			image: itemUrl,
			bidders: bidders,
			times: time,
		    maxBid: maxBid,
           increment:increment,
            password:passprotected
    , url: generatedUrl
       };
        
 // *setTimeout prevents the browser from crashing but feels very dirty :( 

           setTimeout(function() {
	
			auctions.push(newAuction);  
       myAuctions.push(newAuction); 
}, 5000);
     
  });                
               
                


// on loading auction details check if user is the admin

thisAuction.on('value',  function(snapshot) {  
var admin= snapshot.val().admin; 
//if user is admin
if (user.id===admin){
//change ui (show a button that on click starts the auction)  
}

});
    
//is this a proper way of doing it? Security rules are the answer ? I'm not 'getting it' yet. http://stackoverflow.com/questions/14491496/granting-access-to-firebase-locations-to-a-group-of-users?rq=1 looks a bit too much for what I'm aiming for