From cebe92014e90b6796b193fdca4d80f886753a779 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sat, 2 Feb 2019 16:21:20 +0100 Subject: [PATCH 1/8] proper //go:generate --- server/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/main.go b/server/main.go index b222652..55de793 100644 --- a/server/main.go +++ b/server/main.go @@ -2,16 +2,17 @@ package main import ( "fmt" - "github.com/sirupsen/logrus" "mapserver/app" "mapserver/mapobject" "mapserver/params" "mapserver/tilerendererjob" "mapserver/web" "runtime" + + "github.com/sirupsen/logrus" ) -//go:generate -command go run github.com/mjibson/esc -o vfs/static.go -prefix="static/" -pkg vfs static +//go:generate go run github.com/mjibson/esc -o vfs/static.go -prefix="static/" -pkg vfs static func main() { //Parse command line From 0da44f534e359686a2eb08423c61d65008693ce8 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sat, 2 Feb 2019 16:23:01 +0100 Subject: [PATCH 2/8] vfs generate in makefile --- server/Makefile | 2 +- server/main.go | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/server/Makefile b/server/Makefile index 58fd06f..1ab7b37 100644 --- a/server/Makefile +++ b/server/Makefile @@ -18,7 +18,7 @@ clean: rm -rf $(OUT_DIR) $(STATIC_VFS): - go generate + go run github.com/mjibson/esc -o vfs/static.go -prefix="static/" -pkg vfs static build: $(STATIC_VFS) $(OUT_DIR) # native diff --git a/server/main.go b/server/main.go index 55de793..6b219de 100644 --- a/server/main.go +++ b/server/main.go @@ -12,8 +12,6 @@ import ( "github.com/sirupsen/logrus" ) -//go:generate go run github.com/mjibson/esc -o vfs/static.go -prefix="static/" -pkg vfs static - func main() { //Parse command line From 258993b836021732e23932da22be516dd9c6655a Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sat, 2 Feb 2019 17:28:35 +0100 Subject: [PATCH 3/8] working travelnet layer --- server/static/index.html | 10 +++++++ server/static/js/main.js | 2 +- server/static/js/overlays/TravelnetOverlay.js | 28 +++++++++++-------- server/static/js/util/debounce.js | 14 ++++++++++ 4 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 server/static/js/util/debounce.js diff --git a/server/static/index.html b/server/static/index.html index 2616f63..a2b1458 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -11,15 +11,25 @@
+ + + + + + + + + + diff --git a/server/static/js/main.js b/server/static/js/main.js index 1704b4b..699c3a2 100644 --- a/server/static/js/main.js +++ b/server/static/js/main.js @@ -18,7 +18,7 @@ api.getConfig().then(function(cfg){ crs: SimpleCRS }); - map.attributionControl.addAttribution('Mapserver'); + map.attributionControl.addAttribution('Minetest Mapserver'); var layers = {}; var overlays = {} diff --git a/server/static/js/overlays/TravelnetOverlay.js b/server/static/js/overlays/TravelnetOverlay.js index d1335b6..0e8235b 100644 --- a/server/static/js/overlays/TravelnetOverlay.js +++ b/server/static/js/overlays/TravelnetOverlay.js @@ -10,11 +10,13 @@ var TravelnetIcon = L.icon({ var TravelnetOverlay = L.LayerGroup.extend({ initialize: function(wsChannel, layerMgr) { + L.LayerGroup.prototype.initialize.call(this); + this.layerMgr = layerMgr; this.wsChannel = wsChannel; this.onLayerChange = this.onLayerChange.bind(this); - this.onMapMove = this.onMapMove.bind(this); + this.onMapMove = debounce(this.onMapMove.bind(this), 50); }, onLayerChange: function(layer){ @@ -36,21 +38,20 @@ var TravelnetOverlay = L.LayerGroup.extend({ .then(function(travelnets){ //TODO: remove non-existing markers, add new ones if (!full) - this.clearLayers(); - - console.log(travelnets); - - //TODO: attributes, coords - var marker = L.marker([travelnet.z, travelnet.x], {icon: TravelnetIcon}); + self.clearLayers(); travelnets.forEach(function(travelnet){ - var popup = "

" + travelnet.name + "


" + + + console.log(travelnet); + + var popup = "

" + travelnet.attributes.station_name + "


" + "X: " + travelnet.x + "
" + "Y: " + travelnet.y + "
" + "Z: " + travelnet.z + "
" + - "Network: " + travelnet.network + "
" + - "Owner: " + travelnet.owner + "
"; + "Network: " + travelnet.attributes.station_network + "
" + + "Owner: " + travelnet.attributes.owner + "
"; + var marker = L.marker([travelnet.z, travelnet.x], {icon: TravelnetIcon}); marker.bindPopup(popup).addTo(self); }); }) @@ -58,12 +59,15 @@ var TravelnetOverlay = L.LayerGroup.extend({ }, onAdd: function(map) { + map.on("zoomend", this.onMapMove); + map.on("moveend", this.onMapMove); this.layerMgr.addListener(this.onLayerChange); - console.log("TravelnetOverlay.onAdd", map); + this.reDraw(true) }, onRemove: function(map) { + map.off("zoomend", this.onMapMove); + map.off("moveend", this.onMapMove); this.layerMgr.removeListener(this.onLayerChange); - console.log("TravelnetOverlay.onRemove"); } }); diff --git a/server/static/js/util/debounce.js b/server/static/js/util/debounce.js new file mode 100644 index 0000000..8985c23 --- /dev/null +++ b/server/static/js/util/debounce.js @@ -0,0 +1,14 @@ +function debounce(func, wait, immediate) { + var timeout; + return function() { + var context = this, args = arguments; + var later = function() { + timeout = null; + if (!immediate) func.apply(context, args); + }; + var callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); + }; +}; From 481c9477bbec91eaf2fe2d975f361930d453a001 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sat, 2 Feb 2019 17:40:52 +0100 Subject: [PATCH 4/8] current layer cache --- server/static/js/overlays/TravelnetOverlay.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/server/static/js/overlays/TravelnetOverlay.js b/server/static/js/overlays/TravelnetOverlay.js index 0e8235b..a9ede83 100644 --- a/server/static/js/overlays/TravelnetOverlay.js +++ b/server/static/js/overlays/TravelnetOverlay.js @@ -15,6 +15,8 @@ var TravelnetOverlay = L.LayerGroup.extend({ this.layerMgr = layerMgr; this.wsChannel = wsChannel; + this.currentLayers = []; + this.onLayerChange = this.onLayerChange.bind(this); this.onMapMove = debounce(this.onMapMove.bind(this), 50); }, @@ -30,15 +32,18 @@ var TravelnetOverlay = L.LayerGroup.extend({ reDraw: function(full){ var self = this; - if (full) + if (full){ this.clearLayers(); + this.currentLayers = []; + } //TODO: get coords api.getMapObjects(-10,-10,-10,10,10,10,"travelnet") .then(function(travelnets){ //TODO: remove non-existing markers, add new ones - if (!full) + if (!full){ self.clearLayers(); + } travelnets.forEach(function(travelnet){ From c87027931cd6dcbebdbb43bf435d56ef499f7819 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sat, 2 Feb 2019 18:03:07 +0100 Subject: [PATCH 5/8] dynamic overlay query --- server/static/js/overlays/TravelnetOverlay.js | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/server/static/js/overlays/TravelnetOverlay.js b/server/static/js/overlays/TravelnetOverlay.js index a9ede83..1b3cf30 100644 --- a/server/static/js/overlays/TravelnetOverlay.js +++ b/server/static/js/overlays/TravelnetOverlay.js @@ -15,7 +15,7 @@ var TravelnetOverlay = L.LayerGroup.extend({ this.layerMgr = layerMgr; this.wsChannel = wsChannel; - this.currentLayers = []; + this.currentObjects = []; this.onLayerChange = this.onLayerChange.bind(this); this.onMapMove = debounce(this.onMapMove.bind(this), 50); @@ -32,13 +32,33 @@ var TravelnetOverlay = L.LayerGroup.extend({ reDraw: function(full){ var self = this; - if (full){ + if (this._map.getZoom() < 10) { this.clearLayers(); - this.currentLayers = []; + this.currentObjects = []; + return; } + if (full){ + this.clearLayers(); + this.currentObjects = []; + } + + var mapLayer = this.layerMgr.getCurrentLayer() + var min = this._map.getBounds().getSouthWest(); + var max = this._map.getBounds().getNorthEast(); + + var y1 = parseInt(mapLayer.from/16); + var y2 = parseInt(mapLayer.to/16); + var x1 = parseInt(min.lng); + var x2 = parseInt(max.lng); + var z1 = parseInt(min.lat); + var z2 = parseInt(max.lat); + //TODO: get coords - api.getMapObjects(-10,-10,-10,10,10,10,"travelnet") + api.getMapObjects( + x1, y1, y1, + x2, y2, z2, + "travelnet") .then(function(travelnets){ //TODO: remove non-existing markers, add new ones if (!full){ @@ -46,9 +66,6 @@ var TravelnetOverlay = L.LayerGroup.extend({ } travelnets.forEach(function(travelnet){ - - console.log(travelnet); - var popup = "

" + travelnet.attributes.station_name + "


" + "X: " + travelnet.x + "
" + "Y: " + travelnet.y + "
" + @@ -71,6 +88,7 @@ var TravelnetOverlay = L.LayerGroup.extend({ }, onRemove: function(map) { + this.clearLayers(); map.off("zoomend", this.onMapMove); map.off("moveend", this.onMapMove); this.layerMgr.removeListener(this.onLayerChange); From 40c9386fdd6a121d491050df9320cea7ec4abde4 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sun, 3 Feb 2019 13:08:57 +0100 Subject: [PATCH 6/8] bridge wip --- mapserver_mod/mapserver/bridge.lua | 8 ++++--- server/web/minetest.go | 36 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/mapserver_mod/mapserver/bridge.lua b/mapserver_mod/mapserver/bridge.lua index fbbd40d..039b227 100644 --- a/mapserver_mod/mapserver/bridge.lua +++ b/mapserver_mod/mapserver/bridge.lua @@ -23,7 +23,7 @@ function send_stats() local data = { time = minetest.get_timeofday() * 24000, uptime = minetest.get_server_uptime(), - max_lag = get_max_lag(), + max_lag = tonumber(get_max_lag()), players = {} } @@ -36,11 +36,13 @@ function send_stats() velocity = player:get_player_velocity() } - table.insert(data.players, player) + table.insert(data.players, info) end + print(minetest.write_json(data)) --XXX + http.fetch({ - url = url, + url = url .. "/api/minetest", extra_headers = { "Content-Type: application/json", "Authorization: " .. key }, timeout = 1, post_data = minetest.write_json(data) diff --git a/server/web/minetest.go b/server/web/minetest.go index f8991cb..5dd9b0f 100644 --- a/server/web/minetest.go +++ b/server/web/minetest.go @@ -4,14 +4,48 @@ import ( "encoding/json" "mapserver/app" "net/http" + + "github.com/sirupsen/logrus" ) +type PlayerPos struct { + X float64 `json:"x"` + Y float64 `json:"y"` + Z float64 `json:"z"` +} + +type Player struct { + Pos PlayerPos `json:"pos"` + Name string `json:"name"` + HP int `json:"hp"` + Breath int `json:"breath"` + //TODO: stamina, skin, etc +} + +type MinetestInfo struct { + MaxLag float64 `json:"max_lag"` + Players []Player `json:"players"` + Time float64 `json:"time"` + Uptime float64 `json:"uptime"` +} + type Minetest struct { ctx *app.App } func (t *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) { resp.Header().Add("content-type", "application/json") - json.NewEncoder(resp).Encode("stub") + data := &MinetestInfo{} + err := json.NewDecoder(req.Body).Decode(data) + logrus.Info(data) + + if err != nil { + resp.WriteHeader(500) + resp.Write([]byte(err.Error())) + logrus.Warn(err) + return + } + + json.NewEncoder(resp).Encode("stub") } From 4231be5e124fceb9cc27e8a6b21fd7351ebffc9a Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sun, 3 Feb 2019 18:04:01 +0100 Subject: [PATCH 7/8] route mt updates to frontend --- mapserver_mod/mapserver/bridge.lua | 2 -- server/static/js/WebSocketChannel.js | 2 +- server/static/js/main.js | 4 ++++ server/web/minetest.go | 14 +++++++++----- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mapserver_mod/mapserver/bridge.lua b/mapserver_mod/mapserver/bridge.lua index 039b227..a66637c 100644 --- a/mapserver_mod/mapserver/bridge.lua +++ b/mapserver_mod/mapserver/bridge.lua @@ -39,8 +39,6 @@ function send_stats() table.insert(data.players, info) end - print(minetest.write_json(data)) --XXX - http.fetch({ url = url .. "/api/minetest", extra_headers = { "Content-Type: application/json", "Authorization: " .. key }, diff --git a/server/static/js/WebSocketChannel.js b/server/static/js/WebSocketChannel.js index 5d49d19..42adbec 100644 --- a/server/static/js/WebSocketChannel.js +++ b/server/static/js/WebSocketChannel.js @@ -25,7 +25,7 @@ WebSocketChannel.prototype.connect = function(){ var listeners = self.listenerMap[event.type]; if (listeners){ - self.listeners.forEach(function(listener){ + listeners.forEach(function(listener){ listener(event.data); }); } diff --git a/server/static/js/main.js b/server/static/js/main.js index 699c3a2..4dc35a7 100644 --- a/server/static/js/main.js +++ b/server/static/js/main.js @@ -5,6 +5,10 @@ api.getConfig().then(function(cfg){ var wsChannel = new WebSocketChannel(); wsChannel.connect(); + wsChannel.addListener("minetest-info", function(e){ + console.log(e); + }); + var rtTiles = new RealtimeTileLayer(wsChannel); var initialZoom = 11; diff --git a/server/web/minetest.go b/server/web/minetest.go index 5dd9b0f..ebf13cc 100644 --- a/server/web/minetest.go +++ b/server/web/minetest.go @@ -4,8 +4,6 @@ import ( "encoding/json" "mapserver/app" "net/http" - - "github.com/sirupsen/logrus" ) type PlayerPos struct { @@ -33,19 +31,25 @@ type Minetest struct { ctx *app.App } -func (t *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) { +func (this *Minetest) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + if req.Header.Get("Authorization") != this.ctx.Config.WebApi.SecretKey { + resp.WriteHeader(403) + resp.Write([]byte("invalid key!")) + return + } + resp.Header().Add("content-type", "application/json") data := &MinetestInfo{} err := json.NewDecoder(req.Body).Decode(data) - logrus.Info(data) if err != nil { resp.WriteHeader(500) resp.Write([]byte(err.Error())) - logrus.Warn(err) return } + this.ctx.WebEventbus.Emit("minetest-info", data) + json.NewEncoder(resp).Encode("stub") } From 8c98a1129bb02cc7cc91e5dcb71396a1fb468613 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sun, 3 Feb 2019 18:18:14 +0100 Subject: [PATCH 8/8] player overlay stub --- server/static/index.html | 3 ++ server/static/js/WebSocketChannel.js | 11 +++++ server/static/js/main.js | 3 +- server/static/js/overlays/PlayerOverlay.js | 53 ++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 server/static/js/overlays/PlayerOverlay.js diff --git a/server/static/index.html b/server/static/index.html index a2b1458..17e8320 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -27,7 +27,10 @@ + + + diff --git a/server/static/js/WebSocketChannel.js b/server/static/js/WebSocketChannel.js index 42adbec..d17af8c 100644 --- a/server/static/js/WebSocketChannel.js +++ b/server/static/js/WebSocketChannel.js @@ -15,6 +15,17 @@ WebSocketChannel.prototype.addListener = function(type, listener){ list.push(listener); }; +WebSocketChannel.prototype.removeListener = function(type, listener){ + var list = this.listenerMap[type]; + if (!list){ + return + } + + this.listenerMap[type] = list.filter(function(l){ + return l != listener; + }); +}; + WebSocketChannel.prototype.connect = function(){ var ws = new WebSocket(this.wsUrl); var self = this; diff --git a/server/static/js/main.js b/server/static/js/main.js index 4dc35a7..78d5c0f 100644 --- a/server/static/js/main.js +++ b/server/static/js/main.js @@ -6,7 +6,7 @@ api.getConfig().then(function(cfg){ wsChannel.connect(); wsChannel.addListener("minetest-info", function(e){ - console.log(e); + console.log(e); //XXX }); var rtTiles = new RealtimeTileLayer(wsChannel); @@ -33,6 +33,7 @@ api.getConfig().then(function(cfg){ tileLayer.addTo(map); layers["Base"] = tileLayer; + overlays["Players"] = new PlayerOverlay(wsChannel, layerMgr); overlays["Travelnet"] = new TravelnetOverlay(wsChannel, layerMgr); L.control.layers(layers, overlays).addTo(map); diff --git a/server/static/js/overlays/PlayerOverlay.js b/server/static/js/overlays/PlayerOverlay.js new file mode 100644 index 0000000..bfd8c5e --- /dev/null +++ b/server/static/js/overlays/PlayerOverlay.js @@ -0,0 +1,53 @@ +'use strict'; + +var PlayerIcon = L.icon({ + iconUrl: 'pics/sam.png', + + iconSize: [16, 32], + iconAnchor: [8, 16], + popupAnchor: [0, -16] +}); + +var PlayerOverlay = L.LayerGroup.extend({ + initialize: function(wsChannel, layerMgr) { + L.LayerGroup.prototype.initialize.call(this); + + this.layerMgr = layerMgr; + this.wsChannel = wsChannel; + + this.currentObjects = []; + + this.onLayerChange = this.onLayerChange.bind(this); + this.reDraw = this.reDraw.bind(this); + this.onMapMove = debounce(this.onMapMove.bind(this), 50); + + this.wsChannel.addListener("minetest-info", this.onMinetestUpdate.bind(this)); + }, + + onLayerChange: function(layer){ + this.reDraw(); + }, + + onMinetestUpdate: function(info){ + //TODO + }, + + reDraw: function(){ + var self = this; + + this.clearLayers(); + + var mapLayer = this.layerMgr.getCurrentLayer() + //TODO + }, + + onAdd: function(map) { + this.layerMgr.addListener(this.reDraw); + this.reDraw(true) + }, + + onRemove: function(map) { + this.clearLayers(); + this.layerMgr.removeListener(this.reDraw); + } +});