cards are now passed as PortableCard objects
client supports all actions A bunch of stuff uses *Decks now instead of []Card since json marshaling all of this code is absolutely awful
This commit is contained in:
parent
7c593faa51
commit
eb7b405641
2
Makefile
2
Makefile
@ -10,7 +10,7 @@ all: client server engine
|
|||||||
|
|
||||||
client: $(GAMEFILES) $(wildcard cmd/client/*.go)
|
client: $(GAMEFILES) $(wildcard cmd/client/*.go)
|
||||||
go build -ldflags "-X main.GitCommit=$(GIT_COMMIT)" ./cmd/client
|
go build -ldflags "-X main.GitCommit=$(GIT_COMMIT)" ./cmd/client
|
||||||
server: $(GAMEFILES) $(wildcard cmd/server/*.go)
|
server: $(GAMEFILES) $(COORDFILEs) $(wildcard cmd/server/*.go)
|
||||||
go build -ldflags "-X main.GitCommit=$(GIT_COMMIT)" ./cmd/server
|
go build -ldflags "-X main.GitCommit=$(GIT_COMMIT)" ./cmd/server
|
||||||
engine: $(GAMEFILES)
|
engine: $(GAMEFILES)
|
||||||
go build -ldflags "-X main.GitCommit=$(GIT_COMMIT)" ./cmd/engine
|
go build -ldflags "-X main.GitCommit=$(GIT_COMMIT)" ./cmd/engine
|
||||||
|
@ -1,20 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"git.saintnet.tech/stryan/snengame/internal/coordinator"
|
"git.saintnet.tech/stryan/snengame/internal/coordinator"
|
||||||
|
"git.saintnet.tech/stryan/snengame/internal/game"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var done chan interface{}
|
var done chan interface{}
|
||||||
var interrupt chan os.Signal
|
var interrupt chan os.Signal
|
||||||
|
var pid int
|
||||||
|
|
||||||
func receiveHandler(connection *websocket.Conn) {
|
func receiveHandler(connection *websocket.Conn) {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
@ -23,9 +27,23 @@ func receiveHandler(connection *websocket.Conn) {
|
|||||||
err := connection.ReadJSON(&resp)
|
err := connection.ReadJSON(&resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error in receive:", err)
|
log.Println("Error in receive:", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
log.Printf("Received: %s\n", resp)
|
if resp.Result == coordinator.SessionRespJoined1 {
|
||||||
|
pid = game.SentinalID
|
||||||
|
} else if resp.Result == coordinator.SessionRespJoined2 {
|
||||||
|
pid = game.ScourgeID
|
||||||
|
} else if resp.Result == coordinator.SessionRespPlayed {
|
||||||
|
if resp.GameResult != nil {
|
||||||
|
switch resp.GameResult.ResultType {
|
||||||
|
case game.ActCmd:
|
||||||
|
fmt.Println(resp.GameResult.ActionResult)
|
||||||
|
case game.StateCmd:
|
||||||
|
fmt.Println(resp.GameResult.StateResult)
|
||||||
|
case game.DebugCmd:
|
||||||
|
fmt.Println(resp.GameResult.DebugResult)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,11 +115,12 @@ func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand) {
|
|||||||
if t == -1 {
|
if t == -1 {
|
||||||
panic("quitting")
|
panic("quitting")
|
||||||
}
|
}
|
||||||
_, err = fmt.Scanf("%s", &cmd)
|
cmd, err = bufio.NewReader(os.Stdin).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
cmd = strings.TrimSpace(cmd)
|
cmd = strings.TrimSpace(cmd)
|
||||||
|
fmt.Println(cmd)
|
||||||
switch t {
|
switch t {
|
||||||
case 0:
|
case 0:
|
||||||
//session
|
//session
|
||||||
@ -124,6 +143,34 @@ func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand) {
|
|||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
case 1:
|
||||||
|
//state
|
||||||
|
resp <- coordinator.SessionCommand{
|
||||||
|
ID: uid,
|
||||||
|
Command: coordinator.SessionCmdPlay,
|
||||||
|
GameCommand: &game.Command{
|
||||||
|
PlayerID: pid,
|
||||||
|
Type: game.StateCmd,
|
||||||
|
Cmd: cmd,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
//action
|
||||||
|
resp <- coordinator.SessionCommand{
|
||||||
|
ID: uid,
|
||||||
|
Command: coordinator.SessionCmdPlay,
|
||||||
|
GameCommand: &game.Command{
|
||||||
|
PlayerID: pid,
|
||||||
|
Type: game.ActCmd,
|
||||||
|
Cmd: cmd,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimFirstRune(s string) string {
|
||||||
|
_, i := utf8.DecodeRuneInString(s)
|
||||||
|
return s[i:]
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,10 @@ func serveWs(c *Coordinator, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
log.Printf("Received: %s", cmd)
|
log.Printf("Received: %s", cmd)
|
||||||
resp := c.Coordinate(&cmd)
|
resp := c.Coordinate(&cmd)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
log.Printf("sending: %v", resp)
|
||||||
err = conn.WriteJSON(resp)
|
err = conn.WriteJSON(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error during message writing:", err)
|
log.Println("Error during message writing:", err)
|
||||||
|
@ -74,13 +74,14 @@ func (s *Session) Play(id uuid.UUID, cmd *game.Command) *game.CommandResult {
|
|||||||
if s.pMap[id] != cmd.PlayerID {
|
if s.pMap[id] != cmd.PlayerID {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return s.Game.Parse(cmd)
|
res := s.Game.Parse(cmd)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionCommand struct {
|
type SessionCommand struct {
|
||||||
ID uuid.UUID `json:"player_id"`
|
ID uuid.UUID `json:"player_id"`
|
||||||
Command SessionCmd `json:"command"`
|
Command SessionCmd `json:"command"`
|
||||||
GameCommand *game.Command `json:"game_command"`
|
GameCommand *game.Command `json:"game_command,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionCommand) String() string {
|
func (s *SessionCommand) String() string {
|
||||||
@ -90,7 +91,7 @@ func (s *SessionCommand) String() string {
|
|||||||
type SessionCommandResult struct {
|
type SessionCommandResult struct {
|
||||||
ID uuid.UUID `json:"player_id"`
|
ID uuid.UUID `json:"player_id"`
|
||||||
Result SessionResp `json:"result"`
|
Result SessionResp `json:"result"`
|
||||||
GameResult *game.CommandResult `json:"game_result"`
|
GameResult *game.CommandResult `json:"game_result,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionCommandResult) String() string {
|
func (s *SessionCommandResult) String() string {
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type Board struct {
|
type Board struct {
|
||||||
Sentinal [4]Card `json:"sentinal"`
|
Sentinal [4]Card `json:"sentinal"`
|
||||||
@ -9,8 +12,8 @@ type Board struct {
|
|||||||
|
|
||||||
func NewBoard() *Board {
|
func NewBoard() *Board {
|
||||||
return &Board{
|
return &Board{
|
||||||
Sentinal: [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)},
|
Sentinal: [4]Card{CreateCard(-1), CreateCard(-1), CreateCard(-1), CreateCard(-1)},
|
||||||
Scourge: [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)},
|
Scourge: [4]Card{CreateCard(-1), CreateCard(-1), CreateCard(-1), CreateCard(-1)},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +65,7 @@ func (b *Board) Move(id, src, dest int) bool {
|
|||||||
}
|
}
|
||||||
brd[dest].Act()
|
brd[dest].Act()
|
||||||
brd[dest] = brd[src]
|
brd[dest] = brd[src]
|
||||||
brd[src] = NewCard(-1)
|
brd[src] = CreateCard(-1)
|
||||||
if id == 1 {
|
if id == 1 {
|
||||||
b.Sentinal = brd
|
b.Sentinal = brd
|
||||||
} else {
|
} else {
|
||||||
@ -150,7 +153,7 @@ func (b *Board) Attack(id int, atk, def int) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if attacker > defender {
|
if attacker > defender {
|
||||||
dBrd[def] = NewCard(-1)
|
dBrd[def] = CreateCard(-1)
|
||||||
if id == 1 {
|
if id == 1 {
|
||||||
b.Sentinal = aBrd
|
b.Sentinal = aBrd
|
||||||
b.Scourge = dBrd
|
b.Scourge = dBrd
|
||||||
@ -161,8 +164,8 @@ func (b *Board) Attack(id int, atk, def int) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if attacker == defender {
|
if attacker == defender {
|
||||||
aBrd[atk] = NewCard(-1)
|
aBrd[atk] = CreateCard(-1)
|
||||||
dBrd[def] = NewCard(-1)
|
dBrd[def] = CreateCard(-1)
|
||||||
if id == 1 {
|
if id == 1 {
|
||||||
b.Sentinal = aBrd
|
b.Sentinal = aBrd
|
||||||
b.Scourge = dBrd
|
b.Scourge = dBrd
|
||||||
@ -175,3 +178,23 @@ func (b *Board) Attack(id int, atk, def int) int {
|
|||||||
return -1
|
return -1
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Board) MarshalJSON() ([]byte, error) {
|
||||||
|
res := [][]*PortableCard{{b.Sentinal[0].Port(), b.Sentinal[1].Port(), b.Sentinal[2].Port(), b.Sentinal[3].Port()}, {b.Scourge[0].Port(), b.Scourge[1].Port(), b.Scourge[2].Port(), b.Scourge[3].Port()}}
|
||||||
|
return json.Marshal(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Board) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
var ported [][]PortableCard
|
||||||
|
err = json.Unmarshal(data, &ported)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
b.Sentinal[i] = NewCard(ported[0][i].Type, ported[0][i].ID)
|
||||||
|
}
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
b.Scourge[i] = NewCard(ported[1][i].Type, ported[0][i].ID)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type Card interface {
|
type Card interface {
|
||||||
Cast(g *Game) *Game
|
Cast(g *Game) *Game
|
||||||
@ -13,11 +17,13 @@ type Card interface {
|
|||||||
Empty() bool
|
Empty() bool
|
||||||
String() string
|
String() string
|
||||||
CanAttack(int, int) bool
|
CanAttack(int, int) bool
|
||||||
|
Port() *PortableCard
|
||||||
}
|
}
|
||||||
|
|
||||||
type GenericCard struct {
|
type GenericCard struct {
|
||||||
Val CardValue
|
Val CardValue `json:"value"`
|
||||||
Sick bool
|
Id uuid.UUID `json:"id"`
|
||||||
|
Sick bool `json:"sick"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GenericCard) Cast(game *Game) *Game {
|
func (g *GenericCard) Cast(game *Game) *Game {
|
||||||
@ -55,6 +61,13 @@ func (g *GenericCard) String() string {
|
|||||||
return fmt.Sprintf("%v", g.Val)
|
return fmt.Sprintf("%v", g.Val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GenericCard) Port() *PortableCard {
|
||||||
|
return &PortableCard{
|
||||||
|
Type: int(g.Val),
|
||||||
|
ID: g.Id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GenericCard) CanAttack(x, y int) bool {
|
func (g *GenericCard) CanAttack(x, y int) bool {
|
||||||
if x == y && !g.Sick {
|
if x == y && !g.Sick {
|
||||||
return true
|
return true
|
||||||
@ -86,12 +99,17 @@ func (c CardValue) String() string {
|
|||||||
return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c]
|
return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCard(v int) Card {
|
func CreateCard(v int) Card {
|
||||||
|
return NewCard(v, uuid.New())
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCard(v int, id uuid.UUID) Card {
|
||||||
switch v {
|
switch v {
|
||||||
case -1:
|
case -1:
|
||||||
return &EmptyCard{
|
return &EmptyCard{
|
||||||
&GenericCard{
|
&GenericCard{
|
||||||
Val: EmptyValue,
|
Val: EmptyValue,
|
||||||
|
Id: id,
|
||||||
Sick: true,
|
Sick: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -99,6 +117,7 @@ func NewCard(v int) Card {
|
|||||||
return &AceCard{
|
return &AceCard{
|
||||||
&GenericCard{
|
&GenericCard{
|
||||||
Val: Ace,
|
Val: Ace,
|
||||||
|
Id: id,
|
||||||
Sick: false,
|
Sick: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -106,6 +125,7 @@ func NewCard(v int) Card {
|
|||||||
return &FourCard{
|
return &FourCard{
|
||||||
&GenericCard{
|
&GenericCard{
|
||||||
Val: Four,
|
Val: Four,
|
||||||
|
Id: id,
|
||||||
Sick: true,
|
Sick: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -113,6 +133,7 @@ func NewCard(v int) Card {
|
|||||||
return &EightCard{
|
return &EightCard{
|
||||||
&GenericCard{
|
&GenericCard{
|
||||||
Val: Eight,
|
Val: Eight,
|
||||||
|
Id: id,
|
||||||
Sick: true,
|
Sick: true,
|
||||||
},
|
},
|
||||||
0,
|
0,
|
||||||
@ -121,12 +142,14 @@ func NewCard(v int) Card {
|
|||||||
return &Valkyrie{
|
return &Valkyrie{
|
||||||
&GenericCard{
|
&GenericCard{
|
||||||
Val: Valk,
|
Val: Valk,
|
||||||
|
Id: id,
|
||||||
Sick: false,
|
Sick: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return &GenericCard{
|
return &GenericCard{
|
||||||
Val: CardValue(v),
|
Val: CardValue(v),
|
||||||
|
Id: id,
|
||||||
Sick: true,
|
Sick: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,7 +178,7 @@ func (f *FourCard) Enters(g *Game) *Game {
|
|||||||
|
|
||||||
type EightCard struct {
|
type EightCard struct {
|
||||||
*GenericCard
|
*GenericCard
|
||||||
Counters int
|
Counters int `json:"counters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EightCard) CanAttack(x, y int) bool {
|
func (e *EightCard) CanAttack(x, y int) bool {
|
||||||
@ -179,8 +202,8 @@ type Valkyrie struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *Valkyrie) Cast(g *Game) *Game {
|
func (v *Valkyrie) Cast(g *Game) *Game {
|
||||||
g.GameBoard.Sentinal = [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)}
|
g.GameBoard.Sentinal = [4]Card{CreateCard(-1), CreateCard(-1), CreateCard(-1), CreateCard(-1)}
|
||||||
g.GameBoard.Scourge = [4]Card{NewCard(-1), NewCard(-1), NewCard(-1), NewCard(-1)}
|
g.GameBoard.Scourge = [4]Card{CreateCard(-1), CreateCard(-1), CreateCard(-1), CreateCard(-1)}
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,3 +211,8 @@ func (v *Valkyrie) Enters(g *Game) *Game {
|
|||||||
v = nil
|
v = nil
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PortableCard struct {
|
||||||
|
Type int `json:"type"`
|
||||||
|
ID uuid.UUID `json:"ID"`
|
||||||
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type CmdType string
|
type CmdType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -14,10 +16,18 @@ type Command struct {
|
|||||||
Cmd string `json:"cmd"`
|
Cmd string `json:"cmd"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) String() string {
|
||||||
|
return fmt.Sprintf("%v %v %v", c.PlayerID, c.Type, c.Cmd)
|
||||||
|
}
|
||||||
|
|
||||||
type CommandResult struct {
|
type CommandResult struct {
|
||||||
PlayerID int `json:"player_id"`
|
PlayerID int `json:"player_id"`
|
||||||
ResultType CmdType `json:"result_type"`
|
ResultType CmdType `json:"result_type"`
|
||||||
StateResult *GameView `json:"state_result"`
|
StateResult *GameView `json:"state_result,omitempty"`
|
||||||
ActionResult []Card `json:"action_result"`
|
ActionResult *Deck `json:"action_result,omitempty"`
|
||||||
DebugResult *Game `json:"debug_result"`
|
DebugResult *Game `json:"debug_result,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommandResult) String() string {
|
||||||
|
return fmt.Sprintf("%v %v\nState Result: %v\nAction Result: %v\nDebug: %v\n", c.PlayerID, c.ResultType, c.StateResult, c.ActionResult, c.DebugResult)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -10,10 +11,10 @@ type Deck struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewDeck() *Deck {
|
func NewDeck() *Deck {
|
||||||
cards := []Card{NewCard(0)}
|
cards := []Card{CreateCard(0)}
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
for j := 1; j < 9; j++ {
|
for j := 1; j < 9; j++ {
|
||||||
cards = append(cards, Card(NewCard(j)))
|
cards = append(cards, Card(CreateCard(j)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &Deck{
|
return &Deck{
|
||||||
@ -21,6 +22,12 @@ func NewDeck() *Deck {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeckFromCards(c []Card) *Deck {
|
||||||
|
return &Deck{
|
||||||
|
Cards: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Deck) Shuffle() {
|
func (d *Deck) Shuffle() {
|
||||||
cards := d.Cards
|
cards := d.Cards
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
@ -50,3 +57,25 @@ func (d *Deck) Bottom(result []Card) {
|
|||||||
func (d *Deck) Size() int {
|
func (d *Deck) Size() int {
|
||||||
return len(d.Cards)
|
return len(d.Cards)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Deck) MarshalJSON() ([]byte, error) {
|
||||||
|
var ported []*PortableCard
|
||||||
|
for i, _ := range d.Cards {
|
||||||
|
ported = append(ported, d.Cards[i].Port())
|
||||||
|
}
|
||||||
|
return json.Marshal(ported)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Deck) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
var ported []PortableCard
|
||||||
|
err = json.Unmarshal(data, &ported)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cards := []Card{}
|
||||||
|
for _, v := range ported {
|
||||||
|
cards = append(cards, NewCard(v.Type, v.ID))
|
||||||
|
}
|
||||||
|
d.Cards = cards
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@ -34,7 +34,7 @@ type Game struct {
|
|||||||
SentinalDeck *Deck
|
SentinalDeck *Deck
|
||||||
ScourgeDeck *Deck
|
ScourgeDeck *Deck
|
||||||
CurrentTurn int
|
CurrentTurn int
|
||||||
CardBuffer []Card
|
CardBuffer *Deck
|
||||||
CanDraw bool
|
CanDraw bool
|
||||||
HasDrawn bool
|
HasDrawn bool
|
||||||
Status GameStatus
|
Status GameStatus
|
||||||
@ -52,7 +52,7 @@ func NewGame() *Game {
|
|||||||
SentinalDeck: deckA,
|
SentinalDeck: deckA,
|
||||||
ScourgeDeck: deckB,
|
ScourgeDeck: deckB,
|
||||||
CurrentTurn: 0, //start with no turn
|
CurrentTurn: 0, //start with no turn
|
||||||
CardBuffer: []Card{},
|
CardBuffer: DeckFromCards([]Card{}),
|
||||||
CanDraw: false,
|
CanDraw: false,
|
||||||
HasDrawn: false,
|
HasDrawn: false,
|
||||||
Status: StatusLobby,
|
Status: StatusLobby,
|
||||||
@ -65,7 +65,7 @@ func (g *Game) String() string {
|
|||||||
|
|
||||||
func (g *Game) Parse(cmd *Command) *CommandResult {
|
func (g *Game) Parse(cmd *Command) *CommandResult {
|
||||||
|
|
||||||
if g.Status != StatusLobby || g.Status != StatusReady || g.Status != StatusPlaying {
|
if g.Status == StatusDraw || g.Status == StatusScourgeWin || g.Status == StatusSentinalWin || g.Status == StatusStop {
|
||||||
return &CommandResult{
|
return &CommandResult{
|
||||||
PlayerID: cmd.PlayerID,
|
PlayerID: cmd.PlayerID,
|
||||||
ResultType: cmd.Type,
|
ResultType: cmd.Type,
|
||||||
@ -76,17 +76,23 @@ func (g *Game) Parse(cmd *Command) *CommandResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var state_res *GameView
|
var state_res *GameView
|
||||||
var action_res []Card
|
var action_res *Deck
|
||||||
var debug_res *Game
|
var debug_res *Game
|
||||||
var res_type CmdType
|
var res_type CmdType
|
||||||
|
|
||||||
if cmd.Type == ActCmd {
|
if cmd.Type == ActCmd {
|
||||||
action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd)
|
action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd)
|
||||||
|
state_res = nil
|
||||||
|
debug_res = nil
|
||||||
res_type = ActCmd
|
res_type = ActCmd
|
||||||
} else if cmd.Type == StateCmd {
|
} else if cmd.Type == StateCmd {
|
||||||
state_res = g.PlayerStateAct(cmd.PlayerID, cmd.Cmd)
|
state_res = g.PlayerStateAct(cmd.PlayerID, cmd.Cmd)
|
||||||
|
action_res = nil
|
||||||
|
debug_res = nil
|
||||||
res_type = StateCmd
|
res_type = StateCmd
|
||||||
} else {
|
} else {
|
||||||
|
state_res = nil
|
||||||
|
action_res = nil
|
||||||
debug_res = g
|
debug_res = g
|
||||||
res_type = DebugCmd
|
res_type = DebugCmd
|
||||||
}
|
}
|
||||||
@ -168,7 +174,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *GameView {
|
|||||||
if id != g.CurrentTurn {
|
if id != g.CurrentTurn {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
g.CardBuffer = []Card{}
|
g.CardBuffer = DeckFromCards([]Card{})
|
||||||
if id == SentinalID {
|
if id == SentinalID {
|
||||||
for _, v := range g.GameBoard.Sentinal {
|
for _, v := range g.GameBoard.Sentinal {
|
||||||
v.Endstep(g)
|
v.Endstep(g)
|
||||||
@ -187,7 +193,7 @@ func (g *Game) PlayerStateAct(id int, cmd string) *GameView {
|
|||||||
return NewView(id, g)
|
return NewView(id, g)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) PlayerAct(id int, cmd string) []Card {
|
func (g *Game) PlayerAct(id int, cmd string) *Deck {
|
||||||
if id != g.CurrentTurn {
|
if id != g.CurrentTurn {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -210,10 +216,10 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
|
|||||||
switch cmd_s[0] {
|
switch cmd_s[0] {
|
||||||
case "s":
|
case "s":
|
||||||
//scry: return scry options off top of deck
|
//scry: return scry options off top of deck
|
||||||
if !g.CanDraw || len(g.CardBuffer) > 0 {
|
if !g.CanDraw || g.CardBuffer.Size() > 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
g.CardBuffer = currD.Scry(curr.Life)
|
g.CardBuffer = DeckFromCards(currD.Scry(curr.Life))
|
||||||
return g.CardBuffer
|
return g.CardBuffer
|
||||||
case "d":
|
case "d":
|
||||||
//draw: return player hand
|
//draw: return player hand
|
||||||
@ -227,18 +233,18 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
x := g.CardBuffer[x_i]
|
x := g.CardBuffer.Cards[x_i]
|
||||||
buf := g.CardBuffer
|
buf := g.CardBuffer
|
||||||
for i, v := range buf {
|
for i, v := range buf.Cards {
|
||||||
if v == x {
|
if v == x {
|
||||||
buf = append(buf[:i], buf[i+1:]...)
|
buf.Cards = append(buf.Cards[:i], buf.Cards[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
currD.Bottom(buf)
|
currD.Bottom(buf.Cards)
|
||||||
curr.Hand = append(curr.Hand, x)
|
curr.Hand = append(curr.Hand, x)
|
||||||
g.CanDraw = false
|
g.CanDraw = false
|
||||||
g.HasDrawn = true
|
g.HasDrawn = true
|
||||||
return curr.Hand
|
return DeckFromCards(curr.Hand)
|
||||||
|
|
||||||
case "m":
|
case "m":
|
||||||
//move: return player board or [] if invalid
|
//move: return player board or [] if invalid
|
||||||
@ -252,9 +258,9 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
|
|||||||
y_i, _ := strconv.Atoi(cmd_s[2])
|
y_i, _ := strconv.Atoi(cmd_s[2])
|
||||||
res := g.GameBoard.Move(g.CurrentTurn, x_i, y_i)
|
res := g.GameBoard.Move(g.CurrentTurn, x_i, y_i)
|
||||||
if res {
|
if res {
|
||||||
return g.GameBoard.GetRow(g.CurrentTurn)
|
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
|
||||||
} else {
|
} else {
|
||||||
return []Card{}
|
return DeckFromCards([]Card{})
|
||||||
}
|
}
|
||||||
case "a":
|
case "a":
|
||||||
//attack
|
//attack
|
||||||
@ -269,12 +275,12 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
|
|||||||
res := g.GameBoard.Attack(g.CurrentTurn, x_i, y_i)
|
res := g.GameBoard.Attack(g.CurrentTurn, x_i, y_i)
|
||||||
if res == 1 {
|
if res == 1 {
|
||||||
opp.Life = opp.Life - 1
|
opp.Life = opp.Life - 1
|
||||||
return g.GameBoard.GetRow(g.CurrentTurn)
|
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
|
||||||
} else if res == 0 {
|
} else if res == 0 {
|
||||||
return g.GameBoard.GetRow(g.CurrentTurn)
|
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("can't attack")
|
fmt.Println("can't attack")
|
||||||
return []Card{}
|
return DeckFromCards([]Card{})
|
||||||
}
|
}
|
||||||
case "p":
|
case "p":
|
||||||
//play: return player boad or [] if invalid
|
//play: return player boad or [] if invalid
|
||||||
@ -295,10 +301,10 @@ func (g *Game) PlayerAct(id int, cmd string) []Card {
|
|||||||
if res {
|
if res {
|
||||||
curr.Hand[x_i].Enters(g)
|
curr.Hand[x_i].Enters(g)
|
||||||
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
|
curr.Hand = append(curr.Hand[:x_i], curr.Hand[x_i+1:]...)
|
||||||
return g.GameBoard.GetRow(g.CurrentTurn)
|
return DeckFromCards(g.GameBoard.GetRow(g.CurrentTurn))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("couldn't play")
|
fmt.Println("couldn't play")
|
||||||
return []Card{}
|
return DeckFromCards([]Card{})
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Println("Invalid act command")
|
fmt.Println("Invalid act command")
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
@ -15,3 +23,44 @@ func NewPlayer(name string, id int) *Player {
|
|||||||
Life: 3,
|
Life: 3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Player) MarshalJSON() ([]byte, error) {
|
||||||
|
var ported_hand []*PortableCard
|
||||||
|
for i, _ := range p.Hand {
|
||||||
|
ported_hand = append(ported_hand, p.Hand[i].Port())
|
||||||
|
}
|
||||||
|
res := []interface{}{p.Name, p.Id, ported_hand, p.Life}
|
||||||
|
return json.Marshal(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) UnmarshalJSON(data []byte) (err error) {
|
||||||
|
ported := []interface{}{}
|
||||||
|
err = json.Unmarshal(data, &ported)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(ported)
|
||||||
|
p.Name = ported[0].(string)
|
||||||
|
p.Id = int(ported[1].(float64))
|
||||||
|
var tmp []interface{}
|
||||||
|
if ported[2] != nil {
|
||||||
|
tmp = ported[2].([]interface{})
|
||||||
|
} else {
|
||||||
|
tmp = []interface{}{}
|
||||||
|
}
|
||||||
|
p.Life = int(ported[3].(float64))
|
||||||
|
hand := []Card{}
|
||||||
|
for _, v := range tmp {
|
||||||
|
m := v.(map[string]interface{})
|
||||||
|
t := int(m["type"].(float64))
|
||||||
|
i := m["ID"].(string)
|
||||||
|
uid, err := uuid.Parse(i)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("invalid card parse")
|
||||||
|
} else {
|
||||||
|
hand = append(hand, NewCard(t, uid))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.Hand = hand
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user