oracle #1

Closed
stryan wants to merge 1 commits from oracle into master
4 changed files with 37 additions and 13 deletions
Showing only changes of commit 707b770ad4 - Show all commits

View File

@ -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

View File

@ -8,6 +8,7 @@ const (
ActCmd = "a"
StateCmd = "s"
DebugCmd = "d"
InvalidCmd = "e"
)
type Command struct {

View File

@ -1,6 +1,7 @@
package game
import (
"fmt"
"math/rand"
"time"
@ -11,6 +12,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(owner int) *Deck {
cards := []*Card{NewCard(0, owner, -1, uuid.Nil)}
for i := 0; i < 3; i++ {

View File

@ -81,7 +81,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
@ -91,11 +91,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{