-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
88 lines (72 loc) · 3.46 KB
/
firebase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var userName;
/* FACEBOOK LOGIN */
var loginRef = new Firebase('https://phaser-flappy-bird.firebaseio.com/');
var auth = new FirebaseSimpleLogin(loginRef, function(error, user) {
if (error) {
$('.facebook-status').html('Ocorreu algum erro, você não está logado no Facebook.');
console.log(error);
} else if (user) {
// user authenticated with Firebase
userName = user.name;
console.log(user);
$('.facebook-status').html('Você está logado no Facebook, ' + user.name + '.');
} else {
// user is logged out
}
});
/* RANKING */
var LEADERBOARD_SIZE = 10;
// Create our Firebase reference
var scoreListRef = new Firebase('https://phaser-flappy-bird.firebaseio.com//scoreList');
// Keep a mapping of firebase locations to HTML elements, so we can move / remove elements as necessary.
var htmlForPath = {};
// Helper function that takes a new score snapshot and adds an appropriate row to our leaderboard table.
function handleScoreAdded(scoreSnapshot, prevScoreName) {
var newScoreRow = $("<tr/>");
newScoreRow.append($("<td/>").append($("<em/>").text(scoreSnapshot.val().name)));
newScoreRow.append($("<td/>").addClass('new-score').text(scoreSnapshot.val().score));
// Store a reference to the table row so we can get it again later.
htmlForPath[scoreSnapshot.name()] = newScoreRow;
// Insert the new score in the appropriate place in the table.
if (prevScoreName === null) {
$("#leaderboardTable").append(newScoreRow);
}
else {
var lowerScoreRow = htmlForPath[prevScoreName];
lowerScoreRow.before(newScoreRow);
}
}
// Helper function to handle a score object being removed; just removes the corresponding table row.
function handleScoreRemoved(scoreSnapshot) {
var removedScoreRow = htmlForPath[scoreSnapshot.name()];
removedScoreRow.remove();
delete htmlForPath[scoreSnapshot.name()];
}
// Create a view to only receive callbacks for the last LEADERBOARD_SIZE scores
var scoreListView = scoreListRef.limit(LEADERBOARD_SIZE);
// Add a callback to handle when a new score is added.
scoreListView.on('child_added', function (newScoreSnapshot, prevScoreName) {
handleScoreAdded(newScoreSnapshot, prevScoreName);
});
// Add a callback to handle when a score is removed
scoreListView.on('child_removed', function (oldScoreSnapshot) {
handleScoreRemoved(oldScoreSnapshot);
});
// Add a callback to handle when a score changes or moves positions.
var changedCallback = function (scoreSnapshot, prevScoreName) {
handleScoreRemoved(scoreSnapshot);
handleScoreAdded(scoreSnapshot, prevScoreName);
};
scoreListView.on('child_moved', changedCallback);
scoreListView.on('child_changed', changedCallback);
// When the user presses enter on scoreInput, add the score, and update the highest score.
var updateScore = function () {
var newScore = game_score.player.score;
var userScoreRef = scoreListRef.child(userName);
scoreListRef.on('value', function(snapshot) {
var userScore = snapshot.child(userName).val();
if (userScore && userScore.score > newScore) return;
// Use setWithPriority to put the name / score in Firebase, and set the priority to be the score.
userScoreRef.setWithPriority({ name:userName, score:newScore }, newScore);
});
};