euchre-live

Euchre web-app for the socially distant family
git clone git://git.alexkarle.com/euchre-live.git
Log | Files | Refs | README | LICENSE

commit f6b3d2acbe3c9d78314d3bf9e60e4edae3a33bfe (patch)
parent 25b30737caf2fd5cd4a10cca0e012eeba91e132c
Author: Alex Karle <alex@karle.co>
Date:   Mon, 30 Mar 2020 00:23:58 -0400

Euchre::Dealer: [refactor] store game obj in player hash

There's almost no time where we need the game_id in the player object.
It is much more likely (a.k.a literally every handler) that the first
thing we do is go get the game object out of %GAMES.

Storing the object reference in the player is just much cleaner.

Diffstat:
Mlib/Euchre/Dealer.pm | 10+++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm @@ -47,7 +47,7 @@ our @EXPORT = qw( # websocket id # # { -# game_id => key in %GAMES, +# game => reference to current game object, # name => username, # seat => 0-3, # ws => websocket obj @@ -138,7 +138,7 @@ sub join_game { # Add player to Game and cross-link in %PLAYERS for handle_msg # All players start as spectators and have to take a seat explicitly $PLAYERS{$cid}->{name} = $msg->{player_name}; - $PLAYERS{$cid}->{game_id} = $id; + $PLAYERS{$cid}->{game} = $GAMES{$id}; push @{$GAMES{$id}->{spectators}}, $cid; # XXX: for fast prototyping we just broadcast gamestate @@ -150,7 +150,7 @@ sub join_game { sub take_seat { my ($cid, $msg) = @_; - my $game = $GAMES{$PLAYERS{$cid}->{game_id}}; + my $game = $PLAYERS{$cid}->{game}; my $seat = $msg->{seat}; if (defined $game->{players}->[$seat]) { @@ -171,7 +171,7 @@ sub take_seat { sub stand_up { my ($cid) = @_; - my $game = $GAMES{$PLAYERS{$cid}->{game_id}}; + my $game = $PLAYERS{$cid}->{game}; my $seat = $PLAYERS{$cid}->{seat}; if (!defined $seat) { @@ -187,7 +187,7 @@ sub stand_up { sub start_game { my ($cid) = @_; - my $game = $GAMES{$PLAYERS{$cid}->{game_id}}; + my $game = $PLAYERS{$cid}->{game}; if (num_players($game->{id}) < 4) { send_error($cid, "Can't start with empty seats!");