Skip to content

Commit

Permalink
viewer: switch to modern fetch API, do not cache metadata and entity …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
neocturne committed Jan 7, 2024
1 parent bd7ca88 commit 710420a
Showing 1 changed file with 49 additions and 57 deletions.
106 changes: 49 additions & 57 deletions viewer/MinedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,76 +284,71 @@ function createSign(sign, back) {
return wrapper;
}

function loadSigns(signLayer) {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
const res = JSON.parse(this.responseText);
const groups = {};

// Group signs by x,z coordinates
for (const sign of res.signs) {
const key = coordKey([sign.x, sign.z]);
const group = groups[key] ??= [];
group.push(sign);
}
async function loadSigns(signLayer) {
const response = await fetch('data/entities.json', {cache: 'no-store'});
const res = await response.json();

for (const [key, group] of Object.entries(groups)) {
const el = document.createElement('div');
const groups = {};

let material;
let kind;
// Group signs by x,z coordinates
for (const sign of res.signs) {
const key = coordKey([sign.x, sign.z]);
const group = groups[key] ??= [];
group.push(sign);
}

// Sort from top to bottom
group.sort((a, b) => b.y - a.y);
for (const [key, group] of Object.entries(groups)) {
const el = document.createElement('div');

for (const sign of group) {
el.appendChild(createSign(sign, false));
let material;
let kind;

if (sign.back_text)
el.appendChild(createSign(sign, true));
// Sort from top to bottom
group.sort((a, b) => b.y - a.y);

material ??= sign.material;
kind ??= sign.kind;
}
for (const sign of group) {
el.appendChild(createSign(sign, false));

// Default material
material ??= 'oak';
if (sign.back_text)
el.appendChild(createSign(sign, true));

const [x, z] = key.split(',').map((i) => +i);
material ??= sign.material;
kind ??= sign.kind;
}

const popup = L.popup().setContent(el);
// Default material
material ??= 'oak';

popup.on('add', () => {
params.marker = [x, z];
updateHash();
});
popup.on('remove', () => {
params.marker = null;
updateHash();
});
const [x, z] = key.split(',').map((i) => +i);

const marker = L.marker([-z-0.5, x+0.5], {
icon: signIcon(material, kind),
}).addTo(signLayer).bindPopup(popup);
const popup = L.popup().setContent(el);

markers[coordKey([x, z])] = marker;
popup.on('add', () => {
params.marker = [x, z];
updateHash();
});
popup.on('remove', () => {
params.marker = null;
updateHash();
});

if (params.marker && x === params.marker[0] && z === params.marker[1])
marker.openPopup();
}
}
const marker = L.marker([-z-0.5, x+0.5], {
icon: signIcon(material, kind),
}).addTo(signLayer).bindPopup(popup);

markers[coordKey([x, z])] = marker;

xhr.open('GET', 'data/entities.json', true);
xhr.send();
if (params.marker && x === params.marker[0] && z === params.marker[1])
marker.openPopup();
}
}

window.createMap = function () {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
const res = JSON.parse(this.responseText),
mipmaps = res.mipmaps,
spawn = res.spawn,
features = res.features || {};
(async function () {
const response = await fetch('data/info.json', {cache: 'no-store'});
const res = await response.json();
const {mipmaps, spawn} = res;
const features = res.features || {};

const updateParams = function () {
const args = parseHash();
Expand Down Expand Up @@ -497,8 +492,5 @@ window.createMap = function () {
updateHash();
};

};

xhr.open('GET', 'data/info.json', true);
xhr.send();
})();
}

0 comments on commit 710420a

Please sign in to comment.