Skip to content

Commit 735b408

Browse files
committed
Track suspects in a database table
they don't sync across processes well otherwise.
1 parent 08e0cab commit 735b408

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

src/actions.ts

+20-27
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,12 @@ function loadData(path: string | null) {
5353
}
5454
}
5555

56-
type SuspectReqs = Partial<{elo: number, gxe: number, coil: number}>;
57-
const suspects: Record<string, {startDate: number, reqs: SuspectReqs}> = loadData(Config.suspectpath);
58-
const coil: Record<string, number> = loadData(Config.coilpath);
59-
60-
if (Config.suspectpath) {
61-
watchFile(
62-
Config.suspectpath,
63-
() => Object.assign(suspects, loadData(Config.suspectpath))
64-
);
65-
}
56+
export let coil: Record<string, number> = loadData(Config.coilpath);
57+
6658
if (Config.coilpath) {
67-
watchFile(Config.coilpath, () => Object.assign(coil, loadData(Config.coilpath)));
59+
watchFile(Config.coilpath, () => {
60+
coil = loadData(Config.coilpath);
61+
});
6862
}
6963

7064
const redundantFetch = async (targetUrl: string, data: RequestInit, attempts = 0): Promise<void> => {
@@ -374,8 +368,9 @@ export const actions: {[k: string]: QueryHandler} = {
374368
const out: {[k: string]: any} = {};
375369
const [p1rating, p2rating] = await ladder.addMatch(params.p1!, params.p2!, parseFloat(params.score));
376370

377-
if (suspects[formatid]) {
378-
const reqs = suspects[formatid].reqs;
371+
const suspect = await tables.suspects.get(formatid);
372+
if (suspect) {
373+
const reqs = {elo: suspect.elo, gxe: suspect.gxe, coil: suspect.gxe};
379374
for (const rating of [p1rating, p2rating]) {
380375
let reqsMet = 0;
381376
let reqCount = 0;
@@ -405,13 +400,13 @@ export const actions: {[k: string]: QueryHandler} = {
405400
// sanity check for reqs existing just to be totally safe
406401
(reqsMet >= 1 && reqsMet === reqCount) &&
407402
// did not play games before the test began
408-
(ratingData?.first_played && ratingData.first_played > suspects[formatid].startDate)
403+
(ratingData?.first_played && ratingData.first_played > suspect.start_date)
409404
) {
410405
void smogonFetch("tools/api/suspect-verify", "POST", {
411406
userid: rating.userid,
412407
format: formatid,
413408
reqs: {required: reqs, actual: userData},
414-
suspectStartDate: suspects[formatid].startDate,
409+
suspectStartDate: suspect.start_date,
415410
});
416411
}
417412
}
@@ -1061,13 +1056,13 @@ export const actions: {[k: string]: QueryHandler} = {
10611056
} catch (e: any) {
10621057
throw new ActionError("Failed to update Smogon suspect test record: " + e.message);
10631058
}
1064-
suspects[id] = {
1065-
startDate: time(),
1066-
reqs,
1067-
};
1068-
if (Config.suspectpath) {
1069-
await fs.writeFile(Config.suspectpath, JSON.stringify(suspects));
1070-
}
1059+
await tables.suspects.replace({
1060+
formatid: id,
1061+
start_date: time(),
1062+
elo: reqs.elo,
1063+
gxe: reqs.gxe,
1064+
coil: reqs.coil,
1065+
});
10711066
return {success: true};
10721067
},
10731068
async 'suspects/end'(params) {
@@ -1076,11 +1071,9 @@ export const actions: {[k: string]: QueryHandler} = {
10761071
}
10771072
const id = toID(params.format);
10781073
if (!id) throw new ActionError("No format ID specified.");
1079-
if (!suspects[id]) throw new ActionError("There is no ongoing suspect for " + id);
1080-
delete suspects[id];
1081-
if (Config.suspectpath) {
1082-
await fs.writeFile(Config.suspectpath, JSON.stringify(suspects));
1083-
}
1074+
const suspect = await tables.suspects.get(id);
1075+
if (!suspect) throw new ActionError("There is no ongoing suspect for " + id);
1076+
await tables.suspects.delete(id);
10841077
return {success: true};
10851078
},
10861079
};

src/schemas/ntbb-suspects.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Table structure for suspect tracking
2+
-- Unfortunately necessary to be a table in order to properly synchronize
3+
-- cross-processes
4+
5+
CREATE TABLE ntbb_suspects (
6+
formatid varchar(100) NOT NULL PRIMARY KEY,
7+
start_date bigint(20) NOT NULL,
8+
elo int,
9+
coil int,
10+
gxe int
11+
);

src/tables.ts

+8
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,11 @@ export const teams = pgdb.getTable<{
142142
title: string;
143143
private: number;
144144
}>('teams', 'teamid');
145+
146+
export const suspects = psdb.getTable<{
147+
formatid: string;
148+
start_date: number;
149+
coil: number | null;
150+
gxe: number | null;
151+
elo: number | null;
152+
}>("ntbb_suspects", 'formatid');

0 commit comments

Comments
 (0)