Repos / gorts / 02dc2384ac
commit 02dc2384ace736093cd0dff6f3a13a072839f053
Author: Nhân <hi@imnhan.com>
Date:   Fri Jun 16 23:20:20 2023 +0700

    applystate

diff --git a/main.go b/main.go
index c81e011..f49a57f 100644
--- a/main.go
+++ b/main.go
@@ -68,18 +68,48 @@ func connectTclProc() {
 	// TODO: this should probably be refactored out
 	state := initState()
 
-	io.WriteString(stdin, "readvars\n")
+	io.WriteString(stdin, "readstate\n")
 
-	reqscanner := bufio.NewScanner(stdout)
-	for reqscanner.Scan() {
-		req := reqscanner.Text()
+	scanner := bufio.NewScanner(stdout)
+	for scanner.Scan() {
+		req := scanner.Text()
 		println("=> " + req)
 		switch req {
-		case "readvars":
-			for _, line := range serveReadvars(state) {
+		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.
+			state.Description = next()
+			state.P1name = next()
+			state.P1country = next()
+			state.P1score, _ = strconv.Atoi(next())
+			state.P1team = next()
+			state.P2name = next()
+			state.P2country = next()
+			state.P2score, _ = strconv.Atoi(next())
+			state.P2team = next()
+			state.Write()
+
 		default:
 			println("Skipping bogus command: " + req)
 		}
@@ -87,26 +117,11 @@ func connectTclProc() {
 
 	println("Tcl process terminated.")
 
-	if err := reqscanner.Err(); err != nil {
+	if err := scanner.Err(); err != nil {
 		log.Fatal(err)
 	}
 }
 
-func serveReadvars(s State) []string {
-	return []string{
-		"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"`
@@ -123,6 +138,7 @@ func initState() State {
 	var state State
 	file, err := os.Open(StateFile)
 	if err == nil {
+		defer file.Close()
 		bytes, err := ioutil.ReadAll(file)
 		if err != nil {
 			panic(err)
@@ -131,3 +147,14 @@ func initState() State {
 	}
 	return state
 }
+
+func (s *State) Write() {
+	blob, err := json.MarshalIndent(s, "", "    ")
+	if err != nil {
+		panic(err)
+	}
+	err = ioutil.WriteFile(StateFile, blob, 0644)
+	if err != nil {
+		panic(err)
+	}
+}
diff --git a/tcl/main.tcl b/tcl/main.tcl
index 0e4b9dc..593a6bf 100644
--- a/tcl/main.tcl
+++ b/tcl/main.tcl
@@ -39,7 +39,7 @@ ttk::button .c.players.p2win -text "▲ Win" -width 5
 ttk::label .c.players.p2teamlbl -text "Team 2"
 ttk::combobox .c.players.p2team -textvariable p2team
 ttk::frame .c.buttons
-ttk::button .c.buttons.apply -text "▶ Apply"
+ttk::button .c.buttons.apply -text "▶ Apply" -command applystate
 ttk::button .c.buttons.discard -text "✖ Discard"
 ttk::button .c.buttons.reset -text "↶ Reset scores"
 ttk::button .c.buttons.swap -text "⇄ Swap players"
@@ -79,19 +79,19 @@ grid columnconfigure .c.buttons 3 -pad 15
 # Very simple line-based IPC where Tcl client talks to Go server
 # via stdin/stdout.
 #
-# For this "readvars" method, the Go server returns multiple lines
+# 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.
 #
-# => readvars
+# => readstate
 # <= description Saigon Cup 2023
 # <= p1name BST Diego Umejuarez
 # <= p1score 0
 # [etc.]
 # <= end
-proc readvars {} {
-    puts "readvars"
+proc readstate {} {
+    puts "readstate"
     set line [gets stdin]
     while {$line != "end"} {
         set spaceindex [string first " " $line]
@@ -103,3 +103,25 @@ proc readvars {} {
         set line [gets stdin]
     }
 }
+
+proc applystate {} {
+    puts "applystate"
+    variable description
+    variable p1name
+    variable p1country
+    variable p1score
+    variable p1team
+    variable p2name
+    variable p2country
+    variable p2score
+    variable p2team
+    puts $description
+    puts $p1name
+    puts $p1country
+    puts $p1score
+    puts $p1team
+    puts $p2name
+    puts $p2country
+    puts $p2score
+    puts $p2team
+}