Files
core_ng/@sammo/game_logic/src/City.ts
T
2024-03-15 16:13:27 +00:00

72 lines
2.2 KiB
TypeScript

import type { ICityEntity } from "./Entity/CityEntity.js";
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
import type { IResourceController } from "./IResourceController.js";
import type { CityID, GeneralID, NationID } from "./defs.js";
import type { General } from "./General.js";
import { must } from "@sammo/util";
export class City extends LazyEntityUpdater<ICityEntity>{
protected readonly _indirectNames: (keyof ICityEntity)[] = ["id", "nationID"];
protected readonly _entityType = "City";
protected readonly _raw: ICityEntity;
protected _members = new Map<GeneralID, WeakRef<General>>;
protected _otherMembers = new Map<NationID, Map<GeneralID, WeakRef<General>>>;
constructor(
raw: ICityEntity,
protected readonly rc: IResourceController,
bootstrapInfo: {
general: GeneralID[];
}
){
super();
this._raw = raw;
this._bootstrapInfo = bootstrapInfo;
}
private _bootstrapInfo?: ConstructorParameters<typeof City>[2];
public bootstrap(){
const rc = this.rc;
const bootstrapInfo = must(this._bootstrapInfo);
delete this._bootstrapInfo;
const nationID = this._raw.nationID;
for(const id of bootstrapInfo.general){
const general = must(rc.general(id));
this.notifyJoinGeneral(general);
}
}
get id(): CityID {
return this._raw.id;
}
notifyJoinGeneral(general: General){
const nationID = general.raw.nationID;
if(nationID === this._raw.nationID){
this._members.set(general.id, new WeakRef(general));
return;
}
let otherMembers = this._otherMembers.get(nationID);
if(!otherMembers){
otherMembers = new Map();
this._otherMembers.set(nationID, otherMembers);
}
otherMembers.set(general.id, new WeakRef(general));
}
notifyQuitGeneral(general: General){
const nationID = general.raw.nationID;
if(nationID === this._raw.nationID){
this._members.delete(general.id);
return;
}
const otherMembers = must(this._otherMembers.get(nationID));
otherMembers.delete(general.id);
}
}