rage-framework/server/src/index.ts

151 lines
4.5 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 18:52:00 +00:00
import {
2024-06-13 01:46:29 +00:00
RageFW_CefArgs,
RageFW_CefEvent,
RageFW_CefReturn,
_CefEventHasArgs,
2024-06-13 01:18:33 +00:00
_ClientEventHasArgs,
_ServerEventHasArgs,
2024-06-10 03:15:09 +00:00
RageFW_ClientEvent,
2024-06-13 01:18:33 +00:00
RageFW_ServerClientEventArguments,
RageFW_ServerClientEventReturn,
2024-06-11 15:02:53 +00:00
RageFW_ICustomServerEvent,
2024-06-10 03:15:09 +00:00
RageFW_ServerEvent,
2024-06-11 18:52:00 +00:00
RageFW_ServerEventArguments,
2024-06-10 03:15:09 +00:00
RageFW_ServerEventCallback,
2024-06-11 13:45:47 +00:00
RageFW_ServerEventCallbackCustom,
RageFW_ServerEventCallbackNative,
2024-06-11 18:52:00 +00:00
RageFW_ServerEventReturn,
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,
2024-06-11 18:52:00 +00:00
async (args: RageFW_ServerEventArguments<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-11 18:52:00 +00:00
private unregisterCustom<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
): void {
rpc.unregister(eventName)
}
private unregisterNative<EventName extends keyof IServerEvents>(
eventName: EventName,
): void {
mp.events.remove(eventName)
}
public unregister<EventName extends RageFW_ServerEvent>(
eventName: EventName,
): void {
if (this.isNativeEvent(eventName)) {
this.unregisterNative(eventName)
} else {
this.unregisterCustom(eventName)
}
}
2024-06-12 22:31:07 +00:00
public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
2024-06-11 18:52:00 +00:00
eventName: EventName,
2024-06-13 01:18:33 +00:00
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerEventArguments<EventName>]
: []
2024-06-11 18:52:00 +00:00
): Promise<RageFW_ServerEventReturn<EventName>> {
return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args)
}
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-13 01:18:33 +00:00
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ServerClientEventArguments<EventName>]
: []
): Promise<RageFW_ServerClientEventReturn<EventName>> {
2024-06-10 03:15:09 +00:00
return rpc.callClient(player, eventName, args)
}
2024-06-13 01:46:29 +00:00
public triggerBrowser<EventName extends RageFW_CefEvent>(
player: PlayerMp,
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
: []
2024-06-13 01:46:29 +00:00
): Promise<RageFW_CefReturn<EventName>> {
return rpc.callBrowsers(player, eventName, args)
}
2024-06-10 03:15:09 +00:00
}
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
}
2024-06-13 01:46:29 +00:00
fw.system.log.info(
'Working on Rage Framework. RageFW © Powered by Entity Seven Group',
)