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
+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);
});
}
}