From a0bf4c53f5e2090c05c98a698e85d5912a55abe7 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Thu, 27 Oct 2022 19:38:29 +0800 Subject: [PATCH] feat: remove _bid --- src/app.ts | 28 ++++++++++++++++++---------- src/splatnet3.ts | 7 +------ src/types.ts | 2 -- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/app.ts b/src/app.ts index 890c3d3..1d2d279 100644 --- a/src/app.ts +++ b/src/app.ts @@ -72,13 +72,12 @@ class GameFetcher { this.state = state; this.cache = cache; } - private async getLock(id: string): Promise { - const bid = await gameId(id); + private getLock(id: string): Mutex { + let cur = this.lock[id]; - let cur = this.lock[bid]; if (!cur) { cur = new Mutex(); - this.lock[bid] = cur; + this.lock[id] = cur; } return cur; @@ -137,10 +136,18 @@ class GameFetcher { }; } async getBattleMetaById(id: string): Promise> { - const bid = await gameId(id); + const gid = await gameId(id); const bankaraHistory = await this.getBankaraHistory(); + const gameIdMap = new Map(); + + for (const i of bankaraHistory) { + for (const j of i.historyDetails.nodes) { + gameIdMap.set(j, await gameId(j.id)); + } + } + const group = bankaraHistory.find((i) => - i.historyDetails.nodes.some((i) => i._bid === bid) + i.historyDetails.nodes.some((i) => gameIdMap.get(i) === gid) ); if (!group) { @@ -153,8 +160,9 @@ class GameFetcher { } const { bankaraMatchChallenge } = group; - const listNode = group.historyDetails.nodes.find((i) => i._bid === bid) ?? - null; + const listNode = + group.historyDetails.nodes.find((i) => gameIdMap.get(i) === gid) ?? + null; const index = group.historyDetails.nodes.indexOf(listNode!); let challengeProgress: null | ChallengeProgress = null; @@ -179,11 +187,11 @@ class GameFetcher { challengeProgress, }; } - async cacheDetail( + cacheDetail( id: string, getter: () => Promise, ): Promise { - const lock = await this.getLock(id); + const lock = this.getLock(id); return lock.use(async () => { const cached = await this.cache.read(id); diff --git a/src/splatnet3.ts b/src/splatnet3.ts index aee4e1e..9ba8a41 100644 --- a/src/splatnet3.ts +++ b/src/splatnet3.ts @@ -13,7 +13,6 @@ import { RespMap, VarsMap, } from "./types.ts"; -import { gameId } from "./utils.ts"; async function request( state: State, @@ -154,11 +153,7 @@ export function getCoopDetail( export async function getBankaraBattleHistories(state: State) { const resp = await request(state, Queries.BankaraBattleHistoriesQuery); - for (const i of resp.bankaraBattleHistories.historyGroups.nodes) { - for (const j of i.historyDetails.nodes) { - j._bid = await gameId(j.id); - } - } + return resp; } diff --git a/src/types.ts b/src/types.ts index 505929a..ae76d52 100644 --- a/src/types.ts +++ b/src/types.ts @@ -40,8 +40,6 @@ export type BankaraMatchChallenge = { earnedUdemaePoint: number | null; }; export type BattleListNode = { - // battle id added after fetch - _bid: string; id: string; udemae: string; judgement: "LOSE" | "WIN" | "DEEMED_LOSE" | "EXEMPTED_LOSE" | "DRAW";