From 711f1dc8452275862dd60885281b815aed68a93e Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Wed, 29 Nov 2023 19:43:56 +0100 Subject: [PATCH] Adding tile cache and preprocess. --- js-src/conquer/index.ts | 6 ++- lib/BurguillosInfo.pm | 1 + lib/BurguillosInfo/Controller/ConquerTile.pm | 52 ++++++++++++++++++++ public/js/bundle.js | 2 +- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 lib/BurguillosInfo/Controller/ConquerTile.pm diff --git a/js-src/conquer/index.ts b/js-src/conquer/index.ts index dfb59a8..60f4526 100644 --- a/js-src/conquer/index.ts +++ b/js-src/conquer/index.ts @@ -235,15 +235,17 @@ export default class Conquer { // } //}) olProj.useGeographic() + const osm = new OSM() + osm.setUrls([`${window.location.protocol}//${window.location.hostname}:${window.location.port}/conquer/tile/{z}/{x}/{y}.png`]) this.map = new Map({ target: conquerContainer, layers: [ new TileLayer({ - source: new OSM() + source: osm }) ], view: new View({ - zoom: 21, + zoom: 19, maxZoom: 22, }), }) diff --git a/lib/BurguillosInfo.pm b/lib/BurguillosInfo.pm index 5623732..226262f 100644 --- a/lib/BurguillosInfo.pm +++ b/lib/BurguillosInfo.pm @@ -94,6 +94,7 @@ sub startup ($self) { $r->put('/conquer/user')->to('UserConquer#create'); $r->get('/conquer/user')->to('UserConquer#get_self'); $r->post('/conquer/user/login')->to('UserConquer#login'); + $r->get('/conquer/tile///.png')->to('ConquerTile#tile'); $r->get('/search.json')->to('Search#search'); $r->get('/farmacia-guardia.json')->to('FarmaciaGuardia#current'); $r->get('/<:category>.rss')->to('Page#category_rss'); diff --git a/lib/BurguillosInfo/Controller/ConquerTile.pm b/lib/BurguillosInfo/Controller/ConquerTile.pm new file mode 100644 index 0000000..34a7984 --- /dev/null +++ b/lib/BurguillosInfo/Controller/ConquerTile.pm @@ -0,0 +1,52 @@ +package BurguillosInfo::Controller::ConquerTile; + +use v5.34.1; + +use strict; +use warnings; + +use utf8; + +use Mojo::Base 'Mojolicious::Controller', '-signatures'; +use Path::Tiny; +use Mojo::UserAgent; + +my $cache_files_dir = path(__FILE__)->parent->parent->parent->parent->child('cache/tiles/'); + +sub tile($self) { + my $zoom = $self->stash('zoom'); + my $x = $self->stash('x'); + my $y = $self->stash('y'); + my $candidate_file = $cache_files_dir->child("$zoom-$x-$y.png"); + if (-f $candidate_file) { + return $self->_render_png($candidate_file); + } + if (!defined $self->current_user) { + return $self->render(status => 401, text => '¡¡No estás loggeado, no puedes cargar mapa nuevo.!!'); + } + my $file_to_write = $candidate_file; + my $ua = Mojo::UserAgent->new; + my $png_tile = $ua->get("https://tile.openstreetmap.org/$zoom/$x/$y.png")->result->body; + open my $fh, '|-', 'convert', '/dev/stdin', '-channel', 'RGB', '-negate', $file_to_write; + print $fh $png_tile; + close $fh; + $self->_render_png($file_to_write); + $self->_delete_extra_files(); +} + +sub _delete_extra_files($self) { + my @files = $cache_files_dir->children; + if (scalar @files < 20001) { + return; + } + @files = sort { -M $a <=> -M $b } @files; + for (my $i = 0; $i < (scalar @files) - 20000; $i++) { + system 'rm', '-v', $files[$i]; + } +} + +sub _render_png($self, $file) { + system 'touch', $file; + return $self->render(data => $file->slurp_raw, status => 200, format => 'png'); +} +1; diff --git a/public/js/bundle.js b/public/js/bundle.js index d393a26..64500d0 100644 --- a/public/js/bundle.js +++ b/public/js/bundle.js @@ -79,7 +79,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (/* binding */ Conquer)\n/* harmony export */ });\n/* harmony import */ var ol_Map__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ol/Map */ \"./node_modules/ol/Map.js\");\n/* harmony import */ var ol_MapBrowserEvent__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ol/MapBrowserEvent */ \"./node_modules/ol/MapBrowserEvent.js\");\n/* harmony import */ var ol_View__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__(/*! ol/View */ \"./node_modules/ol/View.js\");\n/* harmony import */ var ol_proj_Projection_js__WEBPACK_IMPORTED_MODULE_20__ = __webpack_require__(/*! ol/proj/Projection.js */ \"./node_modules/ol/proj/Projection.js\");\n/* harmony import */ var ol_layer_Tile__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ol/layer/Tile */ \"./node_modules/ol/layer/Tile.js\");\n/* harmony import */ var ol_source_OSM__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ol/source/OSM */ \"./node_modules/ol/source/OSM.js\");\n/* harmony import */ var ol_proj__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ol/proj */ \"./node_modules/ol/proj.js\");\n/* harmony import */ var ol_Feature__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ol/Feature */ \"./node_modules/ol/Feature.js\");\n/* harmony import */ var ol_geom_Point__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ol/geom/Point */ \"./node_modules/ol/geom/Point.js\");\n/* harmony import */ var ol_layer_Vector__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__(/*! ol/layer/Vector */ \"./node_modules/ol/layer/Vector.js\");\n/* harmony import */ var ol_source_Vector__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__(/*! ol/source/Vector */ \"./node_modules/ol/source/Vector.js\");\n/* harmony import */ var ol_style_Stroke__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ol/style/Stroke */ \"./node_modules/ol/style/Stroke.js\");\n/* harmony import */ var ol_style_Fill__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ol/style/Fill */ \"./node_modules/ol/style/Fill.js\");\n/* harmony import */ var ol_style_Circle__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ol/style/Circle */ \"./node_modules/ol/style/Circle.js\");\n/* harmony import */ var ol_style_Style__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ol/style/Style */ \"./node_modules/ol/style/Style.js\");\n/* harmony import */ var _burguillosinfo_conquer_login__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @burguillosinfo/conquer/login */ \"./js-src/conquer/login.ts\");\n/* harmony import */ var _burguillosinfo_conquer_interface_manager__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @burguillosinfo/conquer/interface-manager */ \"./js-src/conquer/interface-manager.ts\");\n/* harmony import */ var _burguillosinfo_conquer_interface_self_player__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! @burguillosinfo/conquer/interface/self-player */ \"./js-src/conquer/interface/self-player.ts\");\n/* harmony import */ var _burguillosinfo_conquer_create_node__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! @burguillosinfo/conquer/create-node */ \"./js-src/conquer/create-node.ts\");\n/* harmony import */ var _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! @burguillosinfo/conquer/map-state */ \"./js-src/conquer/map-state.ts\");\n/* harmony import */ var _burguillosinfo_conquer_map_node__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @burguillosinfo/conquer/map-node */ \"./js-src/conquer/map-node.ts\");\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nclass Conquer {\n getServerNodes() {\n return this.serverNodes;\n }\n getState() {\n return this.state;\n }\n setState(state) {\n this.state = state;\n this.refreshState();\n }\n removeState(state) {\n this.state &= ~state;\n this.refreshState();\n }\n addState(state) {\n this.state |= state;\n this.refreshState();\n }\n refreshState() {\n this.createNodeObject.refreshState();\n return;\n }\n static start() {\n const conquerContainer = document.querySelector(\".conquer-container\");\n if (conquerContainer === null || !(conquerContainer instanceof HTMLDivElement)) {\n Conquer.fail('.conquer-container is not a div.');\n }\n const conquer = new Conquer(conquerContainer);\n conquer.run();\n }\n setCenterDisplaced(lat, lon) {\n if (this.firstSetCenter || !(this.state & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].FREE_MOVE)) {\n const olCoordinates = this.realCoordinatesToOl(lat, lon);\n const size = this.map.getSize();\n if (size === undefined) {\n return;\n }\n this.map.getView().centerOn(olCoordinates, size, [size[0] / 2, size[1] - 60]);\n this.firstSetCenter = false;\n }\n }\n static fail(error) {\n alert('Error de interfaz');\n throw new Error(error);\n }\n isStateCreatingNode() {\n return !!(this.getState() & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].CREATE_NODE);\n }\n isStateSelectWhereToCreateNode() {\n return !!(this.getState() & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].SELECT_WHERE_TO_CREATE_NODE);\n }\n async onClickWhereToCreateNode(event) {\n if (!(event instanceof ol_MapBrowserEvent__WEBPACK_IMPORTED_MODULE_7__[\"default\"])) {\n return;\n }\n const pixel = event.pixel;\n const coordinates = this.map.getCoordinateFromPixel(pixel);\n const feature = new ol_Feature__WEBPACK_IMPORTED_MODULE_8__[\"default\"]({\n geometry: new ol_geom_Point__WEBPACK_IMPORTED_MODULE_9__[\"default\"](coordinates)\n });\n console.log(coordinates);\n const style = new ol_style_Style__WEBPACK_IMPORTED_MODULE_10__[\"default\"]({\n image: new ol_style_Circle__WEBPACK_IMPORTED_MODULE_11__[\"default\"]({\n radius: 14,\n fill: new ol_style_Fill__WEBPACK_IMPORTED_MODULE_12__[\"default\"]({ color: 'white' }),\n stroke: new ol_style_Stroke__WEBPACK_IMPORTED_MODULE_13__[\"default\"]({\n color: 'gray',\n width: 2,\n })\n })\n });\n const mapNode = new _burguillosinfo_conquer_map_node__WEBPACK_IMPORTED_MODULE_6__[\"default\"](style, feature, `server-node-${++this.createNodeCounter}`);\n this.getServerNodes()[mapNode.getId()] = mapNode;\n this.removeState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].SELECT_WHERE_TO_CREATE_NODE);\n this.refreshLayers();\n }\n async onClickMap(event) {\n if (this.isStateCreatingNode() && this.isStateSelectWhereToCreateNode()) {\n this.onClickWhereToCreateNode(event);\n }\n if (!(this.getState() & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL)) {\n return;\n }\n if (this.vectorLayer === null) {\n return;\n }\n if (!(event instanceof ol_MapBrowserEvent__WEBPACK_IMPORTED_MODULE_7__[\"default\"])) {\n return;\n }\n if (event.dragging) {\n return;\n }\n const pixel = event.pixel;\n const features = this.map.getFeaturesAtPixel(pixel);\n const feature = features.length ? features[0] : undefined;\n if (feature === undefined) {\n return;\n }\n if (!(feature instanceof ol_Feature__WEBPACK_IMPORTED_MODULE_8__[\"default\"])) {\n return;\n }\n this.onClickFeature(feature);\n }\n async onClickSelf() {\n if (!(this.state & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL)) {\n return;\n }\n const selfPlayerUI = new _burguillosinfo_conquer_interface_self_player__WEBPACK_IMPORTED_MODULE_3__[\"default\"]();\n selfPlayerUI.on('close', () => {\n this.interfaceManager.remove(selfPlayerUI);\n });\n selfPlayerUI.on('createNodeStart', () => {\n this.addState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].CREATE_NODE);\n this.removeState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL);\n });\n this.interfaceManager.push(selfPlayerUI);\n this.selfPlayerUI = selfPlayerUI;\n }\n async onClickFeature(feature) {\n if (this.isFeatureEnabledMap[feature.getProperties().type] === undefined) {\n this.isFeatureEnabledMap[feature.getProperties().type] = true;\n }\n if (!this.isFeatureEnabledMap[feature.getProperties().type]) {\n return;\n }\n this.isFeatureEnabledMap[feature.getProperties().type] = false;\n window.setTimeout(() => {\n this.isFeatureEnabledMap[feature.getProperties().type] = true;\n }, 100);\n if (feature === this.currentPositionFeature) {\n this.onClickSelf();\n return;\n }\n }\n async onLoginSuccess() {\n const currentPositionFeature = this.currentPositionFeature;\n if (currentPositionFeature === null) {\n return;\n }\n this.map.on('click', (event) => {\n this.onClickMap(event);\n });\n }\n runPreStartState() {\n const createNodeObject = new _burguillosinfo_conquer_create_node__WEBPACK_IMPORTED_MODULE_4__[\"default\"](this);\n this.createNodeObject = createNodeObject;\n const interfaceManager = new _burguillosinfo_conquer_interface_manager__WEBPACK_IMPORTED_MODULE_2__[\"default\"]();\n this.interfaceManager = interfaceManager;\n const conquerLogin = new _burguillosinfo_conquer_login__WEBPACK_IMPORTED_MODULE_1__[\"default\"](interfaceManager);\n conquerLogin.on('login', () => {\n this.onLoginSuccess();\n });\n conquerLogin.start();\n this.conquerLogin = conquerLogin;\n }\n async run() {\n this.runPreStartState();\n this.setState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL);\n const conquerContainer = this.conquerContainer;\n //layer.on('prerender', (evt) => {\n // // return\n // if (evt.context) {\n // const context = evt.context as CanvasRenderingContext2D\n // context.filter = 'grayscale(80%) invert(100%) '\n // context.globalCompositeOperation = 'source-over'\n // }\n //})\n //layer.on('postrender', (evt) => {\n // if (evt.context) {\n // const context = evt.context as CanvasRenderingContext2D\n // context.filter = 'none'\n // }\n //})\n ol_proj__WEBPACK_IMPORTED_MODULE_0__.useGeographic();\n this.map = new ol_Map__WEBPACK_IMPORTED_MODULE_14__[\"default\"]({\n target: conquerContainer,\n layers: [\n new ol_layer_Tile__WEBPACK_IMPORTED_MODULE_15__[\"default\"]({\n source: new ol_source_OSM__WEBPACK_IMPORTED_MODULE_16__[\"default\"]()\n })\n ],\n view: new ol_View__WEBPACK_IMPORTED_MODULE_17__[\"default\"]({\n zoom: 21,\n maxZoom: 22,\n }),\n });\n this.setLocationChangeTriggers();\n this.setRotationChangeTriggers();\n }\n setRotationChangeTriggers() {\n if (window.DeviceOrientationEvent) {\n window.addEventListener(\"deviceorientation\", (event) => {\n if (event.alpha !== null && event.beta !== null && event.gamma !== null) {\n this.onRotate(event.alpha, event.beta, event.gamma);\n }\n }, true);\n }\n }\n addCurrentLocationMarkerToMap(currentLatitude, currentLongitude) {\n const currentPositionFeature = new ol_Feature__WEBPACK_IMPORTED_MODULE_8__[\"default\"]({\n type: 'currentPositionFeature',\n geometry: new ol_geom_Point__WEBPACK_IMPORTED_MODULE_9__[\"default\"](this.realCoordinatesToOl(currentLatitude, currentLongitude))\n });\n this.currentPositionFeature = currentPositionFeature;\n }\n processLocation(location) {\n this.currentLatitude = location.coords.latitude;\n this.currentLongitude = location.coords.longitude;\n if (location.coords.heading !== null && (this.alpha != 0 || this.beta != 0 || this.gamma != 0) && !this.disableSetRotationOffset) {\n this.disableSetRotationOffset = true;\n this.heading = location.coords.heading;\n this.rotationOffset = this.compassHeading(this.alpha, this.beta, this.gamma) + (location.coords.heading * Math.PI * 2) / 360;\n }\n this.setCenterDisplaced(this.currentLatitude, this.currentLongitude);\n this.addCurrentLocationMarkerToMap(this.currentLatitude, this.currentLongitude);\n this.refreshLayers();\n }\n refreshLayers() {\n if (this.currentPositionFeature === null) {\n return;\n }\n const styles = {\n currentPositionFeature: new ol_style_Style__WEBPACK_IMPORTED_MODULE_10__[\"default\"]({\n image: new ol_style_Circle__WEBPACK_IMPORTED_MODULE_11__[\"default\"]({\n radius: 14,\n fill: new ol_style_Fill__WEBPACK_IMPORTED_MODULE_12__[\"default\"]({ color: 'white' }),\n stroke: new ol_style_Stroke__WEBPACK_IMPORTED_MODULE_13__[\"default\"]({\n color: 'blue',\n width: 2,\n })\n })\n })\n };\n const features = [this.currentPositionFeature];\n for (const key in this.getServerNodes()) {\n styles[key] = this.getServerNodes()[key].getStyle();\n features.push(this.getServerNodes()[key].getNode());\n }\n const vectorLayer = new ol_layer_Vector__WEBPACK_IMPORTED_MODULE_18__[\"default\"]({\n source: new ol_source_Vector__WEBPACK_IMPORTED_MODULE_19__[\"default\"]({\n features: features\n }),\n });\n if (this.vectorLayer !== null) {\n this.map.removeLayer(this.vectorLayer);\n }\n vectorLayer.setStyle((feature) => {\n return styles[feature.getProperties().type];\n });\n this.map.addLayer(vectorLayer);\n this.vectorLayer = vectorLayer;\n this.map.on('click', (event) => {\n this.onClickMap(event);\n });\n }\n setLocationChangeTriggers() {\n window.setInterval(() => {\n this.disableSetRotationOffset = false;\n }, 10000);\n this.currentPositionFeature = null;\n window.setTimeout(() => {\n window.setInterval(() => {\n navigator.geolocation.getCurrentPosition((location) => {\n this.processLocation(location);\n }, () => {\n return;\n }, {\n enableHighAccuracy: true,\n });\n }, 3000);\n }, 1000);\n // const initialLatitude = 37.58237\n //const initialLongitude = -5.96766\n const initialLongitude = 2.500845037550267;\n const initialLatitude = 48.81050698635832;\n this.setCenterDisplaced(initialLatitude, initialLongitude);\n this.addCurrentLocationMarkerToMap(initialLatitude, initialLongitude);\n this.refreshLayers();\n navigator.geolocation.watchPosition((location) => {\n this.processLocation(location);\n }, (err) => {\n return;\n }, {\n enableHighAccuracy: true,\n });\n }\n realCoordinatesToOl(lat, lon) {\n return ol_proj__WEBPACK_IMPORTED_MODULE_0__.transform([lon, lat], new ol_proj_Projection_js__WEBPACK_IMPORTED_MODULE_20__[\"default\"]({ code: \"WGS84\" }), new ol_proj_Projection_js__WEBPACK_IMPORTED_MODULE_20__[\"default\"]({ code: \"EPSG:900913\" }));\n }\n compassHeading(alpha, beta, gamma) {\n const alphaRad = alpha * (Math.PI / 180);\n return alphaRad;\n }\n logToServer(logValue) {\n const urlLog = new URL('/conquer/log', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port);\n urlLog.searchParams.append('log', logValue);\n fetch(urlLog).then(() => {\n return;\n }).catch((error) => {\n console.error(error);\n });\n }\n onRotate(alpha, beta, gamma) {\n if (this.enabledOnRotate) {\n this.alpha = alpha;\n this.beta = beta;\n this.gamma = gamma;\n this.enabledOnRotate = false;\n if (this.firstSetRotation || !(this.state & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].FREE_ROTATION)) {\n this.map.getView().setRotation((this.compassHeading(alpha, beta, gamma) - this.rotationOffset));\n this.firstSetRotation = false;\n }\n window.setTimeout(() => {\n this.enabledOnRotate = true;\n }, 10);\n }\n this.setCenterDisplaced(this.currentLatitude, this.currentLongitude);\n }\n constructor(conquerContainer) {\n this.rotationOffset = 0;\n this.heading = 0;\n this.disableSetRotationOffset = false;\n this.vectorLayer = null;\n this.alpha = 0;\n this.beta = 0;\n this.gamma = 0;\n this.selfPlayerUI = null;\n this.firstSetCenter = true;\n this.firstSetRotation = true;\n this.state = _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NOTHING;\n this.serverNodes = {};\n this.createNodeCounter = 0;\n this.isFeatureEnabledMap = {};\n this.enabledOnRotate = true;\n this.conquerContainer = conquerContainer;\n }\n}\n\n\n//# sourceURL=webpack://BurguillosInfo/./js-src/conquer/index.ts?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (/* binding */ Conquer)\n/* harmony export */ });\n/* harmony import */ var ol_Map__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ol/Map */ \"./node_modules/ol/Map.js\");\n/* harmony import */ var ol_MapBrowserEvent__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ol/MapBrowserEvent */ \"./node_modules/ol/MapBrowserEvent.js\");\n/* harmony import */ var ol_View__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__(/*! ol/View */ \"./node_modules/ol/View.js\");\n/* harmony import */ var ol_proj_Projection_js__WEBPACK_IMPORTED_MODULE_20__ = __webpack_require__(/*! ol/proj/Projection.js */ \"./node_modules/ol/proj/Projection.js\");\n/* harmony import */ var ol_layer_Tile__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ol/layer/Tile */ \"./node_modules/ol/layer/Tile.js\");\n/* harmony import */ var ol_source_OSM__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ol/source/OSM */ \"./node_modules/ol/source/OSM.js\");\n/* harmony import */ var ol_proj__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ol/proj */ \"./node_modules/ol/proj.js\");\n/* harmony import */ var ol_Feature__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ol/Feature */ \"./node_modules/ol/Feature.js\");\n/* harmony import */ var ol_geom_Point__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ol/geom/Point */ \"./node_modules/ol/geom/Point.js\");\n/* harmony import */ var ol_layer_Vector__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__(/*! ol/layer/Vector */ \"./node_modules/ol/layer/Vector.js\");\n/* harmony import */ var ol_source_Vector__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__(/*! ol/source/Vector */ \"./node_modules/ol/source/Vector.js\");\n/* harmony import */ var ol_style_Stroke__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ol/style/Stroke */ \"./node_modules/ol/style/Stroke.js\");\n/* harmony import */ var ol_style_Fill__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ol/style/Fill */ \"./node_modules/ol/style/Fill.js\");\n/* harmony import */ var ol_style_Circle__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ol/style/Circle */ \"./node_modules/ol/style/Circle.js\");\n/* harmony import */ var ol_style_Style__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ol/style/Style */ \"./node_modules/ol/style/Style.js\");\n/* harmony import */ var _burguillosinfo_conquer_login__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @burguillosinfo/conquer/login */ \"./js-src/conquer/login.ts\");\n/* harmony import */ var _burguillosinfo_conquer_interface_manager__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @burguillosinfo/conquer/interface-manager */ \"./js-src/conquer/interface-manager.ts\");\n/* harmony import */ var _burguillosinfo_conquer_interface_self_player__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! @burguillosinfo/conquer/interface/self-player */ \"./js-src/conquer/interface/self-player.ts\");\n/* harmony import */ var _burguillosinfo_conquer_create_node__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! @burguillosinfo/conquer/create-node */ \"./js-src/conquer/create-node.ts\");\n/* harmony import */ var _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! @burguillosinfo/conquer/map-state */ \"./js-src/conquer/map-state.ts\");\n/* harmony import */ var _burguillosinfo_conquer_map_node__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @burguillosinfo/conquer/map-node */ \"./js-src/conquer/map-node.ts\");\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nclass Conquer {\n getServerNodes() {\n return this.serverNodes;\n }\n getState() {\n return this.state;\n }\n setState(state) {\n this.state = state;\n this.refreshState();\n }\n removeState(state) {\n this.state &= ~state;\n this.refreshState();\n }\n addState(state) {\n this.state |= state;\n this.refreshState();\n }\n refreshState() {\n this.createNodeObject.refreshState();\n return;\n }\n static start() {\n const conquerContainer = document.querySelector(\".conquer-container\");\n if (conquerContainer === null || !(conquerContainer instanceof HTMLDivElement)) {\n Conquer.fail('.conquer-container is not a div.');\n }\n const conquer = new Conquer(conquerContainer);\n conquer.run();\n }\n setCenterDisplaced(lat, lon) {\n if (this.firstSetCenter || !(this.state & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].FREE_MOVE)) {\n const olCoordinates = this.realCoordinatesToOl(lat, lon);\n const size = this.map.getSize();\n if (size === undefined) {\n return;\n }\n this.map.getView().centerOn(olCoordinates, size, [size[0] / 2, size[1] - 60]);\n this.firstSetCenter = false;\n }\n }\n static fail(error) {\n alert('Error de interfaz');\n throw new Error(error);\n }\n isStateCreatingNode() {\n return !!(this.getState() & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].CREATE_NODE);\n }\n isStateSelectWhereToCreateNode() {\n return !!(this.getState() & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].SELECT_WHERE_TO_CREATE_NODE);\n }\n async onClickWhereToCreateNode(event) {\n if (!(event instanceof ol_MapBrowserEvent__WEBPACK_IMPORTED_MODULE_7__[\"default\"])) {\n return;\n }\n const pixel = event.pixel;\n const coordinates = this.map.getCoordinateFromPixel(pixel);\n const feature = new ol_Feature__WEBPACK_IMPORTED_MODULE_8__[\"default\"]({\n geometry: new ol_geom_Point__WEBPACK_IMPORTED_MODULE_9__[\"default\"](coordinates)\n });\n console.log(coordinates);\n const style = new ol_style_Style__WEBPACK_IMPORTED_MODULE_10__[\"default\"]({\n image: new ol_style_Circle__WEBPACK_IMPORTED_MODULE_11__[\"default\"]({\n radius: 14,\n fill: new ol_style_Fill__WEBPACK_IMPORTED_MODULE_12__[\"default\"]({ color: 'white' }),\n stroke: new ol_style_Stroke__WEBPACK_IMPORTED_MODULE_13__[\"default\"]({\n color: 'gray',\n width: 2,\n })\n })\n });\n const mapNode = new _burguillosinfo_conquer_map_node__WEBPACK_IMPORTED_MODULE_6__[\"default\"](style, feature, `server-node-${++this.createNodeCounter}`);\n this.getServerNodes()[mapNode.getId()] = mapNode;\n this.removeState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].SELECT_WHERE_TO_CREATE_NODE);\n this.refreshLayers();\n }\n async onClickMap(event) {\n if (this.isStateCreatingNode() && this.isStateSelectWhereToCreateNode()) {\n this.onClickWhereToCreateNode(event);\n }\n if (!(this.getState() & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL)) {\n return;\n }\n if (this.vectorLayer === null) {\n return;\n }\n if (!(event instanceof ol_MapBrowserEvent__WEBPACK_IMPORTED_MODULE_7__[\"default\"])) {\n return;\n }\n if (event.dragging) {\n return;\n }\n const pixel = event.pixel;\n const features = this.map.getFeaturesAtPixel(pixel);\n const feature = features.length ? features[0] : undefined;\n if (feature === undefined) {\n return;\n }\n if (!(feature instanceof ol_Feature__WEBPACK_IMPORTED_MODULE_8__[\"default\"])) {\n return;\n }\n this.onClickFeature(feature);\n }\n async onClickSelf() {\n if (!(this.state & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL)) {\n return;\n }\n const selfPlayerUI = new _burguillosinfo_conquer_interface_self_player__WEBPACK_IMPORTED_MODULE_3__[\"default\"]();\n selfPlayerUI.on('close', () => {\n this.interfaceManager.remove(selfPlayerUI);\n });\n selfPlayerUI.on('createNodeStart', () => {\n this.addState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].CREATE_NODE);\n this.removeState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL);\n });\n this.interfaceManager.push(selfPlayerUI);\n this.selfPlayerUI = selfPlayerUI;\n }\n async onClickFeature(feature) {\n if (this.isFeatureEnabledMap[feature.getProperties().type] === undefined) {\n this.isFeatureEnabledMap[feature.getProperties().type] = true;\n }\n if (!this.isFeatureEnabledMap[feature.getProperties().type]) {\n return;\n }\n this.isFeatureEnabledMap[feature.getProperties().type] = false;\n window.setTimeout(() => {\n this.isFeatureEnabledMap[feature.getProperties().type] = true;\n }, 100);\n if (feature === this.currentPositionFeature) {\n this.onClickSelf();\n return;\n }\n }\n async onLoginSuccess() {\n const currentPositionFeature = this.currentPositionFeature;\n if (currentPositionFeature === null) {\n return;\n }\n this.map.on('click', (event) => {\n this.onClickMap(event);\n });\n }\n runPreStartState() {\n const createNodeObject = new _burguillosinfo_conquer_create_node__WEBPACK_IMPORTED_MODULE_4__[\"default\"](this);\n this.createNodeObject = createNodeObject;\n const interfaceManager = new _burguillosinfo_conquer_interface_manager__WEBPACK_IMPORTED_MODULE_2__[\"default\"]();\n this.interfaceManager = interfaceManager;\n const conquerLogin = new _burguillosinfo_conquer_login__WEBPACK_IMPORTED_MODULE_1__[\"default\"](interfaceManager);\n conquerLogin.on('login', () => {\n this.onLoginSuccess();\n });\n conquerLogin.start();\n this.conquerLogin = conquerLogin;\n }\n async run() {\n this.runPreStartState();\n this.setState(_burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NORMAL);\n const conquerContainer = this.conquerContainer;\n //layer.on('prerender', (evt) => {\n // // return\n // if (evt.context) {\n // const context = evt.context as CanvasRenderingContext2D\n // context.filter = 'grayscale(80%) invert(100%) '\n // context.globalCompositeOperation = 'source-over'\n // }\n //})\n //layer.on('postrender', (evt) => {\n // if (evt.context) {\n // const context = evt.context as CanvasRenderingContext2D\n // context.filter = 'none'\n // }\n //})\n ol_proj__WEBPACK_IMPORTED_MODULE_0__.useGeographic();\n const osm = new ol_source_OSM__WEBPACK_IMPORTED_MODULE_14__[\"default\"]();\n osm.setUrls([`${window.location.protocol}//${window.location.hostname}:${window.location.port}/conquer/tile/{z}/{x}/{y}.png`]);\n this.map = new ol_Map__WEBPACK_IMPORTED_MODULE_15__[\"default\"]({\n target: conquerContainer,\n layers: [\n new ol_layer_Tile__WEBPACK_IMPORTED_MODULE_16__[\"default\"]({\n source: osm\n })\n ],\n view: new ol_View__WEBPACK_IMPORTED_MODULE_17__[\"default\"]({\n zoom: 19,\n maxZoom: 22,\n }),\n });\n this.setLocationChangeTriggers();\n this.setRotationChangeTriggers();\n }\n setRotationChangeTriggers() {\n if (window.DeviceOrientationEvent) {\n window.addEventListener(\"deviceorientation\", (event) => {\n if (event.alpha !== null && event.beta !== null && event.gamma !== null) {\n this.onRotate(event.alpha, event.beta, event.gamma);\n }\n }, true);\n }\n }\n addCurrentLocationMarkerToMap(currentLatitude, currentLongitude) {\n const currentPositionFeature = new ol_Feature__WEBPACK_IMPORTED_MODULE_8__[\"default\"]({\n type: 'currentPositionFeature',\n geometry: new ol_geom_Point__WEBPACK_IMPORTED_MODULE_9__[\"default\"](this.realCoordinatesToOl(currentLatitude, currentLongitude))\n });\n this.currentPositionFeature = currentPositionFeature;\n }\n processLocation(location) {\n this.currentLatitude = location.coords.latitude;\n this.currentLongitude = location.coords.longitude;\n if (location.coords.heading !== null && (this.alpha != 0 || this.beta != 0 || this.gamma != 0) && !this.disableSetRotationOffset) {\n this.disableSetRotationOffset = true;\n this.heading = location.coords.heading;\n this.rotationOffset = this.compassHeading(this.alpha, this.beta, this.gamma) + (location.coords.heading * Math.PI * 2) / 360;\n }\n this.setCenterDisplaced(this.currentLatitude, this.currentLongitude);\n this.addCurrentLocationMarkerToMap(this.currentLatitude, this.currentLongitude);\n this.refreshLayers();\n }\n refreshLayers() {\n if (this.currentPositionFeature === null) {\n return;\n }\n const styles = {\n currentPositionFeature: new ol_style_Style__WEBPACK_IMPORTED_MODULE_10__[\"default\"]({\n image: new ol_style_Circle__WEBPACK_IMPORTED_MODULE_11__[\"default\"]({\n radius: 14,\n fill: new ol_style_Fill__WEBPACK_IMPORTED_MODULE_12__[\"default\"]({ color: 'white' }),\n stroke: new ol_style_Stroke__WEBPACK_IMPORTED_MODULE_13__[\"default\"]({\n color: 'blue',\n width: 2,\n })\n })\n })\n };\n const features = [this.currentPositionFeature];\n for (const key in this.getServerNodes()) {\n styles[key] = this.getServerNodes()[key].getStyle();\n features.push(this.getServerNodes()[key].getNode());\n }\n const vectorLayer = new ol_layer_Vector__WEBPACK_IMPORTED_MODULE_18__[\"default\"]({\n source: new ol_source_Vector__WEBPACK_IMPORTED_MODULE_19__[\"default\"]({\n features: features\n }),\n });\n if (this.vectorLayer !== null) {\n this.map.removeLayer(this.vectorLayer);\n }\n vectorLayer.setStyle((feature) => {\n return styles[feature.getProperties().type];\n });\n this.map.addLayer(vectorLayer);\n this.vectorLayer = vectorLayer;\n this.map.on('click', (event) => {\n this.onClickMap(event);\n });\n }\n setLocationChangeTriggers() {\n window.setInterval(() => {\n this.disableSetRotationOffset = false;\n }, 10000);\n this.currentPositionFeature = null;\n window.setTimeout(() => {\n window.setInterval(() => {\n navigator.geolocation.getCurrentPosition((location) => {\n this.processLocation(location);\n }, () => {\n return;\n }, {\n enableHighAccuracy: true,\n });\n }, 3000);\n }, 1000);\n // const initialLatitude = 37.58237\n //const initialLongitude = -5.96766\n const initialLongitude = 2.500845037550267;\n const initialLatitude = 48.81050698635832;\n this.setCenterDisplaced(initialLatitude, initialLongitude);\n this.addCurrentLocationMarkerToMap(initialLatitude, initialLongitude);\n this.refreshLayers();\n navigator.geolocation.watchPosition((location) => {\n this.processLocation(location);\n }, (err) => {\n return;\n }, {\n enableHighAccuracy: true,\n });\n }\n realCoordinatesToOl(lat, lon) {\n return ol_proj__WEBPACK_IMPORTED_MODULE_0__.transform([lon, lat], new ol_proj_Projection_js__WEBPACK_IMPORTED_MODULE_20__[\"default\"]({ code: \"WGS84\" }), new ol_proj_Projection_js__WEBPACK_IMPORTED_MODULE_20__[\"default\"]({ code: \"EPSG:900913\" }));\n }\n compassHeading(alpha, beta, gamma) {\n const alphaRad = alpha * (Math.PI / 180);\n return alphaRad;\n }\n logToServer(logValue) {\n const urlLog = new URL('/conquer/log', window.location.protocol + '//' + window.location.hostname + ':' + window.location.port);\n urlLog.searchParams.append('log', logValue);\n fetch(urlLog).then(() => {\n return;\n }).catch((error) => {\n console.error(error);\n });\n }\n onRotate(alpha, beta, gamma) {\n if (this.enabledOnRotate) {\n this.alpha = alpha;\n this.beta = beta;\n this.gamma = gamma;\n this.enabledOnRotate = false;\n if (this.firstSetRotation || !(this.state & _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].FREE_ROTATION)) {\n this.map.getView().setRotation((this.compassHeading(alpha, beta, gamma) - this.rotationOffset));\n this.firstSetRotation = false;\n }\n window.setTimeout(() => {\n this.enabledOnRotate = true;\n }, 10);\n }\n this.setCenterDisplaced(this.currentLatitude, this.currentLongitude);\n }\n constructor(conquerContainer) {\n this.rotationOffset = 0;\n this.heading = 0;\n this.disableSetRotationOffset = false;\n this.vectorLayer = null;\n this.alpha = 0;\n this.beta = 0;\n this.gamma = 0;\n this.selfPlayerUI = null;\n this.firstSetCenter = true;\n this.firstSetRotation = true;\n this.state = _burguillosinfo_conquer_map_state__WEBPACK_IMPORTED_MODULE_5__[\"default\"].NOTHING;\n this.serverNodes = {};\n this.createNodeCounter = 0;\n this.isFeatureEnabledMap = {};\n this.enabledOnRotate = true;\n this.conquerContainer = conquerContainer;\n }\n}\n\n\n//# sourceURL=webpack://BurguillosInfo/./js-src/conquer/index.ts?"); /***/ }),