use cmd and cmd_res, return views
This commit is contained in:
parent
b24a69ac7e
commit
b09d0b37f1
8
cmd.go
8
cmd.go
@ -12,3 +12,11 @@ type Command struct {
|
|||||||
Type CmdType `json:"type"`
|
Type CmdType `json:"type"`
|
||||||
Cmd string `json:"cmd"`
|
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"`
|
||||||
|
}
|
||||||
|
66
game.go
66
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)
|
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 {
|
switch cmd {
|
||||||
case "d":
|
case "d":
|
||||||
//debug game state
|
//debug game state
|
||||||
return g
|
return nil, g
|
||||||
case "g":
|
case "g":
|
||||||
//game state
|
//game state
|
||||||
if id == SentinalID {
|
return NewView(id, g), nil
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case "b":
|
case "b":
|
||||||
//begin game
|
//begin game
|
||||||
g.Status = StatusReady
|
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.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.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]
|
g.SentinalDeck.Cards = g.ScourgeDeck.Cards[0 : len(g.ScourgeDeck.Cards)-5]
|
||||||
return g
|
|
||||||
case "s":
|
case "s":
|
||||||
//start turn
|
//start turn
|
||||||
if g.Status == StatusReady { //first turn
|
if g.Status == StatusReady { //first turn
|
||||||
g.Status = StatusPlaying
|
g.Status = StatusPlaying
|
||||||
g.CurrentTurn = id
|
g.CurrentTurn = id
|
||||||
g.CanDraw = false
|
g.CanDraw = false
|
||||||
g.HasDrawn = false
|
g.HasDrawn = true
|
||||||
} else {
|
} else {
|
||||||
g.CanDraw = true
|
g.CanDraw = true
|
||||||
g.HasDrawn = false
|
g.HasDrawn = false
|
||||||
}
|
}
|
||||||
if id != g.CurrentTurn {
|
if id != g.CurrentTurn {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
if id == SentinalID {
|
if id == SentinalID {
|
||||||
for _, v := range g.GameBoard.Sentinal {
|
for _, v := range g.GameBoard.Sentinal {
|
||||||
@ -128,11 +123,10 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game {
|
|||||||
v.Upkeep(g)
|
v.Upkeep(g)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return g
|
|
||||||
case "e":
|
case "e":
|
||||||
//end turn and clean up
|
//end turn and clean up
|
||||||
if id != g.CurrentTurn {
|
if id != g.CurrentTurn {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
g.CardBuffer = []Card{}
|
g.CardBuffer = []Card{}
|
||||||
if id == SentinalID {
|
if id == SentinalID {
|
||||||
@ -149,10 +143,8 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game {
|
|||||||
} else {
|
} else {
|
||||||
g.CurrentTurn = SentinalID
|
g.CurrentTurn = SentinalID
|
||||||
}
|
}
|
||||||
return g
|
|
||||||
}
|
}
|
||||||
fmt.Println("invalid state command")
|
return NewView(id, g), nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) PlayerAct(id int, cmd string) []Card {
|
func (g *Game) PlayerAct(id int, cmd string) []Card {
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type GameView struct {
|
type GameView struct {
|
||||||
Board *Board `json:"board"`
|
Board *Board `json:"board"`
|
||||||
Player *Player `json:"player"`
|
Player *Player `json:"player"`
|
||||||
Deck *Deck `json:"deck"`
|
Deck *Deck `json:"deck"`
|
||||||
|
EnemyLife int `json:"enemy_life"`
|
||||||
EnemyDeckSize int `json:"enemy_deck_size"`
|
EnemyDeckSize int `json:"enemy_deck_size"`
|
||||||
EnemyHandSize int `json:"enemy_hand_size"`
|
EnemyHandSize int `json:"enemy_hand_size"`
|
||||||
CurrentTurn int `json:"current_turn"`
|
CurrentTurn int `json:"current_turn"`
|
||||||
@ -18,6 +21,7 @@ func NewView(id int, g *Game) *GameView {
|
|||||||
Board: g.GameBoard,
|
Board: g.GameBoard,
|
||||||
Player: g.SentinalPlayer,
|
Player: g.SentinalPlayer,
|
||||||
Deck: g.SentinalDeck,
|
Deck: g.SentinalDeck,
|
||||||
|
EnemyLife: g.ScourgePlayer.Life,
|
||||||
EnemyDeckSize: g.ScourgeDeck.Size(),
|
EnemyDeckSize: g.ScourgeDeck.Size(),
|
||||||
EnemyHandSize: len(g.ScourgePlayer.Hand),
|
EnemyHandSize: len(g.ScourgePlayer.Hand),
|
||||||
CurrentTurn: g.CurrentTurn,
|
CurrentTurn: g.CurrentTurn,
|
||||||
@ -30,6 +34,7 @@ func NewView(id int, g *Game) *GameView {
|
|||||||
Board: g.GameBoard,
|
Board: g.GameBoard,
|
||||||
Player: g.ScourgePlayer,
|
Player: g.ScourgePlayer,
|
||||||
Deck: g.ScourgeDeck,
|
Deck: g.ScourgeDeck,
|
||||||
|
EnemyLife: g.SentinalPlayer.Life,
|
||||||
EnemyDeckSize: g.SentinalDeck.Size(),
|
EnemyDeckSize: g.SentinalDeck.Size(),
|
||||||
EnemyHandSize: len(g.SentinalPlayer.Hand),
|
EnemyHandSize: len(g.SentinalPlayer.Hand),
|
||||||
CurrentTurn: g.CurrentTurn,
|
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)
|
||||||
|
}
|
||||||
|
28
main.go
28
main.go
@ -22,9 +22,33 @@ func main() {
|
|||||||
cmd_raw, _ := reader.ReadString('\n')
|
cmd_raw, _ := reader.ReadString('\n')
|
||||||
cmd = strings.TrimSpace(cmd_raw)
|
cmd = strings.TrimSpace(cmd_raw)
|
||||||
if t == "s" {
|
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" {
|
} 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 {
|
} else {
|
||||||
fmt.Println("error parsing")
|
fmt.Println("error parsing")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user