From 84541e245c19b7d0458cc6ab93759955263c151f Mon Sep 17 00:00:00 2001 From: Tau Date: Sun, 28 Apr 2019 21:42:59 -0400 Subject: [PATCH] idz: Temporarily delete fs backend --- src/idz/file/_facet.ts | 20 ---------- src/idz/file/_flag.ts | 21 ---------- src/idz/file/_util.ts | 48 ----------------------- src/idz/file/car.ts | 76 ------------------------------------ src/idz/file/coursePlays.ts | 43 -------------------- src/idz/file/index.ts | 78 ------------------------------------- src/idz/file/profile.ts | 38 ------------------ src/idz/file/tickets.ts | 42 -------------------- src/idz/file/timeAttack.ts | 47 ---------------------- 9 files changed, 413 deletions(-) delete mode 100644 src/idz/file/_facet.ts delete mode 100644 src/idz/file/_flag.ts delete mode 100644 src/idz/file/_util.ts delete mode 100644 src/idz/file/car.ts delete mode 100644 src/idz/file/coursePlays.ts delete mode 100644 src/idz/file/index.ts delete mode 100644 src/idz/file/profile.ts delete mode 100644 src/idz/file/tickets.ts delete mode 100644 src/idz/file/timeAttack.ts diff --git a/src/idz/file/_facet.ts b/src/idz/file/_facet.ts deleted file mode 100644 index e0373b8..0000000 --- a/src/idz/file/_facet.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { loadJson, saveJson } from "./_util"; -import { FacetRepository } from "../repo"; -import { Id } from "../model/base"; -import { Profile } from "../model/profile"; - -export class FacetRepositoryImpl implements FacetRepository { - private _path: string; - - constructor(root: string, name: string) { - this._path = `${root}/${name}.json`; - } - - load(id: Id): Promise { - return loadJson(this._path); - } - - save(id: Id, obj: T) { - return saveJson(this._path, obj); - } -} diff --git a/src/idz/file/_flag.ts b/src/idz/file/_flag.ts deleted file mode 100644 index 428b3d0..0000000 --- a/src/idz/file/_flag.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { loadNumberList, saveNumberList } from "./_util"; -import { FlagRepository } from "../repo"; -import { Id } from "../model/base"; -import { Profile } from "../model/profile"; - -export class FlagRepositoryImpl - implements FlagRepository { - private _path: string; - - constructor(root: string, name: string) { - this._path = `${root}/${name}.txt`; - } - - loadAll(profileId: Id): Promise { - return loadNumberList(this._path); - } - - saveAll(profileId: Id, codes: T[]): Promise { - return saveNumberList(this._path, codes); - } -} diff --git a/src/idz/file/_util.ts b/src/idz/file/_util.ts deleted file mode 100644 index a962724..0000000 --- a/src/idz/file/_util.ts +++ /dev/null @@ -1,48 +0,0 @@ -import * as fs from "fs"; -import { promisify } from "util"; - -const readFile = promisify(fs.readFile); -const writeFile = promisify(fs.writeFile); - -export async function loadJson(path: string): Promise { - console.log("Idz: Repository: Loading ", path); - - const json = await readFile(path, { encoding: "utf8" }); - - return JSON.parse(json); -} - -export async function saveJson(path: string, obj: T): Promise { - console.log("Idz: Repository: Saving ", path); - - const json = JSON.stringify(obj, undefined, 2); - - return writeFile(path, json, { encoding: "utf8" }); -} - -export async function loadNumberList( - path: string -): Promise { - console.log("Idz: Repository: Loading ", path); - - if (!fs.existsSync(path)) { - return new Array() as T; - } - - const text = await readFile(path, { encoding: "ascii" }); - const lines = text.split("\n").filter(x => x !== ""); - const codes = lines.map(Number); // parseInt fails due to radix arg from map - - return codes as T; -} - -export async function saveNumberList( - path: string, - codes: T -): Promise { - console.log("Idz: Repository: Saving ", path); - - const text = codes.map(code => code.toString()).join("\n") + "\n"; - - return writeFile(path, text, { encoding: "ascii" }); -} diff --git a/src/idz/file/car.ts b/src/idz/file/car.ts deleted file mode 100644 index ebc36a6..0000000 --- a/src/idz/file/car.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { existsSync } from "fs"; - -import { loadJson, saveJson } from "./_util"; -import { CarRepository } from "../repo"; -import { Id } from "../model/base"; -import { Car, CarSelector } from "../model/car"; -import { Profile } from "../model/profile"; - -interface GarageJson { - selected?: CarSelector; - cars: Car[]; -} - -export class CarRepositoryImpl implements CarRepository { - private readonly _path: string; - - constructor(root: string) { - this._path = root + "/garage.json"; - } - - private async _loadJson(): Promise { - if (existsSync(this._path)) { - return loadJson(this._path); - } else { - return { cars: [] }; - } - } - - async countCars(profileId: Id): Promise { - const garage: GarageJson = await this._loadJson(); - - return garage.cars.length; - } - - async loadAllCars(profileId: Id): Promise { - const garage: GarageJson = await this._loadJson(); - - return garage.cars; - } - - async loadSelectedCar(profileId: Id): Promise { - const garage: GarageJson = await this._loadJson(); - const sel = garage.cars.find(item => item.selector === garage.selected); - - if (sel === undefined) { - throw new Error("No car in garage matching current selector"); - } - - return sel; - } - - async saveCar(profileId: Id, car: Car): Promise { - const garage: GarageJson = await this._loadJson(); - const rest = garage.cars.filter(item => item.selector != car.selector); - - const updated = { ...garage, cars: [...rest, car] }; - - return saveJson(this._path, updated); - } - - async saveSelection( - profileId: Id, - selection: CarSelector - ): Promise { - const garage: GarageJson = await this._loadJson(); - const match = garage.cars.find(item => item.selector === selection); - - if (!match) { - throw new Error(`No match for selector ${selection} in garage`); - } - - garage.selected = selection; - - return saveJson(this._path, garage); - } -} diff --git a/src/idz/file/coursePlays.ts b/src/idz/file/coursePlays.ts deleted file mode 100644 index bb3471d..0000000 --- a/src/idz/file/coursePlays.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { loadNumberList, saveNumberList } from "./_util"; -import { CoursePlaysRepository } from "../repo"; -import { Id } from "../model/base"; -import { Profile } from "../model/profile"; - -export class CoursePlaysRepositoryImpl implements CoursePlaysRepository { - private readonly _path; - - constructor(root) { - this._path = root + "/coursePlays.txt"; - } - - async loadAll(profileId: Id): Promise> { - const lines = await loadNumberList(this._path); - const map = new Map(); - - for (let i = 0; i < lines.length; i++) { - map.set(i, lines[i]); - } - - return map; - } - - saveAll(profileId: Id, counts: Map): Promise { - let max = 0; - - for (const key of counts.keys()) { - if (max < key) { - max = key; - } - } - - const array = new Array(max + 1); - - array.fill(0); - - for (const [k, v] of counts) { - array[k] = v; - } - - return saveNumberList(this._path, array); - } -} diff --git a/src/idz/file/index.ts b/src/idz/file/index.ts deleted file mode 100644 index 7822316..0000000 --- a/src/idz/file/index.ts +++ /dev/null @@ -1,78 +0,0 @@ -import * as fs from "fs"; - -import { FacetRepositoryImpl } from "./_facet"; -import { FlagRepositoryImpl } from "./_flag"; -import { CarRepositoryImpl } from "./car"; -import { CoursePlaysRepositoryImpl } from "./coursePlays"; -import { Repositories } from "../repo"; -import { ProfileRepositoryImpl } from "./profile"; -import { TicketsRepositoryImpl } from "./tickets"; -import { TimeAttackRepositoryImpl } from "./timeAttack"; -import * as Model from "../model"; - -export { Repositories as World }; - -class WorldImpl implements Repositories { - constructor(private readonly _root: string) {} - - backgrounds() { - return new FlagRepositoryImpl( - this._root, - "backgrounds" - ); - } - - car() { - return new CarRepositoryImpl(this._root); - } - - chara() { - return new FacetRepositoryImpl(this._root, "chara"); - } - - coursePlays() { - return new CoursePlaysRepositoryImpl(this._root); - } - - missions() { - return new FacetRepositoryImpl(this._root, "missions"); - } - - profile() { - return new ProfileRepositoryImpl(this._root); - } - - settings() { - return new FacetRepositoryImpl(this._root, "settings"); - } - - story() { - return new FacetRepositoryImpl(this._root, "story"); - } - - tickets() { - return new TicketsRepositoryImpl(this._root); - } - - timeAttack() { - return new TimeAttackRepositoryImpl(this._root); - } - - titles() { - return new FlagRepositoryImpl(this._root, "titles"); - } - - unlocks() { - return new FacetRepositoryImpl(this._root, "unlocks"); - } -} - -export async function beginFilesystemSession( - root: string -): Promise { - if (!fs.existsSync(root)) { - fs.mkdirSync(root); - } - - return new WorldImpl(root); -} diff --git a/src/idz/file/profile.ts b/src/idz/file/profile.ts deleted file mode 100644 index 3f2aa48..0000000 --- a/src/idz/file/profile.ts +++ /dev/null @@ -1,38 +0,0 @@ -import * as fs from "fs"; -import { promisify } from "util"; - -import { loadJson, saveJson } from "./_util"; -import { ProfileRepository } from "../repo"; -import { Id } from "../model/base"; -import { Profile } from "../model/profile"; -import { AimeId } from "../../model"; - -const exists = promisify(fs.exists); - -export class ProfileRepositoryImpl implements ProfileRepository { - private readonly _path: string; - - constructor(root) { - this._path = root + "/profile.json"; - } - - async generateId(): Promise> { - return 1 as Id; - } - - discoverByAimeId(id: AimeId): Promise { - return exists(this._path); - } - - loadByAimeId(id: AimeId): Promise { - return loadJson(this._path); - } - - load(id: Id): Promise { - return loadJson(this._path); - } - - save(id: Id, profile: Profile): Promise { - return saveJson(this._path, profile); - } -} diff --git a/src/idz/file/tickets.ts b/src/idz/file/tickets.ts deleted file mode 100644 index 5605c3d..0000000 --- a/src/idz/file/tickets.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { loadJson, saveJson } from "./_util"; -import { FacetRepository } from "../repo"; -import { Tickets } from "../model/tickets"; -import { Id, Profile } from "../model"; - -interface TicketsJson { - freeCar?: { - validFrom: string; - // Valid To cannot be controlled, it is always 14 days after issue date. - }; - freeContinue?: { - validFrom: string; - validTo: string; - }; -} - -export class TicketsRepositoryImpl implements FacetRepository { - private readonly _path: string; - - constructor(root: string) { - this._path = root + "/tickets.json"; - } - - async load(profileId: Id): Promise { - const json: TicketsJson = await loadJson(this._path); - - return { - freeCar: json.freeCar && { - validFrom: new Date(json.freeCar.validFrom), - }, - freeContinue: json.freeContinue && { - validFrom: new Date(json.freeContinue.validFrom), - validTo: new Date(json.freeContinue.validTo), - }, - }; - } - - save(profileId: Id, tickets: Tickets): Promise { - // Date objects stringify to ISO8601 so this does this right thing. - return saveJson(this._path, tickets); - } -} diff --git a/src/idz/file/timeAttack.ts b/src/idz/file/timeAttack.ts deleted file mode 100644 index add1707..0000000 --- a/src/idz/file/timeAttack.ts +++ /dev/null @@ -1,47 +0,0 @@ -import * as fs from "fs"; - -import { loadJson, saveJson } from "./_util"; -import { TimeAttackRepository } from "../repo"; -import { Id } from "../model/base"; -import { Profile } from "../model/profile"; -import { TimeAttackScore } from "../model/timeAttack"; - -interface TimeAttackJson { - scores: TimeAttackScore[]; -} - -export class TimeAttackRepositoryImpl implements TimeAttackRepository { - private readonly _path: string; - - constructor(root) { - this._path = root + "/timeAttack.json"; - } - - async loadAll(profileId: Id): Promise { - if (!fs.existsSync(this._path)) { - return []; - } - - const json: TimeAttackJson = await loadJson(this._path); - - return json.scores; - } - - async load( - profileId: Id, - courseId: number - ): Promise { - const scores = await this.loadAll(profileId); - - return scores.find(item => item.courseId === courseId); - } - - async save(profileId: Id, score: TimeAttackScore): Promise { - const scores = await this.loadAll(profileId); - const rest = scores.filter(item => item.courseId !== score.courseId); - - const updated: TimeAttackJson = { scores: [...rest, score] }; - - return saveJson(this._path, updated); - } -}