tome_ws/main.go

246 lines
5.4 KiB
Go

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"
)
var GitCommit string
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)
}
log.Printf("starting tome_ws, commit %v", GitCommit)
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 "grave":
if match == uuid.Nil {
fmt.Println("needs match")
continue
}
if player == -1 {
fmt.Println("needs player")
continue
}
if player == tome_lib.SentinalID {
grave := c.Matches[match].Game.SentinalGrave
fmt.Println(grave)
if len(cmd) == 3 {
crd, err := strconv.Atoi(cmd[2])
if err != nil {
fmt.Println("invalid index")
continue
}
if crd < len(grave.Cards) {
fmt.Println(view_card(grave.Cards[crd]))
}
}
} else {
grave := c.Matches[match].Game.ScourgeGrave
fmt.Println(grave)
if len(cmd) == 3 {
crd, err := strconv.Atoi(cmd[2])
if err != nil {
fmt.Println("invalid index")
continue
}
if crd < len(grave.Cards) {
fmt.Println(view_card(grave.Cards[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 %v %v %v", c.Type, c.BasePower, c.Power, c.Id, c.Sick, c.Spell, c.Token, c.Phased, c.Counters, c.Owner, c.Position, c.Effects)
}