commit 9f03574af4a98cb593454970164e1ccaa5b4b772 (patch)
parent b84e209f5728839e4cfd7c029a61d6f1164e8c3e
Author: Alex Karle <alex@karle.co>
Date: Tue, 14 Apr 2020 22:54:36 -0400
Euchre::Dealer: Sort players hands for clients
To save the client some work (since we know how we assign values to each
card), this commit adds a "sort_hands" sub which gets called (1) when
the initial deal happens AND (2) when trump is picked. The ordering of
cards is obviously updated when trump is picked.
Cool, huh?
Diffstat:
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm
@@ -146,7 +146,7 @@ sub join_game {
spectators => [],
turn => -1,
dealer => -1,
- trump => -1,
+ trump => undef,
tricks => [0, 0, 0, 0],
table => [undef, undef, undef, undef],
caller => -1,
@@ -265,11 +265,11 @@ sub start_new_round {
# Shift dealer and deal
$game->{dealer} = ($game->{dealer} + 1) % 4;
$game->{table} = [undef, undef, undef, undef];
+ $game->{trump} = undef;
$game->{tricks} = [0,0,0,0];
$game->{out_player} = -1;
deal_players_hands($game);
-
# Signal vote of player next to dealer...
reset_turn($game);
$game->{phase} = 'vote';
@@ -286,6 +286,8 @@ sub deal_players_hands {
for my $p (@{$game->{players}}) {
$p->{hand} = shift @$handsA;
}
+
+ sort_hands($game);
}
@@ -315,6 +317,7 @@ sub order {
$game->{tricks}->[$partner_seat] = 'X';
}
reset_turn($game);
+ sort_hands($game);
broadcast_gamestate($game);
} else {
send_error($p, "Bad vote");
@@ -484,4 +487,15 @@ sub reset_turn {
next_turn($game);
}
+# We only need this when trump suit voted, not every broadcast
+sub sort_hands {
+ my ($game) = @_;
+
+ my $t = $game->{trump};
+ for my $p (@{$game->{players}}) {
+ my @sorted = sort { card_value($a, $t) <=> card_value($b, $t) } @{$p->{hand}};
+ $p->{hand} = \@sorted;
+ }
+}
+
1;
diff --git a/lib/Euchre/Game.pm b/lib/Euchre/Game.pm
@@ -41,12 +41,16 @@ sub card_value {
my ($c, $trump, $led) = @_;
# Lower to numeric value
- $trump = $SUIT_VALS{$trump};
my $cval = raw_card_value($c);
return $cval if $cval < 0;
+ # If neither trump or led defined, we just use raw_value
+ # (useful for initial deal hand orderings)
+ return $cval unless defined $trump;
+
# Gather more data on it
+ $trump = $SUIT_VALS{$trump};
my $suit = int($cval / 6);
my $is_jack = ($cval % 6 == 2);
my $is_trump = ($suit == $trump);