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 749714362a48bf4b8d9b6741d717db9015aa64ed (patch)
parent b318bc82788a58362b39779c003e0e9083e6f901
Author: Alex Karle <alex@karle.co>
Date:   Mon, 20 Apr 2020 23:21:32 -0400

Euchre::Dealer: Use timer post-trick over prev_table

The previous, hacky, logic for having the clients show the table after
the trick was done was to include the table as prev_table forever.

This commit scraps that logic in favor of a 'pause' phase with
mojolicious' Mojo::IOLoop Timer, which is 100% cleaner and what we
wanted in the first place!

Who knew it pays to read the docs?

Diffstat:
Mlib/Euchre/Dealer.pm | 28+++++++++++++++++++---------
Mpublic/debug.html | 15---------------
2 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm @@ -4,6 +4,7 @@ use warnings; package Euchre::Dealer; +use Mojo::IOLoop; use List::Util qw(sum); use Euchre::Game; @@ -34,7 +35,6 @@ our @EXPORT = qw( # led => suit, # caller => 0-3, # table => [ c1, c2, c3, c4 ], # exactly 4, undef if not played -# prev_table => [ c1, c2, c3, c4 ], # Last trick (for client-viewing) # score => [X, Y], # phase => 'lobby', 'play', 'vote', 'end' # trump_nominee => card, @@ -152,7 +152,6 @@ sub join_game { trump => undef, tricks => [0, 0, 0, 0], table => [undef, undef, undef, undef], - prev_table => [undef, undef, undef, undef], caller => -1, score => [0, 0], phase => 'lobby', @@ -422,11 +421,13 @@ sub play_card { my @table = map { defined($_) ? $_ : 'X' } @{$game->{table}}; my $winner_id = trick_winner($game->{trump}, $game->{led}, @table); + # Update the gamestate and pause so all can see $game->{tricks}->[$winner_id]++; $game->{turn} = $winner_id; - $game->{prev_table} = $game->{table}; - $game->{table} = [undef, undef, undef, undef]; - $game->{led} = undef; + $game->{phase} = 'pause'; + + # Sub to call after pause + my $post_pause = sub {}; my @num_tricks = grep { /^\d+$/ } @{$game->{tricks}}; if (sum(@num_tricks) >= 5) { @@ -435,14 +436,23 @@ sub play_card { $game->{score}->[$team_id] += $score; if ($game->{score}->[$team_id] >= 10) { - # End game... no need to redeal - signal_game_end($game); + $post_pause = sub { signal_game_end($game) }; } else { - start_new_round($game); + $post_pause = sub { start_new_round($game) }; } } - } + + Mojo::IOLoop->timer(3 => sub { + $game->{table} = [undef, undef, undef, undef]; + $game->{led} = undef; + $game->{phase} = 'play'; + + $post_pause->(); + broadcast_gamestate($game); + }); + + } broadcast_gamestate($game); } diff --git a/public/debug.html b/public/debug.html @@ -7,9 +7,6 @@ img.card { width: 100px; } - img.prev_card { - width: 20px; - } </style> </head> <body> @@ -56,16 +53,6 @@ } else { document.getElementById('table').innerHTML = 'No cards on table' } - if (msg.game.prev_table.length) { - document.getElementById('prev_table').innerHTML = 'Last Trick: ' - for (var i = 0; i < msg.game.prev_table.length; i++) { - var c = msg.game.prev_table[i] - if (c === null) { - c = '2B' // show back - } - document.getElementById('prev_table').innerHTML += '<img class="prev_card" src="cards/' + c + '.svg">' - } - } document.getElementById('game').innerHTML = gameState } else if (msg.msg_type === 'error') { document.getElementById('error').innerHTML += msg.msg + '<br>' @@ -133,8 +120,6 @@ <br><br> <div id="table">No cards on table</div> <br> <br> - <div id="prev_table"></div> - <br> <br> <div id="hand">No cards in hand</div> <br> <button onclick="vote(0)">Order</button>