update paths

This commit is contained in:
stryan 2021-10-01 13:10:55 -04:00
parent e6661a26f4
commit a73928bbd1
4 changed files with 134 additions and 126 deletions

View File

@ -12,6 +12,7 @@ import (
"time"
"unicode/utf8"
"git.saintnet.tech/tomecraft/tome_lib"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
@ -24,65 +25,65 @@ var matchID uuid.UUID
func receiveHandler(connection *websocket.Conn, output chan string) {
defer close(done)
for {
var resp coordinator.SessionCommandResult
var resp tome_lib.SessionCommandResult
err := connection.ReadJSON(&resp)
if err != nil {
log.Println("Error in receive:", err)
}
switch resp.Result {
case coordinator.SessionRespJoined1:
pid = game.SentinalID
case tome_lib.SessionRespJoined1:
pid = tome_lib.SentinalID
output <- "joined as sentinal"
case coordinator.SessionRespJoined2:
pid = game.ScourgeID
case tome_lib.SessionRespJoined2:
pid = tome_lib.ScourgeID
output <- "joined as scourge"
case coordinator.SessionRespFound:
case tome_lib.SessionRespFound:
matchID = resp.MatchID
output <- "game found"
case coordinator.SessionRespJoinError:
output <- "error joining game"
case coordinator.SessionRespPlayed:
output <- "tome_lib found"
case tome_lib.SessionRespJoinError:
output <- "error joining tome_lib"
case tome_lib.SessionRespPlayed:
if resp.GameResult != nil {
output <- "played succesfully"
switch resp.GameResult.ResultType {
case game.ActCmd:
case tome_lib.ActCmd:
output <- resp.GameResult.ActionResult.String()
case game.StateCmd:
case tome_lib.StateCmd:
output <- resp.GameResult.StateResult.String()
case game.DebugCmd:
output <- resp.GameResult.DebugResult.String()
case tome_lib.DebugCmd:
output <- "debug response" //resp.GameResult.DebugResult
}
} else {
output <- "error playing"
}
case coordinator.SessionRespLeft:
output <- "game left"
case tome_lib.SessionRespLeft:
output <- "tome_lib left"
break
case coordinator.SessionRespBroadcastSenTurn:
case tome_lib.SessionRespBroadcastSenTurn:
output <- "Sentinal may take their turn"
case coordinator.SessionRespBroadcastScoTrun:
case tome_lib.SessionRespBroadcastScoTrun:
output <- "Scourge may take their turn"
case coordinator.SessionRespBroadcastSenWin:
case tome_lib.SessionRespBroadcastSenWin:
output <- "Sentinal wins!"
case coordinator.SessionRespBroadcastScoWin:
case tome_lib.SessionRespBroadcastScoWin:
output <- "Scourge wins!"
case coordinator.SessionRespBroadcastUpdate:
case tome_lib.SessionRespBroadcastUpdate:
//we don't handle updates
case coordinator.SessionRespBroadcastScoJoin:
output <- "Scourge has joined the game"
case coordinator.SessionRespBroadcastSenJoin:
output <- "Sentinal has joined the game"
case coordinator.SessionRespBroadcastScoLeft:
output <- "Scourge has left the game"
case coordinator.SessionRespBroadcastSenLeft:
output <- "Sentinal has left the game"
case coordinator.SessionRespBroadcastSenReady:
case tome_lib.SessionRespBroadcastScoJoin:
output <- "Scourge has joined the tome_lib"
case tome_lib.SessionRespBroadcastSenJoin:
output <- "Sentinal has joined the tome_lib"
case tome_lib.SessionRespBroadcastScoLeft:
output <- "Scourge has left the tome_lib"
case tome_lib.SessionRespBroadcastSenLeft:
output <- "Sentinal has left the tome_lib"
case tome_lib.SessionRespBroadcastSenReady:
output <- "Sentinal is ready"
case coordinator.SessionRespBroadcastScoReady:
case tome_lib.SessionRespBroadcastScoReady:
output <- "scourge is ready"
case coordinator.SessionRespBroadcastNone:
case tome_lib.SessionRespBroadcastNone:
case coordinator.SessionRespError:
case tome_lib.SessionRespError:
output <- "generic error"
default:
@ -94,7 +95,7 @@ func receiveHandler(connection *websocket.Conn, output chan string) {
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)
cmd := make(chan tome_lib.SessionCommand)
output := make(chan string)
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT
@ -117,7 +118,7 @@ func main() {
// Our main loop for the client
// We send our relevant packets here
for {
var c coordinator.SessionCommand
var c tome_lib.SessionCommand
var o string
select {
case c = <-cmd:
@ -126,7 +127,7 @@ func main() {
log.Println("Error during writing to websocket:", err)
return
}
if c.Command == coordinator.SessionCmdLeave {
if c.Command == tome_lib.SessionCmdLeave {
break
}
case o = <-output:
@ -136,9 +137,9 @@ func main() {
log.Println("Received SIGINT interrupt signal. Closing all pending connections")
// Close our websocket connection
err := conn.WriteJSON(coordinator.SessionCommand{
err := conn.WriteJSON(tome_lib.SessionCommand{
ID: id,
Command: coordinator.SessionCmdLeave,
Command: tome_lib.SessionCmdLeave,
})
if err != nil {
log.Println("Error during closing websocket:", err)
@ -154,10 +155,10 @@ func main() {
return
case <-ticker.C:
if matchID != uuid.Nil {
err := conn.WriteJSON(coordinator.SessionCommand{
err := conn.WriteJSON(tome_lib.SessionCommand{
ID: id,
MatchID: matchID,
Command: coordinator.SessionCmdPoll,
Command: tome_lib.SessionCmdPoll,
})
if err != nil {
log.Println("Error writing to websocket:", err)
@ -168,7 +169,7 @@ func main() {
}
}
func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand) {
func GetCommand(uid uuid.UUID, resp chan tome_lib.SessionCommand) {
for {
var cmd string
var t int
@ -188,48 +189,48 @@ func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand) {
switch t {
case 0:
//session
switch coordinator.SessionCmd(cmd) {
case coordinator.SessionCmdQuery:
resp <- coordinator.SessionCommand{
switch tome_lib.SessionCmd(cmd) {
case tome_lib.SessionCmdQuery:
resp <- tome_lib.SessionCommand{
ID: uid,
Command: coordinator.SessionCmdQuery,
Command: tome_lib.SessionCmdQuery,
}
case coordinator.SessionCmdJoin:
resp <- coordinator.SessionCommand{
case tome_lib.SessionCmdJoin:
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdJoin,
Command: tome_lib.SessionCmdJoin,
}
case coordinator.SessionCmdLeave:
resp <- coordinator.SessionCommand{
case tome_lib.SessionCmdLeave:
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdLeave,
Command: tome_lib.SessionCmdLeave,
}
default:
break
}
case 1:
//state
resp <- coordinator.SessionCommand{
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdPlay,
GameCommand: &game.Command{
Command: tome_lib.SessionCmdPlay,
GameCommand: &tome_lib.Command{
PlayerID: pid,
Type: game.StateCmd,
Type: tome_lib.StateCmd,
Cmd: cmd,
},
}
case 2:
//action
resp <- coordinator.SessionCommand{
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdPlay,
GameCommand: &game.Command{
Command: tome_lib.SessionCmdPlay,
GameCommand: &tome_lib.Command{
PlayerID: pid,
Type: game.ActCmd,
Type: tome_lib.ActCmd,
Cmd: cmd,
},
}

View File

@ -10,8 +10,7 @@ import (
"time"
"unicode/utf8"
"git.saintnet.tech/stryan/snengame/internal/coordinator"
"git.saintnet.tech/stryan/snengame/internal/game"
"git.saintnet.tech/tomecraft/tome_lib"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
@ -24,65 +23,65 @@ var matchID uuid.UUID
func receiveHandler(connection *websocket.Conn, container *UIContainer) {
defer close(done)
for {
var resp coordinator.SessionCommandResult
var resp tome_lib.SessionCommandResult
err := connection.ReadJSON(&resp)
if err != nil {
log.Println("Error in receive:", err)
break
}
switch resp.Result {
case coordinator.SessionRespJoined1:
pid = game.SentinalID
case tome_lib.SessionRespJoined1:
pid = tome_lib.SentinalID
container.Output <- "joined as sentinal"
case coordinator.SessionRespJoined2:
pid = game.ScourgeID
case tome_lib.SessionRespJoined2:
pid = tome_lib.ScourgeID
container.Output <- "joined as scourge"
case coordinator.SessionRespFound:
case tome_lib.SessionRespFound:
matchID = resp.MatchID
container.Output <- "game found"
case coordinator.SessionRespPlayed:
container.Output <- "tome_lib found"
case tome_lib.SessionRespPlayed:
if resp.GameResult != nil {
switch resp.GameResult.ResultType {
case game.ActCmd:
case tome_lib.ActCmd:
container.Output <- resp.GameResult.ActionResult.String()
case game.StateCmd:
case tome_lib.StateCmd:
container.State = resp.GameResult.StateResult
container.Updated <- true
case game.DebugCmd:
container.Output <- resp.GameResult.DebugResult.String()
case tome_lib.DebugCmd:
container.Output <- "debug response"
}
} else {
container.Output <- "error playing"
}
case coordinator.SessionRespLeft:
container.Output <- "game left"
case tome_lib.SessionRespLeft:
container.Output <- "tome_lib left"
matchID = uuid.Nil
return
case coordinator.SessionRespBroadcastSenTurn:
case tome_lib.SessionRespBroadcastSenTurn:
container.Output <- "Sentinal may take their turn"
case coordinator.SessionRespBroadcastScoTrun:
case tome_lib.SessionRespBroadcastScoTrun:
container.Output <- "Scourge may take their turn"
case coordinator.SessionRespBroadcastSenWin:
case tome_lib.SessionRespBroadcastSenWin:
container.Output <- "Sentinal wins!"
case coordinator.SessionRespBroadcastScoWin:
case tome_lib.SessionRespBroadcastScoWin:
container.Output <- "Scourge wins!"
case coordinator.SessionRespBroadcastUpdate:
case tome_lib.SessionRespBroadcastUpdate:
container.GetUpdate <- true
case coordinator.SessionRespBroadcastScoJoin:
container.Output <- "Scourge has joined the game"
case coordinator.SessionRespBroadcastSenJoin:
container.Output <- "Sentinal has joined the game"
case coordinator.SessionRespBroadcastScoLeft:
container.Output <- "Scourge has left the game"
case coordinator.SessionRespBroadcastSenLeft:
container.Output <- "Sentinal has left the game"
case coordinator.SessionRespBroadcastSenReady:
case tome_lib.SessionRespBroadcastScoJoin:
container.Output <- "Scourge has joined the tome_lib"
case tome_lib.SessionRespBroadcastSenJoin:
container.Output <- "Sentinal has joined the tome_lib"
case tome_lib.SessionRespBroadcastScoLeft:
container.Output <- "Scourge has left the tome_lib"
case tome_lib.SessionRespBroadcastSenLeft:
container.Output <- "Sentinal has left the tome_lib"
case tome_lib.SessionRespBroadcastSenReady:
container.Output <- "Sentinal is ready"
case coordinator.SessionRespBroadcastScoReady:
case tome_lib.SessionRespBroadcastScoReady:
container.Output <- "scourge is ready"
case coordinator.SessionRespBroadcastNone:
case tome_lib.SessionRespBroadcastNone:
case coordinator.SessionRespError:
case tome_lib.SessionRespError:
container.Output <- "generic error"
default:
@ -94,7 +93,7 @@ func receiveHandler(connection *websocket.Conn, container *UIContainer) {
func backend(container *UIContainer) {
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)
cmd := make(chan tome_lib.SessionCommand)
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT
hostname := flag.String("host", "localhost", "server hostname to connect to")
@ -116,7 +115,7 @@ func backend(container *UIContainer) {
// Our main loop for the client
// We send our relevant packets here
for {
var c coordinator.SessionCommand
var c tome_lib.SessionCommand
select {
case c = <-cmd:
err := conn.WriteJSON(c)
@ -124,18 +123,18 @@ func backend(container *UIContainer) {
log.Println("Error during writing to websocket:", err)
return
}
if c.Command == coordinator.SessionCmdLeave {
if c.Command == tome_lib.SessionCmdLeave {
break
}
case <-container.GetUpdate:
if container.State != nil {
err = conn.WriteJSON(&coordinator.SessionCommand{
err = conn.WriteJSON(&tome_lib.SessionCommand{
ID: id,
MatchID: matchID,
Command: coordinator.SessionCmdPlay,
GameCommand: &game.Command{
Command: tome_lib.SessionCmdPlay,
GameCommand: &tome_lib.Command{
PlayerID: pid,
Type: game.StateCmd,
Type: tome_lib.StateCmd,
Cmd: "g",
},
})
@ -151,9 +150,9 @@ func backend(container *UIContainer) {
log.Println("Received SIGINT interrupt signal. Closing all pending connections")
// Close our websocket connection
err := conn.WriteJSON(coordinator.SessionCommand{
err := conn.WriteJSON(tome_lib.SessionCommand{
ID: id,
Command: coordinator.SessionCmdLeave,
Command: tome_lib.SessionCmdLeave,
})
if err != nil {
log.Println("Error during closing websocket:", err)
@ -173,10 +172,10 @@ func backend(container *UIContainer) {
return
default:
if matchID != uuid.Nil {
err := conn.WriteJSON(coordinator.SessionCommand{
err := conn.WriteJSON(tome_lib.SessionCommand{
ID: id,
MatchID: matchID,
Command: coordinator.SessionCmdPoll,
Command: tome_lib.SessionCmdPoll,
})
if err != nil {
log.Println("Error writing to websocket:", err)
@ -188,7 +187,7 @@ func backend(container *UIContainer) {
}
}
func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand, container *UIContainer) {
func GetCommand(uid uuid.UUID, resp chan tome_lib.SessionCommand, container *UIContainer) {
for {
var cmd string
var t int
@ -204,48 +203,48 @@ func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand, container *
switch t {
case 0:
//session
switch coordinator.SessionCmd(cmd) {
case coordinator.SessionCmdQuery:
resp <- coordinator.SessionCommand{
switch tome_lib.SessionCmd(cmd) {
case tome_lib.SessionCmdQuery:
resp <- tome_lib.SessionCommand{
ID: uid,
Command: coordinator.SessionCmdQuery,
Command: tome_lib.SessionCmdQuery,
}
case coordinator.SessionCmdJoin:
resp <- coordinator.SessionCommand{
case tome_lib.SessionCmdJoin:
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdJoin,
Command: tome_lib.SessionCmdJoin,
}
case coordinator.SessionCmdLeave:
resp <- coordinator.SessionCommand{
case tome_lib.SessionCmdLeave:
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdLeave,
Command: tome_lib.SessionCmdLeave,
}
default:
break
}
case 1:
//state
resp <- coordinator.SessionCommand{
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdPlay,
GameCommand: &game.Command{
Command: tome_lib.SessionCmdPlay,
GameCommand: &tome_lib.Command{
PlayerID: pid,
Type: game.StateCmd,
Type: tome_lib.StateCmd,
Cmd: cmd,
},
}
case 2:
//action
resp <- coordinator.SessionCommand{
resp <- tome_lib.SessionCommand{
ID: uid,
MatchID: matchID,
Command: coordinator.SessionCmdPlay,
GameCommand: &game.Command{
Command: tome_lib.SessionCmdPlay,
GameCommand: &tome_lib.Command{
PlayerID: pid,
Type: game.ActCmd,
Type: tome_lib.ActCmd,
Cmd: cmd,
},
}

View File

@ -4,12 +4,12 @@ import (
"fmt"
"code.rocketnine.space/tslocum/cview"
"git.saintnet.tech/stryan/snengame/internal/game"
"git.saintnet.tech/tomecraft/tome_lib"
"github.com/gdamore/tcell/v2"
)
type UIContainer struct {
State *game.GameView
State *tome_lib.GameView
Updated chan bool
Output chan string
Input chan string
@ -60,9 +60,9 @@ func main() {
var pv string
if container.State != nil && container.State.Player != nil {
ct := "Noone"
if container.State.CurrentTurn == game.SentinalID {
if container.State.CurrentTurn == tome_lib.SentinalID {
ct = "Sentinal"
} else if container.State.CurrentTurn == game.ScourgeID {
} else if container.State.CurrentTurn == tome_lib.ScourgeID {
ct = "Scourge"
}
pv = fmt.Sprintf("Your Life: %v\nEnemy Life: %v\nEnemy Hand size: %v\nEnemy Deck Size: %v\n\nCT:%v CD: %v, HD %v, Status: %v\n", container.State.Player.Life, container.State.EnemyLife, container.State.EnemyHandSize, container.State.EnemyDeckSize, ct, container.State.CanDraw, container.State.HasDrawn, container.State.Status)

8
go.mod
View File

@ -1,3 +1,11 @@
module git.saintnet.tech/tome_client
go 1.16
require (
code.rocketnine.space/tslocum/cview v1.5.7 // indirect
git.saintnet.tech/tomecraft/tome_lib v0.1.0 // indirect
github.com/gdamore/tcell/v2 v2.4.1-0.20210828201608-73703f7ed490 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
)