From b1a9e2a2fcddc5272b5e013e2af273ec34ac7467 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Wed, 29 Apr 2020 23:25:42 -0400 Subject: [PATCH] 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...) --- lib/Euchre/Dealer.pm | 30 ++++++++++++++++++++++++++++++ public/debug.html | 4 ++++ 2 files changed, 34 insertions(+) diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm index 0b750a8..7fbbcbf 100644 --- 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 index b140660..2035d89 100644 --- 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); @@ -112,6 +115,7 @@ +

-- libgit2 1.1.1