Rename and implement randomize teams event.
This commit is contained in:
parent
be446a0390
commit
c0d907379e
2 changed files with 43 additions and 31 deletions
|
|
@ -1,31 +0,0 @@
|
|||
import { games, log } from '../main';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
|
||||
export default (io: Server, socket: Socket, data: RandomizeTeam) => {
|
||||
try {
|
||||
socket.emit(`RandomizeTeam`, {
|
||||
status: 501,
|
||||
message: `Not Implemented Yet`,
|
||||
source: `RandomizeTeam`,
|
||||
});
|
||||
|
||||
// Assert game exists
|
||||
if (!games[data.game_code]) {
|
||||
log.debug(`Can't find game with code: ${data.game_code}`);
|
||||
socket.emit(`RandomizedTeams`, {
|
||||
status: 404,
|
||||
message: `Game with code ${data.game_code} could not be found`,
|
||||
source: `RandomizeTeams`
|
||||
});
|
||||
return;
|
||||
};
|
||||
}
|
||||
catch (err) {
|
||||
log.prettyError(err);
|
||||
socket.emit(`RandomizeTeam`, {
|
||||
status: 500,
|
||||
message: `${err.name}: ${err.message}`,
|
||||
source: `RandomizeTeam`,
|
||||
});
|
||||
};
|
||||
};
|
||||
43
server/src/events/RandomizeTeams.ts
Normal file
43
server/src/events/RandomizeTeams.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { games, log } from '../main';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
|
||||
export default (io: Server, socket: Socket, data: RandomizeTeams) => {
|
||||
try {
|
||||
|
||||
// Assert game exists
|
||||
if (!games[data.game_code]) {
|
||||
log.debug(`Can't find game with code: ${data.game_code}`);
|
||||
socket.emit(`RandomizedTeams`, {
|
||||
status: 404,
|
||||
message: `Game with code ${data.game_code} could not be found`,
|
||||
source: `RandomizeTeams`
|
||||
});
|
||||
return;
|
||||
};
|
||||
let game = games[data.game_code];
|
||||
|
||||
// Randomly assign each player to a team (only on mediums, let players
|
||||
// pick which of them is the spirit)
|
||||
for (var player of game.players) {
|
||||
let new_team: 1|2 = Math.floor(Math.random() * 2) + 1 as 1|2;
|
||||
player.team = new_team;
|
||||
player.role = `guesser`;
|
||||
game.log.debug(`Set ${player.name} to a medium on team ${player.team}`);
|
||||
};
|
||||
|
||||
game.log.info(`Randomized all players`);
|
||||
// Send the new player list to all players
|
||||
io.to(game.id).send(`RandomizedTeams`, {
|
||||
status: 200,
|
||||
players: game.playerData
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
log.prettyError(err);
|
||||
socket.emit(`RandomizedTeams`, {
|
||||
status: 500,
|
||||
message: `${err.name}: ${err.message}`,
|
||||
source: `RandomizeTeams`,
|
||||
});
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue