refac: boostrap
This commit is contained in:
@@ -1,26 +1,72 @@
|
||||
import type { ICityEntity } from "./Entity/CityEntity.js";
|
||||
import { LazyEntityUpdater } from "./LazyEntityUpdater.js";
|
||||
import type { IResourceController } from "./IResourceController.js";
|
||||
import type { CityID } from "./defs.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 rsc: IResourceController
|
||||
protected readonly rsc: IResourceController,
|
||||
bootstrapInfo: {
|
||||
general: GeneralID[];
|
||||
}
|
||||
){
|
||||
super();
|
||||
this._raw = raw;
|
||||
this._bootstrapInfo = bootstrapInfo;
|
||||
}
|
||||
|
||||
private _bootstrapInfo?: ConstructorParameters<typeof City>[2];
|
||||
public bootstrap(){
|
||||
//do nothing;
|
||||
const rsc = this.rsc;
|
||||
|
||||
const bootstrapInfo = must(this._bootstrapInfo);
|
||||
delete this._bootstrapInfo;
|
||||
|
||||
const nationID = this._raw.nationID;
|
||||
|
||||
for(const id of bootstrapInfo.general){
|
||||
const general = must(rsc.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user