From 651d826963b3dd97a394c993de46de2998ba6268 Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 29 Apr 2019 00:06:13 -0400 Subject: [PATCH] idz: Add SqlCoursePlaysRepository --- src/idz/db/coursePlays.ts | 51 ++++++++++++++++++++++++++++++++++ src/idz/decoder/saveProfile.ts | 6 ++-- src/idz/model/base.ts | 1 + src/idz/model/index.ts | 2 +- src/idz/repo.ts | 6 ++-- src/idz/request/saveProfile.ts | 4 +-- 6 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/idz/db/coursePlays.ts diff --git a/src/idz/db/coursePlays.ts b/src/idz/db/coursePlays.ts new file mode 100644 index 0000000..135ff54 --- /dev/null +++ b/src/idz/db/coursePlays.ts @@ -0,0 +1,51 @@ +import { Client } 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"; + +export class SqlCoursePlaysRepository implements CoursePlaysRepository { + constructor(private readonly _conn: Client) {} + + async loadAll(extId: ExtId): 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) + .toParams(); + + const { rows } = await this._conn.query(sql); + const result = new Map(); + + for (const row of rows) { + result.set(row.course_no, row.count); + } + + return result; + } + + async saveAll( + extId: ExtId, + plays: Map + ): Promise { + const profileId = await _findProfile(this._conn, extId); + + for (const [k, v] of plays) { + const saveSql = sql + .insert("idz.course_plays", { + id: generateId(), + profile_id: profileId, + course_no: k, + count: v, + }) + .onConflict("profile_id", "course_no") + .doUpdate("count"); + + await this._conn.query(saveSql); + } + } +} diff --git a/src/idz/decoder/saveProfile.ts b/src/idz/decoder/saveProfile.ts index 9430dd3..2dc0ea0 100644 --- a/src/idz/decoder/saveProfile.ts +++ b/src/idz/decoder/saveProfile.ts @@ -1,7 +1,7 @@ import { car } from "./_car"; import { mission } from "./_mission"; import { RequestCode } from "./_defs"; -import { BackgroundCode, ExtId, TitleCode } from "../model/base"; +import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base"; import { Profile } from "../model/profile"; import { SaveProfileRequest } from "../request/saveProfile"; import { bitmap } from "./_bitmap"; @@ -29,10 +29,10 @@ export function saveProfile(buf: Buffer): SaveProfileRequest { storyRows.push(row); } - const coursePlays = new Map(); + const coursePlays = new Map(); for (let i = 0; i < 16; i++) { - coursePlays.set(i, buf.readUInt16LE(0x04c0 + 2 * i)); + coursePlays.set(i as CourseNo, buf.readUInt16LE(0x04c0 + 2 * i)); } const freeCar = { diff --git a/src/idz/model/base.ts b/src/idz/model/base.ts index e19bffb..63314f0 100644 --- a/src/idz/model/base.ts +++ b/src/idz/model/base.ts @@ -2,6 +2,7 @@ // exist at run time, we just pretend they do. export type BackgroundCode = number & { __brand: "backgroundCode" }; +export type CourseNo = number & { __brand: "courseNo" }; export type ExtId = number & { __extId: T }; export type RouteNo = number & { __brand: "routeNo" }; export type TitleCode = number & { __brand: "titleCode" }; diff --git a/src/idz/model/index.ts b/src/idz/model/index.ts index 76e027a..d2dc29a 100644 --- a/src/idz/model/index.ts +++ b/src/idz/model/index.ts @@ -1,4 +1,4 @@ -export { BackgroundCode, ExtId, TitleCode } from "./base"; +export { BackgroundCode, CourseNo, ExtId, TitleCode } from "./base"; export { Car, CarSelector } from "./car"; export { Chara } from "./chara"; export { MissionState, MissionGrid } from "./mission"; diff --git a/src/idz/repo.ts b/src/idz/repo.ts index 8456b22..840d10a 100644 --- a/src/idz/repo.ts +++ b/src/idz/repo.ts @@ -27,11 +27,13 @@ export interface CarRepository { } export interface CoursePlaysRepository { - loadAll(profileId: Model.ExtId): Promise>; + loadAll( + profileId: Model.ExtId + ): Promise>; saveAll( profileId: Model.ExtId, - counts: Map + counts: Map ): Promise; } diff --git a/src/idz/request/saveProfile.ts b/src/idz/request/saveProfile.ts index 5050c5a..6d29ea6 100644 --- a/src/idz/request/saveProfile.ts +++ b/src/idz/request/saveProfile.ts @@ -1,4 +1,4 @@ -import { BackgroundCode, ExtId, TitleCode } from "../model/base"; +import { BackgroundCode, CourseNo, ExtId, TitleCode } from "../model/base"; import { Car } from "../model/car"; import { MissionState } from "../model/mission"; import { Profile } from "../model/profile"; @@ -18,7 +18,7 @@ export interface SaveProfileRequest { title: TitleCode; titles: TitleCode[]; background: BackgroundCode; - coursePlays: Map; + coursePlays: Map; missions: MissionState; car: Car; story: Story;