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 894fa1b729e9014b0cb359847b6df91baa0a8813 (patch)
parent 74332a8b94ae0a5e97a565c418b3339404af6ae6
Author: Alex Karle <alex@karle.co>
Date:   Sat, 11 Apr 2020 18:39:00 -0400

Euchre::Dealer: Validate suit of card played

This commit adds validation logic to the suit of the card that is being
played. The Dealer (all knowing) inspects the hand of the player and
ensures that if they are NOT following suit, they CANNOT.

This is, of course, made more complex by the fact that the left bower is
the trump suit, but I think it came out clean enough logic wise.

The reason to validate server-side is that I don't plan to implement an
"undo" action any time soon, and so we want to prevent any user error
(or client error) from wrecking the game. It also means the clients can
be written quicker (~ckarle).

Diffstat:
Mlib/Euchre/Dealer.pm | 31+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+), 0 deletions(-)

diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm @@ -318,6 +318,36 @@ sub play_card { my $game = $p->{game}; my $seat = $p->{seat}; + # Validate they follow suit if they CAN + if (defined $game->{led}) { + my %colors = (H => 'D', D => 'H', S => 'C', C => 'S'); + + # Build up a list of valid cards + my @followers = map { "$_$game->{led}" } qw(N T Q K A); # no jack + if ($game->{led} eq $game->{trump}) { + # Trump led, both jacks valid + push @followers, "J$game->{led}"; + push @followers, "J$colors{$game->{led}}"; + } elsif ($colors{$game->{led}} ne $game->{trump}) { + # Off-color, jack is OK + push @followers, "J$game->{led}"; + } else { + # Played same color as trump, don't add jack + } + my $follower_re = join("|", @followers); + + # Now validate that they are EITHER: + # 1) Following Suit + # 2) Can't follow suit + # By checking negative of both + if ($msg->{card} !~ /$follower_re/ && + grep { $_ =~ /$follower_re/ } @{$p->{hand}}) { + + send_error($p, "Have to follow suit!"); + return; + } + } + # Make sure they have the card, and update their hand my $found = 0; for (my $i = 0; $i < scalar @{$p->{hand}}; $i++) { @@ -354,6 +384,7 @@ sub play_card { $game->{tricks}->[$winner_id]++; $game->{turn} = $winner_id; $game->{table} = [undef, undef, undef, undef]; + $game->{led} = undef; my @num_tricks = grep { /^\d+$/ } @{$game->{tricks}}; if (sum(@num_tricks) >= 5) {