From 5bac5b6abcd60fe97baf8065c73bb527306449df Mon Sep 17 00:00:00 2001 From: Tau Date: Tue, 7 May 2019 18:01:48 -0400 Subject: [PATCH] idz: Tweak how request variants are modelled --- src/idz/encoder/index.ts | 6 +---- src/idz/encoder/loadProfile.ts | 16 +++++++++--- src/idz/encoder/loadProfile1.ts | 12 +++++++++ src/idz/encoder/loadProfile2.ts | 2 +- src/idz/handler/loadProfile.ts | 7 ++--- src/idz/response/index.ts | 2 -- src/idz/response/loadProfile.ts | 45 ++++++++++++++++++++++++++++++-- src/idz/response/loadProfile2.ts | 34 ------------------------ 8 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 src/idz/encoder/loadProfile1.ts delete mode 100644 src/idz/response/loadProfile2.ts diff --git a/src/idz/encoder/index.ts b/src/idz/encoder/index.ts index 97026e7..32e2512 100644 --- a/src/idz/encoder/index.ts +++ b/src/idz/encoder/index.ts @@ -13,7 +13,6 @@ import { loadGarage } from "./loadGarage"; import { loadGeneralReward } from "./loadGeneralReward"; import { loadGhost } from "./loadGhost"; import { loadProfile } from "./loadProfile"; -import { loadProfile2 } from "./loadProfile2"; import { loadRewardTable } from "./loadRewardTable"; import { loadServerList } from "./loadServerList"; import { loadStocker } from "./loadStocker"; @@ -64,12 +63,9 @@ function encode(res: Response): Buffer { case "load_ghost_res": return loadGhost(res); - case "load_profile_v1_res": + case "load_profile_res": return loadProfile(res); - case "load_profile_v2_res": - return loadProfile2(res); - case "load_reward_table_res": return loadRewardTable(res); diff --git a/src/idz/encoder/loadProfile.ts b/src/idz/encoder/loadProfile.ts index 3ecacf3..1435428 100644 --- a/src/idz/encoder/loadProfile.ts +++ b/src/idz/encoder/loadProfile.ts @@ -1,10 +1,18 @@ +import { loadProfile1 } from "./loadProfile1"; +import { loadProfile2 } from "./loadProfile2"; import { LoadProfileResponse } from "../response/loadProfile"; -// Sending this causes an error export function loadProfile(res: LoadProfileResponse) { - const buf = Buffer.alloc(0x0c60); + switch (res.format) { + case 1: + return loadProfile1(res); - buf.writeInt16LE(0x0064, 0x0000); + case 2: + return loadProfile2(res); - return buf; + default: + const exhaust: never = res; + + throw new Error(`Unsupported profile response format ${res["format"]}`); + } } diff --git a/src/idz/encoder/loadProfile1.ts b/src/idz/encoder/loadProfile1.ts new file mode 100644 index 0000000..0dd9d73 --- /dev/null +++ b/src/idz/encoder/loadProfile1.ts @@ -0,0 +1,12 @@ +import { LoadProfileResponse1 } from "../response/loadProfile"; + +// Sending this causes an error in v1.21, so it is currently unmapped and +// unimplemented. + +export function loadProfile1(res: LoadProfileResponse1) { + const buf = Buffer.alloc(0x0c60); + + buf.writeInt16LE(0x0064, 0x0000); + + return buf; +} diff --git a/src/idz/encoder/loadProfile2.ts b/src/idz/encoder/loadProfile2.ts index dff0b4a..9c9bc17 100644 --- a/src/idz/encoder/loadProfile2.ts +++ b/src/idz/encoder/loadProfile2.ts @@ -4,7 +4,7 @@ import { bitmap } from "./_bitmap"; import { car } from "./_car"; import { chara } from "./_chara"; import { mission } from "./_mission"; -import { LoadProfileResponse2 } from "../response/loadProfile2"; +import { LoadProfileResponse2 } from "../response/loadProfile"; export function loadProfile2(res: LoadProfileResponse2) { const buf = Buffer.alloc(0x0d30); diff --git a/src/idz/handler/loadProfile.ts b/src/idz/handler/loadProfile.ts index acce479..2926bfa 100644 --- a/src/idz/handler/loadProfile.ts +++ b/src/idz/handler/loadProfile.ts @@ -1,11 +1,11 @@ import { LoadProfileRequest } from "../request/loadProfile"; -import { LoadProfileResponse2 } from "../response/loadProfile2"; +import { LoadProfileResponse } from "../response/loadProfile"; import { Repositories } from "../repo"; export async function loadProfile( w: Repositories, req: LoadProfileRequest -): Promise { +): Promise { // Promise.all would be messy here, who cares anyway this isn't supposed to // be a high-performance server. @@ -23,7 +23,8 @@ export async function loadProfile( const tickets = await w.tickets().load(profile.id); return { - type: "load_profile_v2_res", + type: "load_profile_res", + format: 2, name: profile.name, profileId: profile.id, lv: profile.lv, diff --git a/src/idz/response/index.ts b/src/idz/response/index.ts index a644b8d..54d75b3 100644 --- a/src/idz/response/index.ts +++ b/src/idz/response/index.ts @@ -10,7 +10,6 @@ import { LoadGarageResponse } from "./loadGarage"; import { LoadGeneralRewardResponse } from "./loadGeneralReward"; import { LoadGhostResponse } from "./loadGhost"; import { LoadProfileResponse } from "./loadProfile"; -import { LoadProfileResponse2 } from "./loadProfile2"; import { LoadRewardTableResponse } from "./loadRewardTable"; import { LoadServerListResponse } from "./loadServerList"; import { LoadStockerResponse } from "./loadStocker"; @@ -40,7 +39,6 @@ export type Response = | LoadGeneralRewardResponse | LoadGhostResponse | LoadProfileResponse - | LoadProfileResponse2 | LoadRewardTableResponse | LoadServerListResponse | LoadStockerResponse diff --git a/src/idz/response/loadProfile.ts b/src/idz/response/loadProfile.ts index 11f2145..7366edb 100644 --- a/src/idz/response/loadProfile.ts +++ b/src/idz/response/loadProfile.ts @@ -1,3 +1,44 @@ -export interface LoadProfileResponse { - type: "load_profile_v1_res"; +import { ExtId, TitleCode } from "../model/base"; +import { Car } from "../model/car"; +import { Chara } from "../model/chara"; +import { MissionState } from "../model/mission"; +import { Profile } from "../model/profile"; +import { Settings } from "../model/settings"; +import { Story } from "../model/story"; +import { Tickets } from "../model/tickets"; +import { TimeAttackScore } from "../model/timeAttack"; +import { Unlocks } from "../model/unlocks"; + +interface LoadProfileResponseBase { + type: "load_profile_res"; + name: string; + profileId: ExtId; + lv: number; + exp: number; + fame: number; + dpoint: number; + mileage: number; + teamId?: number; + settings: Settings; + chara: Chara; + titles: Set; + coursePlays: Map; + missions: MissionState; + timeAttack: TimeAttackScore[]; + car: Car; + carCount: number; + story: Story; + unlocks: Unlocks; + tickets: Tickets; + // giga TODO } + +export interface LoadProfileResponse1 extends LoadProfileResponseBase { + format: 1; +} + +export interface LoadProfileResponse2 extends LoadProfileResponseBase { + format: 2; +} + +export type LoadProfileResponse = LoadProfileResponse1 | LoadProfileResponse2; diff --git a/src/idz/response/loadProfile2.ts b/src/idz/response/loadProfile2.ts deleted file mode 100644 index 143ea1e..0000000 --- a/src/idz/response/loadProfile2.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { ExtId, TitleCode } from "../model/base"; -import { Car } from "../model/car"; -import { Chara } from "../model/chara"; -import { MissionState } from "../model/mission"; -import { Profile } from "../model/profile"; -import { Settings } from "../model/settings"; -import { Story } from "../model/story"; -import { Tickets } from "../model/tickets"; -import { TimeAttackScore } from "../model/timeAttack"; -import { Unlocks } from "../model/unlocks"; - -export interface LoadProfileResponse2 { - type: "load_profile_v2_res"; - name: string; - profileId: ExtId; - lv: number; - exp: number; - fame: number; - dpoint: number; - mileage: number; - teamId?: number; - settings: Settings; - chara: Chara; - titles: Set; - coursePlays: Map; - missions: MissionState; - timeAttack: TimeAttackScore[]; - car: Car; - carCount: number; - story: Story; - unlocks: Unlocks; - tickets: Tickets; - // giga TODO -}