types
This commit is contained in:
parent
2113b5ae29
commit
454fc12d2e
@ -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 {
|
||||
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,
|
||||
)
|
||||
: (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
|
||||
},
|
||||
})
|
||||
|
@ -5,21 +5,24 @@ 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
|
||||
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
|
||||
? ThisifyServerEvents[K]
|
||||
? IServerEvents[K]
|
||||
: never
|
||||
|
||||
// CLIENT
|
||||
|
||||
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
|
||||
|
||||
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
|
||||
|
5
shared-types/types/types/index.d.ts
vendored
5
shared-types/types/types/index.d.ts
vendored
@ -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 {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user