add admin interface

This commit is contained in:
stryan 2021-10-02 13:26:49 -04:00
parent dbd0c4886b
commit fcfebe220f
3 changed files with 199 additions and 2 deletions

1
go.mod
View File

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

196
main.go
View File

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

View File

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