diff --git a/cmd/client/main.go b/cmd/client/main.go index 53d1be1..8a49c49 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -20,7 +20,7 @@ var done chan interface{} var interrupt chan os.Signal var pid int -func receiveHandler(connection *websocket.Conn) { +func receiveHandler(connection *websocket.Conn, output chan string) { defer close(done) for { var resp coordinator.SessionCommandResult @@ -30,19 +30,28 @@ func receiveHandler(connection *websocket.Conn) { } if resp.Result == coordinator.SessionRespJoined1 { pid = game.SentinalID + output <- "joined as sentinal" } else if resp.Result == coordinator.SessionRespJoined2 { pid = game.ScourgeID + output <- "joined as scourge" + } else if resp.Result == coordinator.SessionRespFound { + output <- "game found" } else if resp.Result == coordinator.SessionRespPlayed { if resp.GameResult != nil { switch resp.GameResult.ResultType { case game.ActCmd: - fmt.Println(resp.GameResult.ActionResult) + output <- resp.GameResult.ActionResult.String() case game.StateCmd: - fmt.Println(resp.GameResult.StateResult) + output <- resp.GameResult.StateResult.String() case game.DebugCmd: - fmt.Println(resp.GameResult.DebugResult) + output <- resp.GameResult.DebugResult.String() } } + } else if resp.Result == coordinator.SessionRespLeft { + output <- "game left" + break + } else if resp.Result == coordinator.SessionRespPlayed { + output <- "played succesfully" } } } @@ -51,6 +60,7 @@ func main() { done = make(chan interface{}) // Channel to indicate that the receiverHandler is done interrupt = make(chan os.Signal) // Channel to listen for interrupt signal to terminate gracefully cmd := make(chan coordinator.SessionCommand) + output := make(chan string) signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT @@ -62,22 +72,23 @@ func main() { log.Fatal("Error connecting to Websocket Server:", err) } defer conn.Close() - go receiveHandler(conn) + go receiveHandler(conn, output) go GetCommand(id, cmd) // Our main loop for the client // We send our relevant packets here for { var c coordinator.SessionCommand + var o string select { case c = <-cmd: - // Send an echo packet every second err := conn.WriteJSON(c) if err != nil { log.Println("Error during writing to websocket:", err) return } - + case o = <-output: + fmt.Println(o) case <-interrupt: // We received a SIGINT (Ctrl + C). Terminate gracefully... log.Println("Received SIGINT interrupt signal. Closing all pending connections") @@ -120,7 +131,6 @@ func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand) { log.Println(err) } cmd = strings.TrimSpace(cmd) - fmt.Println(cmd) switch t { case 0: //session diff --git a/internal/game/board.go b/internal/game/board.go index d2a88bf..9c83117 100644 --- a/internal/game/board.go +++ b/internal/game/board.go @@ -191,10 +191,22 @@ func (b *Board) UnmarshalJSON(data []byte) (err error) { return err } for i := 0; i < 4; i++ { - b.Sentinal[i] = NewCard(ported[0][i].Type, ported[0][i].ID) + c := NewCard(ported[0][i].Type, ported[0][i].ID) + if ported[0][i].Sick { + c.Act() + } else { + c.Refresh() + } + b.Sentinal[i] = c } for i := 0; i < 4; i++ { - b.Scourge[i] = NewCard(ported[1][i].Type, ported[0][i].ID) + c := NewCard(ported[1][i].Type, ported[1][i].ID) + if ported[1][i].Sick { + c.Act() + } else { + c.Refresh() + } + b.Scourge[i] = c } return } diff --git a/internal/game/card.go b/internal/game/card.go index fd5b23e..9a38198 100644 --- a/internal/game/card.go +++ b/internal/game/card.go @@ -13,6 +13,7 @@ type Card interface { Enters(g *Game) *Game Value() CardValue Act() + Refresh() Acted() bool Empty() bool String() string @@ -52,7 +53,10 @@ func (g *GenericCard) Acted() bool { func (g *GenericCard) Act() { g.Sick = true } - +func (g *GenericCard) Refresh() { + fmt.Println("refreshed") + g.Sick = false +} func (g *GenericCard) Empty() bool { return false } @@ -65,6 +69,7 @@ func (g *GenericCard) Port() *PortableCard { return &PortableCard{ Type: int(g.Val), ID: g.Id, + Sick: g.Sick, } } @@ -72,6 +77,7 @@ func (g *GenericCard) CanAttack(x, y int) bool { if x == y && !g.Sick { return true } + fmt.Println("sick") return false } @@ -215,4 +221,5 @@ func (v *Valkyrie) Enters(g *Game) *Game { type PortableCard struct { Type int `json:"type"` ID uuid.UUID `json:"ID"` + Sick bool `json:"sick"` } diff --git a/internal/game/cmd.go b/internal/game/cmd.go index 72cc1be..95c6f9a 100644 --- a/internal/game/cmd.go +++ b/internal/game/cmd.go @@ -5,9 +5,10 @@ import "fmt" type CmdType string const ( - ActCmd = "a" - StateCmd = "s" - DebugCmd = "d" + ActCmd = "a" + StateCmd = "s" + DebugCmd = "d" + InvalidCmd = "e" ) type Command struct { diff --git a/internal/game/deck.go b/internal/game/deck.go index 733c51d..e3c8769 100644 --- a/internal/game/deck.go +++ b/internal/game/deck.go @@ -2,6 +2,7 @@ package game import ( "encoding/json" + "fmt" "math/rand" "time" ) @@ -10,6 +11,13 @@ type Deck struct { Cards []Card `json:"cards"` } +func (d *Deck) String() string { + if d == nil { + return "||" + } + return fmt.Sprintf("|%v|", d.Cards) +} + func NewDeck() *Deck { cards := []Card{CreateCard(0)} for i := 0; i < 3; i++ { @@ -74,7 +82,13 @@ func (d *Deck) UnmarshalJSON(data []byte) (err error) { } cards := []Card{} for _, v := range ported { - cards = append(cards, NewCard(v.Type, v.ID)) + c := NewCard(v.Type, v.ID) + if v.Sick { + c.Act() + } else { + c.Refresh() + } + cards = append(cards, c) } d.Cards = cards return diff --git a/internal/game/game.go b/internal/game/game.go index abab658..af9d487 100644 --- a/internal/game/game.go +++ b/internal/game/game.go @@ -80,7 +80,7 @@ func (g *Game) Parse(cmd *Command) *CommandResult { var debug_res *Game var res_type CmdType - if cmd.Type == ActCmd { + if cmd.Type == ActCmd && g.Status == StatusPlaying { action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd) state_res = nil debug_res = nil @@ -90,11 +90,16 @@ func (g *Game) Parse(cmd *Command) *CommandResult { action_res = nil debug_res = nil res_type = StateCmd - } else { + } else if cmd.Type == DebugCmd { state_res = nil action_res = nil debug_res = g res_type = DebugCmd + } else { + state_res = nil + action_res = nil + debug_res = nil + res_type = InvalidCmd } g.StateChanges() return &CommandResult{ diff --git a/internal/game/player.go b/internal/game/player.go index 0ebaa3a..d95ee6d 100644 --- a/internal/game/player.go +++ b/internal/game/player.go @@ -11,7 +11,7 @@ import ( type Player struct { Name string `json:"name"` Id int `json:"id"` - Hand []Card `json:"hand"` + Hand []Card `json:"hand"` //probably should be a Deck Life int `json:"life"` } @@ -54,11 +54,18 @@ func (p *Player) UnmarshalJSON(data []byte) (err error) { m := v.(map[string]interface{}) t := int(m["type"].(float64)) i := m["ID"].(string) + b := m["sick"].(bool) uid, err := uuid.Parse(i) if err != nil { log.Println("invalid card parse") } else { - hand = append(hand, NewCard(t, uid)) + c := NewCard(t, uid) + if b { + c.Act() + } else { + c.Refresh() + } + hand = append(hand, c) } } p.Hand = hand