Skip to content

Commit 9a22403

Browse files
committed
prettier
1 parent 1875777 commit 9a22403

4 files changed

Lines changed: 142 additions & 80 deletions

File tree

svc/gcdata.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ async function processGcData(job: GcDataJob) {
1212
return;
1313
}
1414
// Currently, just attempt it once and skip if failed
15-
const { data: gcMatch, skipped } = await gcFetcher.getOrFetchData(job.match_id, {
16-
pgroup,
17-
});
15+
const { data: gcMatch, skipped } = await gcFetcher.getOrFetchData(
16+
job.match_id,
17+
{
18+
pgroup,
19+
},
20+
);
1821
if (gcMatch) {
1922
// Reconcile anonymous players
2023
await queueReconcile(gcMatch, pgroup, 'pmh_gcdata');

svc/rater.ts

Lines changed: 122 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,135 @@
1-
import { gcFetcher } from "./fetcher/getGcData";
2-
import db from "./store/db";
3-
import { average, isRadiant } from "./util/utility";
1+
import { gcFetcher } from './fetcher/getGcData';
2+
import db from './store/db';
3+
import { average, isRadiant } from './util/utility';
44

55
const DEFAULT_RATING = 4000;
66
const kFactor = 32;
77

88
async function doRate() {
9-
while(true) {
10-
const { rows } = await db.raw<{rows: { match_seq_num: number, match_id: number, pgroup: PGroup, radiant_win: boolean, gcdata: GcMatch | null}[]}>('SELECT match_seq_num, match_id, pgroup, radiant_win, gcdata from rating_queue order by match_seq_num ASC LIMIT 1');
11-
const row = rows[0];
12-
if (!row) {
13-
// No rows, wait and try again
14-
await new Promise(resolve => setTimeout(resolve, 1000));
15-
}
16-
let gcMatch = row.gcdata;
17-
if (!gcMatch) {
18-
// Fetch gcdata if not available already
19-
const { data } = await gcFetcher.getOrFetchDataWithRetry(row.match_id, { pgroup: row.pgroup });
20-
if (!data) {
21-
throw new Error('no gcdata found for match ' + row.match_id);
22-
}
23-
gcMatch = data;
24-
}
25-
await db.raw('UPDATE rating_queue SET gcdata = ? WHERE match_seq_num = ?', [JSON.stringify(gcMatch), row.match_seq_num]);
26-
// Get ratings of all players in the match, otherwise default value
27-
const accountIds = gcMatch.players.map(p => p.account_id);
28-
// console.log(accountIds);
29-
const { rows: existingRatings } = await db.raw<{rows: {account_id: number, computed_mmr: number}[]}>(`SELECT account_id, computed_mmr FROM player_computed_mmr WHERE account_id IN (${accountIds.map(_ => '?').join(',')})`, [...accountIds]);
30-
const ratingMap = new Map<number, number>();
31-
existingRatings.forEach((r) => {
32-
ratingMap.set(r.account_id, r.computed_mmr);
33-
});
34-
// compute team averages
35-
const currRating1 = average(gcMatch.players.filter(p => isRadiant(p)).map(p => ratingMap.get(p.account_id!) ?? DEFAULT_RATING));
36-
const currRating2 = average(gcMatch.players.filter(p => !isRadiant(p)).map(p => ratingMap.get(p.account_id!) ?? DEFAULT_RATING));
37-
// apply elo formula to get delta
38-
const r1 = 10 ** (currRating1 / 400);
39-
const r2 = 10 ** (currRating2 / 400);
40-
const e1 = r1 / (r1 + r2);
41-
const e2 = r2 / (r1 + r2);
42-
const win1 = Number(row.radiant_win);
43-
const win2 = Number(!row.radiant_win);
44-
const ratingDiff1 = kFactor * (win1 - e1);
45-
const ratingDiff2 = kFactor * (win2 - e2);
46-
// Start transaction
47-
await db.raw('BEGIN TRANSACTION');
48-
// Rate each player
49-
console.log('match %s, radiant_win: %s', row.match_id, row.radiant_win);
50-
for (let i = 0; i < gcMatch.players.length; i++) {
51-
const p = gcMatch.players[i];
52-
const oldRating = (ratingMap.get(p.account_id!) ?? DEFAULT_RATING);
53-
const delta = isRadiant(p) ? ratingDiff1 : ratingDiff2;
54-
// apply delta to each player (all players on a team will have the same rating change)
55-
const newRating = oldRating + delta;
56-
console.log('account_id: %s, oldRating: %s, newRating: %s, delta: %s', p.account_id, oldRating, newRating, delta);
57-
// Write ratings back to players
58-
await db.raw('INSERT INTO player_computed_mmr(account_id, computed_mmr) VALUES(?, ?) ON CONFLICT(account_id) DO UPDATE SET computed_mmr = EXCLUDED.computed_mmr', [p.account_id, newRating]);
59-
}
60-
// Delete row
61-
await db.raw('DELETE FROM rating_queue WHERE match_seq_num = ?', row.match_seq_num);
62-
// Commit transaction
63-
await db.raw('COMMIT');
9+
while (true) {
10+
const { rows } = await db.raw<{
11+
rows: {
12+
match_seq_num: number;
13+
match_id: number;
14+
pgroup: PGroup;
15+
radiant_win: boolean;
16+
gcdata: GcMatch | null;
17+
}[];
18+
}>(
19+
'SELECT match_seq_num, match_id, pgroup, radiant_win, gcdata from rating_queue order by match_seq_num ASC LIMIT 1',
20+
);
21+
const row = rows[0];
22+
if (!row) {
23+
// No rows, wait and try again
24+
await new Promise((resolve) => setTimeout(resolve, 1000));
6425
}
26+
let gcMatch = row.gcdata;
27+
if (!gcMatch) {
28+
// Fetch gcdata if not available already
29+
const { data } = await gcFetcher.getOrFetchDataWithRetry(row.match_id, {
30+
pgroup: row.pgroup,
31+
});
32+
if (!data) {
33+
throw new Error('no gcdata found for match ' + row.match_id);
34+
}
35+
gcMatch = data;
36+
}
37+
await db.raw('UPDATE rating_queue SET gcdata = ? WHERE match_seq_num = ?', [
38+
JSON.stringify(gcMatch),
39+
row.match_seq_num,
40+
]);
41+
// Get ratings of all players in the match, otherwise default value
42+
const accountIds = gcMatch.players.map((p) => p.account_id);
43+
// console.log(accountIds);
44+
const { rows: existingRatings } = await db.raw<{
45+
rows: { account_id: number; computed_mmr: number }[];
46+
}>(
47+
`SELECT account_id, computed_mmr FROM player_computed_mmr WHERE account_id IN (${accountIds.map((_) => '?').join(',')})`,
48+
[...accountIds],
49+
);
50+
const ratingMap = new Map<number, number>();
51+
existingRatings.forEach((r) => {
52+
ratingMap.set(r.account_id, r.computed_mmr);
53+
});
54+
// compute team averages
55+
const currRating1 = average(
56+
gcMatch.players
57+
.filter((p) => isRadiant(p))
58+
.map((p) => ratingMap.get(p.account_id!) ?? DEFAULT_RATING),
59+
);
60+
const currRating2 = average(
61+
gcMatch.players
62+
.filter((p) => !isRadiant(p))
63+
.map((p) => ratingMap.get(p.account_id!) ?? DEFAULT_RATING),
64+
);
65+
// apply elo formula to get delta
66+
const r1 = 10 ** (currRating1 / 400);
67+
const r2 = 10 ** (currRating2 / 400);
68+
const e1 = r1 / (r1 + r2);
69+
const e2 = r2 / (r1 + r2);
70+
const win1 = Number(row.radiant_win);
71+
const win2 = Number(!row.radiant_win);
72+
const ratingDiff1 = kFactor * (win1 - e1);
73+
const ratingDiff2 = kFactor * (win2 - e2);
74+
// Start transaction
75+
await db.raw('BEGIN TRANSACTION');
76+
// Rate each player
77+
console.log('match %s, radiant_win: %s', row.match_id, row.radiant_win);
78+
for (let i = 0; i < gcMatch.players.length; i++) {
79+
const p = gcMatch.players[i];
80+
const oldRating = ratingMap.get(p.account_id!) ?? DEFAULT_RATING;
81+
const delta = isRadiant(p) ? ratingDiff1 : ratingDiff2;
82+
// apply delta to each player (all players on a team will have the same rating change)
83+
const newRating = oldRating + delta;
84+
console.log(
85+
'account_id: %s, oldRating: %s, newRating: %s, delta: %s',
86+
p.account_id,
87+
oldRating,
88+
newRating,
89+
delta,
90+
);
91+
// Write ratings back to players
92+
await db.raw(
93+
'INSERT INTO player_computed_mmr(account_id, computed_mmr) VALUES(?, ?) ON CONFLICT(account_id) DO UPDATE SET computed_mmr = EXCLUDED.computed_mmr',
94+
[p.account_id, newRating],
95+
);
96+
}
97+
// Delete row
98+
await db.raw(
99+
'DELETE FROM rating_queue WHERE match_seq_num = ?',
100+
row.match_seq_num,
101+
);
102+
// Commit transaction
103+
await db.raw('COMMIT');
104+
}
65105
}
66106

67107
async function prefetchGcData() {
68-
while (true) {
69-
// Find a row in the queue that doesn't have gcdata
70-
const { rows } = await db.raw<{rows: { match_seq_num: number, match_id: number, pgroup: PGroup}[]}>('SELECT match_seq_num, match_id, pgroup from rating_queue WHERE gcdata IS NULL LIMIT 1');
71-
const row = rows[0];
72-
if (!row) {
73-
// No rows, wait and try again
74-
await new Promise(resolve => setTimeout(resolve, 1000));
75-
}
76-
// Attempt to fetch once
77-
const { data } = await gcFetcher.getOrFetchData(row.match_id, { pgroup: row.pgroup });
78-
if (data) {
79-
// If successful, update
80-
await db.raw('UPDATE rating_queue SET gcdata = ? WHERE match_seq_num = ?', [JSON.stringify(data), row.match_seq_num]);
81-
}
108+
while (true) {
109+
// Find a row in the queue that doesn't have gcdata
110+
const { rows } = await db.raw<{
111+
rows: { match_seq_num: number; match_id: number; pgroup: PGroup }[];
112+
}>(
113+
'SELECT match_seq_num, match_id, pgroup from rating_queue WHERE gcdata IS NULL LIMIT 1',
114+
);
115+
const row = rows[0];
116+
if (!row) {
117+
// No rows, wait and try again
118+
await new Promise((resolve) => setTimeout(resolve, 1000));
119+
}
120+
// Attempt to fetch once
121+
const { data } = await gcFetcher.getOrFetchData(row.match_id, {
122+
pgroup: row.pgroup,
123+
});
124+
if (data) {
125+
// If successful, update
126+
await db.raw(
127+
'UPDATE rating_queue SET gcdata = ? WHERE match_seq_num = ?',
128+
[JSON.stringify(data), row.match_seq_num],
129+
);
82130
}
131+
}
83132
}
84133

85134
doRate();
86-
prefetchGcData();
135+
prefetchGcData();

svc/util/buildStatus.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ export async function buildStatus() {
9393
pmh_parsed_last_day: async () => countDay('pmh_parsed'),
9494
reconcile_last_day: async () => countDay('reconcile'),
9595
requests_last_day: async () => countDay('request'),
96-
distinct_requests_last_day: async () => countDayDistinct('distinct_request'),
96+
distinct_requests_last_day: async () =>
97+
countDayDistinct('distinct_request'),
9798
requests_ui_last_day: async () => countDay('request_ui'),
9899
requests_api_key_last_day: async () => countDay('request_api_key'),
99100
request_api_fail_last_day: async () => countDay('request_api_fail'),
@@ -109,7 +110,7 @@ export async function buildStatus() {
109110
regcdata_last_day: async () => countDay('regcdata'),
110111
reparse_last_day: async () => countDay('reparse'),
111112
// oldparse_last_day: async () => countDay('oldparse'),
112-
113+
113114
steam_api_calls_last_day: async () => countDay('steam_api_call'),
114115
steam_proxy_calls_last_day: async () => countDay('steam_proxy_call'),
115116
steam_429_last_day: async () => countDay('steam_429'),

svc/util/insert.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,22 @@ export async function insertMatch(
537537
async function decideRate(match: InsertMatchInput) {
538538
// Decide whether to rate the match
539539
// Rate a percentage of ranked matches
540-
if (options.origin === 'scanner' &&
540+
if (
541+
options.origin === 'scanner' &&
541542
options.type === 'api' &&
542543
'lobby_type' in match &&
543544
match.lobby_type === 7 &&
544545
match.match_id % 100 < Number(config.RATING_PERCENT)
545546
) {
546-
await db.raw('INSERT INTO rating_queue(match_seq_num, match_id, pgroup, radiant_win) VALUES(?, ?, ?, ?) ON CONFLICT DO NOTHING', [match.match_seq_num, match.match_id, JSON.stringify(pgroup), match.radiant_win]);
547+
await db.raw(
548+
'INSERT INTO rating_queue(match_seq_num, match_id, pgroup, radiant_win) VALUES(?, ?, ?, ?) ON CONFLICT DO NOTHING',
549+
[
550+
match.match_seq_num,
551+
match.match_id,
552+
JSON.stringify(pgroup),
553+
match.radiant_win,
554+
],
555+
);
547556
}
548557
}
549558

0 commit comments

Comments
 (0)