용어 재정의
This commit is contained in:
@@ -2,23 +2,23 @@ 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 TroopID } from "./defs.js";
|
||||
import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, type NationID, OfficerLevel, type SquadID } from "./defs.js";
|
||||
import { must } from "@sammo/util";
|
||||
import { Nation } from "./Nation.js";
|
||||
import { Troop } from "./Troop.js";
|
||||
import { Squad } from "./Squad.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", "turntime"
|
||||
"id", "nationID", "squadID", "nationID", "turntime"
|
||||
];
|
||||
protected readonly _entityType = "General";
|
||||
protected readonly _raw: IGeneralEntity;
|
||||
|
||||
protected _city: WeakRef<City>;
|
||||
protected _troop: WeakRef<Troop> | undefined;
|
||||
protected _squad: WeakRef<Squad> | undefined;
|
||||
protected _nation: WeakRef<Nation>;
|
||||
|
||||
protected readonly iActionHandlers: Map<IActionKey, IAction>;
|
||||
@@ -32,8 +32,8 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||
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(rsc.troop(raw.troopID, raw.nationID)));
|
||||
if(raw.squadID){
|
||||
this._squad = new WeakRef(must(rsc.squad(raw.squadID, raw.nationID)));
|
||||
}
|
||||
|
||||
//TODO: iAction 처리해야함
|
||||
@@ -52,8 +52,8 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||
return must(this._city.deref());
|
||||
}
|
||||
|
||||
public get troop(): Troop | undefined {
|
||||
return this._troop?.deref();
|
||||
public get squad(): Squad | undefined {
|
||||
return this._squad?.deref();
|
||||
}
|
||||
|
||||
public get nation(): Nation {
|
||||
@@ -79,43 +79,43 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||
});
|
||||
}
|
||||
|
||||
public quitTroop(){
|
||||
if(!this._troop){
|
||||
public quitSquad(){
|
||||
if(!this._squad){
|
||||
return;
|
||||
}
|
||||
|
||||
const troop = must(this._troop.deref());
|
||||
if(troop.leader.id === this._raw.id){
|
||||
troop.destructTroop(false);
|
||||
const squad = must(this._squad.deref());
|
||||
if(squad.leader.id === this._raw.id){
|
||||
squad.destructSquad(false);
|
||||
}
|
||||
else{
|
||||
troop.members.delete(this._raw.id);
|
||||
squad.members.delete(this._raw.id);
|
||||
}
|
||||
|
||||
this._troop = undefined;
|
||||
this._squad = undefined;
|
||||
this.forceUpdate((raw) => {
|
||||
raw.troopID = undefined;
|
||||
raw.squadID = undefined;
|
||||
});
|
||||
}
|
||||
|
||||
public changeTroop(troop: TroopID | Troop){
|
||||
this.quitTroop();
|
||||
public changeSquad(squad: SquadID | Squad){
|
||||
this.quitSquad();
|
||||
|
||||
const troopObj: Troop = (()=>{
|
||||
if(troop instanceof Troop){
|
||||
return troop;
|
||||
const squadObj: Squad = (()=>{
|
||||
if(squad instanceof Squad){
|
||||
return squad;
|
||||
}
|
||||
const troopObj = this.rsc.troop(troop, this._raw.nationID);
|
||||
if(!troopObj){
|
||||
throw new InvalidArgument(`Troop not found: ${troop}`);
|
||||
const squadObj = this.rsc.squad(squad, this._raw.nationID);
|
||||
if(!squadObj){
|
||||
throw new InvalidArgument(`Squad not found: ${squad}`);
|
||||
}
|
||||
return troopObj;
|
||||
return squadObj;
|
||||
})();
|
||||
|
||||
troopObj.members.set(this._raw.id, new WeakRef(this));
|
||||
this._troop = new WeakRef(troopObj);
|
||||
squadObj.members.set(this._raw.id, new WeakRef(this));
|
||||
this._squad = new WeakRef(squadObj);
|
||||
this.forceUpdate((raw) => {
|
||||
raw.troopID = troopObj.id;
|
||||
raw.squadID = squadObj.id;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||
return nationObj;
|
||||
})();
|
||||
this._nation = new WeakRef(nationObj);
|
||||
this.quitTroop();
|
||||
this.quitSquad();
|
||||
|
||||
this.forceUpdate((raw) => {
|
||||
raw.nationID = nationObj.id;
|
||||
@@ -147,10 +147,10 @@ export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||
}
|
||||
|
||||
public updateTurntime(){
|
||||
const turntermSec = this.rsc.gameEnv().raw.turntermSec;
|
||||
const secondsPerTurn = this.rsc.gameEnv().raw.secondsPerTurn;
|
||||
|
||||
this.forceUpdate((raw) => {
|
||||
raw.turntime = addSeconds(raw.turntime, turntermSec);
|
||||
raw.turntime = addSeconds(raw.turntime, secondsPerTurn);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user