Merge remote-tracking branch 'origin/master'
# Conflicts: # server/src/index.ts # server/src/types.ts
This commit is contained in:
commit
fa4c218a7a
@ -7,17 +7,19 @@ 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 {
|
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
|
||||||
return nativeEvents.includes(eventName)
|
return nativeEvents.includes(eventName)
|
||||||
}
|
}
|
||||||
|
|
||||||
private isCustomEvent(
|
public testRegister(
|
||||||
eventName: string,
|
event: string,
|
||||||
): eventName is keyof RageFW_ICustomServerEvent {
|
callback: (player: PlayerMp, ...args: unknown[]) => void,
|
||||||
return !nativeEvents.includes(eventName)
|
) {
|
||||||
|
rpc.register(event, async (data, info) => {
|
||||||
|
callback(info.player as PlayerMp, ...data)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public register<EventName extends RageFW_ServerEvent>(
|
public register<EventName extends RageFW_ServerEvent>(
|
||||||
@ -26,29 +28,30 @@ class Server {
|
|||||||
): void {
|
): void {
|
||||||
if (this.isNativeEvent(eventName)) {
|
if (this.isNativeEvent(eventName)) {
|
||||||
mp.events.add(eventName, callback)
|
mp.events.add(eventName, callback)
|
||||||
} else if (this.isCustomEvent(eventName)) {
|
} else {
|
||||||
rpc.register(eventName, callback as rpc.ProcedureListener)
|
rpc.register(eventName, async (data: any[], info) => {
|
||||||
|
return callback(info.player as PlayerMp, data)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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<RageFW_ServerEventCallback<EventName>>(events).map(
|
Object.entries(events).map(([eventName, callback]) => {
|
||||||
([eventName, callback]) => {
|
if (nativeEvents.includes(eventName)) {
|
||||||
if (this.isNativeEvent(eventName)) {
|
mp.events.add(
|
||||||
mp.events.add(eventName, callback)
|
eventName as keyof IServerEvents,
|
||||||
} else if (this.isCustomEvent(eventName)) {
|
callback as (...arg: unknown[]) => void,
|
||||||
|
)
|
||||||
|
} 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)(
|
? (callback as (...arg: typeof args) => void)(...args)
|
||||||
...args,
|
|
||||||
)
|
|
||||||
: (callback as (arg: typeof args) => void)(args)
|
: (callback as (arg: typeof args) => void)(args)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,9 +70,8 @@ export const fw = {
|
|||||||
player: new Player(),
|
player: new Player(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fw.event.register('ggfdgfd', player => {})
|
||||||
|
|
||||||
fw.event.registerMany({
|
fw.event.registerMany({
|
||||||
playerDeath: (player, arg, killer) => undefined,
|
trailerAttached: player => {},
|
||||||
customServerEvent(player, rofls) {
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
@ -5,8 +5,6 @@ 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
|
||||||
@ -18,11 +16,9 @@ export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
|
|||||||
...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
|
||||||
? IServerEvents[K]
|
? ThisifyServerEvents[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> =
|
||||||
|
Loading…
Reference in New Issue
Block a user