commit b0f6de8b7f83a1a7f6f9efac099eda87e2df80f8 (patch)
parent 745f83e2e706ef79026430d9fe2aa35518e947dc
Author: Alex Karle <alex@karle.co>
Date: Sat, 11 Apr 2020 18:10:57 -0400
Euchre::Dealer: Add check that username is unique to game
This is key to the idea that someone could leave a game (by accident or
on purpose) and come back to find their cards. It also just generally
prevents a whole lot of confusion.
Names need not be unique to the whole server, just to the game.
Diffstat:
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm
@@ -159,14 +159,26 @@ sub join_game {
if ($GAMES{$id}->{phase} ne 'lobby') {
send_error($p, 'Game already in progress');
} else {
+ my $game = $GAMES{$id};
+
+ # Make sure name is unique to game
+ my @all_names = map { $_->{name} }
+ grep { defined }
+ (@{$game->{players}}, @{$game->{spectators}});
+
+ if (grep { $_ eq $msg->{player_name} } @all_names) {
+ send_error($p, 'Username not unique');
+ return;
+ }
+
# Add player object to Game
# All players start as spectators and have to take a seat explicitly
$p->{name} = $msg->{player_name};
$p->{hand} = [];
- $p->{game} = $GAMES{$id};
- push @{$GAMES{$id}->{spectators}}, $p;
+ $p->{game} = $game;
+ push @{$game->{spectators}}, $p;
- broadcast_gamestate($GAMES{$id});
+ broadcast_gamestate($game);
}
}