Skip to content

Instantly share code, notes, and snippets.

@ehgoodenough
Last active September 29, 2016 08:40
Show Gist options
  • Save ehgoodenough/97b078aebdb63df57468 to your computer and use it in GitHub Desktop.
Save ehgoodenough/97b078aebdb63df57468 to your computer and use it in GitHub Desktop.
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
}
}
@ehgoodenough
Copy link
Author

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