From b09d0b37f1d14f8614ca5928b4f5e55f5ac47547 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 22 Jul 2021 13:09:56 -0400 Subject: [PATCH] use cmd and cmd_res, return views --- cmd.go | 8 +++++++ game.go | 66 +++++++++++++++++++++++----------------------------- game_view.go | 9 +++++++ main.go | 28 ++++++++++++++++++++-- 4 files changed, 72 insertions(+), 39 deletions(-) diff --git a/cmd.go b/cmd.go index 0db50e9..e73eb0c 100644 --- a/cmd.go +++ b/cmd.go @@ -12,3 +12,11 @@ type Command struct { Type CmdType `json:"type"` Cmd string `json:"cmd"` } + +type CommandResult struct { + PlayerID int `json:"player_id"` + ResultType CmdType `json:"result_type"` + StateResult *GameView `json:"state_result"` + ActionResult []Card `json:"action_result"` + DebugResult *Game `json:"debug_result"` +} diff --git a/game.go b/game.go index cfd72e0..0be03ea 100644 --- a/game.go +++ b/game.go @@ -62,40 +62,36 @@ func (g *Game) String() string { return fmt.Sprintf("Sen(%v): %v\n\n%v\n\nSco(%v): %v\nStatus:%v Draw:%v Turn:%v\n%v\n", g.SentinalPlayer.Life, g.SentinalPlayer.Hand, g.GameBoard, g.ScourgePlayer.Life, g.ScourgePlayer.Hand, g.Status, g.CanDraw, g.CurrentTurn, g.CardBuffer) } -func (g *Game) PlayerStateAct(id int, cmd string) *Game { +func (g *Game) Parse(cmd *Command) *CommandResult { + var state_res *GameView + var action_res []Card + var debug_res *Game + var res_type CmdType + + if cmd.Type == ActCmd { + action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd) + res_type = ActCmd + } else if cmd.Type == StateCmd { + state_res, debug_res = g.PlayerStateAct(cmd.PlayerID, cmd.Cmd) + res_type = StateCmd + } + return &CommandResult{ + PlayerID: cmd.PlayerID, + ResultType: res_type, + StateResult: state_res, + ActionResult: action_res, + DebugResult: debug_res, + } +} + +func (g *Game) PlayerStateAct(id int, cmd string) (*GameView, *Game) { switch cmd { case "d": //debug game state - return g + return nil, g case "g": //game state - if id == SentinalID { - return &Game{ - GameBoard: g.GameBoard, - SentinalPlayer: g.SentinalPlayer, - ScourgePlayer: nil, - SentinalDeck: g.SentinalDeck, - ScourgeDeck: nil, - CurrentTurn: g.CurrentTurn, - CardBuffer: nil, - CanDraw: g.CanDraw, - HasDrawn: g.HasDrawn, - Status: g.Status, - } - } else { - return &Game{ - GameBoard: g.GameBoard, - SentinalPlayer: nil, - ScourgePlayer: g.ScourgePlayer, - SentinalDeck: nil, - ScourgeDeck: g.ScourgeDeck, - CurrentTurn: g.CurrentTurn, - CardBuffer: nil, - CanDraw: g.CanDraw, - HasDrawn: g.HasDrawn, - Status: g.Status, - } - } + return NewView(id, g), nil case "b": //begin game g.Status = StatusReady @@ -104,20 +100,19 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { g.SentinalDeck.Cards = g.SentinalDeck.Cards[0 : len(g.SentinalDeck.Cards)-5] g.ScourgePlayer.Hand = g.ScourgeDeck.Cards[len(g.ScourgeDeck.Cards)-5 : len(g.ScourgeDeck.Cards)] g.SentinalDeck.Cards = g.ScourgeDeck.Cards[0 : len(g.ScourgeDeck.Cards)-5] - return g case "s": //start turn if g.Status == StatusReady { //first turn g.Status = StatusPlaying g.CurrentTurn = id g.CanDraw = false - g.HasDrawn = false + g.HasDrawn = true } else { g.CanDraw = true g.HasDrawn = false } if id != g.CurrentTurn { - return nil + return nil, nil } if id == SentinalID { for _, v := range g.GameBoard.Sentinal { @@ -128,11 +123,10 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { v.Upkeep(g) } } - return g case "e": //end turn and clean up if id != g.CurrentTurn { - return nil + return nil, nil } g.CardBuffer = []Card{} if id == SentinalID { @@ -149,10 +143,8 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { } else { g.CurrentTurn = SentinalID } - return g } - fmt.Println("invalid state command") - return nil + return NewView(id, g), nil } func (g *Game) PlayerAct(id int, cmd string) []Card { diff --git a/game_view.go b/game_view.go index 43855fc..eaa81d6 100644 --- a/game_view.go +++ b/game_view.go @@ -1,9 +1,12 @@ package main +import "fmt" + type GameView struct { Board *Board `json:"board"` Player *Player `json:"player"` Deck *Deck `json:"deck"` + EnemyLife int `json:"enemy_life"` EnemyDeckSize int `json:"enemy_deck_size"` EnemyHandSize int `json:"enemy_hand_size"` CurrentTurn int `json:"current_turn"` @@ -18,6 +21,7 @@ func NewView(id int, g *Game) *GameView { Board: g.GameBoard, Player: g.SentinalPlayer, Deck: g.SentinalDeck, + EnemyLife: g.ScourgePlayer.Life, EnemyDeckSize: g.ScourgeDeck.Size(), EnemyHandSize: len(g.ScourgePlayer.Hand), CurrentTurn: g.CurrentTurn, @@ -30,6 +34,7 @@ func NewView(id int, g *Game) *GameView { Board: g.GameBoard, Player: g.ScourgePlayer, Deck: g.ScourgeDeck, + EnemyLife: g.SentinalPlayer.Life, EnemyDeckSize: g.SentinalDeck.Size(), EnemyHandSize: len(g.SentinalPlayer.Hand), CurrentTurn: g.CurrentTurn, @@ -40,3 +45,7 @@ func NewView(id int, g *Game) *GameView { } } + +func (v *GameView) String() string { + return fmt.Sprintf("Enemy Life: %v Enemy Hand Size: %v Enemy DeckSize: %v\n\n%v\n\n%v\nYou Life: %v\nCT:%v CD: %v, HD %v, Status: %v\n", v.EnemyLife, v.EnemyHandSize, v.EnemyDeckSize, v.Board, v.Player.Hand, v.Player.Life, v.CurrentTurn, v.CanDraw, v.HasDrawn, v.Status) +} diff --git a/main.go b/main.go index fe08f5c..c4ce455 100644 --- a/main.go +++ b/main.go @@ -22,9 +22,33 @@ func main() { cmd_raw, _ := reader.ReadString('\n') cmd = strings.TrimSpace(cmd_raw) if t == "s" { - fmt.Println(g.PlayerStateAct(i, cmd)) + c := &Command{ + PlayerID: i, + Type: CmdType(t), + Cmd: cmd, + } + res := g.Parse(c) + if res.StateResult != nil { + fmt.Println(res.StateResult) + } else if res.DebugResult != nil { + fmt.Println(res.DebugResult) + } else if res.StateResult == nil && res.DebugResult == nil { + fmt.Println("invalid state command") + } else { + fmt.Println("uh oh") + } } else if t == "a" { - fmt.Println(g.PlayerAct(i, cmd)) + c := &Command{ + PlayerID: i, + Type: CmdType(t), + Cmd: cmd, + } + res := g.Parse(c) + if res.ActionResult != nil { + fmt.Println(res.ActionResult) + } else { + fmt.Println("invalid action command") + } } else { fmt.Println("error parsing") fmt.Println(err)