-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
152 lines (124 loc) · 3.9 KB
/
server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var express = require('express'),
redis = require('redis'),
JSONStream = require('JSONStream'),
base32 = require('base32'),
app = express(),
client = redis.createClient({
host: process.env.REDIS_HOST || 'localhost'
}),
thisPlayerId;
app.set('port', 3000);
app.use(express.static(__dirname));
app.use(express.urlencoded());
app.use(express.json());
// inits the game, returns the gameId back to the client
app.post("/init", function(req, res, next){
var data = base32.decode(req.body['data']).split('+');
client.incr('game.id', function(err, id){
var gameId = 'game:'+id;
client.hmset(gameId+':init', {time: data[0]}, function(err, response){
res.send({id: gameId});
})
});
})
// the client pings when reaching mid-game
app.post("/ping", function(req, res, next){
var data = base32.decode(req.body['data']).split('+');
var gameId = data[1];
client.hmset(gameId+':ping', {time: data[0], score: data[2]}, function(err, response){
res.send({id: gameId});
})
})
var validateRank = function(req, res, next){
// rank validation - Date.now()+"+"+gameServerId+"+"+this.value+"+"+gameTime.toFixed(3)
var data = base32.decode(req.body['data']).split("+");
var gameTime = data[3],
gameId = data[1];
client.zrange(['s', 5, 6, 'WITHSCORES'], function(err, response) {
if(response.length){
var maxTopTime = response[1];
// a time that will make the rankings
if(gameTime < maxTopTime){
client.hget([gameId+':init', 'time'], function(err, init){
var startTs = init;
client.hget([gameId+':ping', 'time'], function(err, ping){
var pingTs = ping;
if(!startTs || !pingTs ||
parseInt(pingTs) - parseInt(startTs) > (gameTime*1000)){
res.send(500);
} else {
next();
}
});
})
} else {
next();
}
} else {
next();
}
})
}
// the final rank post
app.post("/rank", validateRank, function(req, res){
var data = base32.decode(req.body['data']);
var arr = data.split("+");
res.send(
insertPlayerInfo(arr[1], arr[2], arr[3])
);
})
client.on("error", function(err){
console.log("Error: " + err);
})
client.setnx('game.id', 0);
app.get('/rank/:gameTime', function(req, res){
var before,
after,
gameTime = parseFloat(req.params['gameTime']),
retObj = {rank: []},
rank = 1,
multi = client.multi();
// getting the top 5
client.zrangebyscore(['s', '-inf', '+inf', 'WITHSCORES', 'LIMIT', 0, 5], function(err, response){
for(var i=0; i<response.length; i+=2){
multi.hget(response[i], 'name');
}
// now the names...
multi.exec(function(err, replies){
for(var i=0, j=0; i<response.length; i+=2, j++){
response[i] = replies[j];
retObj.rank.push({rank: rank, name: replies[j], gameTime: response[i+1]})
rank++;
}
// top 5 done
// getting before and after players...
client.zrangebyscore(['s', gameTime, '+inf', 'WITHSCORES', 'LIMIT', 0, 1], function(err, response){
client.zrank(['s', response[0]], function(err, rank){
client.hget([response[0], 'name'], function(err, name){
if(name)
retObj['after'] = {name: name, gameTime: response[1], rank: rank+1};
client.zrevrangebyscore(['s', '('+gameTime, '-inf', 'WITHSCORES', 'LIMIT', 0, 1], function(err, response){
client.zrank(['s', response[0]], function(err, rank){
client.hget([response[0], 'name'], function(err, name){
if(name)
retObj['before'] = {name: name, gameTime: response[1], rank: rank+1};
res.send(retObj);
});
});
});
});
});
});
});
});
})
function insertPlayerInfo(gameId, name, gameTime){
client.zadd(['s', gameTime, gameId], function(err, response){
client.hmset(gameId, {name: name}, function(err, response){
return {};
});
});
}
module.exports = app.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});