From 974020db79c862314130fd65e2b74f7bb1f6fd39 Mon Sep 17 00:00:00 2001 From: Daithi Hearn Date: Tue, 30 Jan 2024 23:19:38 +0100 Subject: [PATCH 1/2] feat: adding previous round --- pkg/game/game-methods.go | 9 ++++++++- pkg/game/game.go | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/game/game-methods.go b/pkg/game/game-methods.go index a92fc1d..1a0aba3 100644 --- a/pkg/game/game-methods.go +++ b/pkg/game/game-methods.go @@ -36,7 +36,13 @@ func (g *Game) GetState(playerID string) (State, error) { me.Cards = append(me.Cards, g.Dummy...) } - // 3. Return player's game state + // 3. Get Previous round if there is one + var prevRound Round + if len(g.Completed) > 0 { + prevRound = g.Completed[len(g.Completed)-1] + } + + // 4. Return player's game state gameState := State{ ID: g.ID, Revision: g.Revision, @@ -49,6 +55,7 @@ func (g *Game) GetState(playerID string) (State, error) { Cards: me.Cards, Status: g.Status, Round: g.CurrentRound, + PrevRound: prevRound, MaxCall: maxCall, Players: g.Players, } diff --git a/pkg/game/game.go b/pkg/game/game.go index b7acd77..a37efa7 100644 --- a/pkg/game/game.go +++ b/pkg/game/game.go @@ -94,5 +94,6 @@ type State struct { MaxCall Call `json:"maxCall"` Players []Player `json:"players"` Round Round `json:"round"` + PrevRound Round `json:"previousRound"` Cards []CardName `json:"cards"` } From 5dd330119ea34f6038f721aaf83adb780c6f25c5 Mon Sep 17 00:00:00 2001 From: Daithi Hearn Date: Tue, 30 Jan 2024 23:32:35 +0100 Subject: [PATCH 2/2] test: unit test --- pkg/game/game_test.go | 23 ++++++++++++ pkg/game/testdata.go | 87 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/pkg/game/game_test.go b/pkg/game/game_test.go index 8f89367..7d2e596 100644 --- a/pkg/game/game_test.go +++ b/pkg/game/game_test.go @@ -151,6 +151,26 @@ func TestGame_GetState(t *testing.T) { Round: PlayingGame_RoundStart("3").CurrentRound, }, }, + { + name: "Game with completed rounds", + game: GameWithCompletedRounds(), + playerID: "1", + expectedState: State{ + ID: GameWithCompletedRounds().ID, + Revision: GameWithCompletedRounds().Revision, + Me: GameWithCompletedRounds().Players[0], + Cards: GameWithCompletedRounds().Players[0].Cards, + IamDealer: true, + IamGoer: true, + IamSpectator: false, + IsMyGo: false, + Status: GameWithCompletedRounds().Status, + MaxCall: 0, + Players: GameWithCompletedRounds().Players, + Round: GameWithCompletedRounds().CurrentRound, + PrevRound: GameWithCompletedRounds().Completed[len(GameWithCompletedRounds().Completed)-1], + }, + }, } for _, test := range tests { @@ -215,6 +235,9 @@ func TestGame_GetState(t *testing.T) { if state.Round.Number != test.expectedState.Round.Number { t.Errorf("expected Round Number to be %d, got %d", test.expectedState.Round.Number, state.Round.Number) } + if state.PrevRound.Number != test.expectedState.PrevRound.Number { + t.Errorf("expected PrevRound Number to be %d, got %d", test.expectedState.PrevRound.Number, state.PrevRound.Number) + } } }) } diff --git a/pkg/game/testdata.go b/pkg/game/testdata.go index 2214e8f..efd7a86 100644 --- a/pkg/game/testdata.go +++ b/pkg/game/testdata.go @@ -720,3 +720,90 @@ func CompletedGame() Game { AdminID: "1", } } + +func GameWithCompletedRounds() Game { + p1 := Player1() + p1.Score = 110 + p2 := Player2() + p2.Score = 90 + + return Game{ + ID: "2", + Name: "Test Game", + Status: Completed, + Timestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Players: []Player{p1, p2}, + AdminID: "1", + CurrentRound: Round{ + Number: 1, + DealerID: "1", + GoerID: "1", + Suit: Spades, + Status: Completed, + CompletedHands: []Hand{ + { + LeadOut: FIVE_SPADES, + CurrentPlayerID: "2", + PlayedCards: []PlayedCard{ + { + PlayerID: "1", + Card: FIVE_SPADES, + }, + { + PlayerID: "2", + Card: TEN_SPADES, + }, + }, + }, + }, + }, + Completed: []Round{ + { + Number: 1, + DealerID: "1", + GoerID: "1", + Suit: Spades, + Status: Completed, + CompletedHands: []Hand{ + { + LeadOut: FIVE_SPADES, + CurrentPlayerID: "2", + PlayedCards: []PlayedCard{ + { + PlayerID: "1", + Card: FIVE_SPADES, + }, + { + PlayerID: "2", + Card: TEN_SPADES, + }, + }, + }, + }, + }, + { + Number: 2, + DealerID: "1", + GoerID: "1", + Suit: Spades, + Status: Completed, + CompletedHands: []Hand{ + { + LeadOut: FIVE_SPADES, + CurrentPlayerID: "2", + PlayedCards: []PlayedCard{ + { + PlayerID: "1", + Card: FIVE_SPADES, + }, + { + PlayerID: "2", + Card: TEN_SPADES, + }, + }, + }, + }, + }, + }, + } +}