refactored server file structure + init middlewares
This commit is contained in:
parent
22e2c6f980
commit
22ed5c4588
3
server/src/core/index.ts
Normal file
3
server/src/core/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './player'
|
||||
export * from './server'
|
||||
export * from './logger'
|
@ -1,7 +1,7 @@
|
||||
import winston, { format } from 'winston'
|
||||
const { timestamp, printf, colorize } = format
|
||||
|
||||
export default class Logger {
|
||||
export class Logger {
|
||||
private format = printf(({ message, level, timestamp }) => {
|
||||
return `[${new Date(timestamp).toLocaleTimeString()}] [${level}]: ${message}`
|
||||
})
|
34
server/src/core/player.ts
Normal file
34
server/src/core/player.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import rpc from 'rage-rpc'
|
||||
|
||||
import {
|
||||
_CefEventHasArgs,
|
||||
_ClientEventHasArgs,
|
||||
RageFW_CefArgs,
|
||||
RageFW_CefEvent,
|
||||
RageFW_CefReturn,
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ServerClientEventArguments,
|
||||
RageFW_ServerClientEventReturn,
|
||||
} from '../types'
|
||||
|
||||
export class Player {
|
||||
public triggerClient<EventName extends RageFW_ClientEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerClientEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerClientEventReturn<EventName>> {
|
||||
return rpc.callClient(player, eventName, args)
|
||||
}
|
||||
|
||||
public triggerBrowser<EventName extends RageFW_CefEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: _CefEventHasArgs<EventName> extends true
|
||||
? [RageFW_CefArgs<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_CefReturn<EventName>> {
|
||||
return rpc.callBrowsers(player, eventName, args)
|
||||
}
|
||||
}
|
147
server/src/core/server.ts
Normal file
147
server/src/core/server.ts
Normal file
@ -0,0 +1,147 @@
|
||||
import rpc from 'rage-rpc'
|
||||
import { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
|
||||
|
||||
import { nativeEvents } from '../native.events'
|
||||
import {
|
||||
_CefEventHasArgs,
|
||||
_ClientEventHasArgs,
|
||||
_ServerEventHasArgs,
|
||||
RageFW_CefArgs,
|
||||
RageFW_CefEvent,
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ServerClientEventArguments,
|
||||
RageFW_ServerEvent,
|
||||
RageFW_ServerEventArguments,
|
||||
RageFW_ServerEventCallback,
|
||||
RageFW_ServerEventCallbackCustom,
|
||||
RageFW_ServerEventCallbackNative,
|
||||
RageFW_ServerEventReturn,
|
||||
} from '../types'
|
||||
|
||||
type MiddlewarePoolServer<EventName extends RageFW_ServerEvent> = Partial<{
|
||||
[K in EventName]: {
|
||||
mwName: string
|
||||
mw: MiddlewareFunction<K>
|
||||
}
|
||||
}>
|
||||
|
||||
type MiddlewareFunction<EventName extends RageFW_ServerEvent> = (
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerEventArguments<EventName>]
|
||||
: []
|
||||
) => void
|
||||
|
||||
export class Server {
|
||||
private middlewarePool: MiddlewarePoolServer<RageFW_ServerEvent> = {}
|
||||
|
||||
public use<EventName extends RageFW_ServerEvent>(
|
||||
mwName: string,
|
||||
eventName: EventName,
|
||||
mw: MiddlewareFunction<EventName>,
|
||||
) {
|
||||
this.middlewarePool = {
|
||||
...this.middlewarePool,
|
||||
[eventName]: {
|
||||
mwName,
|
||||
mw,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
|
||||
return nativeEvents.includes(eventName)
|
||||
}
|
||||
|
||||
private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallbackCustom<EventName>,
|
||||
): void {
|
||||
rpc.register(
|
||||
eventName,
|
||||
async (args: RageFW_ServerEventArguments<EventName>, info) => {
|
||||
callback([info.player as PlayerMp, ...args])
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private registerNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallbackNative<EventName>,
|
||||
): void {
|
||||
mp.events.add(
|
||||
eventName,
|
||||
(...args: Parameters<IServerEvents[EventName]>) =>
|
||||
callback([...args]),
|
||||
)
|
||||
}
|
||||
|
||||
public register<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallback<EventName>,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.registerNative(
|
||||
eventName,
|
||||
callback as RageFW_ServerEventCallbackNative,
|
||||
)
|
||||
} else {
|
||||
this.registerCustom(
|
||||
eventName,
|
||||
callback as unknown as RageFW_ServerEventCallbackCustom,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
||||
[event in EventName]: RageFW_ServerEventCallback<event>
|
||||
}): void {
|
||||
Object.entries<RageFW_ServerEventCallback<EventName>>(events).map(
|
||||
([eventName, callback]) => {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.registerNative(
|
||||
eventName,
|
||||
callback as RageFW_ServerEventCallbackNative,
|
||||
)
|
||||
} else {
|
||||
this.registerCustom(
|
||||
eventName as keyof RageFW_ICustomServerEvent,
|
||||
callback as unknown as RageFW_ServerEventCallbackCustom,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private unregisterCustom<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
rpc.unregister(eventName)
|
||||
}
|
||||
|
||||
private unregisterNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
mp.events.remove(eventName)
|
||||
}
|
||||
|
||||
public unregister<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.unregisterNative(eventName)
|
||||
} else {
|
||||
this.unregisterCustom(eventName)
|
||||
}
|
||||
}
|
||||
|
||||
public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerEventReturn<EventName>> {
|
||||
return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args)
|
||||
}
|
||||
}
|
@ -1,145 +1,4 @@
|
||||
import rpc from 'rage-rpc'
|
||||
|
||||
import Logger from './logger'
|
||||
|
||||
import {
|
||||
_CefEventHasArgs,
|
||||
_ClientEventHasArgs,
|
||||
_ServerEventHasArgs,
|
||||
RageFW_CefArgs,
|
||||
RageFW_CefEvent,
|
||||
RageFW_CefReturn,
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ICustomServerEvent,
|
||||
RageFW_ServerClientEventArguments,
|
||||
RageFW_ServerClientEventReturn,
|
||||
RageFW_ServerEvent,
|
||||
RageFW_ServerEventArguments,
|
||||
RageFW_ServerEventCallback,
|
||||
RageFW_ServerEventCallbackCustom,
|
||||
RageFW_ServerEventCallbackNative,
|
||||
RageFW_ServerEventReturn,
|
||||
} from './types'
|
||||
import { nativeEvents } from './native.events'
|
||||
|
||||
class Server {
|
||||
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
|
||||
return nativeEvents.includes(eventName)
|
||||
}
|
||||
|
||||
private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallbackCustom<EventName>,
|
||||
): void {
|
||||
rpc.register(
|
||||
eventName,
|
||||
async (args: RageFW_ServerEventArguments<EventName>, info) => {
|
||||
callback([info.player as PlayerMp, ...args])
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private registerNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallbackNative<EventName>,
|
||||
): void {
|
||||
mp.events.add(
|
||||
eventName,
|
||||
(...args: Parameters<IServerEvents[EventName]>) =>
|
||||
callback([...args]),
|
||||
)
|
||||
}
|
||||
|
||||
public register<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallback<EventName>,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.registerNative(
|
||||
eventName,
|
||||
callback as RageFW_ServerEventCallbackNative,
|
||||
)
|
||||
} else {
|
||||
this.registerCustom(
|
||||
eventName,
|
||||
callback as unknown as RageFW_ServerEventCallbackCustom,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
||||
[event in EventName]: RageFW_ServerEventCallback<event>
|
||||
}): void {
|
||||
Object.entries<RageFW_ServerEventCallback<EventName>>(events).map(
|
||||
([eventName, callback]) => {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.registerNative(
|
||||
eventName,
|
||||
callback as RageFW_ServerEventCallbackNative,
|
||||
)
|
||||
} else {
|
||||
this.registerCustom(
|
||||
eventName as keyof RageFW_ICustomServerEvent,
|
||||
callback as unknown as RageFW_ServerEventCallbackCustom,
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private unregisterCustom<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
rpc.unregister(eventName)
|
||||
}
|
||||
|
||||
private unregisterNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
mp.events.remove(eventName)
|
||||
}
|
||||
|
||||
public unregister<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.unregisterNative(eventName)
|
||||
} else {
|
||||
this.unregisterCustom(eventName)
|
||||
}
|
||||
}
|
||||
|
||||
public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerEventReturn<EventName>> {
|
||||
return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
class Player {
|
||||
public triggerClient<EventName extends RageFW_ClientEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerClientEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerClientEventReturn<EventName>> {
|
||||
return rpc.callClient(player, eventName, args)
|
||||
}
|
||||
|
||||
public triggerBrowser<EventName extends RageFW_CefEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: _CefEventHasArgs<EventName> extends true
|
||||
? [RageFW_CefArgs<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_CefReturn<EventName>> {
|
||||
return rpc.callBrowsers(player, eventName, args)
|
||||
}
|
||||
}
|
||||
import { Server, Logger, Player } from './core'
|
||||
|
||||
export const fw = {
|
||||
event: new Server(),
|
||||
|
@ -65,10 +65,19 @@ export type RageFW_ServerEventCallbackNative<
|
||||
K extends keyof IServerEvents = keyof IServerEvents,
|
||||
> = (payload: Parameters<IServerEvents[K]>) => ReturnType<IServerEvents[K]>
|
||||
|
||||
export type _ServerEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomServerEvent,
|
||||
> = keyof RageFW_ICustomClientEvent extends never
|
||||
? false
|
||||
: Parameters<RageFW_ICustomServerEvent[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
export type _ServerEventHasArgs<EventName extends RageFW_ServerEvent> =
|
||||
EventName extends keyof RageFW_ICustomServerEvent
|
||||
? keyof RageFW_ICustomClientEvent extends never
|
||||
? false
|
||||
: Parameters<
|
||||
RageFW_ICustomServerEvent[EventName]
|
||||
>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
: EventName extends keyof IServerEvents
|
||||
? keyof IServerEvents extends never
|
||||
? false
|
||||
: Parameters<IServerEvents[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
: false
|
||||
|
12
shared-types/types/types/index.d.ts
vendored
12
shared-types/types/types/index.d.ts
vendored
@ -1,7 +1,13 @@
|
||||
declare module 'rage-fw-shared-types' {
|
||||
export interface RageFW_ICustomServerEvent {}
|
||||
export interface RageFW_ICustomServerEvent {
|
||||
customServerEvent(arg1: string): number
|
||||
}
|
||||
|
||||
export interface RageFW_ICustomClientEvent {}
|
||||
export interface RageFW_ICustomClientEvent {
|
||||
customClientEvent(arg1: string): number
|
||||
}
|
||||
|
||||
export interface RageFW_ICustomCefEvent {}
|
||||
export interface RageFW_ICustomCefEvent {
|
||||
customCefEvent(arg1: string): number
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user