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 b1a9e2a2fcddc5272b5e013e2af273ec34ac7467 (patch)
parent 475b38ee76d2342af34f441c0b9df3a45627b4c9
Author: Alex Karle <alex@karle.co>
Date:   Wed, 29 Apr 2020 23:25:42 -0400

Euchre::Dealer: Add leave_game action for endgame

In theory, this is doable at any time, but we'd really only like people
to do this in lobby/end for now (a future commit will hopefully change
it).

Anyways, the critical thing is that we take the player out of the $game
so that the gamestate is no longer sent to them. It'll be up to the
client to at this point redraw a start screen or something (does this
make debug.html no longer a valid client? it certainly is a hack...)

Diffstat:
Mlib/Euchre/Dealer.pm | 30++++++++++++++++++++++++++++++
Mpublic/debug.html | 4++++
2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm @@ -38,6 +38,7 @@ use constant { BAD_VOTE => 14, FOLLOW_SUIT => 16, DONT_HAVE_CARD => 17, + NOT_IN_GAME => 18, }; our @ERRORS = (); @@ -59,6 +60,7 @@ $ERRORS[VOTE_OFF_KITTY] = "Can't vote for kitty card suit after turned down"; $ERRORS[BAD_VOTE] = "Bad vote"; $ERRORS[FOLLOW_SUIT] = "Have to follow suit!"; $ERRORS[DONT_HAVE_CARD] = "You don't have that card!"; +$ERRORS[NOT_IN_GAME] = "You're not in any game"; # XXX: The first draft of this was written quickly and chose @@ -176,6 +178,7 @@ sub handle_msg { # Game management endpoints ping => [\&pong], join_game => [\&join_game], + leave_game => [\&leave_game], take_seat => [\&take_seat, 'lobby', CHANGE_SEAT], stand_up => [\&stand_up, 'lobby', STAND_UP], start_game => [\&start_game, 'lobby', START_GAME], @@ -267,6 +270,33 @@ sub join_game { broadcast_gamestate($game); } +sub leave_game { + my ($p) = @_; + + my $game = $p->{game}; + if (!defined $game) { + send_error($p, NOT_IN_GAME); + return; + } + + delete $p->{game}; + if (exists $p->{seat}) { + $game->{players}->[$p->{seat}] = undef; + delete $p->{seat}; + delete $p->{hand}; + } else { + # Check for specatator + for (my $i = 0; $i < @{$game->{spectators}}; $i++) { + if ($game->{spectators}->[$i]->{id} eq $p->{id}) { + splice(@{$game->{spectators}}, $i, 1); + last; + } + } + } + + broadcast_gamestate($game); # let others know +} + # seat sub take_seat { my ($p, $msg) = @_; diff --git a/public/debug.html b/public/debug.html @@ -101,6 +101,9 @@ ws.send(JSON.stringify({action:'chat', msg: document.getElementById('chat').value })) document.getElementById('chat').value = ''; } + function leaveGame() { + ws.send(JSON.stringify({action:'leave_game'})) + } window.setInterval(() => { ws.send(JSON.stringify({action:'ping'})) }, 5000); </script> @@ -112,6 +115,7 @@ <input type="text" id="gamename"> <button onclick="joinGame(0)">Join Game</button> <button onclick="joinGame(1)">Force Join</button> + <button onclick="leaveGame()">Leave Game</button> <br><br> <label for="seat_no">Seat:</label> <input type="number" id="seat_no">