event register types + triggerClient
This commit is contained in:
parent
d67073d739
commit
dc9e713e6b
@ -1,36 +1,93 @@
|
|||||||
/// <reference types="@ragempcommunity/types-server" />
|
/// <reference types="@ragempcommunity/types-server" />
|
||||||
import rpc from 'rage-rpc'
|
import rpc from 'rage-rpc'
|
||||||
|
|
||||||
type ServerEvent<K> = K extends keyof IServerEvents ? K : never
|
type RageFW_ServerEvent<K> = K extends keyof RageFW_ICustomServerEvent
|
||||||
type ServerEventCallback<K> = K extends keyof IServerEvents
|
? K
|
||||||
? ThisifyServerEvents[K]
|
: K extends keyof IServerEvents
|
||||||
|
? K
|
||||||
|
: never
|
||||||
|
|
||||||
|
type RageFW_ServerEventCallback<K> = K extends keyof RageFW_ICustomServerEvent
|
||||||
|
? (
|
||||||
|
player: PlayerMp,
|
||||||
|
...args: Parameters<RageFW_ICustomServerEvent[K]>
|
||||||
|
) => ReturnType<RageFW_ICustomServerEvent[K]>
|
||||||
|
: K extends keyof IServerEvents
|
||||||
|
? ThisifyServerEvents[K]
|
||||||
|
: never
|
||||||
|
|
||||||
|
type RageFW_ClientEvent<K> = K extends keyof RageFW_ICustomClientEvent
|
||||||
|
? K
|
||||||
|
: K extends keyof IServerEvents
|
||||||
|
? K
|
||||||
|
: never
|
||||||
|
|
||||||
|
type RageFW_ClientEventArguments<K> = K extends keyof RageFW_ICustomClientEvent
|
||||||
|
? Parameters<RageFW_ICustomClientEvent[K]>
|
||||||
|
: (K extends keyof IServerEvents ? ThisifyServerEvents[K] : never)[]
|
||||||
|
|
||||||
|
type RageFW_ClientEventReturn<K> = K extends keyof RageFW_ICustomClientEvent
|
||||||
|
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||||
: never
|
: never
|
||||||
|
|
||||||
declare class Server {
|
interface RageFW_ICustomServerEvent {
|
||||||
public register<K = ServerEvent<K>>(
|
customEvent(arg1: number, arg2: boolean): void
|
||||||
eventName: K,
|
}
|
||||||
callback: ServerEventCallback,
|
|
||||||
|
interface RageFW_ICustomClientEvent {
|
||||||
|
customClientEvent(arg1: number, arg2: boolean): boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
class Server {
|
||||||
|
register<EventName extends string>(
|
||||||
|
eventName: RageFW_ServerEvent<EventName>,
|
||||||
|
callback: RageFW_ServerEventCallback<EventName>,
|
||||||
): void
|
): void
|
||||||
|
|
||||||
public registerMultiple(events: {
|
public register(
|
||||||
[name: string]: (player: PlayerMp, ...args: any[]) => any
|
eventName: string,
|
||||||
}): void
|
callback: (...args: unknown[]) => unknown,
|
||||||
|
) {
|
||||||
registerMultiple(events: { [name: string]: (...args: any[]) => any }) {
|
rpc.register(eventName, callback)
|
||||||
Object.entries(events).forEach(([name, callback]) =>
|
|
||||||
rpc.register(name, (data: any[]) => {
|
|
||||||
return Array.isArray(data) ? callback(...data) : callback(data)
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
register() {}
|
// public registerMultiple(events: {
|
||||||
|
// [name: string]: (player: PlayerMp, ...args: any[]) => any
|
||||||
|
// }): void
|
||||||
|
//
|
||||||
|
// registerMultiple(events: { [name: string]: (...args: any[]) => any }) {
|
||||||
|
// Object.entries(events).forEach(([name, callback]) =>
|
||||||
|
// rpc.register(name, (data: any[]) => {
|
||||||
|
// return Array.isArray(data) ? callback(...data) : callback(data)
|
||||||
|
// }),
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// register() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Player {
|
||||||
|
triggerClient<EventName extends string>(
|
||||||
|
player: PlayerMp,
|
||||||
|
eventName: RageFW_ClientEvent<EventName>,
|
||||||
|
...args: RageFW_ClientEventArguments<EventName>
|
||||||
|
): RageFW_ClientEventReturn<EventName>
|
||||||
|
public triggerClient(
|
||||||
|
player: PlayerMp,
|
||||||
|
eventName: string,
|
||||||
|
...args: unknown[]
|
||||||
|
) {
|
||||||
|
return rpc.callClient(player, eventName, ...args)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const rage = {
|
export const rage = {
|
||||||
event: new Server(),
|
event: new Server(),
|
||||||
|
player: new Player(),
|
||||||
}
|
}
|
||||||
|
|
||||||
rage.event.register('customevent', vehicle => {})
|
rage.event.register('customEvent', (player, arg1, arg2) => {
|
||||||
|
rage.player.triggerClient(player, 'customClientEvent', arg1, arg2)
|
||||||
|
})
|
||||||
|
|
||||||
function test() {}
|
function test() {}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"target": "ESNext",
|
"target": "ESNext",
|
||||||
"lib": ["ESNext", "ES5"],
|
"lib": ["ESNext", "ES5"],
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
Loading…
Reference in New Issue
Block a user