Skip to content

Commit

Permalink
Merge pull request #8 from marianogappa/streamline-state
Browse files Browse the repository at this point in the history
Streamline state
  • Loading branch information
marianogappa authored Jun 26, 2024
2 parents 91c4cab + 879f4a7 commit 21f7d25
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 247 deletions.
60 changes: 33 additions & 27 deletions exampleclient/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (u *ui) play(playerID int, gameState truco.GameState) (truco.Action, error)
return nil, err
}

if gameState.IsEnded {
if gameState.IsGameEnded {
return nil, nil
}

Expand Down Expand Up @@ -76,11 +76,13 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {
mx, my = termbox.Size()
you = playerID
them = state.OpponentOf(you)
hand = *state.Hands[them]
hand = *state.Players[them].Hand
)

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 @@ -101,13 +103,15 @@ func (u *ui) render(playerID int, state truco.GameState, mode printMode) error {
themMano = " (mano)"
}

printUpToAt(mx-1, 1, fmt.Sprintf("Vos%v %v", youMano, spanishScore(state.Scores[you])))
printUpToAt(mx-1, 2, fmt.Sprintf("Elle%v %v", themMano, spanishScore(state.Scores[them])))
printUpToAt(mx-1, 1, fmt.Sprintf("Vos%v %v", youMano, spanishScore(state.Players[you].Score)))
printUpToAt(mx-1, 2, fmt.Sprintf("Elle%v %v", themMano, spanishScore(state.Players[them].Score)))

hand = *state.Hands[you]
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.RoundJustStarted {
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
2 changes: 1 addition & 1 deletion exampleclient/websocket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Player(playerID int, address string) {
log.Fatal(err)
}

if gameState.IsEnded {
if gameState.IsGameEnded {
_ = ui.render(playerID, *gameState, PRINT_MODE_END)
return
}
Expand Down
22 changes: 12 additions & 10 deletions truco/action_any_quiero.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ type ActionSayTrucoQuiero struct{ act }
type ActionSayTrucoNoQuiero struct{ act }

func (a ActionSayEnvidoNoQuiero) IsPossible(g GameState) bool {
if g.EnvidoFinished {
if g.IsEnvidoFinished {
return false
}
return g.EnvidoSequence.CanAddStep(a.GetName())
}

func (a ActionSayEnvidoQuiero) IsPossible(g GameState) bool {
if g.EnvidoFinished {
if g.IsEnvidoFinished {
return false
}
return g.EnvidoSequence.CanAddStep(a.GetName())
Expand Down Expand Up @@ -53,12 +53,14 @@ func (a ActionSayEnvidoNoQuiero) Run(g *GameState) error {
return errActionNotPossible
}
g.EnvidoSequence.AddStep(a.GetName())
g.EnvidoFinished = true
cost, err := g.EnvidoSequence.Cost(g.CurrentPlayerScore(), g.OpponentPlayerScore())
g.IsEnvidoFinished = true
cost, err := g.EnvidoSequence.Cost(g.Players[g.TurnPlayerID].Score, g.Players[g.TurnOpponentPlayerID].Score)
if err != nil {
return err
}
g.Scores[g.OpponentPlayerID()] += cost
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 @@ -84,14 +86,14 @@ func (a ActionSayTrucoNoQuiero) Run(g *GameState) error {
return errActionNotPossible
}
g.TrucoSequence.AddStep(a.GetName())
g.RoundFinished = true
g.IsRoundFinished = true
cost, err := g.TrucoSequence.Cost()
if err != nil {
return err
}
g.CurrentRoundResult.TrucoPoints = cost
g.CurrentRoundResult.TrucoWinnerPlayerID = g.OpponentPlayerID()
g.Scores[g.OpponentPlayerID()] += cost
g.RoundsLog[g.RoundNumber].TrucoPoints = cost
g.RoundsLog[g.RoundNumber].TrucoWinnerPlayerID = g.TurnOpponentPlayerID
g.Players[g.TurnOpponentPlayerID].Score += cost
return nil
}

Expand Down
18 changes: 9 additions & 9 deletions truco/action_me_voy_al_mazo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ type ActionSayMeVoyAlMazo struct {
}

func (a ActionSayMeVoyAlMazo) IsPossible(g GameState) bool {
if !g.EnvidoSequence.IsEmpty() && !g.EnvidoFinished && !g.EnvidoSequence.IsFinished() {
if !g.EnvidoSequence.IsEmpty() && !g.IsEnvidoFinished && !g.EnvidoSequence.IsFinished() {
return false
}
if g.EnvidoFinished && !g.TrucoSequence.IsEmpty() && !g.TrucoSequence.IsFinished() {
if g.IsEnvidoFinished && !g.TrucoSequence.IsEmpty() && !g.TrucoSequence.IsFinished() {
return false
}
return true
}

func (a ActionSayMeVoyAlMazo) Run(g *GameState) error {
var cost int
if g.TrucoSequence.IsEmpty() && g.EnvidoFinished {
if g.TrucoSequence.IsEmpty() && g.IsEnvidoFinished {
// Envido is finished, so either the envido cost was updated already, or it's zero
cost = 1
}
if g.EnvidoSequence.IsEmpty() && g.TrucoSequence.IsEmpty() && !g.EnvidoFinished {
if g.EnvidoSequence.IsEmpty() && g.TrucoSequence.IsEmpty() && !g.IsEnvidoFinished {
cost = 2
}
if g.EnvidoFinished && !g.TrucoSequence.IsEmpty() {
if g.IsEnvidoFinished && !g.TrucoSequence.IsEmpty() {
var err error
cost, err = g.TrucoSequence.Cost()
if err != nil {
return err
}
}
g.CurrentRoundResult.TrucoPoints = cost
g.CurrentRoundResult.TrucoWinnerPlayerID = g.OpponentPlayerID()
g.Scores[g.OpponentPlayerID()] += cost
g.RoundFinished = true
g.RoundsLog[g.RoundNumber].TrucoPoints = cost
g.RoundsLog[g.RoundNumber].TrucoWinnerPlayerID = g.TurnOpponentPlayerID
g.Players[g.TurnOpponentPlayerID].Score += cost
g.IsRoundFinished = true
return nil
}
20 changes: 10 additions & 10 deletions truco/action_reveal_card.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func NewActionRevealCard(card Card) ActionRevealCard {

func (a ActionRevealCard) IsPossible(g GameState) bool {
// If envido was said and it hasn't finished, then the card can't be revealed
if !g.EnvidoFinished && !g.EnvidoSequence.IsEmpty() && !g.EnvidoSequence.IsFinished() {
if !g.IsEnvidoFinished && !g.EnvidoSequence.IsEmpty() && !g.EnvidoSequence.IsFinished() {
return false
}

Expand All @@ -22,7 +22,7 @@ func (a ActionRevealCard) IsPossible(g GameState) bool {

step := CardRevealSequenceStep{
card: a.Card,
playerID: g.CurrentPlayerID(),
playerID: g.TurnPlayerID,
}

return g.CardRevealSequence.CanAddStep(step, g)
Expand All @@ -34,15 +34,15 @@ func (a ActionRevealCard) Run(g *GameState) error {
}
step := CardRevealSequenceStep{
card: a.Card,
playerID: g.CurrentPlayerID(),
playerID: g.TurnPlayerID,
}
g.CardRevealSequence.AddStep(step, *g)
err := g.Hands[g.CurrentPlayerID()].RevealCard(a.Card)
err := g.Players[g.TurnPlayerID].Hand.RevealCard(a.Card)
if err != nil {
return err
}
if g.CardRevealSequence.IsFinished() {
g.RoundFinished = true
g.IsRoundFinished = true

var score int

Expand All @@ -57,13 +57,13 @@ func (a ActionRevealCard) Run(g *GameState) error {
score = cost
}

g.Scores[g.CardRevealSequence.WinnerPlayerID()] += score
g.CurrentRoundResult.TrucoPoints = score
g.CurrentRoundResult.TrucoWinnerPlayerID = g.CardRevealSequence.WinnerPlayerID()
g.Players[g.CardRevealSequence.WinnerPlayerID()].Score += score
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.EnvidoFinished && len(g.Hands[g.TurnPlayerID].Revealed) >= 1 && len(g.Hands[g.OpponentOf(g.TurnPlayerID)].Revealed) >= 1 {
g.EnvidoFinished = true
if !g.IsEnvidoFinished && len(g.Players[g.TurnPlayerID].Hand.Revealed) >= 1 && len(g.Players[g.TurnOpponentPlayerID].Hand.Revealed) >= 1 {
g.IsEnvidoFinished = true
}
return nil
}
Expand Down
19 changes: 9 additions & 10 deletions truco/action_son_buenas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ type ActionSaySonBuenas struct {
}

func (a ActionSaySonBuenas) IsPossible(g GameState) bool {
if g.EnvidoFinished {
if g.IsEnvidoFinished {
return false
}

var (
mano = g.RoundTurnPlayerID
me = g.TurnPlayerID
other = g.OpponentOf(g.TurnPlayerID)
meScore = g.Hands[me].EnvidoScore()
otherScore = g.Hands[other].EnvidoScore()
other = g.TurnOpponentPlayerID
meScore = g.Players[me].Hand.EnvidoScore()
otherScore = g.Players[other].Hand.EnvidoScore()
)

// TODO: should I allow people to lose voluntarily?
Expand All @@ -35,15 +35,14 @@ func (a ActionSaySonBuenas) Run(g *GameState) error {
return errActionNotPossible
}
g.EnvidoSequence.AddStep(a.GetName())
cost, err := g.EnvidoSequence.Cost(g.CurrentPlayerScore(), g.OpponentPlayerScore())
cost, err := g.EnvidoSequence.Cost(g.Players[g.TurnPlayerID].Score, g.Players[g.TurnOpponentPlayerID].Score)
if err != nil {
return err
}
g.CurrentRoundResult.EnvidoPoints = cost
g.CurrentRoundResult.EnvidoWinnerPlayerID = g.OpponentOf(g.CurrentPlayerID())
g.EnvidoWinnerPlayerID = g.OpponentOf(g.CurrentPlayerID())
g.EnvidoFinished = true
g.Scores[g.OpponentPlayerID()] += cost
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
Loading

0 comments on commit 21f7d25

Please sign in to comment.