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

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

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

View File

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