Implement SelectObject event.
This commit is contained in:
parent
e3fbf16e9c
commit
4b2a13460d
1 changed files with 31 additions and 5 deletions
|
|
@ -1,13 +1,39 @@
|
||||||
|
import { games, log } from '../main';
|
||||||
import { Server, Socket } from 'socket.io';
|
import { Server, Socket } from 'socket.io';
|
||||||
|
|
||||||
export default (io: Server, socket: Socket, data: SelectObject) => {
|
export default (io: Server, socket: Socket, data: SelectObject) => {
|
||||||
try {
|
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`, {
|
socket.emit(`Error`, {
|
||||||
status: 501,
|
status: 404,
|
||||||
message: `SelectObject: Not Implemented Yet`,
|
message: `Game with code ${data.game_code} could not be found`,
|
||||||
source: `SelectObject`,
|
source: `SelectObject`
|
||||||
});
|
});
|
||||||
} catch (err) {
|
return;
|
||||||
|
};
|
||||||
|
let game = games[data.game_code];
|
||||||
|
|
||||||
|
// Assert that the object is actually a valid choice
|
||||||
|
if (!game.objects.includes(data.object)) {
|
||||||
|
log.warn(`[${game.id}] Someone tried selecting an object that doesn't exist: ${data.object}`);
|
||||||
|
socket.emit(`Error`, {
|
||||||
|
status: 409,
|
||||||
|
message: `That object isn't on the card.`,
|
||||||
|
source: `SelectObject`
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
log.debug(`[${game.id}] Object has been chosen: ${data.object}`);
|
||||||
|
game.object = data.object;
|
||||||
|
io.to(game.id).emit(`ChosenObject`, {
|
||||||
|
object: data.object
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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