Created
March 1, 2016 09:45
-
-
Save bartvanremortele/26277a18ab555d973583 to your computer and use it in GitHub Desktop.
Testing Customer Support from Firebase :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 myBids =player.child("myBidsList"); | |
var ui = $('#interface'); | |
var hash = window.location.hash.replace(/#/g, ''); | |
if (hash) { | |
$(body).removeClass("edit").addClass("play"); | |
var thisAuction = auctions.child(hash); | |
var thisAuctionBids = thisAuction.child("bids"); //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().time); //countdown defined by creator | |
if ($('#countDownTimer').val() ===0){ alert("Auction is not active yet");} | |
} | |
}); | |
ui.on('click','#doBid', function(event) { | |
var currentBestBid = $('#currentBestBid').val(); | |
var increment = $('#incrementInput').val(); | |
var currentBid = currentBestBid+increment; | |
if (!increment) { alert("error, something is missing");} else | |
{ | |
var myBid = { | |
bid: currentBid, | |
me: player.avatar.Val(), | |
moment:Firebase.ServerValue.TIMESTAMP | |
}; | |
thisAuctionBids.push(myBid); | |
myBids.push(myBid); | |
} }); //push offer/bid | |
thisAuctionBids.limit(3).on('value', function(snapshot) { | |
snapshot.forEach(function(childSnapshot) { | |
$('#bidsList').prepend('<div class="offer"><img src="'+childSnapshot.val().avatar+'"></div>'); | |
}); | |
}); | |
//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 bidders = $('#biddersInput').val(); | |
var time = $('#countdownInput').val(); | |
var maxBid = $('#maxBidInput').val(); | |
var increment = $('#incrementInput').val(); | |
var passprotected = $('#passwordInput').val(); | |
var generateUrl = auctions.push(); // generate unique url location | |
var url = generateUrl.name(); | |
console.log(url); | |
var generatedUrl= $("#urlInput").val(url); | |
var newAuction = { | |
bidders: bidders, | |
time: time, | |
maxBid: maxBid, | |
increment:increment, | |
password:passprotected, | |
url: 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