Repos / gorts / 12a074856a
commit 12a074856aca472fb8601819aae260f28900cd34
Author: Nhân <hi@imnhan.com>
Date:   Fri Jun 16 21:03:34 2023 +0700

    spin up web server

diff --git a/README.md b/README.md
index 00bac5d..1c61707 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
 # GORTS
 
 ... is [orts](https://github.com/nhanb/orts) but in pure Go and pure Tcl/Tk
-purely talking via good ole pipes, the way Dennis Ritchie intended.
+passing messages through good ole pipes, the way Bell Labs intended.
diff --git a/main.go b/main.go
index e3673a3..c18c4fa 100644
--- a/main.go
+++ b/main.go
@@ -5,38 +5,55 @@
 	_ "embed"
 	"io"
 	"log"
+	"net/http"
 	"os/exec"
+	"sync"
 )
 
+const WebPort = "1337"
+const WebDir = "web"
+const StateFile = WebDir + "/state.json"
+
 //go:embed tcl/main.tcl
 var mainTcl string
 
 func main() {
-	tcl := initTcl()
-	tcl.Connect()
-}
+	var wg sync.WaitGroup
 
-type Tcl struct {
-	cmd *exec.Cmd
-}
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		connectTclProc()
+	}()
 
-func initTcl() Tcl {
-	cmd := exec.Command("tclsh")
-	return Tcl{cmd}
+	// No need to wait on the http server,
+	// just let it die when the GUI is closed.
+	go func() {
+		println("Serving scoreboard at http://localhost:" + WebPort)
+		fs := http.FileServer(http.Dir(WebDir))
+		http.Handle("/", fs)
+		err := http.ListenAndServe("127.0.0.1:"+WebPort, nil)
+		if err != nil {
+			log.Fatal(err)
+		}
+	}()
+
+	wg.Wait()
 }
 
