-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathracing_sheet.js
More file actions
349 lines (309 loc) · 9.9 KB
/
racing_sheet.js
File metadata and controls
349 lines (309 loc) · 9.9 KB
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
#!/usr/bin/env node
'use strict';
const Moment = require('moment-timezone');
const SHEET_KEY = '1LI4OBEGMPKxBwtGY_xJ1yT_G-rrCJxl8FaC0q0DaBl0';
const TS_PATH = './node_modules/jstrueskill/lib/racingjellyfish/jstrueskill';
const TrueSkillCalculator = require(`${TS_PATH}/TrueSkillCalculator`);
const Player = require(`${TS_PATH}/Player`);
const Team = require(`${TS_PATH}/Team`);
const GameInfo = new (require(`${TS_PATH}/GameInfo`))(
25.0, // Mean for new players (Default: 25)
25.0 / 3.0, // Standard deviatation for new players (Defualt: Mean/3)
25.0 / 2.0, // Beta - Number of skill points for 80% chance to win (Default: Mean/6)
25.0 / 150.0, // Tau - amount added to a user's SD before each match (Default: Mean/300)
0.1); // Draw probability (changing this caused the algorithm to fail more freq.)
const _ = require('lodash');
const DNF = Number.MAX_SAFE_INTEGER / 2;
const UNKNOWN = Number.MAX_SAFE_INTEGER;
function getSheetRows(sheet, tab) {
return new Promise((resolve, reject) => {
sheet.getRows(tab, (err, rows) => {
if (err !== null) {
reject(err);
}
resolve(rows);
});
});
}
// Participants
function getParticipants(rows) {
return _(rows)
.map(row => {
return {
driverId: row.participantid,
fullname: `${row.last}, ${row.first}`,
driverSkill: null,
driverSkills: null
};
})
.keyBy('driverId')
.value();
}
// Races
function getRaces(rows) {
return _(rows)
.map(row => {
return {
raceId: row.raceid,
name: `${row.year} ${row.racename}`,
theme: row.theme,
abbreviation: row.raceabbr,
date: Moment.tz(row.date, 'MM/DD/YYYY', 'America/New_York').toJSON(),
numLegs: Number.parseInt(row.numlegs, 10),
legs: [],
toScore: row.toscore === 'TRUE',
start: row.start,
finish: row.finish,
distance: row.distance
};
})
.keyBy('raceId')
.value();
}
function getLegResults(rows, races) {
const results = rows.map(row => {
const RACE_DATE = new Moment(races[row.raceid].date);
return {
raceId: row.raceid,
leg: Number.parseInt(row.leg, 10),
driverId: Number.parseInt(row.driverid, 10),
navigatorId: 'n/a',
start: RACE_DATE.clone().add(Moment.duration(row.starttime)).toJSON(),
end: RACE_DATE.clone().add(Moment.duration(row.finishtime)).toJSON(),
duration: Moment.duration(row.legduration).toJSON(),
rank: null, // placeholder
dnf: row.dnf === 'TRUE',
unknownTime: row.unknown === 'TRUE'
};
});
return results;
}
function getResultCompare(r) {
if (r.dnf === true) {
return DNF;
}
if (r.unknownTime === true) {
return UNKNOWN;
}
return Moment.duration(r.duration).asMilliseconds();
}
function rankResults(results) {
return _(results)
.groupBy(getResultCompare)
.toPairs()
.sortBy(result => Number(result[0]))
// put these groups back into the result time array, adding their rank
.reduce((prevResults, curResultGroup) => {
curResultGroup[1].forEach(result => {
result.rank = prevResults.length + 1;
});
return _.concat(prevResults, curResultGroup[1]);
}, []);
}
function getRaceResults(legResults, participants, races) {
return _.chain(legResults)
// HACK: puts the legs date order... so that we score the race results in Date
// order (needed for Trueskill). I couldn't figure out how to order the grouping
// below....
.sortBy(leg => races[leg.raceId].date)
.groupBy('raceId')
.mapValues(race => {
// console.log(race);
return _.chain(race)
//
// STEP 1: RANK THE LEGS
//
.groupBy('leg') // groups the results by eaech leg
.flatMap(rankResults) // ranks each leg
// .flatMap(legs => scoreLeg(legs, participants))
//
// STEP 2: BUILD A RESULT TABLE FOR EACH RACE
//
.groupBy('driverId')
.mapValues((legs, driverId) => {
const driverResult = {
driverId: driverId,
driverName: participants[driverId].fullname,
navigatorName: 'n/a',
rank: null,
duration: _.reduce(legs, (duration, leg) => {
return duration.add(Moment.duration(leg.duration));
}, Moment.duration(0)).toJSON(),
legs: _.map(legs, leg =>
_.pick(leg, ['start', 'end', 'duration', 'rank',
'dnf', 'unknownTime'])),
// These two parameters are TRUE at the race level if they are true for any leg
dnf: !(_.every(legs, ['dnf', false])),
unknownTime: !(_.every(legs, ['unknownTime', false]))
};
return driverResult;
})
.value();
})
// Rank the race results
.mapValues(rankResults)
//
// STEP 3: ADD CONSOLIDATED INFORMATION ABOUT EACH RACE
//
.mapValues((results, raceId) => {
return _(races[raceId])
.pick(['raceId', 'name', 'theme', 'date', 'start', 'end',
'distance', 'numLegs', 'toScore'])
.merge({
results: results,
time: results[0].duration,
winner: results[0].driverName
})
.value();
})
.value();
}
function getRaceList(raceResults) {
return _(raceResults)
.flatMap(result => _.omit(result, ['results']))
.orderBy('date')
.value();
}
function getDriverStats(raceResults, participants) {
return _(raceResults)
.flatMap('results')
.groupBy('driverId')
.mapValues((driverResults, driverId) => {
return {
id: driverId,
name: participants[driverId].fullname,
starts: driverResults.length,
finishes: _.filter(driverResults, result => result.dnf === false).length,
wins: _.filter(driverResults, result => result.rank === 1).length,
podiums: _.filter(driverResults, result => result.rank <= 3).length,
driverSkill: participants[driverId].driverSkill,
driverSkills: participants[driverId].driverSkills
};
})
.orderBy('driverSkill.conservativeRating', 'desc')
.value();
}
function scoreLeg(driverData) {
driverData = _.sortBy(driverData, 'rank');
const teams = _.map(driverData, data => {
return new Team(data.driverId.toString(),
new Player(data.driverId),
data.driverSkill);
});
let newSkills;
let ratingsCalculated = false;
while (!ratingsCalculated) {
try {
newSkills = TrueSkillCalculator
.calculateNewRatings(GameInfo,
teams,
_.map(driverData, 'rank'));
ratingsCalculated = true;
} catch (err) {
const lastRanking = _.last(driverData).rank;
const indexToChange = _.findLastIndex(driverData,
data => data.rank !== lastRanking);
console.error(`SCORING ERROR: Changing the rating of: ` +
`${driverData[indexToChange].driverId}`);
driverData[indexToChange].rank = lastRanking;
}
}
return _.reduce(teams, (results, team) => {
const player = team.getPlayers()[0];
results[player.getId()] = newSkills[player];
return results;
}, {});
}
function scoreResults(raceResults, participants) {
let driverSkills = {};
_(raceResults)
.mapValues(raceResult => {
return _(raceResult.results)
.flatMap(driverResults => {
return _(driverResults.legs)
.reject('unknownTime')
.map((leg, index) => {
return {
driverId: driverResults.driverId,
leg: index + 1,
rank: leg.rank
};
})
.value();
})
.groupBy('leg')
.value();
})
.forEach((raceResult, raceId) => {
_.forEach(raceResult, (legResults, legNum) => {
console.log(`Scoring ${raceId} Leg ${Number(legNum)}`);
const newSkills = scoreLeg(_.map(legResults, result => {
const sk = _.has(_.last(driverSkills[result.driverId]), 'driverSkill') ?
_.last(driverSkills[result.driverId]).driverSkill :
GameInfo.getDefaultRating();
return {
driverId: result.driverId,
driverSkill: sk,
rank: result.rank
};
}));
_.forOwn(newSkills, (newSkill, driverId) => {
driverSkills[driverId] = driverSkills[driverId] || [];
driverSkills[driverId].push({
raceId: raceId,
leg: legNum,
driverSkill: newSkill
});
});
});
});
_.forOwn(driverSkills, (skills, driverId) => {
participants[driverId].driverSkills = skills;
participants[driverId].driverSkill = _.last(skills).driverSkill;
});
return participants;
}
// output the ranked list of drivers
function printDriverStats(driverStats) {
_(driverStats)
.forEach((driver, index) => {
console.log(`${index}\t${driver.driverSkill.conservativeRating}` +
`\t${driver.starts}\t${driver.name}`);
});
}
function main() {
console.log('Getting data from Google Sheets');
// spreadsheet key is the long id in the sheets URL
const GoogleSpreadsheet = require('google-spreadsheet');
const Sheet = new GoogleSpreadsheet(SHEET_KEY);
Promise.all([getSheetRows(Sheet, 1).then(getParticipants),
getSheetRows(Sheet, 3).then(getRaces),
getSheetRows(Sheet, 2)
])
.then(promiseResults => {
console.log('Data from sheets pulled');
let participants = promiseResults[0];
const races = promiseResults[1];
const legResults = getLegResults(promiseResults[2], races);
const raceResults = getRaceResults(legResults, participants, races);
const raceList = getRaceList(raceResults);
participants = scoreResults(raceResults, participants);
const driverStats = getDriverStats(raceResults, participants);
console.log('Updating Firebase');
const Firebase = require('firebase');
const fbRef = new Firebase('https://dttdata.firebaseio.com/');
fbRef.set({
races: raceList,
raceResults: raceResults,
driverStats: driverStats
});
printDriverStats(driverStats);
console.log('all done');
})
.catch(err => {
console.error(`ERROR: ${err}`);
console.error(err.stack);
throw err;
});
}
main();