-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync.js
161 lines (146 loc) · 4.45 KB
/
sync.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
const { connectToDatabase } = require('../db');
const glicko2 = require('glicko2');
const algoliasearch = require('algoliasearch');
const { chunk } = require('lodash');
const MONGO_URL = process.env.MONGO_URL;
const DB_NAME = process.env.DB_NAME;
const PLAYER_COLLECTION = process.env.PLAYER_COLLECTION;
const MATCH_COLLECTION = process.env.MATCH_COLLECTION;
const ALGOLIA_APP = process.env.ALGOLIA_APP;
const ALGOLIA_KEY = process.env.ALGOLIA_KEY;
const ALGOLIA_INDEX = process.env.ALGOLIA_INDEX;
const TAU = 0.5;
const SETTINGS = {
tau: TAU,
rating: 1500,
rd: 350,
vol: 0.06,
};
const connectToIndex = (appId, apiKey, index) => {
const client = algoliasearch(appId, apiKey);
return client.initIndex(index);
};
const addToIndex = async (records, index) => {
try {
await index.saveObjects(records);
} catch (e) {
console.error(e);
}
};
const getAllRecords = async (db) => {
try {
const records = await db.collection(PLAYER_COLLECTION).find({}).toArray();
return records;
} catch (e) {
console.error(e);
}
};
const chunkedRecords = (records, chunkSize) => {
const algoliaRecords = records.map(
({ id, name, img, rating, prevRank, currRank }) => {
return {
objectID: id,
name,
img,
rating,
prevRank,
currRank,
};
}
);
return chunk(algoliaRecords, chunkSize);
};
const bulkUpdateCollection = async (db, entries) => {
if (entries.length) {
let col = db.collection(PLAYER_COLLECTION);
let bulk = col.initializeUnorderedBulkOp();
for (const entry of entries) {
await bulk.find({ id: entry[0] }).updateOne({ $set: entry[1] });
}
return bulk.execute();
} else {
return 'None';
}
};
const bulkUpdateRanked = async (db, entries) => {
if (entries.length) {
let col = db.collection(PLAYER_COLLECTION);
let bulk = col.initializeUnorderedBulkOp();
for (const entry of entries) {
await bulk.find({ id: entry.id }).updateOne({ $set: entry });
}
return bulk.execute();
} else {
return 'None';
}
};
const syncRating = async (req, res) => {
const CHUNK_SIZE = 50;
const ranking = new glicko2.Glicko2(SETTINGS);
try {
const db = await connectToDatabase(MONGO_URL, DB_NAME);
const timeNow = Date.now();
const oneDayBefore = timeNow - 24 * 60 * 60 * 1000;
res.send({ message: 'Updating ratings' });
const matches = await db
.collection(MATCH_COLLECTION)
.find({ timestamp: { $gte: oneDayBefore, $lt: timeNow } })
.toArray();
const playersSet = [...new Set(matches.flatMap((m) => [m.p1, m.p2]))];
const players = await db
.collection(PLAYER_COLLECTION)
.find({ id: { $in: playersSet } })
.toArray();
const playerMap = players.reduce(
(acc, { id, rating, deviation, sigma }) => {
acc[id] = {
rating,
deviation,
sigma,
g: ranking.makePlayer(rating, deviation, sigma),
};
return acc;
},
{}
);
const matchArray = matches.map(({ p1, p2, winner }) => {
const result = winner === 'p1' ? 1 : 0;
return [playerMap[p1].g, playerMap[p2].g, result];
});
ranking.updateRatings(matchArray);
const updatedRatings = Object.entries(playerMap).map(([k, v]) => {
const rating = v.g.getRating();
const deviation = v.g.getRd();
const sigma = v.g.getVol();
return [k, { rating, deviation, sigma }];
});
const result = await bulkUpdateCollection(db, updatedRatings);
const allRecords = await getAllRecords(db);
const sortedByRating = allRecords.sort((a, b) => b.rating - a.rating);
const newRanked = sortedByRating.map((r, i) => ({ ...r, prevRank: r.currRank, currRank: i + 1 }));
const resultRanked = await bulkUpdateRanked(db, newRanked);
const algoliaIndex = connectToIndex(ALGOLIA_APP, ALGOLIA_KEY, ALGOLIA_INDEX);
const chunks = chunkedRecords(newRanked, CHUNK_SIZE);
for (const chunk of chunks) {
await addToIndex(chunk, algoliaIndex);
}
return;
} catch (e) {
res.status(500).send({ message: e.message });
}
};
const deleteIncomplete = async (req, res) => {
try {
const db = await connectToDatabase(MONGO_URL, DB_NAME);
await db
.collection(MATCH_COLLECTION)
.deleteMany({ winner: { $exists: false } });
return res.send({ message: 'Deleted unfinished matches' });
} catch (e) {
res.status(500).send({ message: e.message });
}
};
module.exports = {
syncRating,
deleteIncomplete,
};