From 989c9664bfcd7a2f12702f173df2ffac71d274c6 Mon Sep 17 00:00:00 2001 From: Daithi Hearn Date: Tue, 23 Jan 2024 23:25:00 +0100 Subject: [PATCH] fix: isFollowing not working correctly --- pkg/game/game-utils.go | 7 ++-- pkg/game/game-utils_test.go | 16 ++++++++ pkg/game/game_test.go | 74 +++++++++++++++++++++++++++++++++++++ pkg/game/testdata.go | 32 ++++++++++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/pkg/game/game-utils.go b/pkg/game/game-utils.go index 032e9a9..c3bd7d0 100644 --- a/pkg/game/game-utils.go +++ b/pkg/game/game-utils.go @@ -233,12 +233,13 @@ func canRenage(leadOut Card, myTrumps []Card) bool { func isFollowing(myCard CardName, myCards []CardName, currentHand Hand, suit Suit) bool { mySuit := myCard.Card().Suit leadOut := currentHand.LeadOut.Card() - suitLead := leadOut.Suit == suit || leadOut.Suit == Wild + trumpLead := leadOut.Suit == suit || leadOut.Suit == Wild - if suitLead { + if trumpLead { var myTrumps []Card for _, card := range myCards { - if mySuit == suit || mySuit == Wild { + s := card.Card().Suit + if s == suit || s == Wild { myTrumps = append(myTrumps, card.Card()) } } diff --git a/pkg/game/game-utils_test.go b/pkg/game/game-utils_test.go index 9f9719a..dcb5927 100644 --- a/pkg/game/game-utils_test.go +++ b/pkg/game/game-utils_test.go @@ -349,6 +349,22 @@ func TestGameUtils_isFollowing(t *testing.T) { suit: Diamonds, expectedResult: true, }, + { + name: "Following", + myCard: THREE_CLUBS, + myCards: []CardName{TWO_HEARTS, THREE_CLUBS, FOUR_DIAMONDS, FIVE_SPADES, SIX_HEARTS}, + currentHand: Hand{LeadOut: ACE_CLUBS, PlayedCards: []PlayedCard{{PlayerID: "1", Card: ACE_CLUBS}}}, + suit: Clubs, + expectedResult: true, + }, + { + name: "Not following", + myCard: FOUR_DIAMONDS, + myCards: []CardName{TWO_HEARTS, THREE_CLUBS, FOUR_DIAMONDS, FIVE_SPADES, SIX_HEARTS}, + currentHand: Hand{LeadOut: ACE_CLUBS, PlayedCards: []PlayedCard{{PlayerID: "1", Card: ACE_CLUBS}}}, + suit: Clubs, + expectedResult: false, + }, } for _, test := range tests { diff --git a/pkg/game/game_test.go b/pkg/game/game_test.go index 1dd271b..22ac241 100644 --- a/pkg/game/game_test.go +++ b/pkg/game/game_test.go @@ -1201,3 +1201,77 @@ func TestGame_Buy(t *testing.T) { }) } } + +func TestGame_Play(t *testing.T) { + tests := []struct { + name string + game Game + playerID string + card CardName + expectedStatus RoundStatus + expectedNextPlayer string + expectingError bool + }{ + { + name: "Valid play - first card", + game: PlayingGame_RoundStart("1"), + playerID: "1", + card: TWO_HEARTS, + expectedStatus: Playing, + expectedNextPlayer: "2", + }, + { + name: "Try to play a card that isn't in your hand", + game: PlayingGame_RoundStart("1"), + playerID: "1", + card: ACE_SPADES, + expectedStatus: Playing, + expectingError: true, + }, + { + name: "Following suit", + game: PlayingGame_RoundStart_FirstCardPlayed(), + playerID: "2", + card: THREE_CLUBS, + expectedStatus: Playing, + expectedNextPlayer: "1", + }, + { + name: "Not following suit", + game: PlayingGame_RoundStart_FirstCardPlayed(), + playerID: "2", + card: FOUR_DIAMONDS, + expectedStatus: Playing, + expectingError: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + err := test.game.Play(test.playerID, test.card) + if test.expectingError { + if err == nil { + t.Errorf("expected an error, got nil") + } + } else { + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if test.game.CurrentRound.Status != test.expectedStatus { + t.Errorf("expected round status to be %s, got %s", test.expectedStatus, test.game.CurrentRound.Status) + } + if test.game.CurrentRound.CurrentHand.CurrentPlayerID != test.expectedNextPlayer { + t.Errorf("expected next player to be %s, got %s", test.expectedNextPlayer, test.game.CurrentRound.CurrentHand.CurrentPlayerID) + } + // Check that he has all of retained the cards he selected + state, err := test.game.GetState(test.playerID) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if contains(state.Cards, test.card) { + t.Errorf("expected player to not have played card %s, got %v", test.card, state.Cards) + } + } + }) + } +} diff --git a/pkg/game/testdata.go b/pkg/game/testdata.go index 25ed214..5512af6 100644 --- a/pkg/game/testdata.go +++ b/pkg/game/testdata.go @@ -312,6 +312,38 @@ func PlayingGame_RoundStart(dealerId string) Game { return game } +func PlayingGame_RoundStart_FirstCardPlayed() Game { + p1 := Player1() + p1.Cards = []CardName{KING_CLUBS, QUEEN_HEARTS, JACK_SPADES, TEN_DIAMONDS} + p2 := Player2() + p2.Cards = []CardName{TWO_HEARTS, THREE_CLUBS, FOUR_DIAMONDS, FIVE_SPADES, SIX_HEARTS} + + return Game{ + ID: "1", + Name: "Test Game", + Status: Active, + Timestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Players: []Player{p1, p2}, + CurrentRound: Round{ + DealerID: "1", + GoerID: "2", + Status: Playing, + CurrentHand: Hand{ + CurrentPlayerID: "2", + LeadOut: ACE_CLUBS, + PlayedCards: []PlayedCard{{PlayerID: "1", Card: ACE_CLUBS}}, + }, + CompletedHands: []Hand{}, + Suit: Clubs, + Number: 1, + }, + Dummy: Dummy(), + AdminID: "1", + Deck: NewDeck(), + } + +} + func PlayingGame_Hand1Complete(dealerId string) Game { game := PlayingGame_RoundStart(dealerId)