changeset 1790:24e23feca10a

Add server functionality to provide list of maps available for searching, and implement viewing of those map names on the front-end. The idea is to be able to select the maps to be searched in the future.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 29 Oct 2017 17:14:57 +0200
parents 513c467f3a87
children 6ee732cab6ae
files mapsearch.c www/search.js
diffstat 2 files changed, 93 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Sun Oct 29 17:13:04 2017 +0200
+++ b/mapsearch.c	Sun Oct 29 17:14:57 2017 +0200
@@ -759,6 +759,31 @@
                     mapPerformSearch(wsi, udata + 10, len - 10, &verr);
             }
             else
+            if (len >= 7 && strncmp(data, "GETMAPS", 7) == 0)
+            {
+                char *buf = NULL;
+                size_t bufLen = 0, bufSize = 0;
+
+                th_strbuf_puts(&buf, &bufSize, &bufLen, "MAPS:[");
+
+                for (int n = 0; n < optNMaps; n++)
+                {
+                    MAPInfoCtx *info = &optMaps[n];
+                    char *vstr = th_strdup_printf(
+                        "[\"%s\",%d,%d]%s",
+                        info->locFile.continent,
+                        info->locFile.x + optWorldXC,
+                        info->locFile.y + optWorldYC,
+                        (n < optNMaps - 1) ? "," : "");
+
+                    th_strbuf_puts(&buf, &bufSize, &bufLen, vstr);
+                    th_free(vstr);
+                }
+
+                th_strbuf_puts(&buf, &bufSize, &bufLen, "]");
+                lws_write(wsi, (unsigned char *) buf, bufLen, LWS_WRITE_TEXT);
+            }
+            else
             {
                 verr = "Invalid command/search query, not enough data.";
             }
--- a/www/search.js	Sun Oct 29 17:13:04 2017 +0200
+++ b/www/search.js	Sun Oct 29 17:14:57 2017 +0200
@@ -138,10 +138,78 @@
 }
 
 
+function mapGetData()
+{
+  var tmpWS = new WebSocket("ws://localhost:9999/mapSearch");
+  if (!tmpWS)
+  {
+    mapLog("Could not create WebSocket connection?");
+    return;
+  }
+
+  tmpWS.onopen = function()
+  {
+    tmpWS.send("GETMAPS");
+  };
+
+  tmpWS.onmessage = function(evt)
+  {
+    if (evt.data.substr(0, 6) == "ERROR:")
+    {
+      mapLog("ERROR! "+ evt.data.substr(6));
+    }
+    else
+    if (evt.data.substr(0, 5) == "MAPS:" && evt.data.length > 8)
+    {
+      var results = JSON.parse(evt.data.substr(5));
+      mapLog("Receiving map information.");
+      if (results)
+      {
+        var str = "";
+        for (var i = 0; i < results.length; i++)
+        {
+          var res = results[i];
+          var id = "map_"+ res[0];
+          str += "<span class=\"map\"><input id=\""+ id +"\" type=\"checkbox\" checked=\"checked\"><label for=\""+ id +"\">"+ res[0] +"</label></span>";
+        }
+        var elem = document.getElementById("maps");
+        elem.innerHTML = str;
+      }
+      else
+        fieldRes.innerHTML = "ERROR!";
+    }
+    else
+    if (evt.data == "END")
+    {
+      mapLog("Server ending communication.");
+      tmpWS.close();
+    }
+    else
+    {
+      mapLog("Sir! A message for you! "+ evt.data);
+    }
+  };
+
+  tmpWS.onclose = function()
+  {
+    mapLog("WebSocket connection closed.");
+    tmpWS = null;
+  };
+
+  tmpWS.onerror = function()
+  {
+    mapLog("Error!");
+    tmpWS = null;
+  };
+}
+
+
 function mapInitSearch()
 {
   msgLog = [];
 
+  mapGetData();
+
   fieldPattern = document.getElementById("pattern");
   fieldRes = document.getElementById("results");
   fieldLog = document.getElementById("log");