Skip to content

Commit

Permalink
Add validations for player hand (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaldengeki authored Sep 17, 2024
1 parent c0570ab commit a00eec5
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
64 changes: 63 additions & 1 deletion ark_nova_stats/game_server/game_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,62 @@ func (s *gameServer) ValidatePlayerSponsors(ctx context.Context, sponsorCards []
return []string{}
}

func (s *gameServer) ValidatePlayerHandCard(ctx context.Context, playerHandCard *player_game_state.PlayerHandCard, seenCards map[int64]int) []string {
if playerHandCard.GetAnimalCard() != nil {
card := playerHandCard.GetAnimalCard()

if card.Card.CardId < 1 {
return []string{"Card ID must be >= 1"}
}

if _, found := seenCards[card.Card.CardId]; found {
return []string{"Player has duplicate hand cards"}
} else {
seenCards[card.Card.CardId] = 1
}
} else if playerHandCard.GetSponsorCard() != nil {
card := playerHandCard.GetSponsorCard()

if card.Card.CardId < 1 {
return []string{"Card ID must be >= 1"}
}

if _, found := seenCards[card.Card.CardId]; found {
return []string{"Player has duplicate hand cards"}
} else {
seenCards[card.Card.CardId] = 1
}
} else if playerHandCard.GetEndgameScoringCard() != nil {
card := playerHandCard.GetEndgameScoringCard()

if card.Card.CardId < 1 {
return []string{"Card ID must be >= 1"}
}

if _, found := seenCards[card.Card.CardId]; found {
return []string{"Player has duplicate hand cards"}
} else {
seenCards[card.Card.CardId] = 1
}
} else {
return []string{"Hand card must have a Card object set"}
}

return []string{}
}

func (s *gameServer) ValidatePlayerHand(ctx context.Context, hand *player_game_state.PlayerHand) []string {
seenCards := map[int64]int{}

for _, card := range hand.Cards {
if errors := s.ValidatePlayerHandCard(ctx, card, seenCards); len(errors) > 0 {
return errors
}
}

return []string{}
}

func (s *gameServer) ValidatePlayerGameState(ctx context.Context, playerGameState *player_game_state.PlayerGameState) []string {
if playerGameState.PlayerId <= 0 {
return []string{"Player ID not set"}
Expand Down Expand Up @@ -340,7 +396,9 @@ func (s *gameServer) ValidatePlayerGameState(ctx context.Context, playerGameStat

// PlayerMap map = 12;

// PlayerHand hand = 13;
if errors := s.ValidatePlayerHand(ctx, playerGameState.Hand); len(errors) > 0 {
return errors
}

return []string{}
}
Expand Down Expand Up @@ -368,6 +426,10 @@ func (s *gameServer) ValidateState(ctx context.Context, request *server.Validate
return &server.ValidateStateResponse{ValidationErrors: []string{"At least one player game state must be passed"}}, nil
}

// TODO validations for:
// duplicates across player game states, i.e. animals, sponsors, hand cards, or unique buildings
// will probably require sharing global state and passing it in.

for _, playerGameState := range request.GameState.PlayerGameStates {
errs = s.ValidatePlayerGameState(ctx, playerGameState)
if len(errs) > 0 {
Expand Down
69 changes: 69 additions & 0 deletions ark_nova_stats/game_server/game_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,72 @@ func TestValidatePlayerSponsors_WithDuplicateSponsors_ReturnsError(t *testing.T)
t.Fatalf("Should result in a validation error, but got %v", res)
}
}

func TestValidatePlayerHand_WithNoCards_IsOK(t *testing.T) {
s := New(nil)

cards := []*player_game_state.PlayerHandCard{}

res := s.ValidatePlayerHand(nil, &player_game_state.PlayerHand{Cards: cards})
if len(res) > 0 {
t.Fatalf("Should result in no validation errors, but got %v", res)
}
}

func TestValidatePlayerHand_WhenCardIdNotSet_ReturnsError(t *testing.T) {
s := New(nil)

cards := []*player_game_state.PlayerHandCard{
&player_game_state.PlayerHandCard{
Card: &player_game_state.PlayerHandCard_AnimalCard{
AnimalCard: &cards.AnimalCard{
Card: &cards.Card{},
},
},
},
}

res := s.ValidatePlayerHand(nil, &player_game_state.PlayerHand{Cards: cards})
if len(res) < 1 {
t.Fatalf("Should result in a validation error, but got %v", res)
}
}

func TestValidatePlayerHand_WithDuplicateCards_ReturnsError(t *testing.T) {
s := New(nil)

cards := []*player_game_state.PlayerHandCard{
&player_game_state.PlayerHandCard{
Card: &player_game_state.PlayerHandCard_AnimalCard{
AnimalCard: &cards.AnimalCard{
Card: &cards.Card{
CardId: 1,
},
},
},
},
&player_game_state.PlayerHandCard{
Card: &player_game_state.PlayerHandCard_SponsorCard{
SponsorCard: &cards.SponsorCard{
Card: &cards.Card{
CardId: 2,
},
},
},
},
&player_game_state.PlayerHandCard{
Card: &player_game_state.PlayerHandCard_AnimalCard{
AnimalCard: &cards.AnimalCard{
Card: &cards.Card{
CardId: 1,
},
},
},
},
}

res := s.ValidatePlayerHand(nil, &player_game_state.PlayerHand{Cards: cards})
if len(res) < 1 {
t.Fatalf("Should result in a validation error, but got %v", res)
}
}

0 comments on commit a00eec5

Please sign in to comment.