client improvements, but bug with sick

This commit is contained in:
stryan 2021-07-23 17:42:12 -04:00
parent eb7b405641
commit 2b76ae5b5a
7 changed files with 75 additions and 19 deletions

View File

@ -20,7 +20,7 @@ var done chan interface{}
var interrupt chan os.Signal var interrupt chan os.Signal
var pid int var pid int
func receiveHandler(connection *websocket.Conn) { func receiveHandler(connection *websocket.Conn, output chan string) {
defer close(done) defer close(done)
for { for {
var resp coordinator.SessionCommandResult var resp coordinator.SessionCommandResult
@ -30,19 +30,28 @@ func receiveHandler(connection *websocket.Conn) {
} }
if resp.Result == coordinator.SessionRespJoined1 { if resp.Result == coordinator.SessionRespJoined1 {
pid = game.SentinalID pid = game.SentinalID
output <- "joined as sentinal"
} else if resp.Result == coordinator.SessionRespJoined2 { } else if resp.Result == coordinator.SessionRespJoined2 {
pid = game.ScourgeID pid = game.ScourgeID
output <- "joined as scourge"
} else if resp.Result == coordinator.SessionRespFound {
output <- "game found"
} else if resp.Result == coordinator.SessionRespPlayed { } else if resp.Result == coordinator.SessionRespPlayed {
if resp.GameResult != nil { if resp.GameResult != nil {
switch resp.GameResult.ResultType { switch resp.GameResult.ResultType {
case game.ActCmd: case game.ActCmd:
fmt.Println(resp.GameResult.ActionResult) output <- resp.GameResult.ActionResult.String()
case game.StateCmd: case game.StateCmd:
fmt.Println(resp.GameResult.StateResult) output <- resp.GameResult.StateResult.String()
case game.DebugCmd: 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 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 interrupt = make(chan os.Signal) // Channel to listen for interrupt signal to terminate gracefully
cmd := make(chan coordinator.SessionCommand) cmd := make(chan coordinator.SessionCommand)
output := make(chan string)
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT 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) log.Fatal("Error connecting to Websocket Server:", err)
} }
defer conn.Close() defer conn.Close()
go receiveHandler(conn) go receiveHandler(conn, output)
go GetCommand(id, cmd) go GetCommand(id, cmd)
// Our main loop for the client // Our main loop for the client
// We send our relevant packets here // We send our relevant packets here
for { for {
var c coordinator.SessionCommand var c coordinator.SessionCommand
var o string
select { select {
case c = <-cmd: case c = <-cmd:
// Send an echo packet every second
err := conn.WriteJSON(c) err := conn.WriteJSON(c)
if err != nil { if err != nil {
log.Println("Error during writing to websocket:", err) log.Println("Error during writing to websocket:", err)
return return
} }
case o = <-output:
fmt.Println(o)
case <-interrupt: case <-interrupt:
// We received a SIGINT (Ctrl + C). Terminate gracefully... // We received a SIGINT (Ctrl + C). Terminate gracefully...
log.Println("Received SIGINT interrupt signal. Closing all pending connections") 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) log.Println(err)
} }
cmd = strings.TrimSpace(cmd) cmd = strings.TrimSpace(cmd)
fmt.Println(cmd)
switch t { switch t {
case 0: case 0:
//session //session

View File

@ -191,10 +191,22 @@ func (b *Board) UnmarshalJSON(data []byte) (err error) {
return err return err
} }
for i := 0; i < 4; i++ { 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++ { 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 return
} }

View File

@ -13,6 +13,7 @@ type Card interface {
Enters(g *Game) *Game Enters(g *Game) *Game
Value() CardValue Value() CardValue
Act() Act()
Refresh()
Acted() bool Acted() bool
Empty() bool Empty() bool
String() string String() string
@ -52,7 +53,10 @@ func (g *GenericCard) Acted() bool {
func (g *GenericCard) Act() { func (g *GenericCard) Act() {
g.Sick = true g.Sick = true
} }
func (g *GenericCard) Refresh() {
fmt.Println("refreshed")
g.Sick = false
}
func (g *GenericCard) Empty() bool { func (g *GenericCard) Empty() bool {
return false return false
} }
@ -65,6 +69,7 @@ func (g *GenericCard) Port() *PortableCard {
return &PortableCard{ return &PortableCard{
Type: int(g.Val), Type: int(g.Val),
ID: g.Id, ID: g.Id,
Sick: g.Sick,
} }
} }
@ -72,6 +77,7 @@ func (g *GenericCard) CanAttack(x, y int) bool {
if x == y && !g.Sick { if x == y && !g.Sick {
return true return true
} }
fmt.Println("sick")
return false return false
} }
@ -215,4 +221,5 @@ func (v *Valkyrie) Enters(g *Game) *Game {
type PortableCard struct { type PortableCard struct {
Type int `json:"type"` Type int `json:"type"`
ID uuid.UUID `json:"ID"` ID uuid.UUID `json:"ID"`
Sick bool `json:"sick"`
} }

View File

@ -5,9 +5,10 @@ import "fmt"
type CmdType string type CmdType string
const ( const (
ActCmd = "a" ActCmd = "a"
StateCmd = "s" StateCmd = "s"
DebugCmd = "d" DebugCmd = "d"
InvalidCmd = "e"
) )
type Command struct { type Command struct {

View File

@ -2,6 +2,7 @@ package game
import ( import (
"encoding/json" "encoding/json"
"fmt"
"math/rand" "math/rand"
"time" "time"
) )
@ -10,6 +11,13 @@ type Deck struct {
Cards []Card `json:"cards"` Cards []Card `json:"cards"`
} }
func (d *Deck) String() string {
if d == nil {
return "||"
}
return fmt.Sprintf("|%v|", d.Cards)
}
func NewDeck() *Deck { func NewDeck() *Deck {
cards := []Card{CreateCard(0)} cards := []Card{CreateCard(0)}
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
@ -74,7 +82,13 @@ func (d *Deck) UnmarshalJSON(data []byte) (err error) {
} }
cards := []Card{} cards := []Card{}
for _, v := range ported { 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 d.Cards = cards
return return

View File

@ -80,7 +80,7 @@ func (g *Game) Parse(cmd *Command) *CommandResult {
var debug_res *Game var debug_res *Game
var res_type CmdType var res_type CmdType
if cmd.Type == ActCmd { if cmd.Type == ActCmd && g.Status == StatusPlaying {
action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd) action_res = g.PlayerAct(cmd.PlayerID, cmd.Cmd)
state_res = nil state_res = nil
debug_res = nil debug_res = nil
@ -90,11 +90,16 @@ func (g *Game) Parse(cmd *Command) *CommandResult {
action_res = nil action_res = nil
debug_res = nil debug_res = nil
res_type = StateCmd res_type = StateCmd
} else { } else if cmd.Type == DebugCmd {
state_res = nil state_res = nil
action_res = nil action_res = nil
debug_res = g debug_res = g
res_type = DebugCmd res_type = DebugCmd
} else {
state_res = nil
action_res = nil
debug_res = nil
res_type = InvalidCmd
} }
g.StateChanges() g.StateChanges()
return &CommandResult{ return &CommandResult{

View File

@ -11,7 +11,7 @@ import (
type Player struct { type Player struct {
Name string `json:"name"` Name string `json:"name"`
Id int `json:"id"` Id int `json:"id"`
Hand []Card `json:"hand"` Hand []Card `json:"hand"` //probably should be a Deck
Life int `json:"life"` Life int `json:"life"`
} }
@ -54,11 +54,18 @@ func (p *Player) UnmarshalJSON(data []byte) (err error) {
m := v.(map[string]interface{}) m := v.(map[string]interface{})
t := int(m["type"].(float64)) t := int(m["type"].(float64))
i := m["ID"].(string) i := m["ID"].(string)
b := m["sick"].(bool)
uid, err := uuid.Parse(i) uid, err := uuid.Parse(i)
if err != nil { if err != nil {
log.Println("invalid card parse") log.Println("invalid card parse")
} else { } 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 p.Hand = hand