Add function to handle the request for the player list.

This commit is contained in:
Oliver-Akins 2020-09-28 23:05:22 -06:00
parent 4ac0fb2833
commit 11c671ef25

View 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}`,
})
}
};