This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as alt from 'alt-client'; | ||
|
||
/** | ||
* Handle an RPC event from server-side, and return a result back to the server. | ||
* | ||
* The RPC event must be invoked from server-side through the `Athena.systems.rpc.invoke` function. | ||
* | ||
* #### Example | ||
* ```ts | ||
* AthenaClient.systems.rpc.on('returnPlayerLocalPosition', () => { | ||
* return alt.Player.local.pos; | ||
* }) | ||
* ``` | ||
* | ||
* @export | ||
* @template T | ||
* @param {string} eventName | ||
* @param {(...args: any[]) => T} callback | ||
*/ | ||
export function on<T = any>(eventName: string, callback: (...args: any[]) => T) { | ||
alt.onServer(eventName, async (instancedName: string, ...args: any[]) => { | ||
const result = await callback(...args); | ||
alt.emitServer(instancedName, result); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import * as alt from 'alt-server'; | ||
import * as Athena from '@AthenaServer/api'; | ||
|
||
export type Callback = (player: alt.Player, ...args: any[]) => void; | ||
|
||
export interface ServerRpcEvent { | ||
/** | ||
* A general purpose name for the event. | ||
* | ||
* @type {string} | ||
* @memberof ServerRpcEvent | ||
*/ | ||
eventName: string; | ||
|
||
/** | ||
* Arguments to pass down to the client. | ||
* | ||
* @type {any[]} | ||
* @memberof ServerRpcEvent | ||
*/ | ||
args?: any[]; | ||
|
||
/** | ||
* Kick, and log the user for not responding to an event. | ||
* | ||
* The value should be a message to send to the user. | ||
* | ||
* @type {string} | ||
* @memberof ServerRpcEvent | ||
*/ | ||
kickOnNoResponse?: string; | ||
|
||
/** | ||
* Timeout before the event is unregistered and removed. | ||
* | ||
* @type {number} | ||
* @memberof ServerRpcEvent | ||
*/ | ||
msTimeout: number; | ||
} | ||
|
||
/** | ||
* Invoke an RPC event, and get a result. | ||
* | ||
* If the timeout expires; the callback will pass undefined. | ||
* | ||
* #### Example | ||
* ```ts | ||
* Athena.systems.rpc.invoke(somePlayer, 'getLocalPos', (player: alt.Player, pos: alt.IVector3) => { | ||
* alt.log('RPC Position was' + JSON.stringify(pos)); | ||
* }) | ||
* ``` | ||
* | ||
* @export | ||
* @template T | ||
* @param {alt.Player} player | ||
* @param {ServerRpcEvent} event | ||
* @param {(...T) => void} callback | ||
* @return {*} | ||
*/ | ||
export function invoke(player: alt.Player, event: ServerRpcEvent, callback: Callback) { | ||
// Construct instanced event name | ||
// Randomize it to help with collisions as well | ||
const instancedName = Athena.utility.hash.sha256Random(`rpc:${player.id}:${event.eventName}`); | ||
|
||
const callbackHandler = (player: alt.Player, ...args: any[]) => { | ||
alt.clearTimeout(timeout); | ||
callback(player, ...args); | ||
}; | ||
|
||
const timeoutHandler = () => { | ||
alt.offClient(instancedName, callbackHandler); | ||
if (!event.kickOnNoResponse) { | ||
return; | ||
} | ||
|
||
if (!player || !player.valid) { | ||
return; | ||
} | ||
|
||
const data = Athena.document.character.get(player); | ||
const mongoId = data && data._id ? data._id : 'Not Logged In'; | ||
alt.log(`${event.eventName} was called, but no response. ID: ${player.id} | MongoDB: ${mongoId}`); | ||
player.kick(event.kickOnNoResponse); | ||
}; | ||
|
||
// Clear old event if time is exceeded | ||
const timeout = alt.setTimeout(timeoutHandler, event.msTimeout); | ||
|
||
// Automatically unregisters itself once a client has pushed up the data | ||
alt.onceClient(instancedName, callbackHandler); | ||
|
||
// Call normal event name, and pass arguments down | ||
if (event.args) { | ||
player.emit(event.eventName, instancedName, ...event.args); | ||
return; | ||
} | ||
|
||
player.emit(event.eventName, instancedName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
|
||
// server | ||
// rpc.emit -> someEvent, someReturnValue | ||
|
||
// client | ||
// rpc.handle -> someEvent, pushToServer | ||
|