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
+2 -2
View File
@@ -81,7 +81,7 @@ export class GameEngine {
upcomingGeneral.updateTurnTime();
this.rsc.insertGeneralTurnTimeQueue(upcomingGeneral, true);
this.rsc.gameEnv().update((env) => {
this.rsc.gameEnv.update((env) => {
env.lastExecuted = generalTurnTime;
})
processedGeneralCount++;
@@ -89,7 +89,7 @@ export class GameEngine {
if (processedGeneralCount > 0) {
await this.prevSaveWaiter;
const lastExecuted = this.rsc.gameEnv().raw.lastExecuted;
const lastExecuted = this.rsc.gameEnv.raw.lastExecuted;
this.loggerEngine.flushAll();
this.prevSaveWaiter = this.rsc.saveAll().then(() => {
this.postStatus({
+83 -18
View File
@@ -1,19 +1,20 @@
import type { IResourceController } from "@sammo/game_logic";
import type { City } from "@sammo/game_logic/src/City.js";
import { City } from "@sammo/game_logic/src/City.js";
import type { CityID, GeneralID, NationID, SquadID } from "@sammo/game_logic/src/defs.js";
import type { IGeneralEntity } from "@sammo/game_logic/src/Entity/GeneralEntity.js";
import type { ICityEntity, ValidEntityType } from "@sammo/game_logic/src/Entity/index.js";
import type { INationEntity } from "@sammo/game_logic/src/Entity/NationEntity.js";
import type { ReservedTurn } from "@sammo/game_logic/src/defs.js";
import type { ISquadEntity } from "@sammo/game_logic/src/Entity/SquadEntity.js";
import type { GameEnv } from "@sammo/game_logic/src/GameEnv.js";
import { GameEnv } from "@sammo/game_logic/src/GameEnv.js";
import { General } from "@sammo/game_logic/src/General.js";
import type { LazyEntityUpdater } from "@sammo/game_logic/src/LazyEntityUpdater.js";
import type { Nation } from "@sammo/game_logic/src/Nation.js";
import type { Squad } from "@sammo/game_logic/src/Squad.js";
import { insertItemAtArrayLike, NotYetImplemented } from "@sammo/util";
import { Nation } from "@sammo/game_logic/src/Nation.js";
import { Squad } from "@sammo/game_logic/src/Squad.js";
import { insertItemAtArrayLike, must, NotYetImplemented } from "@sammo/util";
import Denque from "denque";
import type { Mongoose } from "mongoose";
import type { IGameEnvEntity } from "@sammo/game_logic/src/Entity/GameEnvEntity.js";
type ChangeType = "insert" | "update" | "delete";
@@ -22,13 +23,17 @@ export class ResourceController implements IResourceController{
constructor(
private readonly db: Mongoose
) {
}
private _nation: Map<number, Nation> = new Map();
private _city: Map<number, City> = new Map();
private _squad: Map<number, Squad> = new Map();
private _general: Map<number, General> = new Map();
private _gameEnv: GameEnv = new GameEnv({} as IGameEnvEntity, this);
public get gameEnv(): GameEnv {
return this._gameEnv;
}
public readonly nationList: Map<NationID, Nation> = new Map();
public readonly cityList: Map<CityID, City> = new Map();
public readonly squadList: Map<SquadID, Squad> = new Map();
public readonly generalList: Map<GeneralID, General> = new Map();
//내부적으로 turnTime asc, id asc로 동작하는 PriorityQueue
private _generalByTurnTime = new Denque<General>();
@@ -45,19 +50,26 @@ export class ResourceController implements IResourceController{
} as const satisfies Record<ValidEntityType, Map<number, [ChangeType, LazyEntityUpdater<any>]>>;
nation(id: NationID): Nation | undefined {
throw new NotYetImplemented();
return this.nationList.get(id);
}
city(id: CityID): City | undefined {
throw new NotYetImplemented();
return this.cityList.get(id);
}
squad(id: SquadID, nation: NationID): Squad | undefined {
throw new NotYetImplemented();
const squad = this.squadList.get(id);
if(!squad){
return undefined;
}
if(squad.raw.nationID !== nation){
return undefined;
}
return squad;
}
general(id: GeneralID): General | undefined {
throw new NotYetImplemented();
return this.generalList.get(id);
}
peekGeneralTurnTimeQueue(): General | undefined {
@@ -72,10 +84,6 @@ export class ResourceController implements IResourceController{
insertItemAtArrayLike(this._generalByTurnTime, general, (a, b)=>a.compareTurnTime(b), maybeTail);
}
gameEnv(): GameEnv {
throw new NotYetImplemented();
}
allNations(): Map<NationID, Nation> {
throw new NotYetImplemented();
}
@@ -159,6 +167,63 @@ export class ResourceController implements IResourceController{
}
resetAndFill(): Promise<void> {
this._generalByTurnTime.clear();
this.nationList.clear();
this.cityList.clear();
this.squadList.clear();
this.generalList.clear();
//MongoDB로부터 데이터를 가져와서 채워넣는다.
const rawGameEnv = {} as IGameEnvEntity;
//TODO: Implement!
const rawNationList = new Map<NationID, INationEntity>();
const rawCityList = new Map<CityID, ICityEntity>();
const rawSquadList = new Map<SquadID, ISquadEntity>();
const rawGeneralList = new Map<GeneralID, IGeneralEntity>();
const rawGeneralListBySquad = new Map<SquadID, GeneralID[]>();
for(const rawSquad of rawSquadList.values()){
rawGeneralListBySquad.set(rawSquad.id, []);
}
for(const rawGeneral of rawGeneralList.values()){
if(!rawGeneral.squadID){
continue;
}
const list = must(rawGeneralListBySquad.get(rawGeneral.squadID));
list.push(rawGeneral.id);
}
this._gameEnv = new GameEnv(rawGameEnv, this);
this._gameEnv.bootstrap();
for(const rawNation of rawNationList.values()){
const nation = new Nation(rawNation, this);
this.nationList.set(nation.id, nation);
}
for(const rawCity of rawCityList.values()){
const city = new City(rawCity, this);
this.cityList.set(city.id, city);
}
for(const rawSquad of rawSquadList.values()){
const squad = new Squad(rawSquad, this, must(rawGeneralListBySquad.get(rawSquad.id)));
this.squadList.set(squad.id, squad);
}
for(const rawGeneral of rawGeneralList.values()){
const general = new General(rawGeneral, this);
this.generalList.set(general.id, general);
}
//lazyInit()을 호출한다.
this.nationList.forEach(nation=>nation.bootstrap());
this.cityList.forEach(city=>city.bootstrap());
this.squadList.forEach(squad=>squad.bootstrap());
//iAction때문에 general의 lazyInit이 가장 마지막
this.generalList.forEach(general=>general.bootstrap());
throw new NotYetImplemented();
}