Repository navigation
Expand file tree
/
Copy pathleaderboard.js
More file actions
95 lines (86 loc) · 3.13 KB
/
Copy pathleaderboard.js
File metadata and controls
95 lines (86 loc) · 3.13 KB
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
89
90
91
92
93
94
95
var mongodb = require("mongodb"),
mongoserver = new mongodb.Server("localhost", 27017, {'auto_reconnect' : true}),
db_connector = new mongodb.Db("leaderboard", mongoserver, {safe : false}),
mustache = require("mustache"),
fs = require("fs")
var html_template = fs.readFileSync("./templates/leaderboard.mustache").toString()
var lb_collection
db_connector.open(function(err, db) {
if (err) {
console.log("Error connecting to database: " + err)
console.log("Leaderboard will not function for this session.")
return
}
console.log("Connected to MongoDB!")
db.createCollection("leaderboard", function(err, collection) {//This will create the collection if it doesn't exist.
lb_collection = collection
});
});
/**
* Call this guy to get the current leaders.
*
* @param {Object} callback (err, leaders) returns (err, null) on failure, (null, leaders) on success.
* Leaders are pretty fresh from the database.
*/
module.exports.getLeaders = function(callback) {
var cursor = lb_collection.find({});
cursor.toArray(function(err, documents) {
if (err) return callback(err, null)
callback(null, documents)
})
}
/**
* Returns an HTML rendering of the current leaderboard.
*
* @param {Object} callback (err, leaders) returns (err, null) on failure, (null, leaders) on success.
*/
module.exports.getLeadersHTML = function(callback) {
module.exports.getLeaders(function(err, leaders) {
if (err) return callback(err, null)
table = mustache.render(html_template, {leaders : leaders})
callback(null, table)
})
}
/**
* Parses the gameState and adds the scores to the DB.
* Call this once at the end of a game.
*
* Here's some example data I used for testing this function!
* state = {engine: {name : "Test Bot", score : 150}, phones: {'test1' : {name : "Test 1", score : 200}, 'test2' : {score : 200}}}
*
* @param {Object} gameState Current state of the game
* @param {Object} callback Calls back with (error) if there are problems.
*/
module.exports.addLeaders = function(gameState, callback) {
var scoreList = []
var phones = gameState.phones
var keys = Object.keys(phones)
for( var i = 0; i < keys.length; i++ ) {
var p_id = keys[i] //phone ID
var phone = phones[p_id]
var name = p_id
if (phone.screenname) name = phone.screenname
var score = 0
if (phone.score != undefined) score = phone.score
//Only add this phone's score to the DB if they scored over 0 points
if(score > 0){
scoreList.push({type : "phone", phoneid : p_id, name : name, score : score})
}
}
var engine = gameState.engine
var botScore = 0
var botName = "NoName Robot"
if (engine.player.score) botScore = engine.player.score
if (engine.player.screenname) botName = engine.player.screenname
//Only add this bot's score to the DB if they scored over 0 points
if(botScore > 0){
scoreList.push({type : "bot", name : botName, score : botScore})
}
//Only insert the score list if there are values in it
if(scoreList.length > 0){
lb_collection.insert(scoreList, {safe: true}, function(err, records){
if (err) return callback(err)
callback(null)
})
}
}