From 5d6a7b9f651c621fa2e91e862b8287b59b270c10 Mon Sep 17 00:00:00 2001 From: itschip Date: Mon, 29 Jan 2024 18:51:50 +0100 Subject: [PATCH] feat(game/server): new router experiments --- apps/game/client/cl_utils.ts | 1 + apps/game/client/router-client.ts | 4 ++++ apps/game/package.json | 1 + apps/game/server/notes/notes.controller.ts | 10 ++++++++++ apps/game/server/router-server.ts | 13 +++++++++++++ config.default.json | 2 +- core/router/package.json | 4 +++- core/router/src/example.ts | 6 +++++- core/router/src/example_client.ts | 6 ++---- core/router/src/index.ts | 22 ++++++++++++++++++---- pnpm-lock.yaml | 3 +++ 11 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 apps/game/client/router-client.ts create mode 100644 apps/game/server/router-server.ts diff --git a/apps/game/client/cl_utils.ts b/apps/game/client/cl_utils.ts index 2adc6e5b0..37177708b 100644 --- a/apps/game/client/cl_utils.ts +++ b/apps/game/client/cl_utils.ts @@ -88,6 +88,7 @@ export const RegisterNuiProxy = (event: string) => { if (!global.isPlayerLoaded) await playerLoaded(); try { const res = await ClUtils.emitNetPromise(event, data); + cb(res); } catch (e) { console.error('Error encountered while listening to resp. Error:', e); diff --git a/apps/game/client/router-client.ts b/apps/game/client/router-client.ts new file mode 100644 index 000000000..324fecce1 --- /dev/null +++ b/apps/game/client/router-client.ts @@ -0,0 +1,4 @@ +import { createClient } from '@core/router'; +import { AppRouter } from '../server/router-server'; + +export const client = createClient(); diff --git a/apps/game/package.json b/apps/game/package.json index a8f2ef5bc..06f627a43 100644 --- a/apps/game/package.json +++ b/apps/game/package.json @@ -14,6 +14,7 @@ "lint": "eslint . --ext .ts" }, "dependencies": { + "@core/router": "workspace:^", "@discordjs/collection": "^0.2.4", "@npwd/config": "workspace:*", "@npwd/database": "workspace:*", diff --git a/apps/game/server/notes/notes.controller.ts b/apps/game/server/notes/notes.controller.ts index 3e979f92e..d843ae6ec 100644 --- a/apps/game/server/notes/notes.controller.ts +++ b/apps/game/server/notes/notes.controller.ts @@ -2,6 +2,16 @@ import { BeforeDBNote, DeleteNoteDTO, NoteItem, NotesEvents } from '@typings/not import NotesService from './notes.service'; import { notesLogger } from './notes.utils'; import { onNetPromise } from '../lib/PromiseNetEvents/onNetPromise'; +import { eventProcedure, router } from '../router-server'; + +export const notesRouter = router({ + addNote: eventProcedure(NotesEvents.ADD_NOTE, async (reqObj, resp) => { + NotesService.handleAddNote(reqObj, resp).catch((e) => { + notesLogger.error(`Error occured in add note event (${reqObj.source}), Error: ${e.message}`); + resp({ status: 'error', errorMsg: 'UNKNOWN_ERROR' }); + }); + }), +}); onNetPromise(NotesEvents.ADD_NOTE, (reqObj, resp) => { NotesService.handleAddNote(reqObj, resp).catch((e) => { diff --git a/apps/game/server/router-server.ts b/apps/game/server/router-server.ts new file mode 100644 index 000000000..afc23d99f --- /dev/null +++ b/apps/game/server/router-server.ts @@ -0,0 +1,13 @@ +import { initRouter } from '@core/router'; +import { notesRouter } from './notes/notes.controller'; + +const t = initRouter.create(); + +export const router = t.router; +export const eventProcedure = t.eventProcedure; + +const appRouter = router({ + notes: notesRouter, +}); + +export type AppRouter = typeof appRouter; diff --git a/config.default.json b/config.default.json index 00fc7d7e8..f4b84d030 100644 --- a/config.default.json +++ b/config.default.json @@ -97,7 +97,7 @@ }, "defaultContacts": [], "disabledApps": [], - "apps": [], + "apps": ["mockapp"], "voiceMessage": { "enabled": false, "authorizationHeader": "Authorization", diff --git a/core/router/package.json b/core/router/package.json index 0a1da1284..a445ee43f 100644 --- a/core/router/package.json +++ b/core/router/package.json @@ -2,7 +2,9 @@ "name": "@core/router", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "./dist/index.js", + "module": "./dist/index.mjs", + "types": "./dist/index.d.ts", "scripts": { "build": "tsup src/index.ts --format cjs,esm --dts --minify --clean --sourcemap" }, diff --git a/core/router/src/example.ts b/core/router/src/example.ts index 1d79b95d3..95d1f72cf 100644 --- a/core/router/src/example.ts +++ b/core/router/src/example.ts @@ -10,10 +10,14 @@ type PlayerData = { amount: number; }; -const appRouter = router({ +const playerRouter = router({ giveMoney: eventProcedure('giveMoney', async (data: PlayerData) => { console.log('Giving money to player: ', data.playerId, data.amount); }), }); +const appRouter = router({ + player: playerRouter, +}); + export type AppRouter = typeof appRouter; diff --git a/core/router/src/example_client.ts b/core/router/src/example_client.ts index 4aea6e7be..feebe0847 100644 --- a/core/router/src/example_client.ts +++ b/core/router/src/example_client.ts @@ -3,7 +3,5 @@ import { AppRouter } from './example'; const client = createClient(); -client.giveMoney.emitNet({ - amount: 100, - playerId: 1, -}); +client.player.giveMoney.emit({ playerId: 1, amount: 100 }); +client.player.giveMoney.emitNet({ playerId: 1, amount: 100 }); diff --git a/core/router/src/index.ts b/core/router/src/index.ts index af09e6a0d..af9ec5492 100644 --- a/core/router/src/index.ts +++ b/core/router/src/index.ts @@ -3,6 +3,8 @@ type FunctionWithEmit = { emit: T; emitNet: T; }; +type NestedRouter = Record>; +type RouterFunctions = Record | NestedRouter>; function createdFunctionWithEmit(fn: T): FunctionWithEmit { return { @@ -23,11 +25,23 @@ function eventProcedure(event: string, callback: T): F return createdFunctionWithEmit(callback); } -type RouterFunctions = Record>; - function createRouter() { return function (routes: T): T { - return routes; + // Implementation to handle both individual route functions and nested routers + const processedRoutes: Partial = {}; + + for (const key in routes) { + const route = routes[key]; + if (typeof route === 'function' || 'emit' in route || 'emitNet' in route) { + // If it's a function or a wrapped function, add it directly + processedRoutes[key] = route; + } else { + // If it's a nested router, merge its routes + Object.assign(processedRoutes, route); + } + } + + return processedRoutes as T; }; } @@ -43,7 +57,7 @@ class RouterBuilder { export const initRouter = new RouterBuilder(); export function createClient< - T extends Record Promise>>, + T extends Record Promise> | NestedRouter>, >(): T { return {} as T; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4ce6b1569..4d4f7b238 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -86,6 +86,9 @@ importers: apps/game: dependencies: + '@core/router': + specifier: workspace:^ + version: link:../../core/router '@discordjs/collection': specifier: ^0.2.4 version: 0.2.4