Skip to content

Commit

Permalink
Simplify reqs checking code to ensure fuewer future issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git committed Oct 29, 2024
1 parent 38a0881 commit 61db6f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 18 additions & 9 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import * as tables from './tables';
import {SQL} from './database';
import IPTools from './ip-tools';

export interface Suspect {
formatid: string;
start_date: number;
coil: number | null;
gxe: number | null;
elo: number | null;
}

const OAUTH_TOKEN_TIME = 2 * 7 * 24 * 60 * 60 * 1000;

async function getOAuthClient(clientId?: string, origin?: string) {
Expand Down Expand Up @@ -83,27 +91,26 @@ export const smogonFetch = async (targetUrl: string, method: string, data: {[k:

export function checkSuspectVerified(
rating: LadderEntry,
suspect: {formatid: string, start_date: number},
reqs: Record<string, number | null>
suspect: Suspect,
) {
let reqsMet = 0;
let reqCount = 0;
const userData: Partial<{elo: number, gxe: number, coil: number}> = {};
const reqKeys = ['elo', 'coil', 'gxe'] as const;
for (const k of reqKeys) {
if (!reqs[k]) continue;
if (!suspect[k]) continue;
reqCount++;
switch (k) {
case 'coil':
const N = rating.w + rating.l + rating.t;
const coilNum = Math.round(40.0 * rating.gxe * Math.pow(2.0, -coil[suspect.formatid] / N));
if (coilNum >= reqs.coil!) {
if (coilNum >= suspect.coil!) {
reqsMet++;
}
userData.coil = coilNum;
break;
case 'elo': case 'gxe':
if (reqs[k] && rating[k] >= reqs[k]!) {
if (suspect[k] && rating[k] >= suspect[k]!) {
reqsMet++;
}
userData[k] = rating[k];
Expand All @@ -119,7 +126,10 @@ export function checkSuspectVerified(
void smogonFetch("tools/api/suspect-verify", "POST", {
userid: rating.userid,
format: suspect.formatid,
reqs: {required: reqs, actual: userData},
reqs: {
required: {elo: suspect.elo, gxe: suspect.gxe, coil: suspect.coil},
actual: userData,
},
suspectStartDate: suspect.start_date,
});
return true;
Expand Down Expand Up @@ -417,9 +427,8 @@ export const actions: {[k: string]: QueryHandler} = {

const suspect = await tables.suspects.get(formatid);
if (suspect) {
const reqs = {elo: suspect.elo, gxe: suspect.gxe, coil: suspect.coil};
for (const rating of [p1rating, p2rating]) {
checkSuspectVerified(rating, suspect, reqs);
checkSuspectVerified(rating, suspect);
}
}
out.actionsuccess = true;
Expand Down Expand Up @@ -1109,7 +1118,7 @@ export const actions: {[k: string]: QueryHandler} = {
const rating = await new Ladder(id).getRating(userid);
if (!rating) throw new ActionError("That user has no ratings in the given ladder.");
return {
result: checkSuspectVerified(rating, suspect, {elo: suspect.elo, coil: suspect.coil, gxe: suspect.gxe}),
result: checkSuspectVerified(rating, suspect),
};
},
};
Expand Down
9 changes: 2 additions & 7 deletions src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Config} from './config-loader';

import type {LadderEntry} from './ladder';
import type {ReplayRow} from './replays';
import type {Suspect} from './actions';

// direct access
export const psdb = new MySQLDatabase(Config.mysql);
Expand Down Expand Up @@ -143,10 +144,4 @@ export const teams = pgdb.getTable<{
private: number;
}>('teams', 'teamid');

export const suspects = psdb.getTable<{
formatid: string;
start_date: number;
coil: number | null;
gxe: number | null;
elo: number | null;
}>("suspects", 'formatid');
export const suspects = psdb.getTable<Suspect>("suspects", 'formatid');

0 comments on commit 61db6f5

Please sign in to comment.