changeset 1800:7c7f24e27a60

Allow specifying of map list to search in the query, and also max number of results.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 29 Oct 2017 21:54:12 +0200
parents 895606ee727a
children 03db1dc33af9
files mapsearch.c www/search.js
diffstat 2 files changed, 81 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Sun Oct 29 21:15:12 2017 +0200
+++ b/mapsearch.c	Sun Oct 29 21:54:12 2017 +0200
@@ -623,16 +623,90 @@
 }
 
 
-void mapPerformSearch(struct lws *wsi, const unsigned char *data, const size_t len, char **verr)
+void mapPerformSearch(struct lws *wsi, unsigned char *data, const size_t len, char **verr)
 {
     int width, height, centerX, centerY;
     MapBlock *pattern = NULL;
+    BOOL mapList[SET_MAX_MAPS];
     MAPMatch matches[SET_MAX_MATCHES];
-    int nmatches = 0, maxMatches = SET_MAX_MATCHES;
+    int nmatches = 0, maxMatches, nmapList;
     BOOL centerFound;
+    size_t offs;
+
+    // Get requested number of matches
+    for (offs = 0; offs < len && data[offs] != ':';)
+        offs++;
+
+    if (data[offs] != ':' || offs >= len)
+    {
+        *verr = "Invalid search query.";
+        goto out;
+    }
+
+    data[offs++] = 0;
+
+    maxMatches = atoi((char *) data);
+    if (maxMatches < 1)
+        maxMatches = 1;
+    else
+    if (maxMatches > SET_MAX_MATCHES)
+        maxMatches = SET_MAX_MATCHES;
+
+    // Get active map list
+    nmapList = 0;
+    memset(&mapList, 0, sizeof(mapList));
+    while (offs < len)
+    {
+        size_t offs2;
+        BOOL found = FALSE;
+
+        // Find next separator or end \n
+        for (offs2 = offs; offs2 < len && data[offs2] != ':' && data[offs2] != '\n';)
+            offs2++;
+
+        // Check against map list
+        if (offs2 > offs)
+        for (int nmap = 0; nmap < optNMaps && !found; nmap++)
+        {
+            const char *name = optMaps[nmap].locFile.continent;
+            const size_t slen = strlen(name);
+
+            if (offs2 >= offs + slen && memcmp(data + offs, name, slen) == 0)
+            {
+                mapList[nmap] = TRUE;
+                nmapList++;
+                found = TRUE;
+            }
+        }
+
+        if (!found)
+        {
+            *verr = "Unknown map spec.";
+            goto out;
+        }
+
+        // Check for separator or end
+        if (data[offs2] != ':')
+        {
+            offs = offs2;
+            break;
+        }
+        else
+            offs = offs2 + 1;
+    }
+
+    // Check for remaining data
+    if (offs + 1 >= len || data[offs] != '\n')
+    {
+        *verr = "No map pattern data!";
+        goto out;
+    }
+    offs++;
+
+    THMSG(0, "Data offs=%d, len=%d\n", offs, len);
 
     // Parse pattern block dimensions
-    mapBlockGetDimensions(data, len, &width, &height);
+    mapBlockGetDimensions(data + offs, len - offs, &width, &height);
     if (width <= 0 || height <= 0)
     {
         *verr = "Could not parse map block dimensions.";
@@ -648,7 +722,7 @@
     if ((pattern = mapBlockAlloc(width, height)) == NULL)
         goto out;
 
-    if (!mapBlockParse(data, len, pattern))
+    if (!mapBlockParse(data + offs, len - offs, pattern))
     {
         *verr = "Error parsing map block data.";
         goto out;
@@ -674,6 +748,7 @@
     // Search the maps .. enabled or if none specified, all of them
     //
     for (int nmap = 0; nmap < optNMaps; nmap++)
+    if (mapList[nmap] || nmapList == 0)
     {
         MAPInfoCtx *info = &optMaps[nmap];
 
@@ -758,7 +833,7 @@
             char *verr = NULL;
 
             // Check what the request is about?
-            if (len >= 10 && strncmp(data, "MAPSEARCH\n", 10) == 0)
+            if (len >= 10 && strncmp(data, "MAPSEARCH:", 10) == 0)
             {
                 // A map search query!
                 if (len <= 10 + 1)
--- a/www/search.js	Sun Oct 29 21:15:12 2017 +0200
+++ b/www/search.js	Sun Oct 29 21:54:12 2017 +0200
@@ -105,7 +105,7 @@
   {
     // Web Socket is connected, send data using send()
     mapLog("Sending query to server.");
-    mapWS.send("MAPSEARCH:"+ searchList.join(":") +"\n" + fieldPattern.value);
+    mapWS.send("MAPSEARCH:"+ 10 +":"+ searchList.join(":") +"\n" + fieldPattern.value);
   };
 
   mapWS.onmessage = function(evt)