0
0
Fork 0

Correct the randomization algorithm

This commit is contained in:
Oliver-Akins 2021-02-28 21:58:39 -07:00
parent c0d907379e
commit a99d7716df

View file

@ -16,21 +16,41 @@ export default (io: Server, socket: Socket, data: RandomizeTeams) => {
}; };
let game = games[data.game_code]; let game = games[data.game_code];
// Randomly assign each player to a team (only on mediums, let players let players = [...game.players];
// pick which of them is the spirit) // game.log.info(players);
for (var player of game.players) { let new_team: 1|2 = 1;
let new_team: 1|2 = Math.floor(Math.random() * 2) + 1 as 1|2; while (players.length > 0) {
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`); let player_index = Math.floor(Math.random() * players.length);
// Send the new player list to all players let player = players[player_index];
io.to(game.id).send(`RandomizedTeams`, { players.splice(player_index, 1);
status: 200,
players: game.playerData game.log.debug(`Randomized ${player.name} onto team ${new_team}`);
});
// Move the socket rooms that the player's socket is in
player.socket?.leave(`${game.id}:*:${player.role}`);
player.socket?.leave(`${game.id}:${player.team}:${player.role}`);
player.socket?.join([
`${game.id}:*:guesser`,
`${game.id}:${new_team}:guesser`
]);
// Update the player's object
player.role = `guesser`;
player.team = new_team;
// Add the next player to the other team
new_team = new_team == 1 ? 2 : 1;
// Alert all connected clients that they need to update the UI
io.to(game.id).emit(`PlayerUpdate`, {
status: 200,
action: `modify`,
name: player.name,
role: player.role,
team: player.team,
});
};
} }
catch (err) { catch (err) {
log.prettyError(err); log.prettyError(err);