From 933b43850537a1bc500df83bdd8c5546791d81ed Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Sat, 5 Nov 2022 12:57:29 +0800 Subject: [PATCH] feat: workaround for incorrect translate from stat.ink --- src/app.ts | 24 +++++++++++++++++++----- src/exporters/stat.ink.ts | 29 +++++++++++++++++++++++------ src/splatnet3.ts | 9 +++++++++ src/types.ts | 10 ++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/app.ts b/src/app.ts index 719ee5b..1c4cfe3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -6,7 +6,7 @@ import { State, StateBackend, } from "./state.ts"; -import { getBattleList, isTokenExpired } from "./splatnet3.ts"; +import { getBattleList, getGearPower, isTokenExpired } from "./splatnet3.ts"; import { BattleListType, Game, GameExporter } from "./types.ts"; import { Cache, FileCache } from "./cache.ts"; import { StatInkExporter } from "./exporters/stat.ink.ts"; @@ -65,6 +65,7 @@ export class App { await this.fetchToken(); }, }; + gearMap: Record | null = null; constructor(public opts: Opts) { this.stateBackend = opts.stateBackend ?? @@ -88,6 +89,16 @@ export class App { await this.writeState(DEFAULT_STATE); } } + async getGearMap() { + if (this.gearMap) { + return this.gearMap; + } + const { gearPowers } = await getGearPower(this.state); + this.gearMap = Object.fromEntries( + gearPowers.nodes.map((i, id) => [i.name, id]), + ); + return this.gearMap; + } getSkipMode(): ("vs" | "coop")[] { const mode = this.opts.skipMode; if (mode === "vs") { @@ -115,10 +126,13 @@ export class App { }); } out.push( - new StatInkExporter( - this.state.statInkApiKey!, - this.opts.monitor ? "Monitoring" : "Manual", - ), + new StatInkExporter({ + statInkApiKey: this.state.statInkApiKey!, + uploadMode: this.opts.monitor ? "Monitoring" : "Manual", + nameDict: { + gearPower: await this.getGearMap(), + }, + }), ); } diff --git a/src/exporters/stat.ink.ts b/src/exporters/stat.ink.ts index 69a6441..5e57573 100644 --- a/src/exporters/stat.ink.ts +++ b/src/exporters/stat.ink.ts @@ -53,6 +53,10 @@ async function _getStage(): Promise { const getAbility = cache(_getAbility); const getStage = cache(_getStage); +export type NameDict = { + gearPower: Record; +}; + /** * Exporter to stat.ink. * @@ -60,11 +64,23 @@ const getStage = cache(_getStage); */ export class StatInkExporter implements GameExporter { name = "stat.ink"; + private statInkApiKey: string; + private uploadMode: string; + private nameDict: NameDict; - constructor(private statInkApiKey: string, private uploadMode: string) { + constructor( + { statInkApiKey, uploadMode, nameDict }: { + statInkApiKey: string; + uploadMode: string; + nameDict: NameDict; + }, + ) { if (statInkApiKey.length !== 43) { throw new Error("Invalid stat.ink API key"); } + this.statInkApiKey = statInkApiKey; + this.uploadMode = uploadMode; + this.nameDict = nameDict; } requestHeaders() { return { @@ -176,12 +192,13 @@ export class StatInkExporter implements GameExporter { async mapGears( { headGear, clothingGear, shoesGear }: VsPlayer, ): Promise { - const amap = (await getAbility()).map((i) => ({ - ...i, - names: Object.values(i.name), - })); + const amap = await getAbility(); const mapAbility = ({ name }: { name: string }): string | null => { - const result = amap.find((a) => a.names.includes(name)); + const abilityIdx = this.nameDict.gearPower[name]; + if (!abilityIdx) { + return null; + } + const result = amap[abilityIdx]; if (!result) { return null; } diff --git a/src/splatnet3.ts b/src/splatnet3.ts index 9ba8a41..9e3e021 100644 --- a/src/splatnet3.ts +++ b/src/splatnet3.ts @@ -162,3 +162,12 @@ export async function getCoopHistories(state: State) { return resp; } + +export async function getGearPower(state: State) { + const resp = await request( + state, + Queries.myOutfitCommonDataFilteringConditionQuery, + ); + + return resp; +} diff --git a/src/types.ts b/src/types.ts index f5f21e1..b0dbf91 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,8 @@ export enum Queries { VsHistoryDetailQuery = "2b085984f729cd51938fc069ceef784a", CoopHistoryQuery = "817618ce39bcf5570f52a97d73301b30", CoopHistoryDetailQuery = "f3799a033f0a7ad4b1b396f9a3bafb1e", + myOutfitCommonDataFilteringConditionQuery = + "d02ab22c9dccc440076055c8baa0fa7a", } export type VarsMap = { [Queries.HomeQuery]: []; @@ -23,6 +25,7 @@ export type VarsMap = { [Queries.CoopHistoryDetailQuery]: [{ coopHistoryDetailId: string; }]; + [Queries.myOutfitCommonDataFilteringConditionQuery]: []; }; export type Image = { @@ -240,6 +243,13 @@ export type RespMap = { [Queries.CoopHistoryDetailQuery]: { coopHistoryDetail: CoopHistoryDetail; }; + [Queries.myOutfitCommonDataFilteringConditionQuery]: { + gearPowers: { + nodes: { + name: string; + }[]; + }; + }; }; export type GraphQLResponse = { data: T;