feat: remove _bid

main
spacemeowx2 2022-10-27 19:38:29 +08:00
parent 4449664c15
commit a0bf4c53f5
3 changed files with 19 additions and 18 deletions

View File

@ -72,13 +72,12 @@ class GameFetcher {
this.state = state; this.state = state;
this.cache = cache; this.cache = cache;
} }
private async getLock(id: string): Promise<Mutex> { private getLock(id: string): Mutex {
const bid = await gameId(id); let cur = this.lock[id];
let cur = this.lock[bid];
if (!cur) { if (!cur) {
cur = new Mutex(); cur = new Mutex();
this.lock[bid] = cur; this.lock[id] = cur;
} }
return cur; return cur;
@ -137,10 +136,18 @@ class GameFetcher {
}; };
} }
async getBattleMetaById(id: string): Promise<Omit<VsInfo, "detail">> { async getBattleMetaById(id: string): Promise<Omit<VsInfo, "detail">> {
const bid = await gameId(id); const gid = await gameId(id);
const bankaraHistory = await this.getBankaraHistory(); const bankaraHistory = await this.getBankaraHistory();
const gameIdMap = new Map<BattleListNode, string>();
for (const i of bankaraHistory) {
for (const j of i.historyDetails.nodes) {
gameIdMap.set(j, await gameId(j.id));
}
}
const group = bankaraHistory.find((i) => const group = bankaraHistory.find((i) =>
i.historyDetails.nodes.some((i) => i._bid === bid) i.historyDetails.nodes.some((i) => gameIdMap.get(i) === gid)
); );
if (!group) { if (!group) {
@ -153,8 +160,9 @@ class GameFetcher {
} }
const { bankaraMatchChallenge } = group; const { bankaraMatchChallenge } = group;
const listNode = group.historyDetails.nodes.find((i) => i._bid === bid) ?? const listNode =
null; group.historyDetails.nodes.find((i) => gameIdMap.get(i) === gid) ??
null;
const index = group.historyDetails.nodes.indexOf(listNode!); const index = group.historyDetails.nodes.indexOf(listNode!);
let challengeProgress: null | ChallengeProgress = null; let challengeProgress: null | ChallengeProgress = null;
@ -179,11 +187,11 @@ class GameFetcher {
challengeProgress, challengeProgress,
}; };
} }
async cacheDetail<T>( cacheDetail<T>(
id: string, id: string,
getter: () => Promise<T>, getter: () => Promise<T>,
): Promise<T> { ): Promise<T> {
const lock = await this.getLock(id); const lock = this.getLock(id);
return lock.use(async () => { return lock.use(async () => {
const cached = await this.cache.read<T>(id); const cached = await this.cache.read<T>(id);

View File

@ -13,7 +13,6 @@ import {
RespMap, RespMap,
VarsMap, VarsMap,
} from "./types.ts"; } from "./types.ts";
import { gameId } from "./utils.ts";
async function request<Q extends Queries>( async function request<Q extends Queries>(
state: State, state: State,
@ -154,11 +153,7 @@ export function getCoopDetail(
export async function getBankaraBattleHistories(state: State) { export async function getBankaraBattleHistories(state: State) {
const resp = await request(state, Queries.BankaraBattleHistoriesQuery); 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; return resp;
} }

View File

@ -40,8 +40,6 @@ export type BankaraMatchChallenge = {
earnedUdemaePoint: number | null; earnedUdemaePoint: number | null;
}; };
export type BattleListNode = { export type BattleListNode = {
// battle id added after fetch
_bid: string;
id: string; id: string;
udemae: string; udemae: string;
judgement: "LOSE" | "WIN" | "DEEMED_LOSE" | "EXEMPTED_LOSE" | "DRAW"; judgement: "LOSE" | "WIN" | "DEEMED_LOSE" | "EXEMPTED_LOSE" | "DRAW";