Repos / gorts / e3a81b240e
commit e3a81b240eb7e7c531eb70159725178625293532
Author: Nhân <hi@imnhan.com>
Date:   Fri Jun 16 21:35:08 2023 +0700

    read init state from json
    
    Tk doesn't seem to play well with flag emojis tho

diff --git a/main.go b/main.go
index c18c4fa..c81e011 100644
--- a/main.go
+++ b/main.go
@@ -3,10 +3,14 @@
 import (
 	"bufio"
 	_ "embed"
+	"encoding/json"
 	"io"
+	"io/ioutil"
 	"log"
 	"net/http"
+	"os"
 	"os/exec"
+	"strconv"
 	"sync"
 )
 
@@ -61,6 +65,9 @@ func connectTclProc() {
 	io.WriteString(stdin, mainTcl)
 	println("Loaded main tcl script.")
 
+	// TODO: this should probably be refactored out
+	state := initState()
+
 	io.WriteString(stdin, "readvars\n")
 
 	reqscanner := bufio.NewScanner(stdout)
@@ -69,7 +76,7 @@ func connectTclProc() {
 		println("=> " + req)
 		switch req {
 		case "readvars":
-			for _, line := range serveReadvars() {
+			for _, line := range serveReadvars(state) {
 				println("<= " + line)
 				io.WriteString(stdin, line+"\n")
 			}
@@ -85,17 +92,42 @@ func connectTclProc() {
 	}
 }
 
-func serveReadvars() []string {
+func serveReadvars(s State) []string {
 	return []string{
-		"description Saigon Cup 2023",
-		"p1name Diego Umejuarez",
-		"p1country jp",
-		"p1score 1",
-		"p1team Japan",
-		"p2name Chokido",
-		"p2country jp",
-		"p2score 2",
-		"p2team Japan2",
+		"description " + s.Description,
+		"p1name " + s.P1name,
+		"p1country " + s.P1country,
+		"p1score " + strconv.Itoa(s.P1score),
+		"p1team " + s.P1team,
+		"p2name " + s.P2name,
+		"p2country " + s.P2country,
+		"p2score " + strconv.Itoa(s.P2score),
+		"p2team " + s.P2team,
 		"end",
 	}
 }
+
+type State struct {
+	Description string `json:"description"`
+	P1name      string `json:"p1name"`
+	P1country   string `json:"p1country"`
+	P1score     int    `json:"p1score"`
+	P1team      string `json:"p1team"`
+	P2name      string `json:"p2name"`
+	P2country   string `json:"p2country"`
+	P2score     int    `json:"p2score"`
+	P2team      string `json:"p2team"`
+}
+
+func initState() State {
+	var state State
+	file, err := os.Open(StateFile)
+	if err == nil {
+		bytes, err := ioutil.ReadAll(file)
+		if err != nil {
+			panic(err)
+		}
+		json.Unmarshal(bytes, &state)
+	}
+	return state
+}
diff --git a/web/state.json b/web/state.json
index 11d2151..b66d8f0 100644
--- a/web/state.json
+++ b/web/state.json
@@ -3,9 +3,9 @@
     "p1name": "CYG BST | | Fuudo",
     "p1country": "🇯🇵",
     "p1team": "",
-    "p1score": "6",
+    "p1score": 6,
     "p2name": "B.O.B | Xiaobao",
     "p2country": "🇯🇵",
-    "p2score": "1",
+    "p2score": 1,
     "p2team": ""
 }