diff --git a/server/src/events/SendCard.ts b/server/src/events/SendCard.ts index 4e15b90..62949b1 100644 --- a/server/src/events/SendCard.ts +++ b/server/src/events/SendCard.ts @@ -1,13 +1,69 @@ +import { games, log } from '../main'; import { Server, Socket } from 'socket.io'; export default (io: Server, socket: Socket, data: SendCard) => { try { - socket.emit(`Error`, { - status: 501, - message: `SendCard: Not Implemented Yet`, - source: `SendCard`, - }); - } catch (err) { + + // 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: 404, + message: `Game with code ${data.game_code} could not be found`, + source: `SendCard` + }); + return; + }; + let game = games[data.game_code]; + let team = game.teams[data.team - 1]; + let deck = game.questions; + + // The writer is answering + if (data.from === "writer") { + log.debug(`[${game.id}] Writer selected question to answer.`); + deck.discard(data.text); + team.selectQuestion(data.text); + + socket.emit(`UpdateHand`, { + mode: "replace", + questions: [] + }); + return; + } + + // The writer is sending the card to the writer + else if (data.from === "guesser") { + log.debug(`[${game.id}] Guesser is sending the card to the writer.`); + + // Update the team's hand + team.removeCard(data.text); + team.addCardsToHand(game.questions.draw(1)); + + // send the question text to the writer player + io.to(`${game.id}:${data.team}:writer`).emit(`UpdateHand`, { + mode: "append", + questions: data.text + }); + + // Alert all the guessers of the + io.to(`${game.id}:${data.team}:guesser`).emit(`UpdateHand`, { + mode: "replace", + questions: team.hand + }); + return; + } + + else { + log.warn(`[${game.id}] Unknown role in the "from" property: ${data.from}`); + socket.emit(`Error`, { + status: 400, + message: `Unknown role in the "from" property: ${data.from}`, + source: `SendCard` + }); + return; + }; + } + catch (err) { socket.emit(`Error`, { status: 500, message: `${err.name}: ${err.message}`,