From c2b0bcabc429e70b635c475c537d9f3f06dd54da Mon Sep 17 00:00:00 2001 From: Tau Date: Sun, 26 May 2019 20:05:01 -0400 Subject: [PATCH] idz: Remove profile ext id Turns out (while researching team creation) the profile ext id is just the aime id. After creating a team a profile discovery request is immediately issued with the "profile id" in the field that holds the aime id during initial profile load. --- schema/init/idz.sql | 4 +- schema/init/meta.sql | 2 +- .../migrate/M0002-idz-drop-profile-ext-id.sql | 3 + src/db.ts | 2 +- src/idz/db/_util.ts | 25 --------- src/idz/db/backgrounds.ts | 14 ++--- src/idz/db/car.ts | 27 ++++----- src/idz/db/chara.ts | 14 ++--- src/idz/db/coursePlays.ts | 12 ++-- src/idz/db/missions.ts | 20 ++----- src/idz/db/profile.ts | 55 ++++++++----------- src/idz/db/settings.ts | 14 ++--- src/idz/db/story.ts | 18 ++---- src/idz/db/tickets.ts | 13 ++--- src/idz/db/timeAttack.ts | 14 ++--- src/idz/db/titles.ts | 13 ++--- src/idz/db/unlocks.ts | 13 ++--- src/idz/decoder/createTeam.ts | 5 +- src/idz/decoder/load2on2.ts | 6 +- src/idz/decoder/loadEventInfo.ts | 5 +- src/idz/decoder/loadGacha.ts | 5 +- src/idz/decoder/loadGarage.ts | 5 +- src/idz/decoder/loadGeneralReward.ts | 7 +-- src/idz/decoder/loadStocker.ts | 5 +- src/idz/decoder/loadTeam.ts | 2 +- src/idz/decoder/loadTopTen1.ts | 4 +- src/idz/decoder/loadTopTen2.ts | 4 +- src/idz/decoder/lockProfile.ts | 2 +- src/idz/decoder/lockProfileExtend.ts | 5 +- src/idz/decoder/saveGarage.ts | 2 +- src/idz/decoder/saveNewCar.ts | 5 +- src/idz/decoder/saveProfile2.ts | 4 +- src/idz/decoder/saveProfile3.ts | 6 +- src/idz/decoder/saveSettings.ts | 5 +- src/idz/decoder/saveStocker.ts | 5 +- src/idz/decoder/saveTimeAttack.ts | 6 +- src/idz/decoder/unlockProfile.ts | 2 +- src/idz/encoder/loadProfile2.ts | 2 +- src/idz/encoder/loadProfile3.ts | 2 +- src/idz/handler/createProfile.ts | 2 +- src/idz/handler/discoverProfile.ts | 4 +- src/idz/handler/loadGarage.ts | 4 +- src/idz/handler/loadProfile.ts | 30 +++++----- src/idz/handler/loadStocker.ts | 3 +- src/idz/handler/saveNewCar.ts | 4 +- src/idz/handler/saveProfile.ts | 23 ++++---- src/idz/handler/saveSettings.ts | 4 +- src/idz/handler/saveStocker.ts | 8 ++- src/idz/handler/saveTimeAttack.ts | 7 ++- src/idz/model/profile.ts | 3 +- src/idz/repo.ts | 47 +++++++--------- src/idz/request/createTeam.ts | 5 +- src/idz/request/load2on2.ts | 4 +- src/idz/request/loadEventInfo.ts | 5 +- src/idz/request/loadGacha.ts | 5 +- src/idz/request/loadGarage.ts | 5 +- src/idz/request/loadGeneralReward.ts | 5 +- src/idz/request/loadStocker.ts | 5 +- src/idz/request/loadTeam.ts | 2 +- src/idz/request/loadTopTen.ts | 4 +- src/idz/request/lockProfile.ts | 2 +- src/idz/request/lockProfileExtend.ts | 5 +- src/idz/request/saveGarage.ts | 2 +- src/idz/request/saveNewCar.ts | 5 +- src/idz/request/saveProfile.ts | 4 +- src/idz/request/saveSettings.ts | 5 +- src/idz/request/saveStocker.ts | 6 +- src/idz/request/saveTimeAttack.ts | 6 +- src/idz/request/unlockProfile.ts | 2 +- src/idz/response/loadProfile.ts | 6 +- 70 files changed, 238 insertions(+), 341 deletions(-) create mode 100644 schema/migrate/M0002-idz-drop-profile-ext-id.sql delete mode 100644 src/idz/db/_util.ts diff --git a/schema/init/idz.sql b/schema/init/idz.sql index 7c22cdd..2a365f4 100644 --- a/schema/init/idz.sql +++ b/schema/init/idz.sql @@ -12,7 +12,6 @@ create table "idz"."profile" ( references "aime"."player"("id") on delete cascade, -- TODO shop_id - "ext_id" integer not null, "name" text not null, "lv" smallint not null, "exp" integer not null, @@ -21,8 +20,7 @@ create table "idz"."profile" ( "mileage" integer not null, "register_time" timestamp not null, "access_time" timestamp not null, - constraint "profile_player_uq" unique ("player_id"), - constraint "profile_ext_id_uq" unique ("ext_id") + constraint "profile_player_uq" unique ("player_id") ); create table "idz"."chara" ( diff --git a/schema/init/meta.sql b/schema/init/meta.sql index 1cb26df..800c80b 100644 --- a/schema/init/meta.sql +++ b/schema/init/meta.sql @@ -1,2 +1,2 @@ create table "meta" ("schemaver" integer not null); -insert into "meta" values (1); +insert into "meta" values (2); diff --git a/schema/migrate/M0002-idz-drop-profile-ext-id.sql b/schema/migrate/M0002-idz-drop-profile-ext-id.sql new file mode 100644 index 0000000..ad9780a --- /dev/null +++ b/schema/migrate/M0002-idz-drop-profile-ext-id.sql @@ -0,0 +1,3 @@ +alter table "idz"."profile" drop column "ext_id"; + +update "meta" set "schemaver" = 1; diff --git a/src/db.ts b/src/db.ts index 4c95e21..caad8f6 100644 --- a/src/db.ts +++ b/src/db.ts @@ -3,7 +3,7 @@ import { Pool, PoolClient } from "pg"; export type Id = bigint & { __id: T }; -const currentSchemaVer = 0; +const currentSchemaVer = 1; const pool = new Pool(); const fence = testConnection(); diff --git a/src/idz/db/_util.ts b/src/idz/db/_util.ts deleted file mode 100644 index e458142..0000000 --- a/src/idz/db/_util.ts +++ /dev/null @@ -1,25 +0,0 @@ -import * as sql from "sql-bricks"; -import { ClientBase } from "pg"; - -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; -import { Id } from "../../db"; - -export async function _findProfile( - conn: ClientBase, - extId: ExtId -): Promise> { - const lookupSql = sql - .select("r.id") - .from("idz.profile r") - .where("r.ext_id", extId) - .toParams(); - - const { rows } = await conn.query(lookupSql); - - if (rows.length > 0) { - return rows[0].id as Id; - } else { - throw new Error("Profile not found"); - } -} diff --git a/src/idz/db/backgrounds.ts b/src/idz/db/backgrounds.ts index 2349cdb..ffa8db4 100644 --- a/src/idz/db/backgrounds.ts +++ b/src/idz/db/backgrounds.ts @@ -1,22 +1,21 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks"; -import { _findProfile } from "./_util"; -import { BackgroundCode, ExtId } from "../model/base"; +import { BackgroundCode } from "../model/base"; import { Profile } from "../model/profile"; import { FlagRepository } from "../repo"; -import { generateId } from "../../db"; +import { generateId, Id } from "../../db"; export class SqlBackgroundsRepository implements FlagRepository { constructor(private readonly _conn: ClientBase) {} - async loadAll(extId: ExtId): Promise> { + async loadAll(id: Id): Promise> { const loadSql = sql .select("bg.background_no") .from("idz.background_unlock bg") .join("idz.profile p", { "bg.profile_id": "p.id" }) - .where("p.ext_id", extId) + .where("p.id", id) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -30,11 +29,10 @@ export class SqlBackgroundsRepository } async saveAll( - extId: ExtId, + profileId: Id, flags: Set ): Promise { - const profileId = await _findProfile(this._conn, extId); - const existing = await this.loadAll(extId); + const existing = await this.loadAll(profileId); for (const flag of flags) { if (existing.has(flag)) { diff --git a/src/idz/db/car.ts b/src/idz/db/car.ts index 90fe611..96a329e 100644 --- a/src/idz/db/car.ts +++ b/src/idz/db/car.ts @@ -1,12 +1,10 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId } from "../model/base"; import { Car, CarSelector } from "../model/car"; import { Profile } from "../model/profile"; import { CarRepository } from "../repo"; -import { generateId } from "../../db"; +import { generateId, Id } from "../../db"; function _extractRow(row: any): Car { return { @@ -31,12 +29,11 @@ function _extractRow(row: any): Car { export class SqlCarRepository implements CarRepository { constructor(private readonly _conn: ClientBase) {} - async countCars(extId: ExtId): Promise { + async countCars(profileId: Id): Promise { const countSql = sql .select("count(*) result") .from("idz.car c") - .join("idz.profile p", { "c.profile_id": "p.id" }) - .where("p.ext_id", extId) + .where("c.profile_id", profileId) .toParams(); const { rows } = await this._conn.query(countSql); @@ -45,12 +42,11 @@ export class SqlCarRepository implements CarRepository { return parseInt(row.result, 10); } - async loadAllCars(extId: ExtId): Promise { + async loadAllCars(profileId: Id): Promise { const loadSql = sql .select("c.*") .from("idz.car c") - .join("idz.profile p", { "c.profile_id": "p.id" }) - .where("p.ext_id", extId) + .where("c.profile_id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -58,13 +54,12 @@ export class SqlCarRepository implements CarRepository { return rows.map(_extractRow); } - async loadSelectedCar(extId: ExtId): Promise { + async loadSelectedCar(profileId: Id): Promise { const loadSql = sql .select("c.*") .from("idz.car c") .join("idz.car_selection s", { "c.id": "s.car_id" }) - .join("idz.profile p", { "s.id": "p.id" }) - .where("p.ext_id", extId) + .where("s.id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -72,11 +67,11 @@ export class SqlCarRepository implements CarRepository { return _extractRow(rows[0]); } - async saveCar(extId: ExtId, car: Car): Promise { + async saveCar(profileId: Id, car: Car): Promise { const saveSql = sql .insert("idz.car", { id: generateId(), - profile_id: await _findProfile(this._conn, extId), + profile_id: profileId, selector: car.selector, field_00: car.field_00, field_02: car.field_02, @@ -116,11 +111,9 @@ export class SqlCarRepository implements CarRepository { } async saveSelection( - extId: ExtId, + profileId: Id, selector: CarSelector ): Promise { - const profileId = await _findProfile(this._conn, extId); - const findSql = sql .select("c.id") .from("idz.car c") diff --git a/src/idz/db/chara.ts b/src/idz/db/chara.ts index 560ba77..60627cb 100644 --- a/src/idz/db/chara.ts +++ b/src/idz/db/chara.ts @@ -1,21 +1,19 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId } from "../model/base"; import { Chara } from "../model/chara"; import { Profile } from "../model/profile"; import { FacetRepository } from "../repo"; +import { Id } from "../../db"; export class SqlCharaRepository implements FacetRepository { constructor(private readonly _conn: ClientBase) {} - async load(extId: ExtId): Promise { + async load(profileId: Id): Promise { const loadSql = sql .select("c.*") - .from("idz.profile p") - .join("idz.chara c", { "p.id": "c.id" }) - .where("p.ext_id", extId) + .from("idz.chara c") + .where("c.id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -35,9 +33,7 @@ export class SqlCharaRepository implements FacetRepository { }; } - async save(extId: ExtId, chara: Chara): Promise { - const profileId = await _findProfile(this._conn, extId); - + async save(profileId: Id, chara: Chara): Promise { const saveSql = sql .insert("idz.chara", { id: profileId, diff --git a/src/idz/db/coursePlays.ts b/src/idz/db/coursePlays.ts index 50ed2a4..aef10ef 100644 --- a/src/idz/db/coursePlays.ts +++ b/src/idz/db/coursePlays.ts @@ -1,21 +1,19 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; import { CourseNo, ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { CoursePlaysRepository } from "../repo"; -import { generateId } from "../../db"; +import { generateId, Id } from "../../db"; export class SqlCoursePlaysRepository implements CoursePlaysRepository { constructor(private readonly _conn: ClientBase) {} - async loadAll(extId: ExtId): Promise> { + async loadAll(profileId: Id): Promise> { const loadSql = sql .select("cp.course_no", "cp.count") .from("idz.course_plays cp") - .join("idz.profile p", { "cp.profile_id": "p.id" }) - .where("p.ext_id", extId) + .where("cp.profile_id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -29,11 +27,9 @@ export class SqlCoursePlaysRepository implements CoursePlaysRepository { } async saveAll( - extId: ExtId, + profileId: Id, plays: Map ): Promise { - const profileId = await _findProfile(this._conn, extId); - for (const [k, v] of plays) { const saveSql = sql .insert("idz.course_plays", { diff --git a/src/idz/db/missions.ts b/src/idz/db/missions.ts index a4b436f..db6fcb6 100644 --- a/src/idz/db/missions.ts +++ b/src/idz/db/missions.ts @@ -1,8 +1,6 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId } from "../model/base"; import { MissionGrid, MissionState } from "../model/mission"; import { Profile } from "../model/profile"; import { FacetRepository } from "../repo"; @@ -11,9 +9,7 @@ import { generateId, Id } from "../../db"; export class SqlMissionsRepository implements FacetRepository { constructor(private readonly _conn: ClientBase) {} - private async _load( - extId: ExtId - ): Promise<[MissionState, Id]> { + async load(profileId: Id): Promise { const result: MissionState = { solo: new Array(), team: new Array(), @@ -32,8 +28,6 @@ export class SqlMissionsRepository implements FacetRepository { result.team.push(teamGrid); } - const profileId = await _findProfile(this._conn, extId); - const loadSoloSql = sql .select("sm.*") .from("idz.solo_mission_state sm") @@ -46,17 +40,11 @@ export class SqlMissionsRepository implements FacetRepository { result.solo[row.grid_no].cells[row.cell_no] = row.value; } - return [result, profileId]; + return result; } - async load(extId: ExtId): Promise { - const [mission] = await this._load(extId); - - return mission; - } - - async save(extId: ExtId, mission: MissionState): Promise { - const [existing, profileId] = await this._load(extId); + async save(profileId: Id, mission: MissionState): Promise { + const existing = await this.load(profileId); for (let i = 0; i < mission.solo.length; i++) { const exGrid = existing.solo[i].cells; diff --git a/src/idz/db/profile.ts b/src/idz/db/profile.ts index 385a960..fcb8c2e 100644 --- a/src/idz/db/profile.ts +++ b/src/idz/db/profile.ts @@ -1,17 +1,16 @@ import * as sql from "sql-bricks"; import { ClientBase } from "pg"; -import { _findProfile } from "./_util"; import { ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { Team } from "../model/team"; import { ProfileSpec, ProfileRepository } from "../repo"; -import { generateExtId, generateId, Id } from "../../db"; +import { generateId, Id } from "../../db"; import { AimeId } from "../../model"; -function _extractRow(row: any): Profile { +function _extractProfile(row: any): Profile { return { - id: row.ext_id, + aimeId: row.ext_id, teamId: 2 as ExtId, // TODO name: row.name, lv: row.lv, @@ -25,11 +24,19 @@ function _extractRow(row: any): Profile { export class SqlProfileRepository implements ProfileRepository { constructor(private readonly _conn: ClientBase) {} - private async _tryLoadByAimeId( - aimeId: AimeId - ): Promise { + async find(aimeId: AimeId): Promise> { + const profileId = await this.peek(aimeId); + + if (profileId === undefined) { + throw new Error(`Profile not found for Aime ID ${aimeId}`); + } + + return profileId; + } + + async peek(aimeId: AimeId): Promise | undefined> { const lookupSql = sql - .select("p.*") + .select("p.id") .from("idz.profile p") .join("aime.player r", { "p.player_id": "r.id" }) .where("r.ext_id", aimeId) @@ -42,35 +49,19 @@ export class SqlProfileRepository implements ProfileRepository { return undefined; } - return _extractRow(row); + return row.id; } - async discoverByAimeId(aimeId: AimeId): Promise { - const result = await this._tryLoadByAimeId(aimeId); - - return result !== undefined; - } - - async loadByAimeId(aimeId: AimeId): Promise { - const result = await this._tryLoadByAimeId(aimeId); - - if (result === undefined) { - throw new Error("Profile not found for Aime ID"); - } - - return result; - } - - async load(extId: ExtId): Promise { + async load(id: Id): Promise { const loadSql = sql .select("p.*") .from("idz.profile p") - .where("ext_id", extId) + .where("id", id) .toParams(); const { rows } = await this._conn.query(loadSql); - return _extractRow(rows[0]); + return _extractProfile(rows[0]); } async save(profile: Profile, timestamp: Date): Promise { @@ -83,7 +74,7 @@ export class SqlProfileRepository implements ProfileRepository { mileage: profile.mileage, access_time: timestamp, }) - .where("ext_id", profile.id) + .where("id", profile.aimeId) .toParams(); await this._conn.query(saveSql); @@ -93,7 +84,7 @@ export class SqlProfileRepository implements ProfileRepository { aimeId: AimeId, profile: ProfileSpec, timestamp: Date - ): Promise> { + ): Promise> { const findSql = sql .select("r.id") .from("aime.player r") @@ -108,14 +99,12 @@ export class SqlProfileRepository implements ProfileRepository { } const id = generateId(); - const extId = generateExtId() as ExtId; const playerId = row.id; const createSql = sql .insert("idz.profile", { id: id, player_id: playerId, - ext_id: extId, name: profile.name, lv: profile.lv, exp: profile.exp, @@ -129,6 +118,6 @@ export class SqlProfileRepository implements ProfileRepository { await this._conn.query(createSql); - return extId; + return id as Id; } } diff --git a/src/idz/db/settings.ts b/src/idz/db/settings.ts index 9460a60..a44ab33 100644 --- a/src/idz/db/settings.ts +++ b/src/idz/db/settings.ts @@ -1,21 +1,19 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId } from "../model/base"; import { Settings } from "../model/settings"; import { Profile } from "../model/profile"; import { FacetRepository } from "../repo"; +import { Id } from "../../db"; export class SqlSettingsRepository implements FacetRepository { constructor(private readonly _conn: ClientBase) {} - async load(extId: ExtId): Promise { + async load(profileId: Id): Promise { const loadSql = sql .select("s.*") - .from("idz.profile p") - .join("idz.settings s", { "p.id": "s.id" }) - .where("p.ext_id", extId) + .from("idz.settings s") + .where("s.id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -29,9 +27,7 @@ export class SqlSettingsRepository implements FacetRepository { }; } - async save(extId: ExtId, settings: Settings): Promise { - const profileId = await _findProfile(this._conn, extId); - + async save(profileId: Id, settings: Settings): Promise { const saveSql = sql .insert("idz.settings", { id: profileId, diff --git a/src/idz/db/story.ts b/src/idz/db/story.ts index a593afd..9bc3776 100644 --- a/src/idz/db/story.ts +++ b/src/idz/db/story.ts @@ -1,8 +1,6 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { Story, StoryRow, StoryCell } from "../model/story"; import { FacetRepository } from "../repo"; @@ -11,9 +9,7 @@ import { generateId, Id } from "../../db"; export class SqlStoryRepository implements FacetRepository { constructor(private readonly _conn: ClientBase) {} - private async _load(extId: ExtId): Promise<[Story, Id]> { - const profileId = await _findProfile(this._conn, extId); - + async load(profileId: Id): Promise { const loadSql = sql .select("s.*") .from("idz.story_state s") @@ -56,17 +52,11 @@ export class SqlStoryRepository implements FacetRepository { cell.b = row.b; } - return [result, profileId]; + return result; } - async load(extId: ExtId): Promise { - const [story] = await this._load(extId); - - return story; - } - - async save(extId: ExtId, story: Story): Promise { - const [existing, profileId] = await this._load(extId); + async save(profileId: Id, story: Story): Promise { + const existing = await this.load(profileId); const headSql = sql .insert("idz.story_state", { diff --git a/src/idz/db/tickets.ts b/src/idz/db/tickets.ts index ebe2621..4328cd7 100644 --- a/src/idz/db/tickets.ts +++ b/src/idz/db/tickets.ts @@ -1,23 +1,21 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { Tickets } from "../model/tickets"; import { FacetRepository } from "../repo"; +import { Id } from "../../db"; // TODO free continue export class SqlTicketsRepository implements FacetRepository { constructor(private readonly _conn: ClientBase) {} - async load(extId: ExtId): Promise { + async load(profileId: Id): Promise { const loadSql = sql .select("fc.*") - .from("idz.profile p") - .join("idz.free_car fc", { "p.id": "fc.id" }) - .where("p.ext_id", extId) + .from("idz.free_car fc") + .where("fc.id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -30,8 +28,7 @@ export class SqlTicketsRepository implements FacetRepository { }; } - async save(extId: ExtId, tickets: Tickets): Promise { - const profileId = await _findProfile(this._conn, extId); + async save(profileId: Id, tickets: Tickets): Promise { const { freeCar } = tickets; if (!freeCar) { diff --git a/src/idz/db/timeAttack.ts b/src/idz/db/timeAttack.ts index 6a8a35f..9466432 100644 --- a/src/idz/db/timeAttack.ts +++ b/src/idz/db/timeAttack.ts @@ -1,12 +1,11 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; -import { ExtId, RouteNo } from "../model/base"; +import { RouteNo } from "../model/base"; import { Profile } from "../model/profile"; import { TimeAttackScore } from "../model/timeAttack"; import { TimeAttackRepository, TopTenResult } from "../repo"; -import { generateId } from "../../db"; +import { generateId, Id } from "../../db"; export class SqlTimeAttackRepository implements TimeAttackRepository { constructor(private readonly _conn: ClientBase) {} @@ -41,12 +40,11 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { })); } - async loadAll(extId: ExtId): Promise { + async loadAll(profileId: Id): Promise { const loadSql = sql .select("ta.*") .from("idz.ta_best ta") - .join("idz.profile p", { "ta.profile_id": "p.id" }) - .where("p.ext_id", extId) + .where("ta.profile_id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -62,9 +60,7 @@ export class SqlTimeAttackRepository implements TimeAttackRepository { })); } - async save(extId: ExtId, score: TimeAttackScore): Promise { - const profileId = await _findProfile(this._conn, extId); - + async save(profileId: Id, score: TimeAttackScore): Promise { const logSql = sql .insert("idz.ta_result", { id: generateId(), diff --git a/src/idz/db/titles.ts b/src/idz/db/titles.ts index b5a9d6d..a72b13b 100644 --- a/src/idz/db/titles.ts +++ b/src/idz/db/titles.ts @@ -1,21 +1,19 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks"; -import { _findProfile } from "./_util"; import { TitleCode, ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { FlagRepository } from "../repo"; -import { generateId } from "../../db"; +import { generateId, Id } from "../../db"; export class SqlTitlesRepository implements FlagRepository { constructor(private readonly _conn: ClientBase) {} - async loadAll(extId: ExtId): Promise> { + async loadAll(profileId: Id): Promise> { const loadSql = sql .select("t.title_no") .from("idz.title_unlock t") - .join("idz.profile p", { "t.profile_id": "p.id" }) - .where("p.ext_id", extId) + .where("t.profile_id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -28,9 +26,8 @@ export class SqlTitlesRepository implements FlagRepository { return result; } - async saveAll(extId: ExtId, flags: Set): Promise { - const profileId = await _findProfile(this._conn, extId); - const existing = await this.loadAll(extId); + async saveAll(profileId: Id, flags: Set): Promise { + const existing = await this.loadAll(profileId); for (const flag of flags) { if (existing.has(flag)) { diff --git a/src/idz/db/unlocks.ts b/src/idz/db/unlocks.ts index a220c72..523ad25 100644 --- a/src/idz/db/unlocks.ts +++ b/src/idz/db/unlocks.ts @@ -1,21 +1,20 @@ import { ClientBase } from "pg"; import * as sql from "sql-bricks-postgres"; -import { _findProfile } from "./_util"; import { ExtId } from "../model/base"; import { Profile } from "../model/profile"; import { Unlocks } from "../model/unlocks"; import { FacetRepository } from "../repo"; +import { Id } from "../../db"; export class SqlUnlocksRepository implements FacetRepository { constructor(private readonly _conn: ClientBase) {} - async load(extId: ExtId): Promise { + async load(profileId: Id): Promise { const loadSql = sql .select("u.*") - .from("idz.profile p") - .join("idz.unlocks u", { "p.id": "u.id" }) - .where("p.ext_id", extId) + .from("idz.unlocks u") + .where("u.id", profileId) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -29,9 +28,7 @@ export class SqlUnlocksRepository implements FacetRepository { }; } - async save(extId: ExtId, unlocks: Unlocks): Promise { - const profileId = await _findProfile(this._conn, extId); - + async save(profileId: Id, unlocks: Unlocks): Promise { const saveSql = sql .insert("idz.unlocks", { id: profileId, diff --git a/src/idz/decoder/createTeam.ts b/src/idz/decoder/createTeam.ts index f30837b..7cb68bb 100644 --- a/src/idz/decoder/createTeam.ts +++ b/src/idz/decoder/createTeam.ts @@ -1,9 +1,8 @@ import iconv = require("iconv-lite"); import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { CreateTeamRequest } from "../request/createTeam"; +import { AimeId } from "../../model"; createTeam.msgCode = 0x0071 as RequestCode; createTeam.msgLen = 0x0050; @@ -11,7 +10,7 @@ createTeam.msgLen = 0x0050; export function createTeam(buf: Buffer): CreateTeamRequest { return { type: "create_team_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, teamName: iconv.decode( buf.slice(0x0008, buf.indexOf("\0", 0x0008)), "shift_jis" diff --git a/src/idz/decoder/load2on2.ts b/src/idz/decoder/load2on2.ts index 2024182..def366c 100644 --- a/src/idz/decoder/load2on2.ts +++ b/src/idz/decoder/load2on2.ts @@ -1,8 +1,8 @@ import { RequestCode } from "./_defs"; import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { Team } from "../model/team"; import { Load2on2Request1, Load2on2Request2 } from "../request/load2on2"; +import { AimeId } from "../../model"; load2on2_v1.msgCode = 0x00b0 as RequestCode; load2on2_v1.msgLen = 0x0010; @@ -12,7 +12,7 @@ export function load2on2_v1(buf: Buffer): Load2on2Request1 { type: "load_2on2_req", format: 1, field_0002: buf.readUInt16LE(0x0002), - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, teamId: buf.readUInt32LE(0x0008) as ExtId, }; } @@ -25,7 +25,7 @@ export function load2on2_v2(buf: Buffer): Load2on2Request2 { type: "load_2on2_req", format: 2, field_0002: buf.readUInt16LE(0x0002), - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, teamId: buf.readUInt32LE(0x0008) as ExtId, }; } diff --git a/src/idz/decoder/loadEventInfo.ts b/src/idz/decoder/loadEventInfo.ts index dcee155..7e67f8d 100644 --- a/src/idz/decoder/loadEventInfo.ts +++ b/src/idz/decoder/loadEventInfo.ts @@ -1,7 +1,6 @@ import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { LoadEventInfoRequest } from "../request/loadEventInfo"; +import { AimeId } from "../../model"; loadEventInfo.msgCode = 0x00be as RequestCode; loadEventInfo.msgLen = 0x0010; @@ -9,6 +8,6 @@ loadEventInfo.msgLen = 0x0010; export function loadEventInfo(buf: Buffer): LoadEventInfoRequest { return { type: "load_event_info_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, }; } diff --git a/src/idz/decoder/loadGacha.ts b/src/idz/decoder/loadGacha.ts index 2b473d9..86cb50a 100644 --- a/src/idz/decoder/loadGacha.ts +++ b/src/idz/decoder/loadGacha.ts @@ -1,7 +1,6 @@ import { RequestCode } from "./_defs"; import { LoadGachaRequest } from "../request/loadGacha"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; loadGacha.msgCode = 0x00c1 as RequestCode; loadGacha.msgLen = 0x0010; @@ -9,6 +8,6 @@ loadGacha.msgLen = 0x0010; export function loadGacha(buf: Buffer): LoadGachaRequest { return { type: "load_gacha_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, }; } diff --git a/src/idz/decoder/loadGarage.ts b/src/idz/decoder/loadGarage.ts index 9d3dbff..dae436f 100644 --- a/src/idz/decoder/loadGarage.ts +++ b/src/idz/decoder/loadGarage.ts @@ -1,7 +1,6 @@ import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { LoadGarageRequest } from "../request/loadGarage"; +import { AimeId } from "../../model"; loadGarage.msgCode = 0x0090 as RequestCode; loadGarage.msgLen = 0x0010; @@ -9,7 +8,7 @@ loadGarage.msgLen = 0x0010; export function loadGarage(buf: Buffer): LoadGarageRequest { return { type: "load_garage_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, fetchOffset: buf.readUInt8(0x0008), field_000A: buf.readUInt16LE(0x000a), }; diff --git a/src/idz/decoder/loadGeneralReward.ts b/src/idz/decoder/loadGeneralReward.ts index 9d474b6..cf8b47d 100644 --- a/src/idz/decoder/loadGeneralReward.ts +++ b/src/idz/decoder/loadGeneralReward.ts @@ -1,10 +1,9 @@ import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { LoadGeneralRewardRequest1, LoadGeneralRewardRequest2, } from "../request/loadGeneralReward"; +import { AimeId } from "../../model"; loadGeneralReward1.msgCode = 0x009c as RequestCode; loadGeneralReward1.msgLen = 0x0010; @@ -13,7 +12,7 @@ export function loadGeneralReward1(buf: Buffer): LoadGeneralRewardRequest1 { return { type: "load_general_reward_req", format: 1, - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, }; } @@ -24,6 +23,6 @@ export function loadGeneralReward2(buf: Buffer): LoadGeneralRewardRequest2 { return { type: "load_general_reward_req", format: 2, - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, }; } diff --git a/src/idz/decoder/loadStocker.ts b/src/idz/decoder/loadStocker.ts index 39c71e5..ab41d29 100644 --- a/src/idz/decoder/loadStocker.ts +++ b/src/idz/decoder/loadStocker.ts @@ -1,7 +1,6 @@ import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { LoadStockerRequest } from "../request/loadStocker"; +import { AimeId } from "../../model"; loadStocker.msgCode = 0x00a7 as RequestCode; loadStocker.msgLen = 0x0010; @@ -9,6 +8,6 @@ loadStocker.msgLen = 0x0010; export function loadStocker(buf: Buffer): LoadStockerRequest { return { type: "load_stocker_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, }; } diff --git a/src/idz/decoder/loadTeam.ts b/src/idz/decoder/loadTeam.ts index 69d7656..e604288 100644 --- a/src/idz/decoder/loadTeam.ts +++ b/src/idz/decoder/loadTeam.ts @@ -7,7 +7,7 @@ loadTeam.msgLen = 0x0010; export function loadTeam(buf: Buffer): LoadTeamRequest { return { type: "load_team_req", - profileId: buf.readUInt32LE(0x0004), + aimeId: buf.readUInt32LE(0x0004), teamId: buf.readUInt32LE(0x0008), }; } diff --git a/src/idz/decoder/loadTopTen1.ts b/src/idz/decoder/loadTopTen1.ts index cfe9e5c..ecf2e3c 100644 --- a/src/idz/decoder/loadTopTen1.ts +++ b/src/idz/decoder/loadTopTen1.ts @@ -1,11 +1,11 @@ import { RequestCode } from "./_defs"; import { ExtId, RouteNo } from "../model/base"; -import { Profile } from "../model/profile"; import { Team } from "../model/team"; import { LoadTopTenRequest, LoadTopTenRequestSelector, } from "../request/loadTopTen"; +import { AimeId } from "../../model"; loadTopTen1.msgCode = 0x00b5 as RequestCode; loadTopTen1.msgLen = 0x00e0; @@ -30,7 +30,7 @@ export function loadTopTen1(buf: Buffer): LoadTopTenRequest { field_C4: buf.readUInt8(0x00c4), // Boolean, true if profile ID is set field_C5: buf.readUInt8(0x00c5), // Always zero field_C6: buf.readUInt16LE(0x00c6), - profileId: profileId !== 0 ? (profileId as ExtId) : undefined, + aimeId: profileId !== 0 ? (profileId as AimeId) : undefined, teamId: teamId !== 0xffffffff ? (teamId as ExtId) : undefined, }; } diff --git a/src/idz/decoder/loadTopTen2.ts b/src/idz/decoder/loadTopTen2.ts index 79e9341..088bdda 100644 --- a/src/idz/decoder/loadTopTen2.ts +++ b/src/idz/decoder/loadTopTen2.ts @@ -1,11 +1,11 @@ import { RequestCode } from "./_defs"; import { ExtId, RouteNo } from "../model/base"; -import { Profile } from "../model/profile"; import { Team } from "../model/team"; import { LoadTopTenRequest, LoadTopTenRequestSelector, } from "../request/loadTopTen"; +import { AimeId } from "../../model"; loadTopTen2.msgCode = 0x012c as RequestCode; loadTopTen2.msgLen = 0x0110; @@ -30,7 +30,7 @@ export function loadTopTen2(buf: Buffer): LoadTopTenRequest { field_C4: buf.readUInt8(0x00f4), // Boolean, true if profile ID is set field_C5: buf.readUInt8(0x00f5), // Always zero field_C6: buf.readUInt16LE(0x00f6), - profileId: profileId !== 0 ? (profileId as ExtId) : undefined, + aimeId: profileId !== 0 ? (profileId as AimeId) : undefined, teamId: teamId !== 0xffffffff ? (teamId as ExtId) : undefined, }; } diff --git a/src/idz/decoder/lockProfile.ts b/src/idz/decoder/lockProfile.ts index 884a7e7..e87db39 100644 --- a/src/idz/decoder/lockProfile.ts +++ b/src/idz/decoder/lockProfile.ts @@ -7,7 +7,7 @@ lockProfile.msgLen = 0x0020; export function lockProfile(buf: Buffer): LockProfileRequest { return { type: "lock_profile_req", - profileId: buf.readUInt32LE(0x0004), + aimeId: buf.readUInt32LE(0x0004), pcbId: buf.slice(0x0008, buf.indexOf("\0", 0x0008)).toString("ascii"), field_0018: buf.readUInt16LE(0x0018), }; diff --git a/src/idz/decoder/lockProfileExtend.ts b/src/idz/decoder/lockProfileExtend.ts index e7d6f02..6a0f11a 100644 --- a/src/idz/decoder/lockProfileExtend.ts +++ b/src/idz/decoder/lockProfileExtend.ts @@ -1,7 +1,6 @@ import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { LockProfileExtendRequest } from "../request/lockProfileExtend"; +import { AimeId } from "../../model"; lockProfileExtend.msgCode = 0x006d as RequestCode; lockProfileExtend.msgLen = 0x0020; @@ -9,7 +8,7 @@ lockProfileExtend.msgLen = 0x0020; export function lockProfileExtend(buf: Buffer): LockProfileExtendRequest { return { type: "lock_profile_extend_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, luid: buf.slice(0x0008, buf.indexOf("\0")).toString("ascii"), }; } diff --git a/src/idz/decoder/saveGarage.ts b/src/idz/decoder/saveGarage.ts index 8a8d8c0..6eae71e 100644 --- a/src/idz/decoder/saveGarage.ts +++ b/src/idz/decoder/saveGarage.ts @@ -14,7 +14,7 @@ export function saveGarage(buf: Buffer): SaveGarageRequest { return { type: "save_garage_req", - profileId: buf.readUInt32LE(0x0004), + aimeId: buf.readUInt32LE(0x0004), payload: car(buf.slice(0x0008, 0x0068)), field_0068, field_0080: buf.readUInt8(0x0080), diff --git a/src/idz/decoder/saveNewCar.ts b/src/idz/decoder/saveNewCar.ts index 2de93af..c857d6d 100644 --- a/src/idz/decoder/saveNewCar.ts +++ b/src/idz/decoder/saveNewCar.ts @@ -1,8 +1,7 @@ import { car } from "./_car"; import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { SaveNewCarRequest } from "../request/saveNewCar"; +import { AimeId } from "../../model"; saveNewCar.msgCode = 0x0079 as RequestCode; saveNewCar.msgLen = 0x0090; @@ -10,7 +9,7 @@ saveNewCar.msgLen = 0x0090; export function saveNewCar(buf: Buffer): SaveNewCarRequest { return { type: "save_new_car_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, luid: buf.slice(0x0008, buf.indexOf(0, 0x0008)).toString("ascii"), car: car(buf.slice(0x0020, 0x0080)), field_0080: buf.readUInt32LE(0x0080), diff --git a/src/idz/decoder/saveProfile2.ts b/src/idz/decoder/saveProfile2.ts index cf1f42c..1f7147a 100644 --- a/src/idz/decoder/saveProfile2.ts +++ b/src/idz/decoder/saveProfile2.ts @@ -2,9 +2,9 @@ import { car } from "./_car"; import { mission } from "./_mission"; import { RequestCode } from "./_defs"; import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base"; -import { Profile } from "../model/profile"; import { SaveProfileRequest2 } from "../request/saveProfile"; import { bitmap } from "./_bitmap"; +import { AimeId } from "../../model"; saveProfile2.msgCode = 0x0068 as RequestCode; saveProfile2.msgLen = 0x0940; @@ -47,7 +47,7 @@ export function saveProfile2(buf: Buffer): SaveProfileRequest2 { return { type: "save_profile_req", format: 2, - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, lv: buf.readUInt16LE(0x0026), exp: buf.readUInt32LE(0x0028), fame: buf.readUInt32LE(0x0468), diff --git a/src/idz/decoder/saveProfile3.ts b/src/idz/decoder/saveProfile3.ts index c7e8c5c..0af3976 100644 --- a/src/idz/decoder/saveProfile3.ts +++ b/src/idz/decoder/saveProfile3.ts @@ -1,10 +1,10 @@ import { car } from "./_car"; import { mission } from "./_mission"; import { RequestCode } from "./_defs"; -import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base"; -import { Profile } from "../model/profile"; +import { BackgroundCode, CourseNo, TitleCode } from "../model/base"; import { SaveProfileRequest2 } from "../request/saveProfile"; import { bitmap } from "./_bitmap"; +import { AimeId } from "../../model"; saveProfile3.msgCode = 0x0138 as RequestCode; saveProfile3.msgLen = 0x0a70; @@ -49,7 +49,7 @@ export function saveProfile3(buf: Buffer): SaveProfileRequest2 { return { type: "save_profile_req", format: 2, - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, lv: buf.readUInt16LE(0x0026), exp: buf.readUInt32LE(0x0028), fame: buf.readUInt32LE(0x04fc), diff --git a/src/idz/decoder/saveSettings.ts b/src/idz/decoder/saveSettings.ts index 3b4fcf1..3e471a7 100644 --- a/src/idz/decoder/saveSettings.ts +++ b/src/idz/decoder/saveSettings.ts @@ -1,7 +1,6 @@ import { RequestCode } from "./_defs"; -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { SaveSettingsRequest } from "../request/saveSettings"; +import { AimeId } from "../../model"; saveSettings.msgCode = 0x00a5 as RequestCode; saveSettings.msgLen = 0x0020; @@ -13,7 +12,7 @@ export function saveSettings(buf: Buffer): SaveSettingsRequest { return { type: "save_settings_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, dpoint: buf.readUInt32LE(0x0008), settings: { music: buf.readUInt16LE(0x0002), diff --git a/src/idz/decoder/saveStocker.ts b/src/idz/decoder/saveStocker.ts index ff7c6b6..8951e3c 100644 --- a/src/idz/decoder/saveStocker.ts +++ b/src/idz/decoder/saveStocker.ts @@ -1,10 +1,9 @@ import { bitmap } from "./_bitmap"; import { chara } from "./_chara"; import { RequestCode } from "./_defs"; -import { BackgroundCode, ExtId } from "../model/base"; import { CarSelector } from "../model/car"; -import { Profile } from "../model/profile"; import { SaveStockerRequest } from "../request/saveStocker"; +import { AimeId } from "../../model"; saveStocker.msgCode = 0x00a6 as RequestCode; saveStocker.msgLen = 0x00c0; @@ -12,7 +11,7 @@ saveStocker.msgLen = 0x00c0; export function saveStocker(buf: Buffer): SaveStockerRequest { return { type: "save_stocker_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, backgrounds: bitmap(buf.slice(0x0008, 0x002c)), selectedCar: buf.readUInt16LE(0x009c) as CarSelector, diff --git a/src/idz/decoder/saveTimeAttack.ts b/src/idz/decoder/saveTimeAttack.ts index fcdbeaa..7546d0e 100644 --- a/src/idz/decoder/saveTimeAttack.ts +++ b/src/idz/decoder/saveTimeAttack.ts @@ -1,13 +1,13 @@ import { RequestCode } from "./_defs"; -import { ExtId, RouteNo } from "../model/base"; +import { RouteNo } from "../model/base"; import { CarSelector } from "../model/car"; -import { Profile } from "../model/profile"; import { SaveTimeAttackRequest } from "../request/saveTimeAttack"; +import { AimeId } from "../../model"; function saveTimeAttack(buf: Buffer): SaveTimeAttackRequest { return { type: "save_time_attack_req", - profileId: buf.readUInt32LE(0x0004) as ExtId, + aimeId: buf.readUInt32LE(0x0004) as AimeId, dayNight: buf.readUInt8(0x0054) & 1, payload: { routeNo: (buf.readUInt8(0x0054) >> 1) as RouteNo, diff --git a/src/idz/decoder/unlockProfile.ts b/src/idz/decoder/unlockProfile.ts index 0a0ba1f..345dc3a 100644 --- a/src/idz/decoder/unlockProfile.ts +++ b/src/idz/decoder/unlockProfile.ts @@ -7,7 +7,7 @@ unlockProfile.msgLen = 0x0020; export function unlockProfile(buf: Buffer): UnlockProfileRequest { return { type: "unlock_profile_req", - profileId: buf.readUInt32LE(0x0004), + aimeId: buf.readUInt32LE(0x0004), pcbId: buf.slice(0x0008, buf.indexOf("\0", 0x0008)).toString("ascii"), }; } diff --git a/src/idz/encoder/loadProfile2.ts b/src/idz/encoder/loadProfile2.ts index 6a31646..7e9f33b 100644 --- a/src/idz/encoder/loadProfile2.ts +++ b/src/idz/encoder/loadProfile2.ts @@ -78,7 +78,7 @@ export function loadProfile2(res: LoadProfileResponse2) { buf.writeUInt16LE(0, 0x037c); // Team leader mission(res.missions.team).copy(buf, 0x038a); buf.writeUInt16LE(0xffff, 0x0388); // [1] - buf.writeUInt32LE(res.profileId, 0x03b8); + buf.writeUInt32LE(res.aimeId, 0x03b8); buf.writeUInt32LE(res.mileage, 0x03bc); buf.writeUInt16LE(res.settings.music, 0x03c8); buf.writeUInt16LE(res.lv, 0x03cc); diff --git a/src/idz/encoder/loadProfile3.ts b/src/idz/encoder/loadProfile3.ts index 968702c..4fa5f61 100644 --- a/src/idz/encoder/loadProfile3.ts +++ b/src/idz/encoder/loadProfile3.ts @@ -78,7 +78,7 @@ export function loadProfile3(res: LoadProfileResponse3) { buf.writeUInt16LE(0, 0x0456); // Team leader mission(res.missions.team).copy(buf, 0x0460); buf.writeUInt16LE(0xffff, 0x0462); // [1] - buf.writeUInt32LE(res.profileId, 0x0494); + buf.writeUInt32LE(res.aimeId, 0x0494); buf.writeUInt32LE(res.mileage, 0x0498); buf.writeUInt16LE(res.settings.music, 0x04a4); buf.writeUInt16LE(res.lv, 0x04a8); diff --git a/src/idz/handler/createProfile.ts b/src/idz/handler/createProfile.ts index bc26cff..d97d637 100644 --- a/src/idz/handler/createProfile.ts +++ b/src/idz/handler/createProfile.ts @@ -46,6 +46,6 @@ export async function createProfile( return { type: "generic_res", - status: profileId, // "Generic response" my fucking *ass* + status: req.aimeId, // "Generic response" my fucking *ass* }; } diff --git a/src/idz/handler/discoverProfile.ts b/src/idz/handler/discoverProfile.ts index 7aa5597..c6ed520 100644 --- a/src/idz/handler/discoverProfile.ts +++ b/src/idz/handler/discoverProfile.ts @@ -6,8 +6,10 @@ export async function discoverProfile( w: Repositories, req: DiscoverProfileRequest ): Promise { + const profileId = await w.profile().find(req.aimeId); + return { type: "discover_profile_res", - exists: await w.profile().discoverByAimeId(req.aimeId), + exists: profileId !== undefined, }; } diff --git a/src/idz/handler/loadGarage.ts b/src/idz/handler/loadGarage.ts index c3bbd23..c3dae24 100644 --- a/src/idz/handler/loadGarage.ts +++ b/src/idz/handler/loadGarage.ts @@ -6,8 +6,10 @@ export async function loadGarage( w: Repositories, req: LoadGarageRequest ): Promise { + const profileId = await w.profile().find(req.aimeId); + return { type: "load_garage_res", - cars: await w.car().loadAllCars(req.profileId), + cars: await w.car().loadAllCars(profileId), }; } diff --git a/src/idz/handler/loadProfile.ts b/src/idz/handler/loadProfile.ts index 7e93dfc..cb37922 100644 --- a/src/idz/handler/loadProfile.ts +++ b/src/idz/handler/loadProfile.ts @@ -6,27 +6,31 @@ export async function loadProfile( w: Repositories, req: LoadProfileRequest ): Promise { + const { aimeId } = req; + + const profileId = await w.profile().find(aimeId); + // Promise.all would be messy here, who cares anyway this isn't supposed to // be a high-performance server. - const profile = await w.profile().loadByAimeId(req.aimeId); - const settings = await w.settings().load(profile.id); - const chara = await w.chara().load(profile.id); - const titles = await w.titles().loadAll(profile.id); - const coursePlays = await w.coursePlays().loadAll(profile.id); - const missions = await w.missions().load(profile.id); - const car = await w.car().loadSelectedCar(profile.id); - const carCount = await w.car().countCars(profile.id); - const story = await w.story().load(profile.id); - const timeAttack = await w.timeAttack().loadAll(profile.id); - const unlocks = await w.unlocks().load(profile.id); - const tickets = await w.tickets().load(profile.id); + const profile = await w.profile().load(profileId); + const settings = await w.settings().load(profileId); + const chara = await w.chara().load(profileId); + const titles = await w.titles().loadAll(profileId); + const coursePlays = await w.coursePlays().loadAll(profileId); + const missions = await w.missions().load(profileId); + const car = await w.car().loadSelectedCar(profileId); + const carCount = await w.car().countCars(profileId); + const story = await w.story().load(profileId); + const timeAttack = await w.timeAttack().loadAll(profileId); + const unlocks = await w.unlocks().load(profileId); + const tickets = await w.tickets().load(profileId); return { type: "load_profile_res", format: req.format as any, // TS fart name: profile.name, - profileId: profile.id, + aimeId, lv: profile.lv, exp: profile.exp, fame: profile.fame, diff --git a/src/idz/handler/loadStocker.ts b/src/idz/handler/loadStocker.ts index 93dc353..5217f9b 100644 --- a/src/idz/handler/loadStocker.ts +++ b/src/idz/handler/loadStocker.ts @@ -6,7 +6,8 @@ export async function loadStocker( w: Repositories, req: LoadStockerRequest ): Promise { - const backgrounds = await w.backgrounds().loadAll(req.profileId); + const profileId = await w.profile().find(req.aimeId); + const backgrounds = await w.backgrounds().loadAll(profileId); return { type: "load_stocker_res", diff --git a/src/idz/handler/saveNewCar.ts b/src/idz/handler/saveNewCar.ts index 8654acf..41f4021 100644 --- a/src/idz/handler/saveNewCar.ts +++ b/src/idz/handler/saveNewCar.ts @@ -6,7 +6,9 @@ export async function saveNewCar( w: Repositories, req: SaveNewCarRequest ): Promise { - await w.car().saveCar(req.profileId, req.car); + const profileId = await w.profile().find(req.aimeId); + + await w.car().saveCar(profileId, req.car); return { type: "save_new_car_res", diff --git a/src/idz/handler/saveProfile.ts b/src/idz/handler/saveProfile.ts index 5cb8a4a..346e7b2 100644 --- a/src/idz/handler/saveProfile.ts +++ b/src/idz/handler/saveProfile.ts @@ -7,8 +7,9 @@ export async function saveProfile( req: SaveProfileRequest ): Promise { const now = new Date(); - const profile = await w.profile().load(req.profileId); - const chara = await w.chara().load(req.profileId); + const profileId = await w.profile().find(req.aimeId); + const profile = await w.profile().load(profileId); + const chara = await w.chara().load(profileId); await w.profile().save( { @@ -22,20 +23,20 @@ export async function saveProfile( now ); - await w.chara().save(req.profileId, { + await w.chara().save(profileId, { ...chara, title: req.title, background: req.background, }); - await w.car().saveCar(req.profileId, req.car); - await w.coursePlays().saveAll(req.profileId, req.coursePlays); - await w.missions().save(req.profileId, req.missions); - await w.story().save(req.profileId, req.story); - await w.titles().saveAll(req.profileId, req.titles); - await w.unlocks().save(req.profileId, req.unlocks); - await w.settings().save(req.profileId, req.settings); - await w.tickets().save(req.profileId, req.tickets); + await w.car().saveCar(profileId, req.car); + await w.coursePlays().saveAll(profileId, req.coursePlays); + await w.missions().save(profileId, req.missions); + await w.story().save(profileId, req.story); + await w.titles().saveAll(profileId, req.titles); + await w.unlocks().save(profileId, req.unlocks); + await w.settings().save(profileId, req.settings); + await w.tickets().save(profileId, req.tickets); return { type: "generic_res", diff --git a/src/idz/handler/saveSettings.ts b/src/idz/handler/saveSettings.ts index b3c5b77..af35a08 100644 --- a/src/idz/handler/saveSettings.ts +++ b/src/idz/handler/saveSettings.ts @@ -6,7 +6,9 @@ export async function saveSettings( w: Repositories, req: SaveSettingsRequest ): Promise { - await w.settings().save(req.profileId, req.settings); + const profileId = await w.profile().find(req.aimeId); + + await w.settings().save(profileId, req.settings); return { type: "generic_res" }; } diff --git a/src/idz/handler/saveStocker.ts b/src/idz/handler/saveStocker.ts index e6fdba1..019076d 100644 --- a/src/idz/handler/saveStocker.ts +++ b/src/idz/handler/saveStocker.ts @@ -6,10 +6,12 @@ export async function saveStocker( w: Repositories, req: SaveStockerRequest ): Promise { + const profileId = await w.profile().find(req.aimeId); + await Promise.all([ - w.backgrounds().saveAll(req.profileId, req.backgrounds), - w.chara().save(req.profileId, req.chara), - w.car().saveSelection(req.profileId, req.selectedCar), + w.backgrounds().saveAll(profileId, req.backgrounds), + w.chara().save(profileId, req.chara), + w.car().saveSelection(profileId, req.selectedCar), ]); return { type: "generic_res" }; diff --git a/src/idz/handler/saveTimeAttack.ts b/src/idz/handler/saveTimeAttack.ts index 296c2f3..a325027 100644 --- a/src/idz/handler/saveTimeAttack.ts +++ b/src/idz/handler/saveTimeAttack.ts @@ -13,9 +13,10 @@ export async function saveTimeAttack( // Override client time since we might be doing some maintenance window // avoidance time warping stuff - await w - .timeAttack() - .save(req.profileId, { ...req.payload, timestamp: new Date() }); + const now = new Date(); + const profileId = await w.profile().find(req.aimeId); + + await w.timeAttack().save(profileId, { ...req.payload, timestamp: now }); } return { diff --git a/src/idz/model/profile.ts b/src/idz/model/profile.ts index 70dd00b..c5a47aa 100644 --- a/src/idz/model/profile.ts +++ b/src/idz/model/profile.ts @@ -1,8 +1,9 @@ import { ExtId } from "./base"; import { Team } from "./team"; +import { AimeId } from "../../model"; export interface Profile { - id: ExtId; + aimeId: AimeId; teamId?: ExtId; name: string; lv: number; diff --git a/src/idz/repo.ts b/src/idz/repo.ts index dcf6b8c..99f0a3a 100644 --- a/src/idz/repo.ts +++ b/src/idz/repo.ts @@ -2,59 +2,52 @@ import { Subtract } from "utility-types"; import * as Model from "./model"; import { AimeId } from "../model"; +import { Id } from "../db"; -export type ProfileSpec = Subtract< - Model.Profile, - { id: Model.ExtId } ->; +export type ProfileSpec = Subtract; export interface CarRepository { - countCars(profileId: Model.ExtId): Promise; + countCars(profileId: Id): Promise; - loadAllCars(profileId: Model.ExtId): Promise; + loadAllCars(profileId: Id): Promise; - loadSelectedCar(profileId: Model.ExtId): Promise; + loadSelectedCar(profileId: Id): Promise; - saveCar( - profileId: Model.ExtId, - car: Model.Car - ): Promise; + saveCar(profileId: Id, car: Model.Car): Promise; saveSelection( - profileId: Model.ExtId, + profileId: Id, selector: Model.CarSelector ): Promise; } export interface CoursePlaysRepository { - loadAll( - profileId: Model.ExtId - ): Promise>; + loadAll(profileId: Id): Promise>; saveAll( - profileId: Model.ExtId, + profileId: Id, counts: Map ): Promise; } export interface FacetRepository { - load(profileId: Model.ExtId): Promise; + load(profileId: Id): Promise; - save(profileId: Model.ExtId, facet: T): Promise; + save(profileId: Id, facet: T): Promise; } export interface FlagRepository { - loadAll(profileId: Model.ExtId): Promise>; + loadAll(profileId: Id): Promise>; - saveAll(profileId: Model.ExtId, items: Set): Promise; + saveAll(profileId: Id, items: Set): Promise; } export interface ProfileRepository { - discoverByAimeId(id: AimeId): Promise; + find(aimeId: AimeId): Promise>; - loadByAimeId(id: AimeId): Promise; + peek(aimeId: AimeId): Promise | undefined>; - load(id: Model.ExtId): Promise; + load(id: Id): Promise; save(profile: Model.Profile, timestamp: Date): Promise; @@ -62,7 +55,7 @@ export interface ProfileRepository { aimeId: AimeId, profile: ProfileSpec, timestamp: Date - ): Promise>; + ): Promise>; } // TODO extend and factorize @@ -77,12 +70,10 @@ export interface TimeAttackRepository { minTimestamp: Date ): Promise; - loadAll( - profileId: Model.ExtId - ): Promise; + loadAll(profileId: Id): Promise; save( - profileId: Model.ExtId, + profileId: Id, score: Model.TimeAttackScore ): Promise; } diff --git a/src/idz/request/createTeam.ts b/src/idz/request/createTeam.ts index aff965f..edd292e 100644 --- a/src/idz/request/createTeam.ts +++ b/src/idz/request/createTeam.ts @@ -1,9 +1,8 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface CreateTeamRequest { type: "create_team_req"; - profileId: ExtId; // u32 + aimeId: AimeId; teamName: string; // len 0x20 field_0028: number; // u16 field_002C: number; // u32 (but only holds a u8) diff --git a/src/idz/request/load2on2.ts b/src/idz/request/load2on2.ts index c11862b..7af40ea 100644 --- a/src/idz/request/load2on2.ts +++ b/src/idz/request/load2on2.ts @@ -1,11 +1,11 @@ import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { Team } from "../model/team"; +import { AimeId } from "../../model"; interface Load2on2RequestBase { type: "load_2on2_req"; field_0002: number; - profileId: ExtId; + aimeId: AimeId; teamId: ExtId; } diff --git a/src/idz/request/loadEventInfo.ts b/src/idz/request/loadEventInfo.ts index 6ef772a..072b628 100644 --- a/src/idz/request/loadEventInfo.ts +++ b/src/idz/request/loadEventInfo.ts @@ -1,7 +1,6 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface LoadEventInfoRequest { type: "load_event_info_req"; - profileId: ExtId; + aimeId: AimeId; } diff --git a/src/idz/request/loadGacha.ts b/src/idz/request/loadGacha.ts index 65b06af..995b2c5 100644 --- a/src/idz/request/loadGacha.ts +++ b/src/idz/request/loadGacha.ts @@ -1,7 +1,6 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface LoadGachaRequest { type: "load_gacha_req"; - profileId: ExtId; + aimeId: AimeId; } diff --git a/src/idz/request/loadGarage.ts b/src/idz/request/loadGarage.ts index ccf6a73..b9625d8 100644 --- a/src/idz/request/loadGarage.ts +++ b/src/idz/request/loadGarage.ts @@ -1,9 +1,8 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface LoadGarageRequest { type: "load_garage_req"; - profileId: ExtId; + aimeId: AimeId; fetchOffset: number; field_000A: number; } diff --git a/src/idz/request/loadGeneralReward.ts b/src/idz/request/loadGeneralReward.ts index 8e6601e..57c5fde 100644 --- a/src/idz/request/loadGeneralReward.ts +++ b/src/idz/request/loadGeneralReward.ts @@ -1,9 +1,8 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; interface LoadGeneralRewardRequestBase { type: "load_general_reward_req"; - profileId: ExtId; + aimeId: AimeId; } export interface LoadGeneralRewardRequest1 diff --git a/src/idz/request/loadStocker.ts b/src/idz/request/loadStocker.ts index 90c2254..50d7b52 100644 --- a/src/idz/request/loadStocker.ts +++ b/src/idz/request/loadStocker.ts @@ -1,7 +1,6 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface LoadStockerRequest { type: "load_stocker_req"; - profileId: ExtId; + aimeId: AimeId; } diff --git a/src/idz/request/loadTeam.ts b/src/idz/request/loadTeam.ts index 66d5fc9..0c5729b 100644 --- a/src/idz/request/loadTeam.ts +++ b/src/idz/request/loadTeam.ts @@ -1,5 +1,5 @@ export interface LoadTeamRequest { type: "load_team_req"; - profileId: number; + aimeId: number; teamId: number; } diff --git a/src/idz/request/loadTopTen.ts b/src/idz/request/loadTopTen.ts index 29a2fde..5a160d9 100644 --- a/src/idz/request/loadTopTen.ts +++ b/src/idz/request/loadTopTen.ts @@ -1,6 +1,6 @@ import { ExtId, RouteNo } from "../model/base"; -import { Profile } from "../model/profile"; import { Team } from "../model/team"; +import { AimeId } from "../../model"; export interface LoadTopTenRequestSelector { routeNo: RouteNo; @@ -14,6 +14,6 @@ export interface LoadTopTenRequest { field_C4: number; field_C5: number; field_C6: number; - profileId?: ExtId; + aimeId?: AimeId; teamId?: ExtId; } diff --git a/src/idz/request/lockProfile.ts b/src/idz/request/lockProfile.ts index f3e5e8e..ea1df1c 100644 --- a/src/idz/request/lockProfile.ts +++ b/src/idz/request/lockProfile.ts @@ -1,6 +1,6 @@ export interface LockProfileRequest { type: "lock_profile_req"; - profileId: number; + aimeId: number; pcbId: string; field_0018: number; } diff --git a/src/idz/request/lockProfileExtend.ts b/src/idz/request/lockProfileExtend.ts index 1e17029..c0e5b0f 100644 --- a/src/idz/request/lockProfileExtend.ts +++ b/src/idz/request/lockProfileExtend.ts @@ -1,8 +1,7 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface LockProfileExtendRequest { type: "lock_profile_extend_req"; - profileId: ExtId; + aimeId: AimeId; luid: string; } diff --git a/src/idz/request/saveGarage.ts b/src/idz/request/saveGarage.ts index 2fabc63..2639629 100644 --- a/src/idz/request/saveGarage.ts +++ b/src/idz/request/saveGarage.ts @@ -2,7 +2,7 @@ import { Car } from "../model/car"; export interface SaveGarageRequest { type: "save_garage_req"; - profileId: number; + aimeId: number; payload: Car; field_0068: number[]; field_0080: number; diff --git a/src/idz/request/saveNewCar.ts b/src/idz/request/saveNewCar.ts index 85f9544..a14eac9 100644 --- a/src/idz/request/saveNewCar.ts +++ b/src/idz/request/saveNewCar.ts @@ -1,10 +1,9 @@ -import { ExtId } from "../model/base"; import { Car } from "../model/car"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface SaveNewCarRequest { type: "save_new_car_req"; - profileId: ExtId; + aimeId: AimeId; luid: string; car: Car; field_0080: number; diff --git a/src/idz/request/saveProfile.ts b/src/idz/request/saveProfile.ts index eac5a46..65283bb 100644 --- a/src/idz/request/saveProfile.ts +++ b/src/idz/request/saveProfile.ts @@ -1,15 +1,15 @@ import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base"; import { Car } from "../model/car"; 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 { Unlocks } from "../model/unlocks"; +import { AimeId } from "../../model"; interface SaveProfileRequestBase { type: "save_profile_req"; - profileId: ExtId; + aimeId: AimeId; lv: number; exp: number; fame: number; diff --git a/src/idz/request/saveSettings.ts b/src/idz/request/saveSettings.ts index fc63876..f106d39 100644 --- a/src/idz/request/saveSettings.ts +++ b/src/idz/request/saveSettings.ts @@ -1,10 +1,9 @@ -import { ExtId } from "../model/base"; -import { Profile } from "../model/profile"; import { Settings } from "../model/settings"; +import { AimeId } from "../../model"; export interface SaveSettingsRequest { type: "save_settings_req"; - profileId: ExtId; + aimeId: AimeId; dpoint: number; // ?? why settings: Settings; field_0010: number; diff --git a/src/idz/request/saveStocker.ts b/src/idz/request/saveStocker.ts index eb97ee4..9c7aef6 100644 --- a/src/idz/request/saveStocker.ts +++ b/src/idz/request/saveStocker.ts @@ -1,11 +1,11 @@ -import { BackgroundCode, ExtId } from "../model/base"; +import { BackgroundCode } from "../model/base"; import { CarSelector } from "../model/car"; import { Chara } from "../model/chara"; -import { Profile } from "../model/profile"; +import { AimeId } from "../../model"; export interface SaveStockerRequest { type: "save_stocker_req"; - profileId: ExtId; + aimeId: AimeId; selectedCar: CarSelector; backgrounds: Set; chara: Chara; diff --git a/src/idz/request/saveTimeAttack.ts b/src/idz/request/saveTimeAttack.ts index 73ea053..ef81205 100644 --- a/src/idz/request/saveTimeAttack.ts +++ b/src/idz/request/saveTimeAttack.ts @@ -1,11 +1,9 @@ -import { ExtId } from "../model/base"; -import { CarSelector } from "../model/car"; -import { Profile } from "../model/profile"; import { TimeAttackScore } from "../model/timeAttack"; +import { AimeId } from "../../model"; export interface SaveTimeAttackRequest { type: "save_time_attack_req"; - profileId: ExtId; // u32 + aimeId: AimeId; dayNight: number; payload: TimeAttackScore; field_0002: number; // u16, always 1 diff --git a/src/idz/request/unlockProfile.ts b/src/idz/request/unlockProfile.ts index f2b4caf..ad1bd25 100644 --- a/src/idz/request/unlockProfile.ts +++ b/src/idz/request/unlockProfile.ts @@ -1,5 +1,5 @@ export interface UnlockProfileRequest { type: "unlock_profile_req"; - profileId: number; + aimeId: number; pcbId: string; } diff --git a/src/idz/response/loadProfile.ts b/src/idz/response/loadProfile.ts index 691b2bc..c7596ae 100644 --- a/src/idz/response/loadProfile.ts +++ b/src/idz/response/loadProfile.ts @@ -1,18 +1,18 @@ -import { ExtId, TitleCode } from "../model/base"; +import { 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"; +import { AimeId } from "../../model"; interface LoadProfileResponseBase { type: "load_profile_res"; name: string; - profileId: ExtId; + aimeId: AimeId; lv: number; exp: number; fame: number;