Last active
September 29, 2016 08:40
-
-
Save ehgoodenough/97b078aebdb63df57468 to your computer and use it in GitHub Desktop.
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 Firebase = require("firebase") | |
class Leaderboard { | |
constructor(firebaseURL) { | |
this.firebase = new Firebase(firebaseURL) | |
this.firebase.orderByChild("score").limitToLast(5).on("value", (value) => { | |
this.scores = value.val() | |
}) | |
} | |
add(data) { | |
if(!data.name || data.name.length > 3) { | |
throw new Error("Requires a valid name.") | |
} else if(!data.score || isNaN(data.score)) { | |
throw new Error("Requires a valid score.") | |
} else { | |
this.firebase.push(data).setPriority(data.score) | |
} | |
} | |
get() { | |
return this.scores | |
} | |
} |
To use it, just pass it a URL to your firebase.
var leaderboard = new Leaderboard("https://ehgoodenough.firebaseio.com/scores")
leaderboard.add({name: "ACM", score: 300})
var scores = leaderboard.get()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to let your players share their highscores through an online leaderboard, but you don't want to initiate and maintain the servers, you can just use Firebase! It's a real-time data-store, and it's totally awesome.
I wrote this snippet for adding scores, and returning the top five scores.