From fcfebe220f3fb6641765a4bdbfb1186de62b83de Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 2 Oct 2021 13:26:49 -0400 Subject: [PATCH] add admin interface --- go.mod | 1 + main.go | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ server.go | 4 +- 3 files changed, 199 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 926ab47..a69e43c 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.16 require ( git.saintnet.tech/tomecraft/tome_lib v0.1.2 // indirect git.saintnet.tech/tomecraft/tome_server v0.1.1 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect ) diff --git a/main.go b/main.go index ffc0f7f..b5f538a 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,207 @@ package main import ( + "bufio" + "flag" + "fmt" + "log" + "os" + "strconv" + "strings" + + "git.saintnet.tech/tomecraft/tome_lib" coordinator "git.saintnet.tech/tomecraft/tome_server" + "github.com/google/uuid" ) func main() { + admin := flag.Bool("admin", false, "enable admin console") + flag.Parse() c := coordinator.NewCoordinator() + if *admin { + file, err := os.OpenFile("serverlog.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + log.SetOutput(file) + go console(c) + } + c.Start() Serve(c) } + +func console(c *coordinator.Coordinator) { + scanner := bufio.NewScanner(os.Stdin) + fmt.Printf("> ") + match := uuid.Nil + var err error + player := -1 + for scanner.Scan() { + in := scanner.Text() + cmd := strings.Split(in, " ") + switch cmd[0] { + case "get": + switch cmd[1] { + case "matches": + fmt.Println(c.Matches) + case "queue": + fmt.Println(c.PlayerPool) + case "match": + fmt.Println(c.Matches[match]) + case "game": + if match == uuid.Nil { + fmt.Println("needs match") + continue + } + fmt.Println(c.Matches[match].Game) + case "player": + if match == uuid.Nil { + fmt.Println("needs match") + continue + } + if player == -1 { + fmt.Println("needs player") + continue + } + if player == tome_lib.SentinalID { + fmt.Println(c.Matches[match].Game.SentinalPlayer) + } else { + fmt.Println(c.Matches[match].Game.ScourgePlayer) + } + case "deck": + if match == uuid.Nil { + fmt.Println("needs match") + continue + } + if player == -1 { + fmt.Println("needs player") + continue + } + if player == tome_lib.SentinalID { + deck := c.Matches[match].Game.SentinalDeck + fmt.Println(deck) + if len(cmd) == 3 { + crd, err := strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("invalid index") + continue + } + if crd < deck.Size() { + fmt.Println(view_card(deck.Cards[crd])) + } + } + } else { + deck := c.Matches[match].Game.ScourgeDeck + fmt.Println(deck) + if len(cmd) == 3 { + crd, err := strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("invalid index") + continue + } + if crd < deck.Size() { + fmt.Println(view_card(deck.Cards[crd])) + } + } + } + case "hand": + if match == uuid.Nil { + fmt.Println("needs match") + continue + } + if player == -1 { + fmt.Println("needs player") + continue + } + if player == tome_lib.SentinalID { + hand := c.Matches[match].Game.SentinalPlayer.Hand + fmt.Println(hand) + if len(cmd) == 3 { + crd, err := strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("invalid index") + continue + } + if crd < len(hand) { + fmt.Println(view_card(hand[crd])) + } + } + } else { + hand := c.Matches[match].Game.ScourgePlayer.Hand + fmt.Println(hand) + if len(cmd) == 3 { + crd, err := strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("invalid index") + continue + } + if crd < len(hand) { + fmt.Println(view_card(hand[crd])) + } + } + } + case "board": + if match == uuid.Nil { + fmt.Println("needs match") + continue + } + if player == -1 { + fmt.Println("needs player") + continue + } + if player == tome_lib.SentinalID { + board := c.Matches[match].Game.GameBoard.Sentinal + fmt.Println(board) + if len(cmd) == 3 { + crd, err := strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("invalid index") + continue + } + if crd < len(board) { + fmt.Println(view_card(board[crd])) + } + } + } else { + board := c.Matches[match].Game.GameBoard.Scourge + fmt.Println(board) + if len(cmd) == 3 { + crd, err := strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("invalid index") + continue + } + if crd < len(board) { + fmt.Println(view_card(board[crd])) + } + } + } + } + case "set": + switch cmd[1] { + case "match": + match, err = uuid.Parse(cmd[2]) + if err != nil { + fmt.Println("Error setting match") + } + case "player": + player, err = strconv.Atoi(cmd[2]) + if err != nil { + fmt.Println("error setting player") + } + } + case "reset": + match = uuid.Nil + player = -1 + case "quit": + os.Exit(0) + } + fmt.Printf("> ") + } +} + +func view_card(c *tome_lib.Card) string { + return fmt.Sprintf("%v %v/%v %v %v %v %v %v %v", c.Type, c.BasePower, c.Power, c.Id, c.Sick, c.Counters, c.Owner, c.Position, c.Effects) +} diff --git a/server.go b/server.go index db5c381..8501411 100644 --- a/server.go +++ b/server.go @@ -40,14 +40,14 @@ func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request) log.Println("Error during message reading:", err) break } - if cmd.Command != tome_lib.SessionCmdPoll && (cmd.GameCommand.Type != tome_lib.StateCmd && cmd.GameCommand.Cmd != "g") { + if cmd.Command != tome_lib.SessionCmdPoll && (cmd.GameCommand != nil && cmd.GameCommand.Type != tome_lib.StateCmd && cmd.GameCommand.Cmd != "g") { log.Printf("Received: %s", cmd) } resp := c.Coordinate(&cmd) if err != nil { panic(err) } - if cmd.Command != tome_lib.SessionCmdPoll && (cmd.GameCommand.Type != tome_lib.StateCmd && cmd.GameCommand.Cmd != "g") { + if cmd.Command != tome_lib.SessionCmdPoll && (cmd.GameCommand != nil && cmd.GameCommand.Type != tome_lib.StateCmd && cmd.GameCommand.Cmd != "g") { log.Printf("sending: %v", resp.Result) } err = conn.WriteJSON(resp)