refac: bootstrap()

- 각 클래스가 WeakRef로 상호 참조한다면, construct 시점엔 불가
- bootstrap() 함수로 이후 참조
This commit is contained in:
2024-03-14 16:26:40 +00:00
parent 3e4129c3f1
commit b4b00e1983
14 changed files with 335 additions and 59 deletions
+36 -10
View File
@@ -2,7 +2,7 @@ import { InvalidArgument } from "@sammo/util";
import { City } from "./City.js";
import type { IGeneralEntity } from "./Entity/GeneralEntity.js";
import type { IResourceController } from "./IResourceController.js";
import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, type NationID, OfficerLevel, type SquadID } from "./defs.js";
import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, type NationID, OfficerLevel, type SquadID, NpcType, StaffLevel, CityOfficerLevel } from "./defs.js";
import { must } from "@sammo/util";
import { Nation } from "./Nation.js";
import { Squad } from "./Squad.js";
@@ -17,11 +17,11 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
protected readonly _entityType = "General";
protected readonly _raw: IGeneralEntity;
protected _city: WeakRef<City>;
protected _squad: WeakRef<Squad> | undefined;
protected _nation: WeakRef<Nation>;
protected _city!: WeakRef<City>;
protected _squad!: WeakRef<Squad> | undefined;
protected _nation!: WeakRef<Nation>;
protected readonly iActionHandlers: Map<IActionKey, IAction>;
protected readonly iActionHandlers = new Map<IActionKey, IAction>;
constructor(
raw: IGeneralEntity,
@@ -29,6 +29,11 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
){
super();
this._raw = raw;
}
public bootstrap(){
const rsc = this.rsc;
const raw = this._raw;
this._city = new WeakRef(must(rsc.city(raw.cityID)));
this._nation = new WeakRef(must(rsc.nation(raw.nationID)));
@@ -36,8 +41,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
this._squad = new WeakRef(must(rsc.squad(raw.squadID, raw.nationID)));
}
//TODO: iAction 처리해야함
this.iActionHandlers = new Map();
//TODO: iActionHandlers 초기화
}
public get id(): GeneralID {
@@ -60,6 +64,28 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
return must(this._nation.deref());
}
public get isUser(): boolean {
return this._raw.npcType <= NpcType.user_borrowed_npc;
}
public get staffLevel(): StaffLevel | undefined {
if(this._raw.officerLevel < OfficerLevel.staffIntel3){
return undefined;
}
return this._raw.officerLevel as number as StaffLevel;
}
public get cityOfficerLevel(): CityOfficerLevel | undefined {
if(this._raw.officerLevel >= OfficerLevel.staffIntel3){
return undefined;
}
if(this._raw.officerLevel < OfficerLevel.cityLeadership){
return undefined;
}
return this._raw.officerLevel as number as CityOfficerLevel;
}
public changeCity(city: CityID | City){
const cityObj: City = (()=>{
if(city instanceof City){
@@ -86,7 +112,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
const squad = must(this._squad.deref());
if(squad.leader.id === this._raw.id){
squad.destructSquad(false);
squad.destroySquad(false);
}
else{
squad.members.delete(this._raw.id);
@@ -142,12 +168,12 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
else{
raw.officerLevel = OfficerLevel.normal;
}
raw.nationJoinedRelYear = 0;
raw.nationJoinedYearMonth = this.rsc.gameEnv.raw.yearMonth;
});
}
public updateTurnTime(){
const secondsPerTurn = this.rsc.gameEnv().raw.secondsPerTurn;
const secondsPerTurn = this.rsc.gameEnv.raw.secondsPerTurn;
this.forceUpdate((raw) => {
raw.turnTime = addSeconds(raw.turnTime, secondsPerTurn);