From e0d10723ecad6586715ba16edb48713054de1f8a Mon Sep 17 00:00:00 2001
From: Thomas Rudin <thomas@rudin.io>
Date: Sun, 29 Dec 2019 15:43:46 +0100
Subject: [PATCH] externalize worldstats module

basis for #76
---
 static/js/components/WorldStats.js | 49 ++++++++++++++++++++++++++++
 static/js/map/WorldInfoDisplay.js  | 52 ++----------------------------
 static/js/stats.js                 |  0
 static/stats.html                  | 22 +++++++++++++
 4 files changed, 73 insertions(+), 50 deletions(-)
 create mode 100644 static/js/components/WorldStats.js
 create mode 100644 static/js/stats.js
 create mode 100644 static/stats.html

diff --git a/static/js/components/WorldStats.js b/static/js/components/WorldStats.js
new file mode 100644
index 0000000..303ea79
--- /dev/null
+++ b/static/js/components/WorldStats.js
@@ -0,0 +1,49 @@
+
+export default function(info){
+
+  var timeIcon = m("span", { class: "fa fa-sun", style: "color: orange;" });
+
+  if (info.time < 5500 || info.time > 19000) //0 - 24'000
+    timeIcon = m("span", { class: "fa fa-moon", style: "color: blue;" });
+
+  function getHour(){
+    return Math.floor(info.time/1000);
+  }
+
+  function getMinute(){
+    var min = Math.floor((info.time % 1000) / 1000 * 60);
+    return min > 10 ? min : "0" + min;
+  }
+
+  function getLag(){
+    var color = "green";
+    if (info.max_lag > 0.8)
+      color = "orange";
+    else if (info.max_lag > 1.2)
+      color = "red";
+
+    return [
+      m("span", { class: "fa fa-wifi", style: "color: " + color }),
+      parseInt(info.max_lag*1000),
+      " ms"
+    ];
+  }
+
+  function getPlayers(){
+    return [
+      m("span", { class: "fa fa-users" }),
+      info.players ? info.players.length : "0"
+    ];
+  }
+
+  return m("div", [
+    getPlayers(),
+    " ",
+    getLag(),
+    " ",
+    m("span", { class: "fa fa-clock" }),
+    timeIcon,
+    getHour(), ":", getMinute()
+  ]);
+
+};
diff --git a/static/js/map/WorldInfoDisplay.js b/static/js/map/WorldInfoDisplay.js
index 33f15ac..8627840 100644
--- a/static/js/map/WorldInfoDisplay.js
+++ b/static/js/map/WorldInfoDisplay.js
@@ -1,53 +1,5 @@
 /* exported WorldInfoDisplay */
-
-var worldInfoRender = function(info){
-
-  var timeIcon = m("span", { class: "fa fa-sun", style: "color: orange;" });
-
-  if (info.time < 5500 || info.time > 19000) //0 - 24'000
-    timeIcon = m("span", { class: "fa fa-moon", style: "color: blue;" });
-
-  function getHour(){
-    return Math.floor(info.time/1000);
-  }
-
-  function getMinute(){
-    var min = Math.floor((info.time % 1000) / 1000 * 60);
-    return min > 10 ? min : "0" + min;
-  }
-
-  function getLag(){
-    var color = "green";
-    if (info.max_lag > 0.8)
-      color = "orange";
-    else if (info.max_lag > 1.2)
-      color = "red";
-
-    return [
-      m("span", { class: "fa fa-wifi", style: "color: " + color }),
-      parseInt(info.max_lag*1000),
-      " ms"
-    ];
-  }
-
-  function getPlayers(){
-    return [
-      m("span", { class: "fa fa-users" }),
-      info.players ? info.players.length : "0"
-    ];
-  }
-
-  return m("div", [
-    getPlayers(),
-    " ",
-    getLag(),
-    " ",
-    m("span", { class: "fa fa-clock" }),
-    timeIcon,
-    getHour(), ":", getMinute()
-  ]);
-
-};
+import WorldStats from '../components/WorldStats.js';
 
 // coord display
 export default L.Control.extend({
@@ -60,7 +12,7 @@ export default L.Control.extend({
       var div = L.DomUtil.create('div', 'leaflet-bar leaflet-custom-display');
 
       this.wsChannel.addListener("minetest-info", function(info){
-        m.render(div, worldInfoRender(info));
+        m.render(div, WorldStats(info));
       });
 
       return div;
diff --git a/static/js/stats.js b/static/js/stats.js
new file mode 100644
index 0000000..e69de29
diff --git a/static/stats.html b/static/stats.html
new file mode 100644
index 0000000..a761b20
--- /dev/null
+++ b/static/stats.html
@@ -0,0 +1,22 @@
+<!DOCTYPE HTML>
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+
+    <!-- styles -->
+    <link rel="stylesheet" href="css/fontawesome.min.css"/>
+    <title>Minetest Mapserver stats</title>
+  </head>
+  <body>
+    <div id="app"></div>
+
+    <!-- libraries -->
+    <script src="js/lib/mithril.min.js"></script>
+
+    <!-- main module -->
+    <script src="js/main.js?v=1.0" type="module"></script>
+
+    <!-- no modules -->
+    <script src="js/bundle.js?v=1.0" nomodule></script>
+  </body>
+</html>