refac: shorthand name. ResourceController > rsc

This commit is contained in:
2024-03-09 07:40:55 +00:00
parent a6833473de
commit a430d45e2a
6 changed files with 61 additions and 36 deletions
+2 -2
View File
@@ -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<ICityEntity>{
@@ -10,7 +10,7 @@ export class City extends LazyEntityUpdater<ICityEntity>{
constructor(
raw: ICityEntity,
protected readonly resourceController: IResourceController
protected readonly rsc: IResourceController
){
super();
this._raw = raw;
+2 -2
View File
@@ -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<IGameEnvEntity>{
protected readonly _indirectNames: readonly (keyof IGameEnvEntity)[] = [
@@ -11,7 +11,7 @@ export class GameEnv extends LazyEntityUpdater<IGameEnvEntity>{
constructor(
raw: IGameEnvEntity,
protected readonly resourceController: IResourceController
protected readonly rsc: IResourceController
){
super();
this._raw = raw;
+29 -17
View File
@@ -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<IGeneralEntity> {
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<IGeneralEntity> {
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<IGeneralEntity> {
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<IGeneralEntity> {
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<IGeneralEntity> {
}
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<IGeneralEntity> {
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<IGeneralEntity> {
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<IGeneralEntity> {
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<IGeneralEntity> {
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<IGeneralEntity> {
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);
});
}
}
+14 -9
View File
@@ -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<Entity extends ValidEntity>{
protected abstract readonly _indirectNames: ReadonlyArray<keyof Entity>;
@@ -7,7 +7,7 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
protected abstract readonly _raw: Entity;
protected abstract readonly resourceController: IResourceController;
protected abstract readonly rsc: IResourceController;
public get raw(): Readonly<Entity> {
return this._raw;
@@ -17,16 +17,21 @@ export abstract class LazyEntityUpdater<Entity extends ValidEntity>{
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);
}
}
+2 -2
View File
@@ -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<INationEntity> {
@@ -10,7 +10,7 @@ export class Nation extends LazyEntityUpdater<INationEntity> {
constructor(
raw: INationEntity,
protected readonly resourceController: IResourceController
protected readonly rsc: IResourceController
){
super();
this._raw = raw;
+12 -4
View File
@@ -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<ITroopEntity> {
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<ITroopEntity> {
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에서 처리
}