Skip to content

Commit

Permalink
refactored code around state and events
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Jan 27, 2024
1 parent db4cbf8 commit d40200b
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 227 deletions.
14 changes: 7 additions & 7 deletions app/cli/liars/board/bet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// addBet takes the value selected on the keyboard and adds it to the
// bet slice and screen.
func (b *Board) addBet(r rune) error {
if b.lastStatus.CurrentAcctID != b.accountID {
if b.lastState.CurrentAcctID != b.accountID {
return errors.New("not your turn")
}

Expand Down Expand Up @@ -37,7 +37,7 @@ func (b *Board) addBet(r rune) error {

// subBet removes a value from the bet slice and screen.
func (b *Board) subBet() error {
if b.lastStatus.CurrentAcctID != b.accountID {
if b.lastState.CurrentAcctID != b.accountID {
return errors.New("not your turn")
}

Expand Down Expand Up @@ -68,24 +68,24 @@ func (b *Board) subBet() error {

// enterBet is called to submit a bet.
func (b *Board) enterBet() error {
status, err := b.engine.QueryStatus(b.lastStatus.GameID)
state, err := b.engine.QueryState(b.lastState.GameID)
if err != nil {
return err
}

if status.Status != "playing" {
return errors.New("invalid status state: " + status.Status)
if state.Status != "playing" {
return errors.New("invalid status state: " + state.Status)
}

if status.CurrentAcctID != b.accountID {
if state.CurrentAcctID != b.accountID {
return errors.New("not your turn")
}

if len(b.bets) == 0 {
return errors.New("missing bet information")
}

if _, err = b.engine.Bet(b.lastStatus.GameID, len(b.bets), b.bets[0]); err != nil {
if _, err = b.engine.Bet(b.lastState.GameID, len(b.bets), b.bets[0]); err != nil {
return err
}

Expand Down
22 changes: 11 additions & 11 deletions app/cli/liars/board/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ var words = []string{"", "one's", "two's", "three's", "four's", "five's", "six's

// Board represents the game board and all its state.
type Board struct {
accountID common.Address
engine *engine.Engine
config engine.Config
screen tcell.Screen
style tcell.Style
bets []rune
messages []string
lastStatus engine.Status
modalUp bool
modalMsg string
modalFn func(r rune)
accountID common.Address
engine *engine.Engine
config engine.Config
screen tcell.Screen
style tcell.Style
bets []rune
messages []string
lastState engine.State
modalUp bool
modalMsg string
modalFn func(r rune)
}

// New contructs a game board and renders the board.
Expand Down
28 changes: 14 additions & 14 deletions app/cli/liars/board/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

// drawInit generates the initial game board and starts the event loop.
func (b *Board) drawInit(active bool) error {
var status engine.Status
if b.lastStatus.GameID != "" {
var state engine.State
if b.lastState.GameID != "" {
var err error
status, err = b.engine.QueryStatus(b.lastStatus.GameID)
state, err = b.engine.QueryState(b.lastState.GameID)
if err != nil {
return err
}
Expand Down Expand Up @@ -51,19 +51,19 @@ func (b *Board) drawInit(active bool) error {
b.screen.SetCursorStyle(tcell.CursorStyleBlinkingBlock)
b.print(betRowX, betRowY, " ")

b.drawBoard(status)
b.drawBoard(state)

return nil
}

// drawBoard display the status information.
func (b *Board) drawBoard(status engine.Status) {
func (b *Board) drawBoard(status engine.State) {
if status.GameID == "" {
return
}

// Save this status for modal support.
b.lastStatus = status
// Save this state for modal support.
b.lastState = status

// Print the current game status and round.
b.print(helpX+11, statusY-6, fmt.Sprintf("%-10s / %s", status.Status, status.GameID))
Expand All @@ -87,13 +87,6 @@ func (b *Board) drawBoard(status engine.Status) {

var pot float64

// Clear the player lines.
for i := 0; i < 5; i++ {
addrY := columnHeight + 1 + i
b.print(playersX, addrY, fmt.Sprintf("%-*s", boardWidth-4, " "))
b.print(myDiceX, myDiceY, fmt.Sprintf("%-20s", " "))
}

// Print the player lines.
for i, cup := range status.Cups {
pot += status.AnteUSD
Expand Down Expand Up @@ -157,6 +150,13 @@ func (b *Board) drawBoard(status engine.Status) {
}
}

// Print any existing messages.
b.print(3, messageHeight+1, b.messages[0])
b.print(3, messageHeight+2, b.messages[1])
b.print(3, messageHeight+3, b.messages[2])
b.print(3, messageHeight+4, b.messages[3])
b.print(3, messageHeight+5, b.messages[4])

// Hide the cursor to show the game is over.
if status.Status == "gameover" {
b.screen.HideCursor()
Expand Down
22 changes: 11 additions & 11 deletions app/cli/liars/board/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func (b *Board) webEvents(event string, address common.Address) {
b.printMessage(message, true)
}

var status engine.Status
var state engine.State
var err error

switch event {
case "start":
status, err = b.engine.RollDice(b.lastStatus.GameID)
state, err = b.engine.RollDice(b.lastState.GameID)
if err != nil {
b.printMessage("error rolling dice", true)
}
Expand All @@ -34,12 +34,12 @@ func (b *Board) webEvents(event string, address common.Address) {
}

case "callliar":
status, err = b.modalWinnerLoser("*** WON ROUND ***", "*** LOST ROUND ***")
state, err = b.modalWinnerLoser("*** WON ROUND ***", "*** LOST ROUND ***")
if err != nil {
b.printMessage("winner/loser", true)
}

status, err = b.reconcile(status)
state, err = b.reconcile(state)
if err != nil {
b.printMessage(err.Error(), true)
}
Expand All @@ -49,19 +49,19 @@ func (b *Board) webEvents(event string, address common.Address) {
}

// If we don't have a new status, retrieve the latest.
if status.Status == "" {
status, err = b.engine.QueryStatus(b.lastStatus.GameID)
if state.Status == "" {
state, err = b.engine.QueryState(b.lastState.GameID)
if err != nil {
return
}
}

// Redraw the screen on any event to keep it up to date.
b.drawBoard(status)
b.drawBoard(state)
}

// reconcile the game the winner gets paid.
func (b *Board) reconcile(status engine.Status) (engine.Status, error) {
func (b *Board) reconcile(status engine.State) (engine.State, error) {
if status.Status != "gameover" {
return status, nil
}
Expand All @@ -70,10 +70,10 @@ func (b *Board) reconcile(status engine.Status) (engine.Status, error) {
return status, nil
}

newStatus, err := b.engine.Reconcile(status.GameID)
newState, err := b.engine.Reconcile(status.GameID)
if err != nil {
return engine.Status{}, err
return engine.State{}, err
}

return newStatus, nil
return newState, nil
}
38 changes: 19 additions & 19 deletions app/cli/liars/board/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ func (b *Board) value(r rune) error {

// newGame starts a new game.
func (b *Board) newGame() error {
status, err := b.engine.NewGame()
state, err := b.engine.NewGame()
if err != nil {
return err
}

b.lastStatus = status
b.lastState = state

b.drawInit(true)

Expand All @@ -120,7 +120,7 @@ func (b *Board) newGame() error {

// joinGame adds the account to the game.
func (b *Board) joinGame() error {
tables, err := b.engine.Tables(b.lastStatus.GameID)
tables, err := b.engine.Tables(b.lastState.GameID)
if err != nil {
return err
}
Expand All @@ -139,36 +139,36 @@ func (b *Board) joinGame() error {

gameID := tables.GameIDs[sel-1]

status, err := b.engine.QueryStatus(gameID)
state, err := b.engine.QueryState(gameID)
if err != nil {
b.closeModal()
b.showModal(err.Error())
return
}

for _, acct := range status.CupsOrder {
for _, acct := range state.CupsOrder {
if acct.Cmp(b.accountID) == 0 {
b.closeModal()
b.lastStatus = status
b.lastState = state
b.drawInit(true)
return
}
}

if status.Status != "newgame" {
if state.Status != "newgame" {
b.closeModal()
b.showModal(fmt.Sprintf("invalid status state: " + status.Status))
b.showModal(fmt.Sprintf("invalid status state: " + state.Status))
return
}

status, err = b.engine.JoinGame(gameID)
state, err = b.engine.JoinGame(gameID)
if err != nil {
b.closeModal()
b.showModal(err.Error())
return
}

b.lastStatus = status
b.lastState = state
}

b.showModalList(tables.GameIDs, fn)
Expand All @@ -178,16 +178,16 @@ func (b *Board) joinGame() error {

// startGame start the game so it can be played.
func (b *Board) startGame() error {
status, err := b.engine.QueryStatus(b.lastStatus.GameID)
state, err := b.engine.QueryState(b.lastState.GameID)
if err != nil {
return err
}

if status.Status != "newgame" {
return errors.New("invalid status state: " + status.Status)
if state.Status != "newgame" {
return errors.New("invalid status state: " + state.Status)
}

if _, err := b.engine.StartGame(b.lastStatus.GameID); err != nil {
if _, err := b.engine.StartGame(b.lastState.GameID); err != nil {
return err
}

Expand All @@ -196,20 +196,20 @@ func (b *Board) startGame() error {

// callLiar calls the last bet a lie.
func (b *Board) callLiar() error {
status, err := b.engine.QueryStatus(b.lastStatus.GameID)
state, err := b.engine.QueryState(b.lastState.GameID)
if err != nil {
return err
}

if status.Status != "playing" {
return errors.New("invalid status state: " + status.Status)
if state.Status != "playing" {
return errors.New("invalid status state: " + state.Status)
}

if status.CurrentAcctID != b.accountID {
if state.CurrentAcctID != b.accountID {
return errors.New("not your turn")
}

if _, err := b.engine.Liar(b.lastStatus.GameID); err != nil {
if _, err := b.engine.Liar(b.lastState.GameID); err != nil {
return err
}

Expand Down
14 changes: 7 additions & 7 deletions app/cli/liars/board/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

// modalWinnerLoser shows the user if they won or lost.
func (b *Board) modalWinnerLoser(win string, los string) (engine.Status, error) {
status, err := b.engine.QueryStatus(b.lastStatus.GameID)
func (b *Board) modalWinnerLoser(win string, los string) (engine.State, error) {
state, err := b.engine.QueryState(b.lastState.GameID)
if err != nil {
return engine.Status{}, err
return engine.State{}, err
}

if status.LastWinAcctID == b.accountID {
if state.LastWinAcctID == b.accountID {
b.showModal(win)
return status, nil
return state, nil
}
b.showModal(los)

return status, nil
return state, nil
}

// showModal displays a modal dialog box.
Expand Down Expand Up @@ -71,7 +71,7 @@ func (b *Board) closeModal() {
b.modalFn = nil

active := false
if b.lastStatus.CurrentAcctID == b.accountID {
if b.lastState.CurrentAcctID == b.accountID {
active = true
}

Expand Down
Loading

0 comments on commit d40200b

Please sign in to comment.