Skip to content

Commit c5d9b9f

Browse files
committed
Summarize: Tabulate timeouts
1 parent b113e6d commit c5d9b9f

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

utils/summarize.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type StatSummary = {
8585
numPlayers: number;
8686
oldestRec?: string;
8787
newestRec?: string;
88+
timeoutRate: number;
8889
ratings: {
8990
highest: UserGameRating[];
9091
avg: UserRating[];
@@ -108,6 +109,7 @@ type StatSummary = {
108109
meta: GameNumList[];
109110
players: UserNumList[];
110111
firstTimers: number[];
112+
timeouts: number[];
111113
};
112114
recent: GameNumber[];
113115
hoursPer: number[];
@@ -157,6 +159,7 @@ export const handler: Handler = async (event: any, context?: any) => {
157159
let oldest: string|undefined;
158160
let newest: string|undefined;
159161
const timeouts: UserNumber[] = [];
162+
const siteTimeouts: number[] = [];
160163

161164
console.log("Segmenting records by meta and player");
162165
for (const rec of recs) {
@@ -187,17 +190,30 @@ export const handler: Handler = async (event: any, context?: any) => {
187190
const moveStr = JSON.stringify(rec.moves);
188191
// if abandoned, assign timeout to all players
189192
if (moveStr.includes("abandoned")) {
193+
const datems = new Date(rec.header["date-end"]).getTime();
194+
siteTimeouts.push(datems);
190195
for (const p of rec.header.players) {
191-
const datems = new Date(rec.header["date-end"]).getTime();
192196
timeouts.push({user: p.userid!, value: datems});
193197
}
194198
}
195199
// if timeout, assign timeout to player who timed out
196200
else if (moveStr.includes("timeout")) {
201+
const datems = new Date(rec.header["date-end"]).getTime();
202+
siteTimeouts.push(datems);
197203
// find the specific move
204+
const fidx = rec.moves.findIndex(mvs => mvs.find(m => m !== null && (typeof m === "object" ? m.move === "timeout" : m === "timeout")));
205+
if (fidx !== -1) {
206+
const mvs = rec.moves[fidx];
207+
const midx = mvs.findIndex(m => m !== null && (typeof m === "object" ? m.move === "timeout" : m === "timeout"));
208+
if (midx !== -1) {
209+
const p = rec.header.players[midx];
210+
timeouts.push({user: p.userid!, value: datems});
211+
}
212+
}
198213
}
199214
}
200215
const numPlayers = playerIDs.size;
216+
const timeoutRate = siteTimeouts.length / recs.length;
201217

202218
// META STATS
203219
console.log("Calculating meta stats");
@@ -499,6 +515,8 @@ export const handler: Handler = async (event: any, context?: any) => {
499515
const histList: {game: string; bucket: number}[] = [];
500516
const histListPlayers: {user: string; bucket: number}[] = [];
501517
const completedList: {user: string; time: number}[] = [];
518+
const histTimeoutBuckets: number[] = [];
519+
const histTimeouts: number[] = [];
502520
const baseline = Date.now();
503521
// all first
504522
for (const rec of recs) {
@@ -512,6 +530,16 @@ export const handler: Handler = async (event: any, context?: any) => {
512530
}
513531
}
514532

533+
// timeouts
534+
for (const t of siteTimeouts) {
535+
const daysAgo = (baseline - t) / (24 * 60 * 60 * 1000);
536+
const bucket = Math.floor(daysAgo / 7);
537+
histTimeoutBuckets.push(bucket);
538+
}
539+
for (let i = 0; i <= Math.max(...histTimeoutBuckets); i++) {
540+
histTimeouts.push(histTimeoutBuckets.filter(x => x === i).length);
541+
}
542+
515543
// all games
516544
const histAll: number[] = [];
517545
const histAllPlayers: number[] = [];
@@ -628,12 +656,12 @@ export const handler: Handler = async (event: any, context?: any) => {
628656
geoStats.push({code: alpha2, n: count, name: name || alpha2});
629657
}
630658

631-
632659
const summary: StatSummary = {
633660
numGames,
634661
numPlayers,
635662
oldestRec: oldest,
636663
newestRec: newest,
664+
timeoutRate,
637665
ratings: {
638666
highest: rawList,
639667
avg: avgRatings,
@@ -657,6 +685,7 @@ export const handler: Handler = async (event: any, context?: any) => {
657685
meta: histMeta,
658686
players: histPlayers,
659687
firstTimers,
688+
timeouts: histTimeouts,
660689
},
661690
hoursPer,
662691
recent,

0 commit comments

Comments
 (0)