import rpc from 'rage-rpc' import type { RageFW_ClientEvent, RageFW_ClientEventArguments, RageFW_ClientEventReturn, RageFW_ICustomServerEvent, RageFW_ServerEvent, RageFW_ServerEventCallback, RageFW_ServerEventCallbackCustom, RageFW_ServerEventCallbackNative, } 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: Parameters, 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 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 RageFW_ServerEventCallbackCustom, ) } }, ) } } class Player { public triggerClient( player: PlayerMp, eventName: EventName, args: RageFW_ClientEventArguments, ): Promise> { return rpc.callClient(player, eventName, args) } } export const fw = { event: new Server(), player: new Player(), }