forked from octachrome/treason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataaccess-couch.js
542 lines (499 loc) · 18.9 KB
/
dataaccess-couch.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Copyright 2015-2016 Christopher Brown and Jackie Niebling.
*
* This work is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/ or send a letter to:
* Creative Commons
* PO Box 1866
* Mountain View
* CA 94042
* USA
*/
'use strict';
var crypto = require('crypto');
var cradle = require('cradle');
var pr = require('promise-ring');
var ms = require('ms');
var debug = require('debug')('dataaccess');
var rankingsDisabled = false;
var connection = new cradle.Connection();
var treasonDb;
var gameStatsDocumentId = 'game_stats';
var stats = null;
var allPlayers = {};
function statsInitialized() {
return new Promise(function (resolve) {
(function waitForInitialStatsToBeGenerated(){
if (stats != null) {
return resolve();
}
setTimeout(waitForInitialStatsToBeGenerated, 30);
})();
});
}
var playerRanksToReturn = 10;
//View recreation
var gameVersionsDocumentId = 'game_versions';
var updateViews = false;
var recreateViews = false;
//NOTE: If you update any view, also increment this version
var currentViewVersion = 1;
// This ensures that unit tests do not try to record stats.
var ready = Promise.reject(new Error('not initialized'));
function init(dbname, options) {
options = options || {};
recreateViews = options.recreateViews || false;
playerRanksToReturn = options.ranksToReturn || 10;
treasonDb = pr.wrapAll(connection.database(dbname));
ready = treasonDb.exists().then(function (exists) {
if (!exists) {
debug('Creating database');
return treasonDb.create();
}
}).then(function () {
if (!rankingsDisabled) {
debug('Database is up. Checking if views should be recreated');
return treasonDb.get(gameVersionsDocumentId)
.then(function (result) {
if (result.currentViewVersion != currentViewVersion) {
updateViews = true;
} else {
debug('View version is up to date');
}
})
.catch(function () {
return treasonDb.save(gameVersionsDocumentId, {
currentViewVersion: currentViewVersion
}).then(function () {
debug('Current view version document not found in database, created it');
updateViews = true;
}).catch(function (error) {
console.error('Failed to create initial current view version document');
console.error(error);
});
});
}
}).then(function () {
if (recreateViews || updateViews) {
debug('Recreating views because ' + (updateViews ? 'view version was updated' : 'of --recreate-views'));
return treasonDb.save('_design/games', {
by_winner: {
map: function (doc) {
if (doc.type === 'game' && doc.playerRank && doc.playerRank[0] && doc.playerRank[0] !== 'ai') {
emit(doc.playerRank[0], doc.humanPlayers);
}
},
reduce: function (keys, values, rereduce) {
var stats = {
wins: 0,
winsAI: 0
};
if (rereduce) {
for (var i = 0; i < values.length; i++) {
stats.wins += values[i].wins;
stats.winsAI += values[i].winsAI;
}
return stats;
}
stats.wins = values.length;
values.forEach(function(value) {
if (value < 2) {
stats.winsAI++;
}
});
return stats;
}
},
by_player: {
map: function (doc) {
if (doc.type === 'game' && doc.playerRank) {
doc.playerRank.forEach(function (playerId) {
if (playerId && playerId !== 'ai') {
emit(playerId);
}
});
}
},
reduce: function (keys, values, rereduce) {
if (rereduce) {
return sum(values);
}
else {
return values.length;
}
}
},
all_players: {
map: function (document) {
if (document.type === 'player') {
emit(null, document.name);
}
}
}
}).then(function() {
return treasonDb.merge(gameVersionsDocumentId, {
currentViewVersion: currentViewVersion
}).then(function () {
debug('Updated current view version to ' + currentViewVersion);
}).catch(function (error) {
console.error('Failed to update current view version.');
console.error(error);
throw error;
});
}).then(function() {
//Exercise a view. This will rebuild all the views and can take some time
debug('Initialising views');
return treasonDb.view('games/all_players').then(function() {
debug('Views initialized');
});
});
}
}).then(function() {
debug('Finished initialising views');
if (!rankingsDisabled) {
debug('Attempting to load game stats document');
return treasonDb.get(gameStatsDocumentId)
.then(function (document) {
debug('Found game stats document');
stats = document.gameStats;
})
.catch(function () {
debug('Game stats document not found');
return calculateAllStats().then(function () {
debug('Stats generated, creating game stats document');
return treasonDb.save(gameStatsDocumentId, {
gameStats: stats
}).catch(function (error) {
console.error('Failed to create game stats document');
console.error(error);
});
});
});
}
}).then(function() {
debug('Database is ready');
}).catch(function(error) {
console.error('Failed to initialise database(s)');
console.error(error);
process.exit(1);
});
return ready;
}
function register(id, name, userAgent) {
return ready.then(function() {
debug('Player ' + name + ', trying to register with id ' + id);
if (!id || id === 'ai') {
id = -1;
}
if (!name) {
debug('Player given a dummy name');
name = "JohnDoe";
}
return treasonDb.get(id)
.then(function (result) {
if (result.name != name || userAgent != result.userAgent) {
debug('Updating player ' + result.name + ' (new name ' + name + ')');
return treasonDb.merge(id, {
name: name,
userAgent: userAgent
}).then(function (result) {
debug('Updated playerId ' + id);
}).catch(function (error) {
console.error('Failed to update player.');
console.error(error);
});
}
})
.then(function () {
debug('Existing player ' + name + ' logged in with id ' + id);
})
.catch(function () {
//failed to find the player, recreate with new id
debug('Id ' + id + ' not recognised, recreating');
id = crypto.randomBytes(16).toString('hex');
debug('Saving new id ' + id + ' for player ' + name);
return treasonDb.save(id, {
type: 'player',
name: name,
userAgent: userAgent
}).then(function (result) {
debug('Allocated new id ' + id + ' to player: ' + name);
}).catch(function (error) {
console.error('Failed to save player');
console.error(error);
return treasonDb.get(id).then(function() {
debug('Collision detected, retrying with new id');
return register(null, name);
}).catch(function(error) {
console.error(error);
throw error;
});
});
})
.then(function() {
allPlayers[id] = name;
return id;
});
});
}
/**
* Captures statistics about each game.
* PlayerRank is ordered by the first outgoing player being last in the array, with the winner first and no disconnects.
* @type {{players: number, humanPlayers: number, playerRank: Array, playerDisconnect: Array, gameStarted: number, gameFinished: number, gameType: string}}
*/
function constructGameStats() {
return {
players: 0,
humanPlayers: 0,
type: 'game',
playerRank: [],
playerDisconnect: [],
gameStarted: new Date().getTime(),
gameFinished: 0,
gameType: 'original'
};
}
function recordGameData(gameData) {
return ready.then(function () {
gameData.gameFinished = new Date().getTime();
return treasonDb.save(gameData).then(function (result) {
debug('Saved game data for game: ' + result._id);
if (!rankingsDisabled) {
updateResults(gameData);
}
}).catch(function (error) {
console.error('failed to save game data');
console.error(error);
});
});
}
function recordPlayerDisconnect(playerId) {
return ready.then(function () {
return treasonDb.get(playerId)
.then(function (player) {
if (player) {
debug('Updating disconnects for player: ' + playerId);
return treasonDb.merge(playerId, {
disconnects: 1 + (player.disconnects || 0)
}).then(function (player) {
debug('Updated disconnect count of player: ' + playerId);
}).catch(function (error) {
console.error('Failed to update player.');
console.error(error);
});
}
});
});
}
function getAllPlayers() {
return ready.then(function () {
return treasonDb.view('games/all_players').then(function (result) {
var players = [];
result.forEach(function (row) {
players.push({
playerName: row.value,
playerId: row.id
})
});
return players;
}).catch(function (error) {
console.error('failed to look up all players');
console.error(error);
});
});
}
function getPlayerRankings(playerId, showPersonalRank) {
if (rankingsDisabled) {
return Promise.resolve([]);
}
return ready.then(function() {
return statsInitialized();
}).then(function () {
var sortedPlayerIds = Object.keys(stats).sort(function (id1, id2) {
return stats[id1].rank - stats[id2].rank;
});
var playerStats = [];
if (showPersonalRank && playerId) {
debug('Getting player rankings for player ' + playerId);
var myRankings = [];
var playerAdded = false;
var playersBelowPlayerRank = 0;
for (var i = 0; i < sortedPlayerIds.length; i++) {
var sortedPlayerId = sortedPlayerIds[i];
if (playerAdded) {
playersBelowPlayerRank++;
if (playersBelowPlayerRank >= playerRanksToReturn / 2 && myRankings.length > playerRanksToReturn - 1) {
break;
}
}
var rankedPlayerStats = Object.assign({}, stats[sortedPlayerId]);
rankedPlayerStats.playerId = sortedPlayerId;
myRankings.push(rankedPlayerStats);
if (sortedPlayerId === playerId) {
playerAdded = true;
}
}
playerStats = myRankings.splice(myRankings.length - playerRanksToReturn, playerRanksToReturn);
} else {
debug('Getting global player rankings');
for (var j = 0; j < playerRanksToReturn; j++) {
if (stats[sortedPlayerIds[j]]) {
var rankedTopPlayerStats = Object.assign({}, stats[sortedPlayerIds[j]]);
rankedTopPlayerStats.playerId = sortedPlayerIds[j];
playerStats.push(rankedTopPlayerStats);
} else {
//There are less than 10-20 players registered in the ranks in this case
break;
}
}
}
playerStats.forEach(function (player) {
if (playerId && playerId == player.playerId) {
player.isPlayer = true;
}
delete player.playerId;
});
return playerStats;
});
}
//This could be a bit dicey, so beware
function updateResults(gameData) {
debug('Updating results for finished game');
for (let player of gameData.playerRank) {
if (player === 'ai') {
continue;
}
if (!stats[player]) {
//first time player
stats[player] = {
games: 0,
rank: 0,
playerName: '',
wins: 0,
winsAI: 0,
percent: 0
}
}
if (gameData.playerRank[0] === player) {
//This was the winner
stats[player].wins++;
if (gameData.humanPlayers < 2) {
//This was an ai game so it counts towards ai wins
stats[player].winsAI++;
}
}
stats[player].games++;
stats[player].percent = Math.floor(100 * stats[player].wins / stats[player].games);
//Make sure we have the player's name if it was a new player
if (!stats[player].playerName) {
stats[player].playerName = allPlayers[player];
}
}
//now reorder ranks
recalculateRanks(stats);
//Persist the whole stats - seems silly to do this rather than recalculating on startup
//This can be async
treasonDb.save(gameStatsDocumentId, {
gameStats: stats
}).catch(function (error) {
console.error('Failed to overwrite game stats document');
console.error(error);
});
}
function recalculateRanks(rankStats) {
var sortedPlayerIds = Object.keys(rankStats).sort(function (id1, id2) {
var first = rankStats[id1];
var second = rankStats[id2];
var result = (second.wins - second.winsAI) - (first.wins - first.winsAI);
if (result == 0) {
result = second.wins - first.wins;
if (result == 0) {
result = second.percent - first.percent;
if (result == 0) {
return first.playerName.localeCompare(second.playerName);
}
}
return result;
} else {
return result;
}
});
var rank = 1;
for (var i = 0; i < sortedPlayerIds.length; i++) {
rankStats[sortedPlayerIds[i]].rank = rank++;
}
}
function calculateAllStats() {
debug('Calculating stats for every player');
return Promise.all([
treasonDb.view('games/by_winner', {reduce: true, group: true}),
treasonDb.view('games/by_player', {reduce: true, group: true}),
treasonDb.view('games/all_players')
]).then(function (results) {
var newStats = {};
var games = results[1];
for (let game of games) {
var playerId = game.key;
var gameCount = game.value;
newStats[playerId] = {
games: gameCount,
rank: 0,
playerName: '',
wins: 0,
winsAI: 0,
percent: 0
};
}
var wins = results[0];
for (let win of wins) {
var playerId = win.key;
var winStats = win.value;
newStats[playerId].wins = winStats.wins;
newStats[playerId].winsAI = winStats.winsAI;
newStats[playerId].percent = Math.floor(100 * winStats.wins / newStats[playerId].games);
}
recalculateRanks(newStats);
var players = results[2];
for (var j = 0; j < players.length; j++) {
var player = players[j];
allPlayers[player.id] = player.value;
if (newStats[player.id]) {
newStats[player.id].playerName = player.value;
}
}
stats = newStats;
debug('Finished calculating all stats');
});
}
module.exports = {
// Called when a new player logs in for the first time
register: timeApi(register),
// Called at the start of a game
constructGameStats: constructGameStats,
// Called when a game ends normally (with a winner)
recordGameData: timeApi(recordGameData),
// Called when a player leaves in the middle of a game
recordPlayerDisconnect: timeApi(recordPlayerDisconnect),
// Returns the rankings, either the top N, or the N around a player's position
getPlayerRankings: timeApi(getPlayerRankings),
// Called when the server initially starts
init: timeApi(init)
};
function timeApi(fn) {
return function () {
var start = new Date().getTime();
function done(err) {
var end = new Date().getTime();
debug(fn.name + ' took ' + ms(end - start) + ' ' + (err && err.stack || ''));
}
debug(fn.name + '...');
return fn.apply(this, arguments).then(function (result) {
done();
return result;
}, function (err) {
done(err);
throw err;
});
};
}