Add live chunk delta streaming for world editor and versioned apply flow

This commit is contained in:
dingo
2026-02-14 14:31:18 -05:00
parent 18d6fe683b
commit 147e7d625d
5 changed files with 312 additions and 52 deletions
+71 -17
View File
@@ -169,7 +169,7 @@
let currentTiles = new Map();
let currentObjects = [];
let currentNpcs = [];
let pollTimer = null;
let eventSource = null;
const regionCanvas = document.getElementById('regionCanvas');
const regionCtx = regionCanvas.getContext('2d');
@@ -263,26 +263,73 @@
currentNpcs = data.npcs || [];
document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`;
renderChunk();
if (pollTimer) clearInterval(pollTimer);
pollTimer = setInterval(checkChunkVersion, 2000);
startStream();
}
async function checkChunkVersion() {
function startStream() {
if (!currentChunk) return;
const s = selection();
const data = await getJson(`/admin/worldeditor?handler=Chunk&regionId=${s.regionId}&zoneX=${s.zoneX}&zoneY=${s.zoneY}&chunkX=${s.chunkX}&chunkY=${s.chunkY}`);
if (!data.success) return;
if (data.chunk.version !== currentChunk.version) {
currentChunk = data.chunk;
currentTiles = new Map();
for (const t of data.tiles) currentTiles.set(key(t.x, t.y), t);
currentObjects = data.objects || [];
currentNpcs = data.npcs || [];
document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`;
renderChunk();
loadZone();
if (eventSource) {
eventSource.close();
eventSource = null;
}
const s = selection();
const url = `/admin/worldeditor?handler=Stream&regionId=${s.regionId}&zoneX=${s.zoneX}&zoneY=${s.zoneY}&chunkX=${s.chunkX}&chunkY=${s.chunkY}`;
eventSource = new EventSource(url);
eventSource.onmessage = (evt) => {
if (!evt.data) return;
try {
const delta = JSON.parse(evt.data);
applyDelta(delta);
} catch {
// ignore malformed stream payload
}
};
}
function applyDelta(delta) {
if (!currentChunk || !delta || delta.kind !== 'chunk_delta' || !delta.op) return;
if (delta.toVersion <= currentChunk.version) return;
if (delta.fromVersion !== currentChunk.version) {
loadChunk();
return;
}
const op = delta.op;
if (op.type === 'tile_upsert') {
currentTiles.set(key(op.x, op.y), {
x: op.x,
y: op.y,
sheetId: op.sheetId,
frameIndex: op.frameIndex,
isWalkable: op.isWalkable,
isInvisiblePaywall: op.isInvisiblePaywall
});
} else if (op.type === 'tile_remove') {
currentTiles.delete(key(op.x, op.y));
} else if (op.type === 'object_upsert') {
const idx = currentObjects.findIndex(o => o.id === op.id);
if (idx >= 0) currentObjects[idx] = { ...currentObjects[idx], ...op };
else currentObjects.push({ ...op });
} else if (op.type === 'object_remove') {
currentObjects = currentObjects.filter(o => o.id !== op.id);
} else if (op.type === 'npc_upsert') {
const idx = currentNpcs.findIndex(n => n.id === op.id);
if (idx >= 0) currentNpcs[idx] = { ...currentNpcs[idx], ...op };
else currentNpcs.push({ ...op });
} else if (op.type === 'npc_remove') {
currentNpcs = currentNpcs.filter(n => n.id !== op.id);
} else {
loadChunk();
return;
}
currentChunk.version = delta.toVersion;
const s = selection();
document.getElementById('chunkMeta').textContent = `Chunk ${s.zoneX}:${s.zoneY}:${s.chunkX}:${s.chunkY} v${currentChunk.version} (${currentChunk.width}x${currentChunk.height})`;
renderChunk();
loadZone();
}
function renderRegionMap() {
@@ -526,6 +573,13 @@
loadZone();
});
window.addEventListener('beforeunload', () => {
if (eventSource) {
eventSource.close();
eventSource = null;
}
});
initSelectors();
loadRegion().then(loadChunk);
</script>