From a430d45e2a479121823f81d85f7f2a837a892f29 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 9 Mar 2024 07:40:55 +0000 Subject: [PATCH] refac: shorthand name. ResourceController > rsc --- @sammo/game_logic/src/City.ts | 4 +- @sammo/game_logic/src/GameEnv.ts | 4 +- @sammo/game_logic/src/General.ts | 46 ++++++++++++++-------- @sammo/game_logic/src/LazyEntityUpdater.ts | 23 ++++++----- @sammo/game_logic/src/Nation.ts | 4 +- @sammo/game_logic/src/Troop.ts | 16 ++++++-- 6 files changed, 61 insertions(+), 36 deletions(-) diff --git a/@sammo/game_logic/src/City.ts b/@sammo/game_logic/src/City.ts index 70615fd..409145f 100644 --- a/@sammo/game_logic/src/City.ts +++ b/@sammo/game_logic/src/City.ts @@ -1,6 +1,6 @@ import type { ICityEntity } from "./Entity/CityEntity.js"; import { LazyEntityUpdater } from "./LazyEntityUpdater.js"; -import type { IResourceController } from "./ResourceController.js"; +import type { IResourceController } from "./IResourceController.js"; import type { CityID } from "./defs.js"; export class City extends LazyEntityUpdater{ @@ -10,7 +10,7 @@ export class City extends LazyEntityUpdater{ constructor( raw: ICityEntity, - protected readonly resourceController: IResourceController + protected readonly rsc: IResourceController ){ super(); this._raw = raw; diff --git a/@sammo/game_logic/src/GameEnv.ts b/@sammo/game_logic/src/GameEnv.ts index 3b2cd9b..839609a 100644 --- a/@sammo/game_logic/src/GameEnv.ts +++ b/@sammo/game_logic/src/GameEnv.ts @@ -1,6 +1,6 @@ import type { IGameEnvEntity } from "./Entity/GameEnvEntity.js"; import { LazyEntityUpdater } from "./LazyEntityUpdater.js"; -import type { IResourceController } from "./ResourceController.js"; +import type { IResourceController } from "./IResourceController.js"; export class GameEnv extends LazyEntityUpdater{ protected readonly _indirectNames: readonly (keyof IGameEnvEntity)[] = [ @@ -11,7 +11,7 @@ export class GameEnv extends LazyEntityUpdater{ constructor( raw: IGameEnvEntity, - protected readonly resourceController: IResourceController + protected readonly rsc: IResourceController ){ super(); this._raw = raw; diff --git a/@sammo/game_logic/src/General.ts b/@sammo/game_logic/src/General.ts index 4f3f876..a06a379 100644 --- a/@sammo/game_logic/src/General.ts +++ b/@sammo/game_logic/src/General.ts @@ -1,16 +1,19 @@ import { InvalidArgument } from "@sammo/util"; import { City } from "./City.js"; import type { IGeneralEntity } from "./Entity/GeneralEntity.js"; -import type { IResourceController } from "./ResourceController.js"; +import type { IResourceController } from "./IResourceController.js"; import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, type NationID, OfficerLevel, type TroopID } from "./defs.js"; import { must } from "@sammo/util"; import { Nation } from "./Nation.js"; import { Troop } from "./Troop.js"; import { LazyEntityUpdater } from "./LazyEntityUpdater.js"; import type { IAction, IActionKey } from "./IAction.js"; +import { addSeconds } from "date-fns"; export class General extends LazyEntityUpdater { - protected readonly _indirectNames: (keyof IGeneralEntity)[] = ["id", "nationID", "troopID", "nationID"]; + protected readonly _indirectNames: (keyof IGeneralEntity)[] = [ + "id", "nationID", "troopID", "nationID", "turntime" + ]; protected readonly _entityType = "General"; protected readonly _raw: IGeneralEntity; @@ -22,15 +25,15 @@ export class General extends LazyEntityUpdater { constructor( raw: IGeneralEntity, - protected readonly resourceController: IResourceController + protected readonly rsc: IResourceController ){ super(); this._raw = raw; - this._city = new WeakRef(must(resourceController.city(raw.cityID))); - this._nation = new WeakRef(must(resourceController.nation(raw.nationID))); + this._city = new WeakRef(must(rsc.city(raw.cityID))); + this._nation = new WeakRef(must(rsc.nation(raw.nationID))); if(raw.troopID){ - this._troop = new WeakRef(must(resourceController.troop(raw.troopID, raw.nationID))); + this._troop = new WeakRef(must(rsc.troop(raw.troopID, raw.nationID))); } //TODO: iAction 처리해야함 @@ -62,7 +65,7 @@ export class General extends LazyEntityUpdater { if(city instanceof City){ return city; } - const cityObj = this.resourceController.city(city); + const cityObj = this.rsc.city(city); if(!cityObj){ throw new InvalidArgument(`City not found: ${city}`); } @@ -71,9 +74,9 @@ export class General extends LazyEntityUpdater { this._city = new WeakRef(cityObj); //TODO: ResourceController에게 알려야 하는가? - this.update((raw) => { + this.forceUpdate((raw) => { raw.cityID = cityObj.id; - }, true); + }); } public quitTroop(){ @@ -90,9 +93,9 @@ export class General extends LazyEntityUpdater { } this._troop = undefined; - this.update((raw) => { + this.forceUpdate((raw) => { raw.troopID = undefined; - }, true); + }); } public changeTroop(troop: TroopID | Troop){ @@ -102,7 +105,7 @@ export class General extends LazyEntityUpdater { if(troop instanceof Troop){ return troop; } - const troopObj = this.resourceController.troop(troop, this._raw.nationID); + const troopObj = this.rsc.troop(troop, this._raw.nationID); if(!troopObj){ throw new InvalidArgument(`Troop not found: ${troop}`); } @@ -111,9 +114,9 @@ export class General extends LazyEntityUpdater { troopObj.members.set(this._raw.id, new WeakRef(this)); this._troop = new WeakRef(troopObj); - this.update((raw) => { + this.forceUpdate((raw) => { raw.troopID = troopObj.id; - }, true); + }); } public changeNation(nation: NationID | Nation){ @@ -121,7 +124,7 @@ export class General extends LazyEntityUpdater { if(nation instanceof Nation){ return nation; } - const nationObj = this.resourceController.nation(nation); + const nationObj = this.rsc.nation(nation); if(!nationObj){ throw new InvalidArgument(`Nation not found: ${nation}`); } @@ -130,7 +133,7 @@ export class General extends LazyEntityUpdater { this._nation = new WeakRef(nationObj); this.quitTroop(); - this.update((raw) => { + this.forceUpdate((raw) => { raw.nationID = nationObj.id; raw.diplomaticPermission = DiplomaticPermission.none; if(nationObj.id == 0){ @@ -140,7 +143,16 @@ export class General extends LazyEntityUpdater { raw.officerLevel = OfficerLevel.normal; } raw.nationBelong = 0; - }, true); + }); + } + + public updateTurntime(){ + const turntermSec = this.rsc.gameEnv().raw.turntermSec; + + this.forceUpdate((raw) => { + raw.turntime = addSeconds(raw.turntime, turntermSec); + }); + } } \ No newline at end of file diff --git a/@sammo/game_logic/src/LazyEntityUpdater.ts b/@sammo/game_logic/src/LazyEntityUpdater.ts index c6b9734..df339ad 100644 --- a/@sammo/game_logic/src/LazyEntityUpdater.ts +++ b/@sammo/game_logic/src/LazyEntityUpdater.ts @@ -1,5 +1,5 @@ import type { ValidEntity, EntityType } from "./Entity/index.js"; -import type { IResourceController } from "./ResourceController.js"; +import type { IResourceController } from "./IResourceController.js"; export abstract class LazyEntityUpdater{ protected abstract readonly _indirectNames: ReadonlyArray; @@ -7,7 +7,7 @@ export abstract class LazyEntityUpdater{ protected abstract readonly _raw: Entity; - protected abstract readonly resourceController: IResourceController; + protected abstract readonly rsc: IResourceController; public get raw(): Readonly { return this._raw; @@ -17,16 +17,21 @@ export abstract class LazyEntityUpdater{ return this._entityType; } - public update(callback: (oldRaw: Entity) => void, force = false) { + protected forceUpdate(callback: (raw: Entity) => void) { + callback(this._raw); + this.rsc.reserveUpdate(this); + } + + public update(callback: (raw: Entity) => void) { const oldRaw = { ...this._raw }; callback(this._raw); - if (!force) { - for (const indirectName of this._indirectNames) { - if (oldRaw[indirectName] !== this._raw[indirectName]) { - throw new Error(`${String(indirectName)} change by update() is not allowed`); - } + + //for구문이 빠른가, Proxy setter로 제어하는게 빠른가 + for (const indirectName of this._indirectNames) { + if (oldRaw[indirectName] !== this._raw[indirectName]) { + throw new Error(`${String(indirectName)} change by update() is not allowed`); } } - this.resourceController.reserveUpdate(this); + this.rsc.reserveUpdate(this); } } \ No newline at end of file diff --git a/@sammo/game_logic/src/Nation.ts b/@sammo/game_logic/src/Nation.ts index 0eec4eb..dd90c70 100644 --- a/@sammo/game_logic/src/Nation.ts +++ b/@sammo/game_logic/src/Nation.ts @@ -1,6 +1,6 @@ import { LazyEntityUpdater } from "./LazyEntityUpdater.js"; import type { INationEntity } from "./Entity/NationEntity.js"; -import type { IResourceController } from "./ResourceController.js"; +import type { IResourceController } from "./IResourceController.js"; import type { NationID, NationName } from "./defs.js"; export class Nation extends LazyEntityUpdater { @@ -10,7 +10,7 @@ export class Nation extends LazyEntityUpdater { constructor( raw: INationEntity, - protected readonly resourceController: IResourceController + protected readonly rsc: IResourceController ){ super(); this._raw = raw; diff --git a/@sammo/game_logic/src/Troop.ts b/@sammo/game_logic/src/Troop.ts index eaa0571..e0f06b1 100644 --- a/@sammo/game_logic/src/Troop.ts +++ b/@sammo/game_logic/src/Troop.ts @@ -1,6 +1,6 @@ import { must } from "@sammo/util"; import type { General } from "./General.js"; -import type { IResourceController } from "./ResourceController.js"; +import type { IResourceController } from "./IResourceController.js"; import type { ITroopEntity } from "./Entity/TroopEntity.js"; import type { GeneralID } from "./defs.js"; import { LazyEntityUpdater } from "./LazyEntityUpdater.js"; @@ -14,12 +14,12 @@ export class Troop extends LazyEntityUpdater { constructor( raw: ITroopEntity, - protected resourceController: IResourceController + protected rsc: IResourceController ){ super(); this._raw = raw; - const members = resourceController.generalByTroop(raw.id); + const members = rsc.generalByTroop(raw.id); const leaderID: GeneralID = raw.id as GeneralID; this._leader = new WeakRef(must(members.get(leaderID))); members.delete(leaderID); @@ -47,7 +47,15 @@ export class Troop extends LazyEntityUpdater { this.leader.quitTroop(); } - this.resourceController.reserveDelete(this); + this.rsc.reserveDelete(this); } + public changeName(name: string){ + this.update((raw)=>{ + raw.name = name; + }); + } + + //joinTroop은 General에서 처리 + //quitTroop은 General에서 처리 } \ No newline at end of file