refac: GameEngine 함수 분리
This commit is contained in:
@@ -55,49 +55,10 @@ export class GameEngine {
|
|||||||
|
|
||||||
private continueRun = true;
|
private continueRun = true;
|
||||||
|
|
||||||
private async gameLoop(): Promise<void> {
|
private prevSaveWaiter: Promise<void> | null = null;
|
||||||
let generalWaitTimer: ReturnType<typeof setTimeout> | null = null;
|
|
||||||
let prevSaveWaiter: Promise<void> | null = null;
|
|
||||||
|
|
||||||
while (this.continueRun) {
|
|
||||||
//가장 빠른 장수
|
|
||||||
do {
|
|
||||||
const now = new Date();
|
|
||||||
const upcomingGeneral = this.rsc.peekGeneralTurntimeQueue();
|
|
||||||
if (!upcomingGeneral) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
const upcomingTurn = upcomingGeneral.raw.turntime;
|
|
||||||
|
|
||||||
const waitGeneralTurnMs = upcomingTurn.getTime() - now.getTime();
|
|
||||||
if (waitGeneralTurnMs > 0) {
|
|
||||||
generalWaitTimer = setTimeout(() => {
|
|
||||||
this.signal.notify();
|
|
||||||
generalWaitTimer = null;
|
|
||||||
}, waitGeneralTurnMs);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.signal.notify();
|
|
||||||
}
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
for(const queue of Object.values(this.actionQueue)){
|
|
||||||
if(!queue.isEmpty()){
|
|
||||||
this.signal.notify();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//이벤트 대기
|
|
||||||
await this.signal.wait();
|
|
||||||
|
|
||||||
if (generalWaitTimer) {
|
|
||||||
clearTimeout(generalWaitTimer);
|
|
||||||
}
|
|
||||||
if (!this.continueRun) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private async processGeneralTurn() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
let processedGeneralCount = 0;
|
let processedGeneralCount = 0;
|
||||||
@@ -118,23 +79,25 @@ export class GameEngine {
|
|||||||
|
|
||||||
upcomingGeneral.updateTurntime();
|
upcomingGeneral.updateTurntime();
|
||||||
this.rsc.insertGeneralTurntimeQueue(upcomingGeneral, true);
|
this.rsc.insertGeneralTurntimeQueue(upcomingGeneral, true);
|
||||||
this.rsc.gameEnv().update((env)=>{
|
this.rsc.gameEnv().update((env) => {
|
||||||
env.lastExecuted = generalTurntime;
|
env.lastExecuted = generalTurntime;
|
||||||
})
|
})
|
||||||
processedGeneralCount++;
|
processedGeneralCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (processedGeneralCount > 0) {
|
if (processedGeneralCount > 0) {
|
||||||
await prevSaveWaiter;
|
await this.prevSaveWaiter;
|
||||||
const lastExecuted = this.rsc.gameEnv().raw.lastExecuted;
|
const lastExecuted = this.rsc.gameEnv().raw.lastExecuted;
|
||||||
prevSaveWaiter = this.rsc.saveAll().then(()=>{
|
this.prevSaveWaiter = this.rsc.saveAll().then(() => {
|
||||||
this.postStatus({
|
this.postStatus({
|
||||||
type: 'update',
|
type: 'update',
|
||||||
lastExecuted: lastExecuted.toISOString()
|
lastExecuted: lastExecuted.toISOString()
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async processActions() {
|
||||||
//서버 이벤트 처리
|
//서버 이벤트 처리
|
||||||
for (const [invoker, queue] of entriesWithType(this.actionQueue)) {
|
for (const [invoker, queue] of entriesWithType(this.actionQueue)) {
|
||||||
let processEventCount = 0;
|
let processEventCount = 0;
|
||||||
@@ -168,8 +131,8 @@ export class GameEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (processEventCount > 0) {
|
if (processEventCount > 0) {
|
||||||
await prevSaveWaiter;
|
await this.prevSaveWaiter;
|
||||||
prevSaveWaiter = null;
|
this.prevSaveWaiter = null;
|
||||||
await this.rsc.saveAll();
|
await this.rsc.saveAll();
|
||||||
for (const response of responseQueue) {
|
for (const response of responseQueue) {
|
||||||
response();
|
response();
|
||||||
@@ -177,12 +140,60 @@ export class GameEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async gameLoop(): Promise<void> {
|
||||||
|
let generalWaitTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
|
while (this.continueRun) {
|
||||||
|
//가장 빠른 장수
|
||||||
|
do {
|
||||||
|
const now = new Date();
|
||||||
|
const upcomingGeneral = this.rsc.peekGeneralTurntimeQueue();
|
||||||
|
if (!upcomingGeneral) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const upcomingTurn = upcomingGeneral.raw.turntime;
|
||||||
|
|
||||||
|
const waitGeneralTurnMs = upcomingTurn.getTime() - now.getTime();
|
||||||
|
if (waitGeneralTurnMs > 0) {
|
||||||
|
generalWaitTimer = setTimeout(() => {
|
||||||
|
this.signal.notify();
|
||||||
|
generalWaitTimer = null;
|
||||||
|
}, waitGeneralTurnMs);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.signal.notify();
|
||||||
|
}
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
for (const queue of Object.values(this.actionQueue)) {
|
||||||
|
if (!queue.isEmpty()) {
|
||||||
|
this.signal.notify();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//이벤트 대기
|
||||||
|
await this.signal.wait();
|
||||||
|
|
||||||
|
if (generalWaitTimer) {
|
||||||
|
clearTimeout(generalWaitTimer);
|
||||||
|
}
|
||||||
|
if (!this.continueRun) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.processGeneralTurn();
|
||||||
|
|
||||||
|
await this.processActions();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private gameLoopPromise: Promise<void> | null = null;
|
private gameLoopPromise: Promise<void> | null = null;
|
||||||
|
|
||||||
public static async initInstance(updateHandler: (msg: GameEngineMsg)=>void): Promise<GameEngine> {
|
public static async initInstance(updateHandler: (msg: GameEngineMsg) => void): Promise<GameEngine> {
|
||||||
if (GameEngine.instance) {
|
if (GameEngine.instance) {
|
||||||
throw new Error('GameEngine is already initialized');
|
throw new Error('GameEngine is already initialized');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user