Add function to handle the request for the player list.
This commit is contained in:
parent
4ac0fb2833
commit
11c671ef25
1 changed files with 31 additions and 0 deletions
31
src/events/GetPlayerList.ts
Normal file
31
src/events/GetPlayerList.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
Get's the current player list. This is only really used by the host. This is
|
||||
almost never called by the client itself
|
||||
*/
|
||||
import * as db from '../utils/db';
|
||||
import { Socket } from 'socket.io';
|
||||
|
||||
export const GetPlayerList = (socket: Socket, data: GetPlayerList) => {
|
||||
try {
|
||||
if (!db.exists(data.game_code)) {
|
||||
socket.emit(`PlayerListResponse`, {
|
||||
success: false,
|
||||
message: `Error: ${data.game_code} does not have an active database.`,
|
||||
});
|
||||
} else {
|
||||
let game: database = db.read(data.game_code);
|
||||
|
||||
// Respond to client
|
||||
socket.emit(`PlayerListResponse`, {
|
||||
success: true,
|
||||
players: [Object.keys(game.players)],
|
||||
});
|
||||
};
|
||||
} catch (err) {
|
||||
// Let client know an error occured
|
||||
socket.emit(`PlayerListResponse`, {
|
||||
success: false,
|
||||
message: `${err.name}: ${err.message}`,
|
||||
})
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue