diff --git a/board.go b/board.go index f681d73..aeaec99 100644 --- a/board.go +++ b/board.go @@ -3,8 +3,8 @@ package main import "fmt" type Board struct { - Sentinal [4]Card - Scourge [4]Card + Sentinal [4]Card `json:"sentinal"` + Scourge [4]Card `json:"scourge"` } func NewBoard() *Board { diff --git a/cmd.go b/cmd.go new file mode 100644 index 0000000..0db50e9 --- /dev/null +++ b/cmd.go @@ -0,0 +1,14 @@ +package main + +type CmdType string + +const ( + ActCmd = "a" + StateCmd = "s" +) + +type Command struct { + PlayerID int `json:"player_id"` + Type CmdType `json:"type"` + Cmd string `json:"cmd"` +} diff --git a/deck.go b/deck.go index c35f2dd..e4fc4d9 100644 --- a/deck.go +++ b/deck.go @@ -6,7 +6,7 @@ import ( ) type Deck struct { - Cards []Card + Cards []Card `json:"cards"` } func NewDeck() *Deck { @@ -46,3 +46,7 @@ func (d *Deck) Scry(s int) []Card { func (d *Deck) Bottom(result []Card) { d.Cards = append(result, d.Cards...) //Should shuffle result first? } + +func (d *Deck) Size() int { + return len(d.Cards) +} diff --git a/game.go b/game.go index 61a89d9..cfd72e0 100644 --- a/game.go +++ b/game.go @@ -13,6 +13,13 @@ const ( StatusReady StatusPlaying StatusStop + StatusSentinalWin + StatusScourgeWin +) + +const ( + SentinalID = 1 + ScourgeID = 2 ) func (g GameStatus) String() string { @@ -28,6 +35,7 @@ type Game struct { CurrentTurn int CardBuffer []Card CanDraw bool + HasDrawn bool Status GameStatus } @@ -38,13 +46,14 @@ func NewGame() *Game { deckB.Shuffle() return &Game{ GameBoard: NewBoard(), - SentinalPlayer: NewPlayer("Sentinal", 1), - ScourgePlayer: NewPlayer("Scourge", 2), + SentinalPlayer: NewPlayer("Sentinal", SentinalID), + ScourgePlayer: NewPlayer("Scourge", ScourgeID), SentinalDeck: deckA, ScourgeDeck: deckB, CurrentTurn: 0, //start with no turn CardBuffer: []Card{}, CanDraw: false, + HasDrawn: false, Status: StatusLobby, } } @@ -60,7 +69,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { return g case "g": //game state - if id == 1 { + if id == SentinalID { return &Game{ GameBoard: g.GameBoard, SentinalPlayer: g.SentinalPlayer, @@ -70,6 +79,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { CurrentTurn: g.CurrentTurn, CardBuffer: nil, CanDraw: g.CanDraw, + HasDrawn: g.HasDrawn, Status: g.Status, } } else { @@ -82,6 +92,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { CurrentTurn: g.CurrentTurn, CardBuffer: nil, CanDraw: g.CanDraw, + HasDrawn: g.HasDrawn, Status: g.Status, } } @@ -94,20 +105,21 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { 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 { + if g.Status == StatusReady { //first turn g.Status = StatusPlaying g.CurrentTurn = id g.CanDraw = false + g.HasDrawn = false } else { g.CanDraw = true + g.HasDrawn = false } if id != g.CurrentTurn { return nil } - if id == 1 { + if id == SentinalID { for _, v := range g.GameBoard.Sentinal { v.Upkeep(g) } @@ -123,7 +135,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { return nil } g.CardBuffer = []Card{} - if id == 1 { + if id == SentinalID { for _, v := range g.GameBoard.Sentinal { v.Endstep(g) } @@ -132,10 +144,10 @@ func (g *Game) PlayerStateAct(id int, cmd string) *Game { v.Endstep(g) } } - if g.CurrentTurn == 1 { - g.CurrentTurn = 2 + if g.CurrentTurn == SentinalID { + g.CurrentTurn = ScourgeID } else { - g.CurrentTurn = 1 + g.CurrentTurn = SentinalID } return g } @@ -193,6 +205,7 @@ func (g *Game) PlayerAct(id int, cmd string) []Card { currD.Bottom(buf) curr.Hand = append(curr.Hand, x) g.CanDraw = false + g.HasDrawn = true return curr.Hand case "m": @@ -200,6 +213,9 @@ func (g *Game) PlayerAct(id int, cmd string) []Card { if len(cmd_s) != 3 { return nil } + if !g.HasDrawn { + return nil + } x_i, _ := strconv.Atoi(cmd_s[1]) y_i, _ := strconv.Atoi(cmd_s[2]) res := g.GameBoard.Move(g.CurrentTurn, x_i, y_i) @@ -213,6 +229,9 @@ func (g *Game) PlayerAct(id int, cmd string) []Card { if len(cmd_s) != 3 { return nil } + if !g.HasDrawn { + return nil + } x_i, _ := strconv.Atoi(cmd_s[1]) y_i, _ := strconv.Atoi(cmd_s[2]) res := g.GameBoard.Attack(g.CurrentTurn, x_i, y_i) @@ -231,7 +250,9 @@ func (g *Game) PlayerAct(id int, cmd string) []Card { fmt.Println("not enough arguments") return nil } - fmt.Println("playing") + if !g.HasDrawn { + return nil + } x_i, _ := strconv.Atoi(cmd_s[1]) y_i, _ := strconv.Atoi(cmd_s[2]) card := curr.Hand[x_i] diff --git a/game_view.go b/game_view.go new file mode 100644 index 0000000..43855fc --- /dev/null +++ b/game_view.go @@ -0,0 +1,42 @@ +package main + +type GameView struct { + Board *Board `json:"board"` + Player *Player `json:"player"` + Deck *Deck `json:"deck"` + EnemyDeckSize int `json:"enemy_deck_size"` + EnemyHandSize int `json:"enemy_hand_size"` + CurrentTurn int `json:"current_turn"` + CanDraw bool `json:"can_draw"` + HasDrawn bool `json:"has_drawn"` + Status GameStatus `json:"game_status"` +} + +func NewView(id int, g *Game) *GameView { + if id == SentinalID { + return &GameView{ + Board: g.GameBoard, + Player: g.SentinalPlayer, + Deck: g.SentinalDeck, + EnemyDeckSize: g.ScourgeDeck.Size(), + EnemyHandSize: len(g.ScourgePlayer.Hand), + CurrentTurn: g.CurrentTurn, + CanDraw: g.CanDraw, + HasDrawn: g.HasDrawn, + Status: g.Status, + } + } else { + return &GameView{ + Board: g.GameBoard, + Player: g.ScourgePlayer, + Deck: g.ScourgeDeck, + EnemyDeckSize: g.SentinalDeck.Size(), + EnemyHandSize: len(g.SentinalPlayer.Hand), + CurrentTurn: g.CurrentTurn, + CanDraw: g.CanDraw, + HasDrawn: g.HasDrawn, + Status: g.Status, + } + + } +} diff --git a/player.go b/player.go index edc64b0..f2aed75 100644 --- a/player.go +++ b/player.go @@ -1,10 +1,10 @@ package main type Player struct { - Name string - Id int - Hand []Card - Life int + Name string `json:"name"` + Id int `json:"id"` + Hand []Card `json:"hand"` + Life int `json:"life"` } func NewPlayer(name string, id int) *Player {