import rpc from 'rage-rpc' import Logger from './logger' import { RageFW_CefArgs, RageFW_CefEvent, RageFW_CefReturn, RageFW_ClientEvent, RageFW_ClientEventArguments, RageFW_ClientEventReturn, RageFW_ICustomServerEvent, RageFW_ServerEvent, RageFW_ServerEventArguments, RageFW_ServerEventCallback, RageFW_ServerEventCallbackCustom, RageFW_ServerEventCallbackNative, RageFW_ServerEventReturn, } from './types' import { nativeEvents } from './native.events' class Server { private isNativeEvent(eventName: string): eventName is keyof IServerEvents { return nativeEvents.includes(eventName) } private registerCustom( eventName: EventName, callback: RageFW_ServerEventCallbackCustom, ): void { rpc.register( eventName, async (args: RageFW_ServerEventArguments, info) => { callback(info.player as PlayerMp, args) }, ) } private registerNative( eventName: EventName, callback: RageFW_ServerEventCallbackNative, ): void { mp.events.add(eventName, callback) } public register( eventName: EventName, callback: RageFW_ServerEventCallback, ): void { if (this.isNativeEvent(eventName)) { this.registerNative( eventName, callback as RageFW_ServerEventCallbackNative, ) } else { this.registerCustom( eventName, callback as unknown as RageFW_ServerEventCallbackCustom, ) } } public registerMany(events: { [event in EventName]: RageFW_ServerEventCallback }): void { Object.entries>(events).map( ([eventName, callback]) => { if (this.isNativeEvent(eventName)) { this.registerNative( eventName, callback as RageFW_ServerEventCallbackNative, ) } else { this.registerCustom( eventName as keyof RageFW_ICustomServerEvent, callback as unknown as RageFW_ServerEventCallbackCustom, ) } }, ) } private unregisterCustom( eventName: EventName, ): void { rpc.unregister(eventName) } private unregisterNative( eventName: EventName, ): void { mp.events.remove(eventName) } public unregister( eventName: EventName, ): void { if (this.isNativeEvent(eventName)) { this.unregisterNative(eventName) } else { this.unregisterCustom(eventName) } } private trigger( eventName: EventName, args: RageFW_ServerEventArguments, ): Promise> { return rpc.call>(eventName, args) } } class Player { public triggerClient( player: PlayerMp, eventName: EventName, args: RageFW_ClientEventArguments, ): Promise> { return rpc.callClient(player, eventName, args) } public triggerBrowser( player: PlayerMp, eventName: EventName, args: RageFW_CefArgs, ): Promise> { return rpc.callBrowsers(player, eventName, args) } } export const fw = { event: new Server(), player: new Player(), system: { log: new Logger(), }, } fw.system.log.info( 'Working on Rage Framework. RageFW © Powered by Entity Seven Group', )