From 23af3b3eff9d5fcb0725dbc1b842705a7ff7f465 Mon Sep 17 00:00:00 2001 From: Mariano Gappa Date: Tue, 23 Jul 2024 19:59:37 +0100 Subject: [PATCH] Fix bug with awarding envido points. Small improvements. --- truco/actions_any_truco.go | 13 ------------- truco/truco.go | 12 ++++++++---- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/truco/actions_any_truco.go b/truco/actions_any_truco.go index 318bec5..0fee5b1 100644 --- a/truco/actions_any_truco.go +++ b/truco/actions_any_truco.go @@ -28,19 +28,6 @@ func (g GameState) AnyTrucoActionIsPossible(a Action) bool { return g.TrucoSequence.CanAddStep(a.GetName()) } -func (g GameState) IsLastActionOfName(name string) bool { - actionsLog := g.RoundsLog[g.RoundNumber].ActionsLog - if len(actionsLog) == 0 { - return false - } - lastActionBs := actionsLog[len(actionsLog)-1].Action - lastAction, err := DeserializeAction(lastActionBs) - if err != nil { - return false - } - return lastAction.GetName() == name -} - func (g *GameState) AnyTrucoActionRunAction(at Action) error { if !g.AnyTrucoActionIsPossible(at) { return errActionNotPossible diff --git a/truco/truco.go b/truco/truco.go index 7c15cf1..683ec39 100644 --- a/truco/truco.go +++ b/truco/truco.go @@ -68,7 +68,7 @@ type GameState struct { // a player reaches 30 points. IsGameEnded bool `json:"isGameEnded"` - // WinnerPlayerID is the player ID of the player who won the game. This is only set when `IsEnded` is + // WinnerPlayerID is the player ID of the player who won the game. This is only set when `IsGameEnded` is // `true`. Otherwise, it's -1. WinnerPlayerID int `json:"winnerPlayerID"` @@ -187,7 +187,7 @@ func (g *GameState) RunAction(action Action) error { } if g.IsGameEnded { - return errGameIsEnded + return fmt.Errorf("%w trying to run [%v]", errGameIsEnded, action) } if !g.IsRoundFinished && action.GetPlayerID() != g.TurnPlayerID { @@ -271,6 +271,9 @@ func (g *GameState) canAwardEnvidoPoints(revealedHand Hand) bool { if wonBy == -1 { return false } + if !g.EnvidoSequence.WasAccepted() { + return false + } if g.EnvidoSequence.EnvidoPointsAwarded { return false } @@ -426,7 +429,8 @@ func _serializeActions(as []Action) []json.RawMessage { } func _deserializeCurrentRoundLastAction(g GameState) Action { - a, _ := DeserializeAction(g.RoundsLog[g.RoundNumber].ActionsLog[len(g.RoundsLog[g.RoundNumber].ActionsLog)-1].Action) + lastAction := g.RoundsLog[g.RoundNumber].ActionsLog[len(g.RoundsLog[g.RoundNumber].ActionsLog)-1].Action + a, _ := DeserializeAction(lastAction) return a } @@ -527,7 +531,7 @@ type ClientGameState struct { IsRoundFinished bool `json:"isRoundFinished"` - // WinnerPlayerID is the player ID of the player who won the game. This is only set when `IsEnded` is + // WinnerPlayerID is the player ID of the player who won the game. This is only set when `IsGameEnded` is // `true`. Otherwise, it's -1. WinnerPlayerID int `json:"winnerPlayerID"`