Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 45 additions & 51 deletions static/scripts/ocap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
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 available on cloud (${cloudMapRes.status})`);
}
} 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) {
Expand Down Expand Up @@ -381,25 +379,19 @@ function initMap (world) {
let colorReliefLayerUrl = "";
let contourLayerUrl = "";

// console.log(ui.useCloudTiles)

switch (ui.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;
}
console.log('world._useCloudTiles', Boolean(world._useCloudTiles));
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) {
Expand Down Expand Up @@ -1264,14 +1256,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);
Expand Down