From 15c0e2096604b0f60c2a4b44e0da61b3b9c840f3 Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Wed, 19 Oct 2022 19:46:03 +0800 Subject: [PATCH] feat: getBattleList --- deps.ts | 1 + s3si.ts | 11 +++--- splatnet3.ts | 62 ++++++++++++++++++++++++++++++--- types.ts | 96 ++++++++++++++++++---------------------------------- 4 files changed, 96 insertions(+), 74 deletions(-) diff --git a/deps.ts b/deps.ts index 48c0da3..3d57a95 100644 --- a/deps.ts +++ b/deps.ts @@ -7,3 +7,4 @@ export * as base64 from "https://deno.land/std@0.160.0/encoding/base64.ts"; export * as flags from "https://deno.land/std@0.160.0/flags/mod.ts"; export * as io from "https://deno.land/std@0.160.0/io/mod.ts"; export * as uuid from "https://deno.land/std@0.160.0/uuid/mod.ts"; +export * as msgpack from "https://deno.land/x/msgpack@v1.4/mod.ts"; diff --git a/s3si.ts b/s3si.ts index 0cc65d3..29cd8e0 100644 --- a/s3si.ts +++ b/s3si.ts @@ -2,7 +2,7 @@ import { getBulletToken, getGToken, loginManually } from "./iksm.ts"; import { APIError } from "./APIError.ts"; import { flags } from "./deps.ts"; import { DEFAULT_STATE, State } from "./state.ts"; -import { checkToken } from "./splatnet3.ts"; +import { checkToken, getBattleList } from "./splatnet3.ts"; type Opts = { configPath: string; @@ -65,9 +65,9 @@ Options: } const sessionToken = this.state.loginState.sessionToken!; - if ( - !this.state.loginState?.gToken || !this.state.loginState.bulletToken - ) { + if (!await checkToken(this.state)) { + console.log("Token expired, refetch tokens."); + const { webServiceToken, userCountry, userLang } = await getGToken({ fApi: this.state.fGen, sessionToken, @@ -94,7 +94,8 @@ Options: await this.writeState(); } - await checkToken(this.state); + const battleList = await getBattleList(this.state); + console.log(battleList); } catch (e) { if (e instanceof APIError) { console.error(`APIError: ${e.message}`, e.response, e.json); diff --git a/splatnet3.ts b/splatnet3.ts index 7998bd1..c743cc0 100644 --- a/splatnet3.ts +++ b/splatnet3.ts @@ -5,6 +5,7 @@ import { APIError } from "./APIError.ts"; import { BattleType, GraphQLResponse, + HistoryGroups, Queries, RespMap, VarsMap, @@ -13,8 +14,9 @@ import { async function request( state: State, query: Q, - variables: VarsMap[Q], + ...rest: VarsMap[Q] ): Promise { + const variables = rest?.[0] ?? {}; const body = { extensions: { persistedQuery: { @@ -68,10 +70,60 @@ export async function checkToken(state: State) { return false; } - await request(state, Queries.HomeQuery, {}); - - return true; + try { + await request(state, Queries.HomeQuery); + return true; + } catch (_e) { + return false; + } } -export async function getBattleList(params: BattleType) { +function getIdsFromGroups({ historyGroups }: { historyGroups: HistoryGroups }) { + return historyGroups.nodes.flatMap((i) => i.historyDetails.nodes).map((i) => + i.id + ); +} + +const BATTLE_LIST_TYPE_MAP: Record< + BattleType, + (state: State) => Promise +> = { + [BattleType.Regular]: (state: State) => + request(state, Queries.RegularBattleHistoriesQuery) + .then((r) => getIdsFromGroups(r.regularBattleHistories)), + [BattleType.Bankara]: (state: State) => + request(state, Queries.BankaraBattleHistoriesQuery) + .then((r) => getIdsFromGroups(r.bankaraBattleHistories)), + [BattleType.Private]: (state: State) => + request(state, Queries.PrivateBattleHistoriesQuery) + .then((r) => getIdsFromGroups(r.privateBattleHistories)), +}; + +export async function getBattleList( + state: State, + types: BattleType[] = [ + BattleType.Regular, + BattleType.Bankara, + BattleType.Private, + ], +) { + const out = []; + for (const battleType of types) { + const ids = await BATTLE_LIST_TYPE_MAP[battleType](state); + out.push(...ids); + } + return out; +} + +export function getBattleDetail( + state: State, + id: string, +) { + return request( + state, + Queries.VsHistoryDetailQuery, + { + vsResultId: id, + }, + ); } diff --git a/types.ts b/types.ts index 889fcc0..21e53ed 100644 --- a/types.ts +++ b/types.ts @@ -9,18 +9,18 @@ export enum Queries { CoopHistoryDetailQuery = "f3799a033f0a7ad4b1b396f9a3bafb1e", } export type VarsMap = { - [Queries.HomeQuery]: Record; - [Queries.LatestBattleHistoriesQuery]: Record; - [Queries.RegularBattleHistoriesQuery]: Record; - [Queries.BankaraBattleHistoriesQuery]: Record; - [Queries.PrivateBattleHistoriesQuery]: Record; - [Queries.VsHistoryDetailQuery]: { + [Queries.HomeQuery]: []; + [Queries.LatestBattleHistoriesQuery]: []; + [Queries.RegularBattleHistoriesQuery]: []; + [Queries.BankaraBattleHistoriesQuery]: []; + [Queries.PrivateBattleHistoriesQuery]: []; + [Queries.VsHistoryDetailQuery]: [{ vsResultId: string; - }; - [Queries.CoopHistoryQuery]: Record; - [Queries.CoopHistoryDetailQuery]: { + }]; + [Queries.CoopHistoryQuery]: []; + [Queries.CoopHistoryDetailQuery]: [{ coopHistoryDetailId: string; - }; + }]; }; export type Image = { @@ -28,6 +28,15 @@ export type Image = { width?: number; height?: number; }; +export type HistoryGroups = { + nodes: { + historyDetails: { + nodes: { + id: string; + }[]; + }; + }[]; +}; export type RespMap = { [Queries.HomeQuery]: { currentPlayer: { @@ -48,56 +57,21 @@ export type RespMap = { footerMessages: unknown[]; }; [Queries.LatestBattleHistoriesQuery]: Record; - [Queries.RegularBattleHistoriesQuery]: Record; - [Queries.BankaraBattleHistoriesQuery]: { - bankaraBattleHistories: { - summary: { - assistAverage: number; - deathAverage: number; - killAverage: number; - lose: number; - perUnitTimeMinute: number; - specialAverage: number; - win: number; - }; - historyGroups: { - nodes: { - bankaraMatchChallenge: null | { - winCount: number; - loseCount: number; - maxWinCount: number; - maxLoseCount: number; - state: "Failed"; - isPromo: boolean; - isUdemaeUp: boolean; - udemaeAfter: string; - earnedUdemaePoint: number; - }; - historyDetails: { - nodes: { - id: string; - vsMode: { - mode: "BANKARA"; - id: string; - }; - vsRule: { - name: string; - id: string; - }; - vsStage: { - name: string; - id: string; - image: Image; - }; - judgement: "LOSE"; - player: unknown; - }[]; - }; - }[]; - }; + [Queries.RegularBattleHistoriesQuery]: { + regularBattleHistories: { + historyGroups: HistoryGroups; + }; + }; + [Queries.BankaraBattleHistoriesQuery]: { + bankaraBattleHistories: { + historyGroups: HistoryGroups; + }; + }; + [Queries.PrivateBattleHistoriesQuery]: { + privateBattleHistories: { + historyGroups: HistoryGroups; }; }; - [Queries.PrivateBattleHistoriesQuery]: Record; [Queries.VsHistoryDetailQuery]: Record; [Queries.CoopHistoryQuery]: Record; [Queries.CoopHistoryDetailQuery]: Record; @@ -115,9 +89,3 @@ export enum BattleType { Bankara, Private, } - -export const BATTLE_QUERY_MAP: Record = { - [BattleType.Regular]: Queries.RegularBattleHistoriesQuery, - [BattleType.Bankara]: Queries.BankaraBattleHistoriesQuery, - [BattleType.Private]: Queries.PrivateBattleHistoriesQuery, -};