feat: LazyEntityUpdater
This commit is contained in:
@@ -6,21 +6,22 @@ import { DiplomaticPermission, type CityID, type GeneralID, type GeneralName, ty
|
||||
import { must } from "@sammo/util";
|
||||
import { Nation } from "./Nation.js";
|
||||
import { Troop } from "./Troop.js";
|
||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||
|
||||
export class General {
|
||||
private _raw: IGeneralEntity;
|
||||
private _city: WeakRef<City>;
|
||||
private _troop: WeakRef<Troop> | undefined;
|
||||
private _nation: WeakRef<Nation>;
|
||||
export class General extends LazyEntityUpdater<IGeneralEntity> {
|
||||
protected readonly _indirectNames: (keyof IGeneralEntity)[] = ["id", "nationID", "troopID", "nationID"];
|
||||
protected readonly _entityType = "General";
|
||||
protected readonly _raw: IGeneralEntity;
|
||||
|
||||
public get raw(): Readonly<IGeneralEntity> {
|
||||
return this._raw;
|
||||
}
|
||||
protected _city: WeakRef<City>;
|
||||
protected _troop: WeakRef<Troop> | undefined;
|
||||
protected _nation: WeakRef<Nation>;
|
||||
|
||||
constructor(
|
||||
raw: IGeneralEntity,
|
||||
private resourceController: IResourceController
|
||||
protected readonly resourceController: IResourceController
|
||||
){
|
||||
super();
|
||||
this._raw = raw;
|
||||
this._city = new WeakRef(must(resourceController.getCity(raw.cityID)));
|
||||
this._nation = new WeakRef(must(resourceController.getNation(raw.nationID)));
|
||||
@@ -38,26 +39,16 @@ export class General {
|
||||
return this._raw.name;
|
||||
}
|
||||
|
||||
public update(callback: (oldRaw: IGeneralEntity) => void, force = false) {
|
||||
public get city(): City {
|
||||
return must(this._city.deref());
|
||||
}
|
||||
|
||||
const oldCityID = this._raw.cityID;
|
||||
const oldTroopID = this._raw.troopID;
|
||||
const oldNationID = this._raw.nationID;
|
||||
public get troop(): Troop | undefined {
|
||||
return this._troop?.deref();
|
||||
}
|
||||
|
||||
callback(this._raw);
|
||||
|
||||
if(!force){
|
||||
if(oldCityID !== this._raw.cityID){
|
||||
throw new Error("City change by update() is not allowed");
|
||||
}
|
||||
if(oldTroopID !== this._raw.troopID){
|
||||
throw new Error("Troop change by update() is not allowed");
|
||||
}
|
||||
if(oldNationID !== this._raw.nationID){
|
||||
throw new Error("Nation change by update() is not allowed");
|
||||
}
|
||||
}
|
||||
this.resourceController.setDirtyGeneral(this._raw);
|
||||
public get nation(): Nation {
|
||||
return must(this._nation.deref());
|
||||
}
|
||||
|
||||
public changeCity(city: CityID | City){
|
||||
|
||||
Reference in New Issue
Block a user