From 25b30737caf2fd5cd4a10cca0e012eeba91e132c Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Mon, 30 Mar 2020 00:08:57 -0400 Subject: [PATCH] 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 (?) --- lib/Euchre/Dealer.pm | 18 +++++++++++++++++- public/debug.html | 14 ++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm index 49a7412..8ec8623 100644 --- 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 index 4a5d506..9cab5c0 100644 --- a/public/debug.html +++ b/public/debug.html @@ -15,7 +15,8 @@ if (msg.msg_type === 'game_state') { gameState = '
Game: ' + msg.game.id + '
' + 'Players: ' + msg.game.players + '
' + - 'Spectators: ' + msg.game.spectators + '
' + 'Spectators: ' + msg.game.spectators + '
' + + 'In Progress?: ' + msg.game.in_progress + '
' document.getElementById('game').innerHTML = gameState } else if (msg.msg_type === 'error') { document.getElementById('error').innerHTML += msg.msg + '
' @@ -35,6 +36,9 @@ function stand() { ws.send(JSON.stringify({action:'stand_up'})) } + function start_game() { + ws.send(JSON.stringify({action:'start_game'})) + }

Hello, World

@@ -43,12 +47,14 @@ -
- -
+

+ +
+ +


Not in a game
-- libgit2 1.1.1