Skip to content

Commit 35528a4

Browse files
authored
use cloud tiles as fallback (#44)
* only use cloud tiles as fallback when enabled * debug log * show cloud map data parsing error * fix `world._useCloudTiles` condition check
1 parent ae41485 commit 35528a4

1 file changed

Lines changed: 45 additions & 51 deletions

File tree

static/scripts/ocap.js

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ function initOCAP () {
157157
if (args.experimental) ui.showExperimental();
158158
}
159159

160-
function getWorldByName (worldName) {
160+
async function getWorldByName (worldName) {
161161
console.log("Getting world " + worldName);
162-
let map = {};
162+
163163
let defaultMap = {
164164
"name": "NOT FOUND",
165165
"worldname": "NOT FOUND",
@@ -175,35 +175,33 @@ function getWorldByName (worldName) {
175175
"attribution": "Bohemia Interactive and 3rd Party Developers"
176176
};
177177

178-
let mapJsonUrl;
178+
// Check for, and return local map data if available
179+
const localMapRes = await fetch('images/maps/' + worldName + '/map.json');
180+
if (localMapRes.status === 200) {
181+
try {
182+
return Object.assign(defaultMap, await localMapRes.json());
183+
} catch (error) {
184+
//ui.showHint(`Error: parsing local map.json`);
185+
console.error('Error parsing local map.json', error.message || error);
186+
}
187+
}
188+
189+
// Fallback to cloud CDN if enabled
179190
if (ui.useCloudTiles) {
180-
mapJsonUrl = `https://maps.ocap2.com/${worldName}/map.json`;
191+
const cloudMapRes = await fetch(`https://maps.ocap2.com/${worldName}/map.json`);
192+
if (cloudMapRes.status === 200) {
193+
try {
194+
return Object.assign(defaultMap, await cloudMapRes.json(), {_useCloudTiles:true});
195+
} catch (error) {
196+
console.error('Error parsing cloud map.json', error.message || error);
197+
return Promise.reject(`Cloud map "${worldName}" data parsing failed.`);
198+
}
199+
} else {
200+
return Promise.reject(`Map "${worldName}" is not available on cloud (${cloudMapRes.status})`);
201+
}
181202
} else {
182-
mapJsonUrl = 'images/maps/' + worldName + '/map.json';
203+
return Promise.reject(`Map "${worldName}" is not installed`);
183204
}
184-
185-
// $.getJSON(mapJsonUrl, function (data) {
186-
// console.log(data);
187-
// console.log(data.responseJSON);
188-
// });
189-
map = $.ajax({
190-
type: "GET",
191-
url: mapJsonUrl,
192-
async: false
193-
}).responseJSON;
194-
return Object.assign(defaultMap, map);
195-
196-
197-
// return fetch(mapJsonUrl)
198-
// .then((res) => res.json())
199-
// .then((data) => {
200-
// // console.log(data);
201-
// map = data;
202-
// return Object.assign(defaultMap, map);
203-
// })
204-
// .catch(() => {
205-
// ui.showHint(`Error: Map "${worldName}" is not installed`);
206-
// });
207205
}
208206

209207
function initMap (world) {
@@ -381,25 +379,19 @@ function initMap (world) {
381379
let colorReliefLayerUrl = "";
382380
let contourLayerUrl = "";
383381

384-
// console.log(ui.useCloudTiles)
385-
386-
switch (ui.useCloudTiles) {
387-
case true: {
388-
topoLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/{z}/{x}/{y}.png');
389-
topoDarkLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoDark/{z}/{x}/{y}.png');
390-
topoReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoRelief/{z}/{x}/{y}.png');
391-
colorReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/colorRelief/{z}/{x}/{y}.png');
392-
contourLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/contours.geojson');
393-
break;
394-
}
395-
case false: {
396-
topoLayerUrl = ('images/maps/' + worldName + '/{z}/{x}/{y}.png');
397-
topoDarkLayerUrl = ('images/maps/' + worldName + '/topoDark/{z}/{x}/{y}.png');
398-
topoReliefLayerUrl = ('images/maps/' + worldName + '/topoRelief/{z}/{x}/{y}.png');
399-
colorReliefLayerUrl = ('images/maps/' + worldName + '/colorRelief/{z}/{x}/{y}.png');
400-
contourLayerUrl = ('images/maps/' + worldName + '/contours.geojson');
401-
break;
402-
}
382+
console.log('world._useCloudTiles', Boolean(world._useCloudTiles));
383+
if (Boolean(world._useCloudTiles)) {
384+
topoLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/{z}/{x}/{y}.png');
385+
topoDarkLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoDark/{z}/{x}/{y}.png');
386+
topoReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/topoRelief/{z}/{x}/{y}.png');
387+
colorReliefLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/colorRelief/{z}/{x}/{y}.png');
388+
contourLayerUrl = ('https://maps.ocap2.com/' + worldName.toLowerCase() + '/contours.geojson');
389+
} else {
390+
topoLayerUrl = ('images/maps/' + worldName + '/{z}/{x}/{y}.png');
391+
topoDarkLayerUrl = ('images/maps/' + worldName + '/topoDark/{z}/{x}/{y}.png');
392+
topoReliefLayerUrl = ('images/maps/' + worldName + '/topoRelief/{z}/{x}/{y}.png');
393+
colorReliefLayerUrl = ('images/maps/' + worldName + '/colorRelief/{z}/{x}/{y}.png');
394+
contourLayerUrl = ('images/maps/' + worldName + '/contours.geojson');
403395
}
404396

405397
if (world.hasTopo) {
@@ -1264,14 +1256,16 @@ function processOp (filepath) {
12641256
const time = new Date();
12651257
fileName = filepath.substr(5, filepath.length);
12661258

1259+
let data;
12671260
return fetch(filepath)
12681261
.then((res) => res.json())
1269-
.then((data) => {
1262+
.then((json) => {
1263+
data = json;
12701264
worldName = data.worldName.toLowerCase();
1271-
// worldName = data.worldName;
1272-
return Promise.all([data, getWorldByName(worldName)]);
1265+
return worldName;
12731266
})
1274-
.then(([data, world]) => {
1267+
.then((wn) => getWorldByName(wn))
1268+
.then((world) => {
12751269
var multiplier = world.multiplier;
12761270
missionName = data.missionName;
12771271
ui.setMissionName(missionName);

0 commit comments

Comments
 (0)