aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Matei Pintilie <lucafulger@gmail.com>2022-08-20 12:35:27 +0000
committerLuca Matei Pintilie <lucafulger@gmail.com>2022-08-20 12:35:27 +0000
commitbc94583564499d32586b6b1d4eb304f127d1100d (patch)
tree8a792cc8e4fc5a9ea2694cafdce8a3b3cdb15702
parent07dabb5c3bec0f6d0ee9ae848ae7151f39de46c6 (diff)
downloadspeedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.tar
speedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.tar.gz
speedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.tar.bz2
speedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.tar.lz
speedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.tar.xz
speedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.tar.zst
speedrunbot-slash-bc94583564499d32586b6b1d4eb304f127d1100d.zip
Increase counting limit to 20k by counting ascending and descending
Diffstat (limited to '')
-rwxr-xr-xsrc/srcom/runs_count.ts15
-rwxr-xr-xsrc/srcom/utils.ts39
2 files changed, 40 insertions, 14 deletions
diff --git a/src/srcom/runs_count.ts b/src/srcom/runs_count.ts
index 45a42b8..4f6e370 100755
--- a/src/srcom/runs_count.ts
+++ b/src/srcom/runs_count.ts
@@ -58,10 +58,9 @@ export async function runsCount(
individualLevelRuns++;
} else fullGameRuns++;
if (games.length && games.length !== 1) {
- // @ts-ignore If I put in the link `embed=game` then this will exist
- const name = run.game.data.names.international;
- if (isNaN(gameCount[name])) gameCount[name] = 1;
- else gameCount[name]++;
+ const gameId = run.game;
+ if (isNaN(gameCount[gameId])) gameCount[gameId] = 1;
+ else gameCount[gameId]++;
}
if (run.status.status === "verified") verifiedRuns++;
else if (run.status.status === "rejected") rejectedRuns++;
@@ -95,7 +94,13 @@ export async function runsCount(
if (Object.keys(gameCount).length) {
output.push("");
for (const game in gameCount) {
- output.push(`${fmt.bold(game)}: ${gameCount[game]}`);
+ output.push(
+ `${
+ fmt.bold(
+ games.find((gameObj) => gameObj.id === game)!.names.international,
+ )
+ }: ${gameCount[game]}`,
+ );
}
output.push("");
}
diff --git a/src/srcom/utils.ts b/src/srcom/utils.ts
index 3239633..a853f41 100755
--- a/src/srcom/utils.ts
+++ b/src/srcom/utils.ts
@@ -10,8 +10,12 @@ export interface Opts {
signal?: AbortSignal;
}
+type ApiData = {
+ id: string;
+};
+
interface ApiArrayResponse {
- data: unknown[];
+ data: (ApiData & unknown)[];
pagination: {
size: number;
max: number;
@@ -90,13 +94,17 @@ export async function getUsers(
) => !!user) as SpeedrunCom.User[];
}
-export async function getAll<T>(
+export async function getAll<T extends ApiData>(
url: URL | string,
- { signal }: { signal?: AbortSignal } = {},
+ { signal, lastId }: { signal?: AbortSignal; lastId?: string } = {},
): Promise<T[]> {
url = new URL(url.toString());
+ url.searchParams.set("orderby", "date");
+ if (lastId) {
+ url.searchParams.set("direction", "desc");
+ }
url.searchParams.set("max", "200");
- let data: unknown[] = [];
+ let data: ApiData[] = [];
let size = 0;
let tmpSize;
let attempts = 0;
@@ -114,21 +122,34 @@ export async function getAll<T>(
const body = await res.json();
// Above 10k speedrun.com just breaks
// With this error message
- if (body.message === "Invalid pagination values.") break;
- else {
+ if (body.message === "Invalid pagination values.") {
+ lastId = data.at(-1)?.id;
+ break;
+ } else {
// This is an unexpected error
// So try to walk it off
- console.error(body);
break;
}
}
continue;
} else attempts = 0;
const resJSON = await res.json() as ApiArrayResponse;
- data = data.concat(resJSON.data);
+ const lastIdIndex = resJSON.data.findIndex((entry) => {
+ return entry.id === lastId;
+ });
+ const lastIdFound = lastIdIndex !== -1;
+ data = data.concat(
+ lastIdFound ? resJSON.data.slice(-(lastIdIndex)) : resJSON.data,
+ );
+ if (lastIdFound) {
+ lastId = undefined;
+ break;
+ }
size += resJSON.pagination.size;
tmpSize = resJSON.pagination.size;
} while (tmpSize === 200);
+ if (lastId) data = data.concat(await getAll<T>(url, { signal, lastId }));
+
return data as T[];
}
@@ -226,7 +247,7 @@ export async function getAllRuns(
emulated: undefined | string | boolean,
{ signal }: { signal?: AbortSignal } = {},
): Promise<ExtensiveRun[]> {
- const url = new URL(`${SRC_API}/runs?embed=game,category,level,players`);
+ const url = new URL(`${SRC_API}/runs?embed=category,level,players`);
if (status) url.searchParams.set("status", status);
if (typeof emulated !== "undefined") {