feat: GameEngineController

This commit is contained in:
2024-03-09 16:15:43 +00:00
parent 554588fb42
commit ddfff44c12
4 changed files with 135 additions and 4 deletions
+14 -4
View File
@@ -4,6 +4,7 @@ import { ResourceController } from "./ResourceController.js";
import { entriesWithType } from "@sammo/util/converter";
import db from "./connectDB.js";
import type { Mongoose } from "mongoose";
import type { GameEngineMsg } from "./GameEngineDefs.js";
export interface ActionRequest {
}
@@ -45,7 +46,10 @@ export class GameEngine {
private rsc: ResourceController
private constructor(db: Mongoose) {
private constructor(
db: Mongoose,
private postStatus: (msg: GameEngineMsg) => void,
) {
this.rsc = new ResourceController(db);
}
@@ -122,7 +126,13 @@ export class GameEngine {
if (processedGeneralCount > 0) {
await prevSaveWaiter;
prevSaveWaiter = this.rsc.saveAll();
const lastExecuted = this.rsc.gameEnv().raw.lastExecuted;
prevSaveWaiter = this.rsc.saveAll().then(()=>{
this.postStatus({
type: 'update',
lastExecuted: lastExecuted.toISOString()
});
});
}
//서버 이벤트 처리
@@ -172,12 +182,12 @@ export class GameEngine {
private gameLoopPromise: Promise<void> | null = null;
public static async initInstance(): Promise<GameEngine> {
public static async initInstance(updateHandler: (msg: GameEngineMsg)=>void): Promise<GameEngine> {
if (GameEngine.instance) {
throw new Error('GameEngine is already initialized');
}
GameEngine.instance = new GameEngine(await db);
GameEngine.instance = new GameEngine(await db, updateHandler);
return GameEngine.instance;
}