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 25b30737caf2fd5cd4a10cca0e012eeba91e132c (patch)
parent 1b45c2cd0f65d0dd0eeda184b55d7e10b076dfc8
Author: Alex Karle <alex@karle.co>
Date:   Mon, 30 Mar 2020 00:08:57 -0400

Euchre::Dealer: Add start_game, with new in_progress state

This does two things:

  1. Add the scaffolding for a start_game action
  2. Add the in_progress state to the game

(2) is only really used for preventing join_game; there is functionally
no equivalent to why this is superior over the checking for 4 players,
except for it is slightly more readable and is a nice field to have for
debug.html / the eventual listing of games (?)

Diffstat:
Mlib/Euchre/Dealer.pm | 18+++++++++++++++++-
Mpublic/debug.html | 14++++++++++----
2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm @@ -36,6 +36,7 @@ our @EXPORT = qw( # caller => 0-3, # table => [ c1, c2, c3, c4 ], # up to 4 cards # score => [X, Y], +# in_progress => 0/1, # } # # We decided the players would keep track of their own hands @@ -96,6 +97,7 @@ sub handle_msg { join_game => \&join_game, take_seat => \&take_seat, stand_up => \&stand_up, + start_game => \&start_game, ); if (exists $dispatch{$msg->{action}}) { @@ -125,11 +127,12 @@ sub join_game { table => [], callers => -1, score => [0, 0], + in_progress => 0, }; } # Handle full game case - if (num_players($GAMES{$id}) >= 4) { + if ($GAMES{$id}->{in_progress}) { send_error($cid, 'Already 4 players'); } else { # Add player to Game and cross-link in %PLAYERS for handle_msg @@ -182,6 +185,19 @@ sub stand_up { } +sub start_game { + my ($cid) = @_; + my $game = $GAMES{$PLAYERS{$cid}->{game_id}}; + + if (num_players($game->{id}) < 4) { + send_error($cid, "Can't start with empty seats!"); + } else { + $game->{in_progress} = 1; + # TODO: kick spectators out? + # TODO: deal! + broadcast_gamestate($game); + } +} sub num_players { my ($gid) = @_; diff --git a/public/debug.html b/public/debug.html @@ -15,7 +15,8 @@ if (msg.msg_type === 'game_state') { gameState = '<br>Game: ' + msg.game.id + '<br>' + 'Players: ' + msg.game.players + '<br>' + - 'Spectators: ' + msg.game.spectators + '<br>' + 'Spectators: ' + msg.game.spectators + '<br>' + + 'In Progress?: ' + msg.game.in_progress + '<br>' document.getElementById('game').innerHTML = gameState } else if (msg.msg_type === 'error') { document.getElementById('error').innerHTML += msg.msg + '<br>' @@ -35,6 +36,9 @@ function stand() { ws.send(JSON.stringify({action:'stand_up'})) } + function start_game() { + ws.send(JSON.stringify({action:'start_game'})) + } </script> <h1>Hello, World</h1> @@ -43,12 +47,14 @@ <label for="gamename">Game:</label> <input type="text" id="gamename"> <button onclick="joinGame()">Join Game</button> - <br> - <button onclick="stand()">Stand</button> - <br> + <br><br> <label for="seat_no">Seat:</label> <input type="number" id="seat_no"> <button onclick="sit()">Sit</button> + <button onclick="stand()">Stand</button> + <br> + <button onclick="start_game()">Start Game</button> + <br> <br><br> <div id="game">Not in a game</div>