-func (t *Tcl) Connect() {
-	stdout, err := t.cmd.StdoutPipe()
+func connectTclProc() {
+	cmd := exec.Command("tclsh")
+	stdout, err := cmd.StdoutPipe()
 	if err != nil {
 		panic(err)
 	}
 
-	stdin, err := t.cmd.StdinPipe()
+	stdin, err := cmd.StdinPipe()
 	if err != nil {
 		panic(err)
 	}
 
-	err = t.cmd.Start()
+	err = cmd.Start()
 	if err != nil {
 		panic(err)
 	}
diff --git a/web/animation.css b/web/animation.css
new file mode 100644
index 0000000..49ba4a8
--- /dev/null
+++ b/web/animation.css
@@ -0,0 +1,13 @@
+@keyframes fade {
+    from {opacity: 0;}
+    to   {opacity: 1;}
+}
+
+.fade {
+    animation-name: fade;
+    animation-duration: 1s;
+    animation-timing-function: ease; /* ease is the default */
+    animation-delay: 0s;             /* 0 is the default */
+    animation-iteration-count: 1;    /* 1 is the default */
+    animation-direction: alternate;  /* normal is the default */
+}
diff --git a/web/assets/fonts/SIL Open Font License.txt b/web/assets/fonts/SIL Open Font License.txt
new file mode 100644
index 0000000..d4bc2f0
--- /dev/null
+++ b/web/assets/fonts/SIL Open Font License.txt	
@@ -0,0 +1,44 @@
+Copyright (c) 2012, Vernon Adams (vern@newtypography.co.uk),
+with Reserved Font Name Oswald
+
+This Font Software is licensed under the SIL Open Font License, Version 1.1.
+This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL
+
+-----------------------------------------------------------
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+-----------------------------------------------------------
+
+PREAMBLE
+The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives.
+
+DEFINITIONS
+"Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the copyright statement(s).
+
+"Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment.
+
+"Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software.
+
+PERMISSION & CONDITIONS
+Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions:
+
+1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission.
+
+5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software.
+
+TERMINATION
+This license becomes null and void if any of the above conditions are not met.
+
+DISCLAIMER
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
\ No newline at end of file
diff --git a/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-500.woff b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-500.woff
new file mode 100644
index 0000000..b3e9884
Binary files /dev/null and b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-500.woff differ
diff --git a/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-600.woff b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-600.woff
new file mode 100644
index 0000000..9795b96
Binary files /dev/null and b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-600.woff differ
diff --git a/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-700.woff b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-700.woff
new file mode 100644
index 0000000..062d835
Binary files /dev/null and b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-700.woff differ
diff --git a/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-regular.woff b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-regular.woff
new file mode 100644
index 0000000..cc6a4b9
Binary files /dev/null and b/web/assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-regular.woff differ
diff --git a/web/assets/logo-sfgc.png b/web/assets/logo-sfgc.png
new file mode 100644
index 0000000..69a125a
Binary files /dev/null and b/web/assets/logo-sfgc.png differ
diff --git a/web/assets/scoreboard.png b/web/assets/scoreboard.png
new file mode 100644
index 0000000..4b3863f
Binary files /dev/null and b/web/assets/scoreboard.png differ
diff --git a/web/index.css b/web/index.css
new file mode 100644
index 0000000..f75719b
--- /dev/null
+++ b/web/index.css
@@ -0,0 +1,155 @@
+/*
+Fonts lifted from https://google-webfonts-helper.herokuapp.com.
+I'm using woff instead of woff2 simply because OBS's Browser Source
+plugin apparently came with a pretty old Chromium.
+*/
+@font-face {
+  font-family: "Oswald";
+  font-style: normal;
+  font-weight: 400;
+  src: local("Oswald Regular"), local("Oswald-Regular"),
+    url("assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-regular.woff")
+      format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
+}
+
+/* oswald-500 - vietnamese_cyrillic_latin_latin-ext */
+@font-face {
+  font-family: "Oswald";
+  font-style: normal;
+  font-weight: 500;
+  src: local("Oswald Medium"), local("Oswald-Medium"),
+    url("assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-500.woff")
+      format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
+}
+
+/* oswald-600 - vietnamese_cyrillic_latin_latin-ext */
+@font-face {
+  font-family: "Oswald";
+  font-style: normal;
+  font-weight: 600;
+  src: local("Oswald SemiBold"), local("Oswald-SemiBold"),
+    url("assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-600.woff")
+      format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
+}
+
+/* oswald-700 - vietnamese_cyrillic_latin_latin-ext */
+@font-face {
+  font-family: "Oswald";
+  font-style: normal;
+  font-weight: 700;
+  src: local("Oswald Bold"), local("Oswald-Bold"),
+    url("assets/fonts/oswald-v17-vietnamese_cyrillic_latin_latin-ext-700.woff")
+      format("woff"); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
+}
+
+body {
+  width: 1920px;
+  height: 1080px;
+  /*box-sizing: border-box;*/
+  /*border: 2px solid red;*/
+  overflow: hidden;
+  padding: 0;
+  margin: 0;
+  position: relative;
+  font-weight: 600;
+}
+
+#scoreboard {
+  text-align: center;
+  position: relative;
+}
+
+#p1score,
+#p2score,
+#p1name,
+#p2name,
+#p1country,
+#p2country,
+#p1team,
+#p2team,
+#description {
+  position: absolute;
+  top: 0;
+  bottom: 0;
+  color: white;
+  font-family: "Oswald", sans-serif;
+  text-transform: uppercase;
+}
+
+#p1team,
+#p2team {
+  font-size: 32px;
+  font-family: "Oswald", sans-serif;
+  text-shadow: 0px 0px 20px black;
+}
+
+#description {
+  left: 0;
+  right: 0;
+  top: 6px;
+  text-transform: uppercase;
+  font-size: 23px;
+}
+
+#p1country,
+#p2country {
+  font-size: 2em;
+  top: -3px;
+}
+
+#p1country {
+  left: 340px;
+}
+
+#p2country {
+  right: 340px;
+}
+
+#p1score,
+#p2score {
+  top: 2px;
+  color: black;
+  font-size: 32px;
+}
+
+#p1score {
+  left: 285px;
+}
+
+#p2score {
+  right: 285px;
+}
+
+#p1name,
+#p2name {
+  top: 1px;
+  font-size: 28px;
+}
+
+#p1name {
+  left: 390px;
+}
+
+#p2name {
+  right: 390px;
+}
+
+#p1team {
+  top: 182px;
+  left: 153px;
+}
+
+#p2team {
+  top: 182px;
+  right: 153px;
+}
+
+#logo {
+  display: block;
+  position: absolute;
+  bottom: 10px;
+  left: 0;
+  right: 0;
+  margin-left: auto;
+  margin-right: auto;
+}
diff --git a/web/index.html b/web/index.html
new file mode 100644
index 0000000..d6c0e05
--- /dev/null
+++ b/web/index.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<meta charset="utf-8" />
+<title>ESPORTS</title>
+
+<body>
+  <div id="scoreboard">
+    <img src="assets/scoreboard.png" />
+
+    <div id="description"></div>
+
+    <div id="p1score"></div>
+    <span id="p1country"></span>
+    <div id="p1name"></div>
+    <div id="p1team"></div>
+
+    <div id="p2score"></div>
+    <span id="p2country"></span>
+    <div id="p2name"></div>
+    <div id="p2team"></div>
+  </div>
+  <img id="logo" src="assets/logo-sfgc.png" />
+</body>
+
+<link href="index.css" rel="stylesheet" />
+<link href="animation.css" rel="stylesheet" />
+<script src="index.js"></script>
diff --git a/web/index.js b/web/index.js
new file mode 100644
index 0000000..ab93e09
--- /dev/null
+++ b/web/index.js
@@ -0,0 +1,71 @@
+const fadeUpdate = (element, newValue, updateFunc) => {
+  element.classList.add("fade");
+  updateFunc();
+  // Assuming whole animation duration == 1s
+  setTimeout(() => {
+    element.classList.remove("fade");
+  }, 1000);
+};
+
+const getDiff = (newState, oldState) => {
+  const diff = {};
+  Object.keys(newState).forEach((key) => {
+    if (newState[key] !== oldState[key]) {
+      diff[key] = {
+        old: oldState[key],
+        new: newState[key],
+      };
+    }
+  });
+  return diff;
+};
+
+const drawDiffToDom = (diff) => {
+  Object.keys(diff).forEach((key) => {
+    const element = document.querySelector(`#${key}`);
+    if (!element) {
+      return;
+    } // skip fields not used in DOM
+
+    const newValue = diff[key]["new"];
+    const oldValue = diff[key]["old"];
+
+    // Most elements are straightforward to overwrite
+    let updateFunc = () => {
+      element.innerHTML = newValue;
+    };
+
+    fadeUpdate(element, newValue, updateFunc);
+  });
+};
+
+const applyNewState = (newState) => {
+  // first calculate just what has changed
+  const diff = getDiff(newState, window.STATE);
+
+  // then only draw the changed stuff to DOM, complete with animation
+  drawDiffToDom(diff);
+
+  // advance the global state
+  window.STATE = newState;
+};
+
+const fetchHeaders = new Headers();
+fetchHeaders.append("pragma", "no-cache");
+fetchHeaders.append("cache-control", "no-cache");
+const fetchInit = {
+  method: "GET",
+  headers: fetchHeaders,
+};
+const pollState = () => {
+  fetch("state.json", fetchInit)
+    .then((response) => response.json())
+    .then(applyNewState);
+};
+
+/*
+ * ACTUAL CODE FLOW STARTS HERE
+ */
+window.STATE = {}; // state singleton, globally accessible
+pollState(); // immediately populate data to avoid empty values on page load
+setInterval(pollState, 1500);
diff --git a/web/state.json b/web/state.json
new file mode 100644
index 0000000..11d2151
--- /dev/null
+++ b/web/state.json
@@ -0,0 +1,11 @@
+{
+    "description": "asdfasdf",
+    "p1name": "CYG BST | | Fuudo",
+    "p1country": "🇯🇵",
+    "p1team": "",
+    "p1score": "6",
+    "p2name": "B.O.B | Xiaobao",
+    "p2country": "🇯🇵",
+    "p2score": "1",
+    "p2team": ""
+}