This commit is contained in:
Danya H 2024-06-11 02:49:56 +01:00
parent 2113b5ae29
commit 454fc12d2e
3 changed files with 52 additions and 27 deletions

View File

@ -7,15 +7,26 @@ import {
RageFW_ServerEventCallback,
} from './types.js'
import { nativeEvents } from './native.events'
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
class Server {
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
return nativeEvents.includes(eventName)
}
private isCustomEvent(
eventName: string,
): eventName is keyof RageFW_ICustomServerEvent {
return !nativeEvents.includes(eventName)
}
public register<EventName extends RageFW_ServerEvent>(
eventName: EventName,
callback: RageFW_ServerEventCallback<EventName>,
): void {
if (nativeEvents.includes(eventName)) {
if (this.isNativeEvent(eventName)) {
mp.events.add(eventName, callback)
} else {
} else if (this.isCustomEvent(eventName)) {
rpc.register(eventName, callback as rpc.ProcedureListener)
}
}
@ -23,20 +34,21 @@ class Server {
public registerMany<EventName extends RageFW_ServerEvent>(events: {
[event in EventName]: RageFW_ServerEventCallback<event>
}): void {
Object.entries(events).map(([eventName, callback]) => {
if (nativeEvents.includes(eventName)) {
mp.events.add(
eventName as keyof IServerEvents,
callback as (...arg: unknown[]) => void,
)
} else {
rpc.register(eventName, (args: unknown[]) => {
return Array.isArray(args)
? (callback as (...arg: typeof args) => void)(...args)
: (callback as (arg: typeof args) => void)(args)
})
}
})
Object.entries<RageFW_ServerEventCallback<EventName>>(events).map(
([eventName, callback]) => {
if (this.isNativeEvent(eventName)) {
mp.events.add(eventName, callback)
} else if (this.isCustomEvent(eventName)) {
rpc.register(eventName, (args: unknown[]) => {
return Array.isArray(args)
? (callback as (...arg: typeof args) => void)(
...args,
)
: (callback as (arg: typeof args) => void)(args)
})
}
},
)
}
}
@ -54,3 +66,10 @@ export const fw = {
event: new Server(),
player: new Player(),
}
fw.event.registerMany({
playerDeath: (player, arg, killer) => undefined,
customServerEvent(player, rofls) {
return true
},
})

View File

@ -5,20 +5,23 @@ import type {
RageFW_ICustomClientEvent,
} from 'rage-fw-shared-types'
// SERVER
export type RageFW_ServerEvent =
| keyof RageFW_ICustomServerEvent
| keyof IServerEvents
export type RageFW_ServerEventCallback<
K extends keyof RageFW_ICustomServerEvent | keyof IServerEvents,
> = K extends keyof RageFW_ICustomServerEvent
? (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents
? ThisifyServerEvents[K]
: never
export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent
? (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents
? IServerEvents[K]
: never
// CLIENT
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent

View File

@ -1,5 +1,8 @@
declare module 'rage-fw-shared-types' {
export interface RageFW_ICustomServerEvent {}
export interface RageFW_ICustomServerEvent {
customServerEvent(customArgs: string[]): boolean
customServerEvent2(customArgs2: number[]): void
}
export interface RageFW_ICustomClientEvent {}
}