From 2faba1d55ad8918cfafa8227fd710ea2d74a4de0 Mon Sep 17 00:00:00 2001 From: goto-10 Date: Thu, 5 May 2022 21:08:02 +0200 Subject: [PATCH 1/4] only use cloud tiles as fallback when enabled --- static/scripts/ocap.js | 69 +++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/static/scripts/ocap.js b/static/scripts/ocap.js index 22b9783d..fef64255 100644 --- a/static/scripts/ocap.js +++ b/static/scripts/ocap.js @@ -157,9 +157,9 @@ function initOCAP () { if (args.experimental) ui.showExperimental(); } -function getWorldByName (worldName) { +async function getWorldByName (worldName) { console.log("Getting world " + worldName); - let map = {}; + let defaultMap = { "name": "NOT FOUND", "worldname": "NOT FOUND", @@ -175,35 +175,33 @@ function getWorldByName (worldName) { "attribution": "Bohemia Interactive and 3rd Party Developers" }; - let mapJsonUrl; + // Check for, and return local map data if available + const localMapRes = await fetch('images/maps/' + worldName + '/map.json'); + if (localMapRes.status === 200) { + try { + return Object.assign(defaultMap, await localMapRes.json()); + } catch (error) { + //ui.showHint(`Error: parsing local map.json`); + console.error('Error parsing local map.json', error.message || error); + } + } + + // Fallback to cloud CDN if enabled if (ui.useCloudTiles) { - mapJsonUrl = `https://maps.ocap2.com/${worldName}/map.json`; + const cloudMapRes = await fetch(`https://maps.ocap2.com/${worldName}/map.json`); + if (cloudMapRes.status === 200) { + try { + return Object.assign(defaultMap, await cloudMapRes.json(), {_useCloudTiles:true}); + } catch (error) { + //ui.showHint(`Error: parsing cloud map.json`); + console.error('Error parsing cloud map.json', error.message || error); + } + } else { + return Promise.reject(`Map "${worldName}" is not installed on cloud`); + } } else { - mapJsonUrl = 'images/maps/' + worldName + '/map.json'; + return Promise.reject(`Map "${worldName}" is not installed`); } - - // $.getJSON(mapJsonUrl, function (data) { - // console.log(data); - // console.log(data.responseJSON); - // }); - map = $.ajax({ - type: "GET", - url: mapJsonUrl, - async: false - }).responseJSON; - return Object.assign(defaultMap, map); - - - // return fetch(mapJsonUrl) - // .then((res) => res.json()) - // .then((data) => { - // // console.log(data); - // map = data; - // return Object.assign(defaultMap, map); - // }) - // .catch(() => { - // ui.showHint(`Error: Map "${worldName}" is not installed`); - // }); } function initMap (world) { @@ -381,9 +379,8 @@ function initMap (world) { let colorReliefLayerUrl = ""; let contourLayerUrl = ""; - // console.log(ui.useCloudTiles) - - switch (ui.useCloudTiles) { + console.log('useCloudTiles', Boolean(useCloudTiles)); + switch (world._useCloudTiles) { case true: { topoLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/{z}/{x}/{y}.png'); topoDarkLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoDark/{z}/{x}/{y}.png'); @@ -1264,14 +1261,16 @@ function processOp (filepath) { const time = new Date(); fileName = filepath.substr(5, filepath.length); + let data; return fetch(filepath) .then((res) => res.json()) - .then((data) => { + .then((json) => { + data = json; worldName = data.worldName.toLowerCase(); - // worldName = data.worldName; - return Promise.all([data, getWorldByName(worldName)]); + return worldName; }) - .then(([data, world]) => { + .then((wn) => getWorldByName(wn)) + .then((world) => { var multiplier = world.multiplier; missionName = data.missionName; ui.setMissionName(missionName); From ef3eac4604e5f8565d17af1dc80e2fe1f7798d87 Mon Sep 17 00:00:00 2001 From: goto-10 Date: Thu, 5 May 2022 21:11:16 +0200 Subject: [PATCH 2/4] debug log --- static/scripts/ocap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/scripts/ocap.js b/static/scripts/ocap.js index fef64255..2c27f055 100644 --- a/static/scripts/ocap.js +++ b/static/scripts/ocap.js @@ -379,7 +379,7 @@ function initMap (world) { let colorReliefLayerUrl = ""; let contourLayerUrl = ""; - console.log('useCloudTiles', Boolean(useCloudTiles)); + console.log('world._useCloudTiles', Boolean(world._useCloudTiles)); switch (world._useCloudTiles) { case true: { topoLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/{z}/{x}/{y}.png'); From d907653683cc82a256fb25f75bb2e6cddb94d761 Mon Sep 17 00:00:00 2001 From: goto-10 Date: Thu, 5 May 2022 21:46:26 +0200 Subject: [PATCH 3/4] show cloud map data parsing error --- static/scripts/ocap.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/scripts/ocap.js b/static/scripts/ocap.js index 2c27f055..ea971ee9 100644 --- a/static/scripts/ocap.js +++ b/static/scripts/ocap.js @@ -193,11 +193,11 @@ async function getWorldByName (worldName) { try { return Object.assign(defaultMap, await cloudMapRes.json(), {_useCloudTiles:true}); } catch (error) { - //ui.showHint(`Error: parsing cloud map.json`); console.error('Error parsing cloud map.json', error.message || error); + return Promise.reject(`Cloud map "${worldName}" data parsing failed.`); } } else { - return Promise.reject(`Map "${worldName}" is not installed on cloud`); + return Promise.reject(`Map "${worldName}" is not available on cloud (${cloudMapRes.status})`); } } else { return Promise.reject(`Map "${worldName}" is not installed`); From 1fa2bfc6c36dbde50a202602ec15b53cda98b66a Mon Sep 17 00:00:00 2001 From: goto-10 Date: Fri, 6 May 2022 15:12:05 +0200 Subject: [PATCH 4/4] fix `world._useCloudTiles` condition check --- static/scripts/ocap.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/static/scripts/ocap.js b/static/scripts/ocap.js index ea971ee9..275df67c 100644 --- a/static/scripts/ocap.js +++ b/static/scripts/ocap.js @@ -380,23 +380,18 @@ function initMap (world) { let contourLayerUrl = ""; console.log('world._useCloudTiles', Boolean(world._useCloudTiles)); - switch (world._useCloudTiles) { - case true: { - topoLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/{z}/{x}/{y}.png'); - topoDarkLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoDark/{z}/{x}/{y}.png'); - topoReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoRelief/{z}/{x}/{y}.png'); - colorReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/colorRelief/{z}/{x}/{y}.png'); - contourLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/contours.geojson'); - break; - } - case false: { - topoLayerUrl = ('images/maps/' + worldName + '/{z}/{x}/{y}.png'); - topoDarkLayerUrl = ('images/maps/' + worldName + '/topoDark/{z}/{x}/{y}.png'); - topoReliefLayerUrl = ('images/maps/' + worldName + '/topoRelief/{z}/{x}/{y}.png'); - colorReliefLayerUrl = ('images/maps/' + worldName + '/colorRelief/{z}/{x}/{y}.png'); - contourLayerUrl = ('images/maps/' + worldName + '/contours.geojson'); - break; - } + if (Boolean(world._useCloudTiles)) { + topoLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/{z}/{x}/{y}.png'); + topoDarkLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoDark/{z}/{x}/{y}.png'); + topoReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoRelief/{z}/{x}/{y}.png'); + colorReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/colorRelief/{z}/{x}/{y}.png'); + contourLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/contours.geojson'); + } else { + topoLayerUrl = ('images/maps/' + worldName + '/{z}/{x}/{y}.png'); + topoDarkLayerUrl = ('images/maps/' + worldName + '/topoDark/{z}/{x}/{y}.png'); + topoReliefLayerUrl = ('images/maps/' + worldName + '/topoRelief/{z}/{x}/{y}.png'); + colorReliefLayerUrl = ('images/maps/' + worldName + '/colorRelief/{z}/{x}/{y}.png'); + contourLayerUrl = ('images/maps/' + worldName + '/contours.geojson'); } if (world.hasTopo) {