types
This commit is contained in:
parent
2113b5ae29
commit
454fc12d2e
@ -7,15 +7,26 @@ import {
|
|||||||
RageFW_ServerEventCallback,
|
RageFW_ServerEventCallback,
|
||||||
} from './types.js'
|
} from './types.js'
|
||||||
import { nativeEvents } from './native.events'
|
import { nativeEvents } from './native.events'
|
||||||
|
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
|
||||||
|
|
||||||
class Server {
|
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>(
|
public register<EventName extends RageFW_ServerEvent>(
|
||||||
eventName: EventName,
|
eventName: EventName,
|
||||||
callback: RageFW_ServerEventCallback<EventName>,
|
callback: RageFW_ServerEventCallback<EventName>,
|
||||||
): void {
|
): void {
|
||||||
if (nativeEvents.includes(eventName)) {
|
if (this.isNativeEvent(eventName)) {
|
||||||
mp.events.add(eventName, callback)
|
mp.events.add(eventName, callback)
|
||||||
} else {
|
} else if (this.isCustomEvent(eventName)) {
|
||||||
rpc.register(eventName, callback as rpc.ProcedureListener)
|
rpc.register(eventName, callback as rpc.ProcedureListener)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -23,20 +34,21 @@ class Server {
|
|||||||
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
||||||
[event in EventName]: RageFW_ServerEventCallback<event>
|
[event in EventName]: RageFW_ServerEventCallback<event>
|
||||||
}): void {
|
}): void {
|
||||||
Object.entries(events).map(([eventName, callback]) => {
|
Object.entries<RageFW_ServerEventCallback<EventName>>(events).map(
|
||||||
if (nativeEvents.includes(eventName)) {
|
([eventName, callback]) => {
|
||||||
mp.events.add(
|
if (this.isNativeEvent(eventName)) {
|
||||||
eventName as keyof IServerEvents,
|
mp.events.add(eventName, callback)
|
||||||
callback as (...arg: unknown[]) => void,
|
} else if (this.isCustomEvent(eventName)) {
|
||||||
)
|
|
||||||
} else {
|
|
||||||
rpc.register(eventName, (args: unknown[]) => {
|
rpc.register(eventName, (args: unknown[]) => {
|
||||||
return Array.isArray(args)
|
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)
|
: (callback as (arg: typeof args) => void)(args)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,3 +66,10 @@ export const fw = {
|
|||||||
event: new Server(),
|
event: new Server(),
|
||||||
player: new Player(),
|
player: new Player(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fw.event.registerMany({
|
||||||
|
playerDeath: (player, arg, killer) => undefined,
|
||||||
|
customServerEvent(player, rofls) {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
})
|
||||||
|
@ -5,21 +5,24 @@ import type {
|
|||||||
RageFW_ICustomClientEvent,
|
RageFW_ICustomClientEvent,
|
||||||
} from 'rage-fw-shared-types'
|
} from 'rage-fw-shared-types'
|
||||||
|
|
||||||
|
// SERVER
|
||||||
|
|
||||||
export type RageFW_ServerEvent =
|
export type RageFW_ServerEvent =
|
||||||
| keyof RageFW_ICustomServerEvent
|
| keyof RageFW_ICustomServerEvent
|
||||||
| keyof IServerEvents
|
| keyof IServerEvents
|
||||||
|
|
||||||
export type RageFW_ServerEventCallback<
|
export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
|
||||||
K extends keyof RageFW_ICustomServerEvent | keyof IServerEvents,
|
K extends keyof RageFW_ICustomServerEvent
|
||||||
> = K extends keyof RageFW_ICustomServerEvent
|
|
||||||
? (
|
? (
|
||||||
player: PlayerMp,
|
player: PlayerMp,
|
||||||
...args: Parameters<RageFW_ICustomServerEvent[K]>
|
...args: Parameters<RageFW_ICustomServerEvent[K]>
|
||||||
) => ReturnType<RageFW_ICustomServerEvent[K]>
|
) => ReturnType<RageFW_ICustomServerEvent[K]>
|
||||||
: K extends keyof IServerEvents
|
: K extends keyof IServerEvents
|
||||||
? ThisifyServerEvents[K]
|
? IServerEvents[K]
|
||||||
: never
|
: never
|
||||||
|
|
||||||
|
// CLIENT
|
||||||
|
|
||||||
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
|
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
|
||||||
|
|
||||||
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
|
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' {
|
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 {}
|
export interface RageFW_ICustomClientEvent {}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user