Repos / gorts / a90cf7130a
commit a90cf7130a5a4471fda6347ad695cb362cd10260
Author: Nhân <hi@imnhan.com>
Date:   Fri Jun 16 23:31:18 2023 +0700

    simplify readstate too

diff --git a/.gitignore b/.gitignore
index 9b1c8b1..58c4c79 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 /dist
+/web/state.json
diff --git a/main.go b/main.go
index f49a57f..8074cc1 100644
--- a/main.go
+++ b/main.go
@@ -71,34 +71,35 @@ func connectTclProc() {
 	io.WriteString(stdin, "readstate\n")
 
 	scanner := bufio.NewScanner(stdout)
+
+	next := func() string {
+		scanner.Scan()
+		v := scanner.Text()
+		println("=>", v)
+		return v
+	}
+
+	respond := func(s string) {
+		println("<=", s)
+		io.WriteString(stdin, s+"\n")
+	}
+
 	for scanner.Scan() {
 		req := scanner.Text()
 		println("=> " + req)
 		switch req {
 		case "readstate":
-			for _, line := range []string{
-				"description " + state.Description,
-				"p1name " + state.P1name,
-				"p1country " + state.P1country,
-				"p1score " + strconv.Itoa(state.P1score),
-				"p1team " + state.P1team,
-				"p2name " + state.P2name,
-				"p2country " + state.P2country,
-				"p2score " + strconv.Itoa(state.P2score),
-				"p2team " + state.P2team,
-				"end",
-			} {
-				println("<= " + line)
-				io.WriteString(stdin, line+"\n")
-			}
-		case "applystate":
-			next := func() string {
-				scanner.Scan()
-				v := scanner.Text()
-				println("=>", v)
-				return v
-			}
 			// TODO: there must be more... civilized way.
+			respond(state.Description)
+			respond(state.P1name)
+			respond(state.P1country)
+			respond(strconv.Itoa(state.P1score))
+			respond(state.P1team)
+			respond(state.P2name)
+			respond(state.P2country)
+			respond(strconv.Itoa(state.P2score))
+			respond(state.P2team)
+		case "applystate":
 			state.Description = next()
 			state.P1name = next()
 			state.P1country = next()
diff --git a/tcl/main.tcl b/tcl/main.tcl
index 593a6bf..7830595 100644
--- a/tcl/main.tcl
+++ b/tcl/main.tcl
@@ -78,30 +78,26 @@ grid columnconfigure .c.buttons 3 -pad 15
 
 # Very simple line-based IPC where Tcl client talks to Go server
 # via stdin/stdout.
-#
-# For this "readstate" method, the Go server returns multiple lines
-# where each line starts with variable name, followed by a space,
-# with the rest of the line being its value. When done, the server
-# sends a literal "end" line.
-#
-# => readstate
-# <= description Saigon Cup 2023
-# <= p1name BST Diego Umejuarez
-# <= p1score 0
-# [etc.]
-# <= end
 proc readstate {} {
     puts "readstate"
-    set line [gets stdin]
-    while {$line != "end"} {
-        set spaceindex [string first " " $line]
-        set key [string range $line 0 $spaceindex-1]
-        set val [string range $line $spaceindex+1 end]
-        # this makes sure it sets the outer scope's variable:
-        variable ${key}
-        set ${key} $val
-        set line [gets stdin]
-    }
+    variable description
+    variable p1name
+    variable p1country
+    variable p1score
+    variable p1team
+    variable p2name
+    variable p2country
+    variable p2score
+    variable p2team
+    set description [gets stdin]
+    set p1name [gets stdin]
+    set p1country [gets stdin]
+    set p1score [gets stdin]
+    set p1team [gets stdin]
+    set p2name [gets stdin]
+    set p2country [gets stdin]
+    set p2score [gets stdin]
+    set p2team [gets stdin]
 }
 
 proc applystate {} {
diff --git a/web/state.json b/web/state.sample.json
similarity index 91%
rename from web/state.json
rename to web/state.sample.json
index 6358e75..1223710 100644
--- a/web/state.json
+++ b/web/state.sample.json
@@ -2,10 +2,10 @@
     "description": "Saigon Cup 2023",
     "p1name": "BST CYG Diego Umejuarez",
     "p1country": "jp",
+    "p1score": 10,
     "p1team": "Team Japan",
-    "p1score": 6,
     "p2name": "Jiyuner",
     "p2country": "us",
     "p2score": 1,
     "p2team": "Team Japan2"
-}
+}
\ No newline at end of file