Skip to content

Instantly share code, notes, and snippets.

@katowulf
Created June 26, 2013 16:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save katowulf/5868739 to your computer and use it in GitHub Desktop.
var authClient = new FirebaseAuthClient(db, function(error, user) {
if (error) {
alert(error);
} else if (user) {
$(body).removeClass("noAuth").addClass("auth");
var db = new Firebase('https://xxxxxx.firebaseio.com');
var auctions = db.child("auctionsList");
var player = db.child("users").child(user.id);
var myAuctions =player.child("myAuctionsList");
var ui = $('#interface');
var hash = window.location.hash.replace(/#/g, '');
if (hash) {
$(body).removeClass("edit").addClass("play");
var thisAuction = auctions.child(hash);
var thisAuctionOffers = thisAuction.child("offers"); //record of all offers/bids
var thisAuctionComments = thisAuction.child("comments")
// IF 50 PLAYERS HAVE JOINED THE AUCTION THE AUCTION IS NO LONGER AVAILABLE.SHOULD I USE PRESENCE TO ACHIEVE THIS GOAL?
var onlineRef = player.child("online");
var connectedRef = db.child(".info/connected");
connectedRef.on('value', function(snap) {
if (snap.val() === true) {
onlineRef.onDisconnect().set(Firebase.ServerValue.TIMESTAMP);
onlineRef.set(true);
}
});
//PRINT TOTAL AMOUNT PLAYERS CURRENTLY ONLINE TO AN INPUT
connectedRef.on('value', function(event) {
var playersConnected = connectedRef.numChildren();
$('#playersConnected').val(playersConnected);
});
// LOAD DATA AND SET THE AUCTION PREFERENCES. USING ONCE() IS OK?
thisAuction.once('value', function(snapshot) {
$('#incrementInput').val(snapshot.val().increment); //increment defined by creator
$('#currentBestOffer').val(0); //starter price is always 0
$('#countDownTimer').val(snapshot.val().countdown); //countdown defined by creator
if ($('#countDownTimer').val() ===0){ alert("Auction is not active yet");}
}
});
// BIG DOUBT SHOULD I PUSH THE BID/OFFER OR BETTER USE TRANSACTION ?
ui.on('click','#doOffer', function(event) {
var currentBestOffer = $('#currentBestOffer').val();
var increment = $('#incrementInput').val();
var currentOffer = currentBestOffer+increment;
if (!increment) { alert("error, something is missing");} else
{
var myOffer = {
offer: currentOffer,
me: player.avatar.Val(),
moment:Firebase.ServerValue.TIMESTAMP
};
thisAuctionOffers.push(myOffer);
} }); //push offer/bid
// LIST THE 3 LATEST/HIGHER OFFERS BY PLAYERS. SORT OF A LEADERBOARD BUT AS TIMESTAMP (AND NOT SCORE) IS WHAT WE LOOK FOR NO //NEED FOR USING 'PRIORITY' RIGHT?
thisAuctionOffers.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
$('#offersList').prepend('<div class="offer"><img src="'+childSnapshot.val().avatarUrl+'"></div>');
}).limit(3); // should limit() be placed before .on('value') ?
});
//list comments
thisAuctionComments.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
$('#commentsList').prepend('<div class="comment"><h2>'+childSnapshot.val().message+'</h2><small>'+childSnapshot.val().me+'</small></div>');
});
});
// PUSH COMMENT
ui.on('click','#doComment', function(event) {
var message = $('#messageInput').val();
if (!message) { alert("Error, message is empty");} else
{
var myComment = {
me:player.me.val(),
message: message
};
thisAuctionComments.push(myComment);
} }); //push comment
}
else{ // IF NO HASH IS FOUND
// CREATE AUCTION
ui.on('click','#doCreate', function(event) {
var playerLimit = $('#playerLimitInput').val();
var countdown = $('#countdownInput').val();
var maxOffer = $('#maxOfferInput').val();
var increment = $('#incrementInput').val();
var passprotected = $('#passprotectedInput').val();
var generateUrl = auctions.push(); // generate unique url location
var url = generateUrl.name();
console.log(url);
var generatedUrl= $("#urlInput").val(url);
var newAuction = {
playerLimit: playerLimit,
countdown: countdown,
maxOffer: maxOffer,
increment:increment,
passprotected:passprotected,
generatedUrl: generatedUrl
};
auctions.push(newAuction);
myAuctions.push(newAuction);
});
// LIST MY CREATED AUCTIONS
myAuctions.on('child_added', function(auctionSnapshot) {
listAuctions(auctionSnapshot.val(), auctionSnapshot.name());
});
function listAuctions(auction, name) {
var myAuctionsList = $('#myAuctions');
myAuctionsList.append('<p>' + auction.name + ', ' + auction.generatedUrl + '</p>');
}
// CREATOR ACTIVATES AUCTION ON CLICK
ui.on('click','#doStartAuction', function(event) {
// TO DO :
//how can I sync with firebase timestamp so that all players 'live' the same countdown? I've played with moment.js, any tips?
//when countdown reaches 0 two things happen:
// winner is set something like thisAuction.set(winner: user.id ) ?
// publish on my Facebook timeline if I've won or lost the bet
});
//SET PROFILE USERNAME AND IMG AVATAR
ui.on('click','#saveProfile', function(event) {
var username = $('#usernameInput').val();
var avatarUrl = $('#avatarInput').val();
if (!username) { alert("Please provide a name");} else
{
var profileEdit = {
me: username,
avatar: avatarUrl
};
player.set(profileEdit); //should I give a default image avatar and do an update() instead ?
}
});
}
ui.on('click','#doLogout', function(event) {
authClient.logout(); });
} else{ //not logged in
$('#doLogin').on('click', function(event) {
// TO DO:
// login logic
}); //AUTH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment