rage-framework/server/src/index.ts

99 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-06-08 22:35:14 +00:00
import rpc from 'rage-rpc'
2024-06-11 15:02:53 +00:00
2024-06-11 17:27:54 +00:00
import Logger from './logger'
2024-06-11 15:02:53 +00:00
import type {
2024-06-10 03:15:09 +00:00
RageFW_ClientEvent,
RageFW_ClientEventArguments,
RageFW_ClientEventReturn,
2024-06-11 15:02:53 +00:00
RageFW_ICustomServerEvent,
2024-06-10 03:15:09 +00:00
RageFW_ServerEvent,
RageFW_ServerEventCallback,
2024-06-11 13:45:47 +00:00
RageFW_ServerEventCallbackCustom,
RageFW_ServerEventCallbackNative,
2024-06-11 15:02:53 +00:00
} from './types'
2024-06-10 03:15:09 +00:00
import { nativeEvents } from './native.events'
2024-06-08 22:35:14 +00:00
2024-06-10 00:53:42 +00:00
class Server {
2024-06-11 00:38:31 +00:00
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
return nativeEvents.includes(eventName)
}
2024-06-11 13:45:47 +00:00
private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
callback: RageFW_ServerEventCallbackCustom<EventName>,
): void {
rpc.register(
eventName,
async (
args: Parameters<RageFW_ICustomServerEvent[EventName]>,
info,
) => {
2024-06-11 14:14:20 +00:00
callback(info.player as PlayerMp, args)
2024-06-11 13:45:47 +00:00
},
)
}
private registerNative<EventName extends keyof IServerEvents>(
eventName: EventName,
callback: RageFW_ServerEventCallbackNative<EventName>,
): void {
mp.events.add(eventName, callback)
2024-06-11 00:38:31 +00:00
}
public register<EventName extends RageFW_ServerEvent>(
eventName: EventName,
2024-06-09 00:06:20 +00:00
callback: RageFW_ServerEventCallback<EventName>,
): void {
2024-06-11 00:38:31 +00:00
if (this.isNativeEvent(eventName)) {
2024-06-11 13:45:47 +00:00
this.registerNative(
eventName,
callback as RageFW_ServerEventCallbackNative,
)
2024-06-10 03:15:09 +00:00
} else {
2024-06-11 13:45:47 +00:00
this.registerCustom(
eventName,
2024-06-11 16:12:03 +00:00
callback as unknown as RageFW_ServerEventCallbackCustom,
2024-06-11 13:45:47 +00:00
)
2024-06-10 03:15:09 +00:00
}
2024-06-09 00:06:20 +00:00
}
2024-06-09 16:19:06 +00:00
public registerMany<EventName extends RageFW_ServerEvent>(events: {
2024-06-09 21:53:59 +00:00
[event in EventName]: RageFW_ServerEventCallback<event>
2024-06-09 16:19:06 +00:00
}): void {
2024-06-11 01:57:19 +00:00
Object.entries<RageFW_ServerEventCallback<EventName>>(events).map(
([eventName, callback]) => {
if (this.isNativeEvent(eventName)) {
2024-06-11 13:45:47 +00:00
this.registerNative(
eventName,
callback as RageFW_ServerEventCallbackNative,
)
2024-06-11 01:57:19 +00:00
} else {
2024-06-11 13:45:47 +00:00
this.registerCustom(
eventName as keyof RageFW_ICustomServerEvent,
2024-06-11 16:12:03 +00:00
callback as unknown as RageFW_ServerEventCallbackCustom,
2024-06-11 13:45:47 +00:00
)
2024-06-11 01:57:19 +00:00
}
},
)
2024-06-09 16:19:06 +00:00
}
2024-06-08 22:35:14 +00:00
}
2024-06-10 03:15:09 +00:00
class Player {
public triggerClient<EventName extends RageFW_ClientEvent>(
player: PlayerMp,
eventName: EventName,
2024-06-11 14:14:20 +00:00
args: RageFW_ClientEventArguments<EventName>,
2024-06-10 03:15:09 +00:00
): Promise<RageFW_ClientEventReturn<EventName>> {
return rpc.callClient(player, eventName, args)
}
}
export const fw = {
2024-06-08 22:35:14 +00:00
event: new Server(),
2024-06-10 03:15:09 +00:00
player: new Player(),
2024-06-11 17:27:54 +00:00
system: {
log: new Logger(),
},
2024-06-08 22:35:14 +00:00
}