forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame3.js
137 lines (124 loc) · 3.33 KB
/
game3.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
var gamesToMigrate = db.game2.find();
var max = gamesToMigrate.count();
var batchSize = 10000;
var collection = db.game3;
var pgnCollection = db.pgn;
print("Migrating " + max + " games");
collection.drop();
pgnCollection.drop();
function rename(arr, from, to) {
if (typeof arr[from] !== 'undefined') {
arr[to] = arr[from];
delete arr[from];
}
}
function map(arr, key, f) {
if (typeof arr[key] !== 'undefined') {
arr[key] = f(arr[key]);
}
}
function clean(arr, key) {
if (typeof arr[key] !== 'undefined' && (arr[key] === null || arr[key] === "")) {
delete arr[key];
}
}
function cleanArray(arr, key) {
if (typeof arr[key] !== 'undefined' && arr[key].length === 0) {
delete arr[key];
}
}
function cleanOrRename(arr, from, to) {
clean(arr, from);
rename(arr, from, to);
}
function removeSpace(str) { return str.replace(/ /g, ''); }
function finishedOrAborted(game) { return game.status >= 25; }
var c, z, pgn;
var it = 0;
var dat = new Date().getTime() / 1000;
var finishedPlayerFieldsToRemove = ['previousMoveTs', 'lastDrawOffer', 'isOfferingDraw', 'isProposingTakeback'];
gamesToMigrate.forEach(function(g) {
cleanOrRename(g, 'castles', 'cs');
if (g.cc === 'white') {
delete g.cc;
} else {
g.cc = false;
}
cleanOrRename(g, 'check', 'ck');
cleanOrRename(g, 'lastMove', 'lm');
map(g, 'lm', removeSpace);
cleanOrRename(g, 'initialFen', 'if');
clean(g, 'clock');
cleanArray(g, 'userIds');
rename(g, 'userIds', 'uids');
rename(g, 'createdAt', 'ca');
rename(g, 'turns', 't');
rename(g, 'status', 's');
cleanOrRename(g, 'updatedAt', 'ua');
cleanOrRename(g, 'winId', 'wid');
delete g.r960;
if (typeof g.v !== 'undefined' && g.v != 2) {
delete g.v;
}
if (typeof g.isRated != 'undefined' && g.isRated !== true) {
delete g.isRated;
} else {
cleanOrRename(g, 'isRated', 'ra');
}
if (finishedOrAborted(g)) {
delete g.positionHashes;
delete g.lmt;
} else {
cleanOrRename(g, 'positionHashes', 'ph');
}
cleanOrRename(g, 'clock', 'c');
if (c = g.c) {
if (finishedOrAborted(g)) {
delete c.timer;
} else {
cleanOrRename(c, 'timer', 't');
}
if (typeof c.c !== 'undefined') {
c.c = c.c === 'white';
}
}
rename(g, 'players', 'p');
g.p.forEach(function(p) {
delete p.c;
map(p, 'ps', removeSpace);
if (finishedOrAborted(g) && typeof p.blurs !== 'undefined' && p.blurs < 7) {
delete p.blurs;
} else {
cleanOrRename(p, 'blurs', 'bs');
}
if (finishedOrAborted(g)) {
for (z in finishedPlayerFieldsToRemove) {
delete p[z];
}
}
delete p.isAi;
rename(p, 'aiLevel', 'ai');
});
pgn = g.pgn;
delete g.pgn;
collection.insert(g);
if (pgn !== null && pgn !== "") {
pgnCollection.insert({_id: g._id, p: pgn});
}
++it;
if (it % batchSize == 0) {
var percent = Math.round((it / max) * 100);
var dat2 = new Date().getTime() / 1000;
var perSec = Math.round(batchSize / (dat2 - dat));
dat = dat2;
print((it / 1000) + "k " + percent + "% " + perSec + "/s");
}
});
print("Building indexes");
collection.ensureIndex({s: 1});
collection.ensureIndex({uids: 1}, {sparse: 1});
collection.ensureIndex({wid: 1}, {sparse: 1});
collection.ensureIndex({ca: -1});
collection.ensureIndex({uids: 1, ca: -1});
collection.ensureIndex({bm: 1}, {sparse: 1});
print("Done!");