diff --git a/src/idz/db/profile.ts b/src/idz/db/profile.ts index fcb8c2e..ae2f071 100644 --- a/src/idz/db/profile.ts +++ b/src/idz/db/profile.ts @@ -1,23 +1,22 @@ import * as sql from "sql-bricks"; import { ClientBase } from "pg"; -import { ExtId } from "../model/base"; import { Profile } from "../model/profile"; -import { Team } from "../model/team"; -import { ProfileSpec, ProfileRepository } from "../repo"; +import { ProfileRepository } from "../repo"; import { generateId, Id } from "../../db"; import { AimeId } from "../../model"; function _extractProfile(row: any): Profile { return { - aimeId: row.ext_id, - teamId: 2 as ExtId, // TODO + aimeId: row.aime_id, name: row.name, lv: row.lv, exp: row.exp, fame: row.fame, dpoint: row.dpoint, mileage: row.mileage, + accessTime: row.access_time, + registerTime: row.register_time, }; } @@ -54,9 +53,10 @@ export class SqlProfileRepository implements ProfileRepository { async load(id: Id): Promise { const loadSql = sql - .select("p.*") + .select("p.*", "r.ext_id as aime_id") .from("idz.profile p") - .where("id", id) + .join("aime.player r", { "p.player_id": "r.id" }) + .where("p.id", id) .toParams(); const { rows } = await this._conn.query(loadSql); @@ -64,7 +64,7 @@ export class SqlProfileRepository implements ProfileRepository { return _extractProfile(rows[0]); } - async save(profile: Profile, timestamp: Date): Promise { + async save(id: Id, profile: Profile): Promise { const saveSql = sql .update("idz.profile", { lv: profile.lv, @@ -72,23 +72,19 @@ export class SqlProfileRepository implements ProfileRepository { fame: profile.fame, dpoint: profile.dpoint, mileage: profile.mileage, - access_time: timestamp, + access_time: profile.accessTime, }) - .where("id", profile.aimeId) + .where("id", id) .toParams(); await this._conn.query(saveSql); } - async create( - aimeId: AimeId, - profile: ProfileSpec, - timestamp: Date - ): Promise> { + async create(profile: Profile): Promise> { const findSql = sql .select("r.id") .from("aime.player r") - .where("r.ext_id", aimeId) + .where("r.ext_id", profile.aimeId) .toParams(); const { rows } = await this._conn.query(findSql); @@ -111,8 +107,8 @@ export class SqlProfileRepository implements ProfileRepository { fame: profile.fame, dpoint: profile.dpoint, mileage: profile.mileage, - register_time: timestamp, - access_time: timestamp, + register_time: profile.registerTime, + access_time: profile.accessTime, }) .toParams(); diff --git a/src/idz/handler/createProfile.ts b/src/idz/handler/createProfile.ts index d97d637..725e08f 100644 --- a/src/idz/handler/createProfile.ts +++ b/src/idz/handler/createProfile.ts @@ -1,26 +1,29 @@ -import { ExtId } from "../model/base"; import { MissionState } from "../model/mission"; +import { Profile } from "../model/profile"; import { Settings } from "../model/settings"; import { Story } from "../model/story"; -import { Team } from "../model/team"; import { Unlocks } from "../model/unlocks"; import { CreateProfileRequest } from "../request/createProfile"; import { GenericResponse } from "../response/generic"; -import { ProfileSpec, Repositories } from "../repo"; +import { Repositories } from "../repo"; export async function createProfile( w: Repositories, req: CreateProfileRequest ): Promise { + const { aimeId, name } = req; const now = new Date(); - const profile: ProfileSpec = { - teamId: 2 as ExtId, // TODO - name: req.name, + + const profile: Profile = { + aimeId, + name, lv: 1, exp: 0, fame: 0, dpoint: 0, mileage: 0, + accessTime: now, + registerTime: now, }; const missions: MissionState = { team: [], solo: [] }; @@ -33,7 +36,7 @@ export async function createProfile( lastMileageReward: 0, }; - const profileId = await w.profile().create(req.aimeId, profile, now); + const profileId = await w.profile().create(profile); await w.chara().save(profileId, req.chara); await w.car().saveCar(profileId, req.car); @@ -46,6 +49,6 @@ export async function createProfile( return { type: "generic_res", - status: req.aimeId, // "Generic response" my fucking *ass* + status: aimeId, // "Generic response" my fucking *ass* }; } diff --git a/src/idz/handler/discoverProfile.ts b/src/idz/handler/discoverProfile.ts index c6ed520..667b9c4 100644 --- a/src/idz/handler/discoverProfile.ts +++ b/src/idz/handler/discoverProfile.ts @@ -6,7 +6,7 @@ export async function discoverProfile( w: Repositories, req: DiscoverProfileRequest ): Promise { - const profileId = await w.profile().find(req.aimeId); + const profileId = await w.profile().peek(req.aimeId); return { type: "discover_profile_res", diff --git a/src/idz/handler/loadProfile.ts b/src/idz/handler/loadProfile.ts index cb37922..3bc9aa6 100644 --- a/src/idz/handler/loadProfile.ts +++ b/src/idz/handler/loadProfile.ts @@ -36,7 +36,7 @@ export async function loadProfile( fame: profile.fame, dpoint: profile.dpoint, mileage: profile.mileage, - teamId: profile.teamId, + // teamId: TODO settings, chara, titles, diff --git a/src/idz/handler/saveProfile.ts b/src/idz/handler/saveProfile.ts index 346e7b2..4f5ba46 100644 --- a/src/idz/handler/saveProfile.ts +++ b/src/idz/handler/saveProfile.ts @@ -11,17 +11,15 @@ export async function saveProfile( const profile = await w.profile().load(profileId); const chara = await w.chara().load(profileId); - await w.profile().save( - { - ...profile, - lv: req.lv, - exp: req.exp, - fame: req.fame, - dpoint: req.dpoint, - mileage: req.mileage, - }, - now - ); + await w.profile().save(profileId, { + ...profile, + lv: req.lv, + exp: req.exp, + fame: req.fame, + dpoint: req.dpoint, + mileage: req.mileage, + accessTime: now, + }); await w.chara().save(profileId, { ...chara, diff --git a/src/idz/model/profile.ts b/src/idz/model/profile.ts index c5a47aa..12fa0e1 100644 --- a/src/idz/model/profile.ts +++ b/src/idz/model/profile.ts @@ -1,14 +1,13 @@ -import { ExtId } from "./base"; -import { Team } from "./team"; import { AimeId } from "../../model"; export interface Profile { aimeId: AimeId; - teamId?: ExtId; name: string; lv: number; exp: number; fame: number; dpoint: number; mileage: number; + accessTime: Date; + registerTime: Date; } diff --git a/src/idz/repo.ts b/src/idz/repo.ts index 99f0a3a..f6c0f0b 100644 --- a/src/idz/repo.ts +++ b/src/idz/repo.ts @@ -1,11 +1,7 @@ -import { Subtract } from "utility-types"; - import * as Model from "./model"; import { AimeId } from "../model"; import { Id } from "../db"; -export type ProfileSpec = Subtract; - export interface CarRepository { countCars(profileId: Id): Promise; @@ -49,13 +45,9 @@ export interface ProfileRepository { load(id: Id): Promise; - save(profile: Model.Profile, timestamp: Date): Promise; + save(id: Id, profile: Model.Profile): Promise; - create( - aimeId: AimeId, - profile: ProfileSpec, - timestamp: Date - ): Promise>; + create(profile: Model.Profile): Promise>; } // TODO extend and factorize