0
0
Fork 0

Implement NewHand event.

This commit is contained in:
Oliver-Akins 2020-12-27 13:24:58 -07:00
parent e19fb1ef3a
commit bd6ab5e02e

View file

@ -1,13 +1,40 @@
import { conf, games, log } from '../main';
import { Server, Socket } from 'socket.io';
export default (io: Server, socket: Socket, data: NewHand) => {
try {
// Assert game exists
if (!games[data.game_code]) {
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`);
socket.emit(`Error`, {
status: 501,
message: `NewHand: Not Implemented Yet`,
source: `NewHand`,
status: 404,
message: `Game with code ${data.game_code} could not be found`,
source: `GetPastQuestions`
});
} catch (err) {
return;
};
let game = games[data.game_code];
let team = game.teams[data.team - 1];
let deck = game.questions;
// Empty the medium's hand to the discard pile so we know where the
// cards are.
for (var card of team.hand) {
deck.discard(card);
team.removeCard(card);
log.silly(`[${game.id}] Removing card: '${card}' from team ${data.team}'s hand.`);
};
// Add the questions and then alert the game clients about the changes
team.addQuestions(deck.draw(conf.game.hand_size));
log.silly(`[${game.id}] Drew a new hand of cards for team ${data.team}.`);
io.to(game.id).emit(`UpdateHand`, {
mode: `replace`,
questions: team.hand,
});
}
catch (err) {
socket.emit(`Error`, {
status: 500,
message: `${err.name}: ${err.message}`,