event register types + triggerClient

This commit is contained in:
Danya H 2024-06-09 01:06:20 +01:00
parent d67073d739
commit dc9e713e6b
2 changed files with 77 additions and 19 deletions

View File

@ -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
: 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] ? ThisifyServerEvents[K]
: never : never
declare class Server { type RageFW_ClientEvent<K> = K extends keyof RageFW_ICustomClientEvent
public register<K = ServerEvent<K>>( ? K
eventName: K, : K extends keyof IServerEvents
callback: ServerEventCallback, ? 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
interface RageFW_ICustomServerEvent {
customEvent(arg1: number, arg2: boolean): void
}
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() {}

View File

@ -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,