Skip to content

Commit

Permalink
Streamline GameState.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Jun 26, 2024
1 parent fa7457e commit 879f4a7
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 98 deletions.
50 changes: 28 additions & 22 deletions exampleclient/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {
)

if mode == PRINT_MODE_SHOW_ROUND_RESULT {
hand = *state.HandsDealt[len(state.HandsDealt)-2][them]
// Note that RoundNumber has already been incremented
// so we need to get the previous round's hands.
hand = *state.RoundsLog[state.RoundNumber-1].HandsDealt[them]
}

var (
Expand All @@ -107,7 +109,9 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {
hand = *state.Players[you].Hand

if mode == PRINT_MODE_SHOW_ROUND_RESULT {
hand = *state.HandsDealt[len(state.HandsDealt)-2][you]
// Note that RoundNumber has already been incremented
// so we need to get the previous round's hands.
hand = *state.RoundsLog[state.RoundNumber-1].HandsDealt[you]
}

unrevealed = getCardsString(hand.Unrevealed, false, false)
Expand All @@ -125,27 +129,27 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {

printAt(0, my/2, lastActionString)
case PRINT_MODE_SHOW_ROUND_RESULT:
lastActionString, err := getActionString(state.Actions[len(state.Actions)-1], state.ActionOwnerPlayerIDs[len(state.ActionOwnerPlayerIDs)-1], you)
lastRoundLog := state.RoundsLog[state.RoundNumber-1]
lastActionString, err := getActionString(lastRoundLog.ActionsLog[len(lastRoundLog.ActionsLog)-1], you)
if err != nil {
return err
}

printAt(0, my/2, lastActionString)
lastRoundResult := state.RoundResults[len(state.RoundResults)-1]

envidoPart := "el envido no se jugó"
if lastRoundResult.EnvidoWinnerPlayerID != -1 {
if lastRoundLog.EnvidoWinnerPlayerID != -1 {
envidoWinner := "vos"
won := "ganaste"
if lastRoundResult.EnvidoWinnerPlayerID == them {
if lastRoundLog.EnvidoWinnerPlayerID == them {
envidoWinner = "elle"
won = "ganó"
}
envidoPart = fmt.Sprintf("%v %v %v puntos por el envido", envidoWinner, won, lastRoundResult.EnvidoPoints)
envidoPart = fmt.Sprintf("%v %v %v puntos por el envido", envidoWinner, won, lastRoundLog.EnvidoPoints)
}
trucoWinner := "vos"
won := "ganaste"
if lastRoundResult.TrucoWinnerPlayerID == them {
if lastRoundLog.TrucoWinnerPlayerID == them {
trucoWinner = "elle"
won = "ganó"
}
Expand All @@ -155,11 +159,12 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {
envidoPart,
trucoWinner,
won,
lastRoundResult.TrucoPoints,
lastRoundLog.TrucoPoints,
)
printAt(0, my/2+1, result)
case PRINT_MODE_END:
lastActionString, err := getActionString(state.Actions[len(state.Actions)-1], state.ActionOwnerPlayerIDs[len(state.ActionOwnerPlayerIDs)-1], you)
lastActionLog := state.RoundsLog[state.RoundNumber].ActionsLog[len(state.RoundsLog[state.RoundNumber].ActionsLog)-1]
lastActionString, err := getActionString(lastActionLog, you)
if err != nil {
return err
}
Expand All @@ -182,7 +187,7 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {
if mode == PRINT_MODE_NORMAL {
actionsString := ""
for i, action := range _deserializeActions(state.PossibleActions) {
action := spanishAction(action, state)
action := spanishAction(action)
actionsString += fmt.Sprintf("%d. %s ", i+1, action)
}
printAt(0, my-2, actionsString)
Expand Down Expand Up @@ -246,28 +251,29 @@ func suitEmoji(suit string) string {
}

func getLastActionString(playerID int, state truco.GameState) (string, error) {
if len(state.Actions) == 0 {
return "¡Empezó el juego!", nil
}
if state.IsRoundJustStarted {
actionsLog := state.RoundsLog[state.RoundNumber].ActionsLog

if len(actionsLog) == 0 {
if state.RoundNumber == 1 {
return "¡Empezó el juego!", nil
}
return "¡Empezó la mano!", nil
}

lastActionBs := state.Actions[len(state.Actions)-1]
lastActionOwnerPlayerID := state.ActionOwnerPlayerIDs[len(state.ActionOwnerPlayerIDs)-1]
return getActionString(lastActionBs, lastActionOwnerPlayerID, playerID)
lastActionLog := actionsLog[len(actionsLog)-1]
return getActionString(lastActionLog, playerID)
}

func getActionString(lastActionBs json.RawMessage, lastActionOwnerPlayerID int, playerID int) (string, error) {
lastAction, err := truco.DeserializeAction(lastActionBs)
func getActionString(log truco.ActionLog, playerID int) (string, error) {
lastAction, err := truco.DeserializeAction(log.Action)
if err != nil {
return "", err
}

said := "dijiste"
revealed := "tiraste"
who := "Vos"
if playerID != lastActionOwnerPlayerID {
if playerID != log.PlayerID {
who = "Elle"
said = "dijo"
revealed = "tiró"
Expand Down Expand Up @@ -370,7 +376,7 @@ func spanishScore(score int) string {
return fmt.Sprintf("%d buenas", score-14)
}

func spanishAction(action truco.Action, state truco.GameState) string {
func spanishAction(action truco.Action) string {
switch action.GetName() {
case truco.REVEAL_CARD:
_action := action.(*truco.ActionRevealCard)
Expand Down
8 changes: 5 additions & 3 deletions truco/action_any_quiero.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (a ActionSayEnvidoNoQuiero) Run(g *GameState) error {
if err != nil {
return err
}
g.RoundsLog[g.RoundNumber].EnvidoPoints = cost
g.RoundsLog[g.RoundNumber].EnvidoWinnerPlayerID = g.TurnOpponentPlayerID
g.Players[g.TurnOpponentPlayerID].Score += cost
return nil
}
Expand All @@ -75,7 +77,7 @@ func (a ActionSayTrucoQuiero) Run(g *GameState) error {
return errActionNotPossible
}
g.TrucoSequence.AddStep(a.GetName())
g.TrucoQuieroOwnerPlayerId = g.TurnPlayerID
g.TrucoSequence.QuieroOwnerPlayerID = g.TurnPlayerID
return nil
}

Expand All @@ -89,8 +91,8 @@ func (a ActionSayTrucoNoQuiero) Run(g *GameState) error {
if err != nil {
return err
}
g.CurrentRoundResult.TrucoPoints = cost
g.CurrentRoundResult.TrucoWinnerPlayerID = g.TurnOpponentPlayerID
g.RoundsLog[g.RoundNumber].TrucoPoints = cost
g.RoundsLog[g.RoundNumber].TrucoWinnerPlayerID = g.TurnOpponentPlayerID
g.Players[g.TurnOpponentPlayerID].Score += cost
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions truco/action_me_voy_al_mazo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (a ActionSayMeVoyAlMazo) Run(g *GameState) error {
return err
}
}
g.CurrentRoundResult.TrucoPoints = cost
g.CurrentRoundResult.TrucoWinnerPlayerID = g.TurnOpponentPlayerID
g.RoundsLog[g.RoundNumber].TrucoPoints = cost
g.RoundsLog[g.RoundNumber].TrucoWinnerPlayerID = g.TurnOpponentPlayerID
g.Players[g.TurnOpponentPlayerID].Score += cost
g.IsRoundFinished = true
return nil
Expand Down
4 changes: 2 additions & 2 deletions truco/action_reveal_card.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (a ActionRevealCard) Run(g *GameState) error {
}

g.Players[g.CardRevealSequence.WinnerPlayerID()].Score += score
g.CurrentRoundResult.TrucoPoints = score
g.CurrentRoundResult.TrucoWinnerPlayerID = g.CardRevealSequence.WinnerPlayerID()
g.RoundsLog[g.RoundNumber].TrucoPoints = score
g.RoundsLog[g.RoundNumber].TrucoWinnerPlayerID = g.CardRevealSequence.WinnerPlayerID()
}
// If both players have revealed a card, then envido cannot be played anymore
if !g.IsEnvidoFinished && len(g.Players[g.TurnPlayerID].Hand.Revealed) >= 1 && len(g.Players[g.TurnOpponentPlayerID].Hand.Revealed) >= 1 {
Expand Down
4 changes: 2 additions & 2 deletions truco/action_son_buenas.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (a ActionSaySonBuenas) Run(g *GameState) error {
if err != nil {
return err
}
g.CurrentRoundResult.EnvidoPoints = cost
g.CurrentRoundResult.EnvidoWinnerPlayerID = g.TurnOpponentPlayerID
g.RoundsLog[g.RoundNumber].EnvidoPoints = cost
g.RoundsLog[g.RoundNumber].EnvidoWinnerPlayerID = g.TurnOpponentPlayerID
g.IsEnvidoFinished = true
g.Players[g.TurnOpponentPlayerID].Score += cost
return nil
Expand Down
4 changes: 2 additions & 2 deletions truco/action_son_mejores.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (a ActionSaySonMejores) Run(g *GameState) error {
if err != nil {
return err
}
g.CurrentRoundResult.EnvidoPoints = cost
g.CurrentRoundResult.EnvidoWinnerPlayerID = g.TurnPlayerID
g.RoundsLog[g.RoundNumber].EnvidoPoints = cost
g.RoundsLog[g.RoundNumber].EnvidoWinnerPlayerID = g.TurnPlayerID
g.IsEnvidoFinished = true
g.Players[g.TurnPlayerID].Score += cost
return nil
Expand Down
9 changes: 6 additions & 3 deletions truco/actions_any_truco.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ func (g GameState) AnyTrucoActionIsPossible(a Action) bool {
}
// Only the player who said "quiero" last can raise the stakes, unless quiero hasn't been said yet,
// which can only happen if the last action is "truco"
if !g.IsLastActionOfName(SAY_TRUCO) && (a.GetName() == SAY_QUIERO_RETRUCO || a.GetName() == SAY_QUIERO_VALE_CUATRO) && g.TrucoQuieroOwnerPlayerId != g.TurnPlayerID {
if !g.IsLastActionOfName(SAY_TRUCO) &&
(a.GetName() == SAY_QUIERO_RETRUCO || a.GetName() == SAY_QUIERO_VALE_CUATRO) &&
g.TrucoSequence.QuieroOwnerPlayerID != g.TurnPlayerID {
return false
}
return g.TrucoSequence.CanAddStep(a.GetName())
}

func (g GameState) IsLastActionOfName(name string) bool {
if len(g.Actions) == 0 {
actionsLog := g.RoundsLog[g.RoundNumber].ActionsLog
if len(actionsLog) == 0 {
return false
}
lastActionBs := g.Actions[len(g.Actions)-1]
lastActionBs := actionsLog[len(actionsLog)-1].Action
lastAction, err := DeserializeAction(lastActionBs)
if err != nil {
return false
Expand Down
Loading

0 comments on commit 879f4a7

Please sign in to comment.