Skip to content

Commit

Permalink
Merge pull request #26 from daithihearn/prevround
Browse files Browse the repository at this point in the history
feat: adding previous round
  • Loading branch information
daithihearn authored Jan 30, 2024
2 parents 77662cd + 5dd3301 commit 6e87a38
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/game/game-methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions pkg/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
23 changes: 23 additions & 0 deletions pkg/game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
})
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/game/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
},
}
}

0 comments on commit 6e87a38

Please sign in to comment.