# HG changeset patch # User Matti Hamalainen # Date 1509290097 -7200 # Node ID 24e23feca10ab4ff9bec2b365ee943d6792ceba5 # Parent 513c467f3a8792c00dcbbd409ef9ddf2b81d3086 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. diff -r 513c467f3a87 -r 24e23feca10a mapsearch.c --- 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."; } diff -r 513c467f3a87 -r 24e23feca10a www/search.js --- 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 += ""; + } + 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");