feat: Action 호출 타입 설계
- 호출 시점에선 강타입으로.
This commit is contained in:
@@ -4,9 +4,11 @@ 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";
|
||||
import type { ActionPack, GameEngineMsg } from "./GameEngineDefs.js";
|
||||
import { ActionRequestList, type QueueType } from "@sammo/game_logic";
|
||||
export interface ActionRequest {
|
||||
|
||||
name: keyof ActionPack;
|
||||
args: Parameters<ActionPack[keyof ActionPack]>[1];
|
||||
}
|
||||
|
||||
export interface ActionRequestContainer {
|
||||
@@ -16,7 +18,7 @@ export interface ActionRequestContainer {
|
||||
|
||||
export type ActionSuccess = {
|
||||
success: true;
|
||||
message?: string;
|
||||
value: ReturnType<ActionPack[keyof ActionPack]>;
|
||||
}
|
||||
|
||||
export type ActionFailure = {
|
||||
@@ -25,8 +27,6 @@ export type ActionFailure = {
|
||||
}
|
||||
export type ActionResult = ActionSuccess | ActionFailure;
|
||||
|
||||
type QueueType = 'server' | 'api';
|
||||
//type QueueType = 'server' | 'npc' | 'api';
|
||||
|
||||
export class GameEngine {
|
||||
|
||||
@@ -107,8 +107,7 @@ export class GameEngine {
|
||||
while (!queue.isEmpty()) {
|
||||
const action = must(queue.shift());
|
||||
|
||||
//TODO: action 처리
|
||||
const errorReason: string | undefined = undefined;
|
||||
const actionResult = this.processAction(invoker, action.action);
|
||||
|
||||
const [ticketResolve, ticketRejected] = must_err(
|
||||
this.queueWaiters.get(action.waiterSeq),
|
||||
@@ -116,14 +115,14 @@ export class GameEngine {
|
||||
);
|
||||
|
||||
this.queueWaiters.delete(action.waiterSeq);
|
||||
if (errorReason) {
|
||||
if(actionResult.success){
|
||||
responseQueue.push(() => {
|
||||
ticketRejected(errorReason);
|
||||
ticketResolve(actionResult);
|
||||
});
|
||||
}
|
||||
else {
|
||||
else{
|
||||
responseQueue.push(() => {
|
||||
ticketResolve({ success: true });
|
||||
ticketRejected(actionResult);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -236,6 +235,26 @@ export class GameEngine {
|
||||
console.log('GameEngine stopped');
|
||||
}
|
||||
|
||||
private processAction(invoker: QueueType, action: ActionRequest): ActionResult {
|
||||
const actionName = action.name;
|
||||
const actionArgs = action.args;
|
||||
|
||||
try{
|
||||
const result = ActionRequestList[actionName](invoker, actionArgs as any, this.rsc);
|
||||
return {
|
||||
success: true,
|
||||
value: result
|
||||
};
|
||||
}
|
||||
catch(e){
|
||||
const msg = e instanceof Error ? e.message : e;
|
||||
return {
|
||||
success: false,
|
||||
reason: String(msg),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async pushAction(queueName: QueueType, action: ActionRequest): Promise<ActionResult> {
|
||||
const queue = this.actionQueue[queueName];
|
||||
|
||||
@@ -5,6 +5,7 @@ import * as nodePath from "node:path";
|
||||
import { delay, must } from "@sammo/util";
|
||||
import type { GameEngineMsg, GameEngineRPCDefs } from "./GameEngineDefs.js";
|
||||
import { RPCClient } from "@sammo/server_util";
|
||||
import type { ActionMainArg, ActionRes, ActionRequestKey, ActionRequestList } from "@sammo/game_logic";
|
||||
|
||||
|
||||
const __filename = fileURLToPath(new URL(import.meta.url));
|
||||
@@ -90,11 +91,23 @@ class GameEngineController {
|
||||
await this.start();
|
||||
}
|
||||
|
||||
async call<T extends keyof GameEngineRPCDefs & string>(name: T, arg: Parameters<GameEngineRPCDefs[T]>[0]): Promise<ReturnType<GameEngineRPCDefs[T]>> {
|
||||
return must(this.rpcClient).callFunction(name, arg);
|
||||
async pushAPIAction<T extends ActionRequestKey>(action: T, arg: ActionMainArg<T>): Promise<ActionRes<T>> {
|
||||
const result = await must(this.rpcClient).callFunction('pushAPIAction', { name: action, args: arg });
|
||||
if (!result.success) {
|
||||
throw new Error(result.reason);
|
||||
}
|
||||
return result.value as ReturnType<typeof ActionRequestList[T]>;
|
||||
}
|
||||
|
||||
async pushServerAction<T extends ActionRequestKey>(action: T, arg: ActionMainArg<T>): Promise<ActionRes<T>> {
|
||||
const result = await must(this.rpcClient).callFunction('pushServerAction', { name: action, args: arg });
|
||||
if (!result.success) {
|
||||
throw new Error(result.reason);
|
||||
}
|
||||
return result.value as ReturnType<typeof ActionRequestList[T]>;
|
||||
}
|
||||
}
|
||||
|
||||
export function getGameEngineController(): GameEngineController {
|
||||
return GameEngineController.instance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import type { RPCLists } from "@sammo/server_util";
|
||||
import type { ActionRequest, ActionResult } from "./GameEngine.js";
|
||||
import type { MessagePort } from "node:worker_threads";
|
||||
import type { CityID, GeneralID, NationID, TroopID } from "@sammo/game_logic/src/defs.js";
|
||||
import type { ActionPackDef } from "@sammo/game_logic";
|
||||
import type { ActionRequestList, QueueType } from "@sammo/game_logic/src/ActionRequest/index.js";
|
||||
//Remove Third argument. Use Infer
|
||||
export type MapRscActionRequest<T extends ActionPackDef> = {
|
||||
[P in keyof T]: (invoker: Parameters<T[P]>[1] & QueueType, arg: Parameters<T[P]>[1]) => ReturnType<T[P]>
|
||||
}
|
||||
|
||||
export type ActionPack = MapRscActionRequest<typeof ActionRequestList>;
|
||||
|
||||
export type GameEngineRPCDefs = {
|
||||
stop: ()=>Promise<void>,
|
||||
pushAPIAction: (action: ActionRequest)=>Promise<ActionResult>,
|
||||
pushServerAction: (action: ActionRequest)=>Promise<ActionResult>,
|
||||
stop: () => Promise<void>,
|
||||
pushAPIAction: (action: ActionRequest) => Promise<ActionResult>,
|
||||
pushServerAction: (action: ActionRequest) => Promise<ActionResult>,
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user