Implement the SendCard event.
This commit is contained in:
parent
b005aed669
commit
1a810185ca
1 changed files with 62 additions and 6 deletions
|
|
@ -1,13 +1,69 @@
|
||||||
|
import { games, log } from '../main';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
|
|
||||||
export default (io: Server, socket: Socket, data: SendCard) => {
|
export default (io: Server, socket: Socket, data: SendCard) => {
|
||||||
try {
|
try {
|
||||||
socket.emit(`Error`, {
|
|
||||||
status: 501,
|
// Assert game exists
|
||||||
message: `SendCard: Not Implemented Yet`,
|
if (!games[data.game_code]) {
|
||||||
source: `SendCard`,
|
log.debug(`Can't delete game that doesn't exist: ${data.game_code}`);
|
||||||
});
|
socket.emit(`Error`, {
|
||||||
} catch (err) {
|
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`, {
|
socket.emit(`Error`, {
|
||||||
status: 500,
|
status: 500,
|
||||||
message: `${err.name}: ${err.message}`,
|
message: `${err.name}: ${err.message}`,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue