From 11c671ef25e2eadfb224dd12e03503e4f8bf5524 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Mon, 28 Sep 2020 23:05:22 -0600 Subject: [PATCH] Add function to handle the request for the player list. --- src/events/GetPlayerList.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/events/GetPlayerList.ts diff --git a/src/events/GetPlayerList.ts b/src/events/GetPlayerList.ts new file mode 100644 index 0000000..94f3b99 --- /dev/null +++ b/src/events/GetPlayerList.ts @@ -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}`, + }) + } +}; \ No newline at end of file