forked from octachrome/treason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat-test.js
165 lines (153 loc) · 4.84 KB
/
stat-test.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
'use strict';
var crypto = require('crypto');
var cradle = require('cradle');
var pr = require('promise-ring');
var rand = require('random-seed')();
var argv = require('optimist')
.usage('$0 [--players <N>] [--games <N>]')
.default('players', 100)
.default('games', 1000)
.default('queries', 10)
.argv;
var connection = new cradle.Connection();
var db = pr.wrapAll(connection.database('treason_test'));
var playerIds;
var stats;
createDatabase()
.then(createViews)
.then(createPlayers)
.then(createGames)
.then(queryRandomPlayers)
.then(countOverallGames)
.then(calculateAllStats)
.then(calculateTopTwenty)
.catch(function (err) {
console.log(err);
});
function queryRandomPlayers() {
console.log('Calculating individual stats');
var results = [];
for (var i = 0; i < argv.queries; i++) {
var somePlayerId = playerIds[rand(playerIds.length)];
results.push(Promise.all([
db.view('games/by_winner', {reduce: true, key: somePlayerId}),
db.view('games/by_player', {reduce: true, key: somePlayerId})
]).then(function (results) {
var wins = results[0][0].value;
var games = results[1][0].value;
var percent = Math.floor(100 * wins / games);
console.log('Player ' + somePlayerId + ' won ' + percent + '% (' + wins + '/' + games + ')');
}));
}
return Promise.all(results);
}
function countOverallGames() {
console.log('Counting total games won');
return db.view('games/by_winner', {reduce: true}).then(function (results) {
var wins = results[0].value;
console.log('Overall games won ' + wins);
});
}
function calculateAllStats() {
console.log('Calculating stats for every player');
stats = {};
return Promise.all([
db.view('games/by_winner', {reduce: true, group: true}),
db.view('games/by_player', {reduce: true, group: true})
]).then(function (results) {
var games = results[1];
games.forEach(function (playerId, gameCount) {
stats[playerId] = {
games: gameCount,
wins: 0,
percent: 0
};
});
var wins = results[0];
wins.forEach(function (playerId, winCount) {
stats[playerId].wins = winCount;
stats[playerId].percent = Math.floor(100 * winCount / stats[playerId].games);
});
});
}
function calculateTopTwenty() {
console.log('Calculating top 20 players');
var sortedPlayerIds = Object.keys(stats).sort(function (id1, id2) {
return stats[id2].percent - stats[id1].percent;
});
console.log('Top twenty:');
for (var i = 0; i < 20; i++) {
var id = sortedPlayerIds[i];
console.log('Player ' + id + ' won ' + stats[id].percent + '% (' + stats[id].wins + '/' + stats[id].games + ')');
}
}
function createDatabase() {
return db.exists().then(function (exists) {
if (exists) {
console.log('Destroying database');
return db.destroy();
}
}).then(function () {
console.log('Creating database');
return db.create();
});
}
function createPlayers() {
console.log('Creating player ids');
playerIds = [];
for (var i = 0; i < argv.players; i++) {
playerIds.push(crypto.randomBytes(16).toString('hex'));
}
}
function createGames() {
console.log('Creating games');
var gamePromises = [];
for (var g = 0; g < argv.games; g++) {
var playerRank = [];
for (var p = 0, len = 2+rand(2); p < len; p++) {
playerRank.push(playerIds[rand(playerIds.length)]);
}
gamePromises.push(db.save({
type: 'game',
playerRank: playerRank
}));
}
return Promise.all(gamePromises);
}
function createViews() {
console.log('Creating views');
return db.save('_design/games', {
by_winner: {
map: function (doc) {
if (doc.type === 'game' && doc.playerRank) {
emit(doc.playerRank[0]);
}
},
reduce: function (keys, values, rereduce) {
if (rereduce) {
return sum(values);
}
else {
return values.length;
}
}
},
by_player: {
map: function (doc) {
if (doc.type === 'game' && doc.playerRank) {
doc.playerRank.forEach(function (player) {
emit(player);
});
}
},
reduce: function (keys, values, rereduce) {
if (rereduce) {
return sum(values);
}
else {
return values.length;
}
}
}
});
}