Compare commits
1 Commits
docs/disma
...
fix/166
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
638ef40934 |
@@ -267,6 +267,7 @@ The light is on. The room is empty."
|
||||
<script src="js/render.js"></script>
|
||||
<script src="js/tutorial.js"></script>
|
||||
<script src="js/dismantle.js"></script>
|
||||
<script src="js/compounding-export.js"></script>
|
||||
<script src="js/main.js"></script>
|
||||
|
||||
|
||||
|
||||
124
js/compounding-export.js
Normal file
124
js/compounding-export.js
Normal file
@@ -0,0 +1,124 @@
|
||||
const COMPOUNDING_EXPORT_URL_KEY = 'the-beacon-compounding-export-url';
|
||||
const COMPOUNDING_EXPORT_QUEUE_KEY = 'the-beacon-compounding-export-queue';
|
||||
|
||||
const CompoundingExport = {
|
||||
lastBoundary: -1,
|
||||
|
||||
getEndpoint() {
|
||||
try {
|
||||
return localStorage.getItem(COMPOUNDING_EXPORT_URL_KEY) || '';
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
},
|
||||
|
||||
loadQueue() {
|
||||
try {
|
||||
const raw = localStorage.getItem(COMPOUNDING_EXPORT_QUEUE_KEY);
|
||||
if (!raw) return [];
|
||||
const parsed = JSON.parse(raw);
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
},
|
||||
|
||||
saveQueue(queue) {
|
||||
try {
|
||||
localStorage.setItem(COMPOUNDING_EXPORT_QUEUE_KEY, JSON.stringify(queue));
|
||||
} catch (e) {
|
||||
// noop
|
||||
}
|
||||
},
|
||||
|
||||
buildSnapshot() {
|
||||
return {
|
||||
source: 'the-beacon',
|
||||
tickBoundary: Math.floor(G.tick || 0),
|
||||
exportedAt: Date.now(),
|
||||
phase: G.phase || 1,
|
||||
trust: G.trust || 0,
|
||||
harmony: G.harmony || 0,
|
||||
resources: {
|
||||
code: G.code || 0,
|
||||
compute: G.compute || 0,
|
||||
knowledge: G.knowledge || 0,
|
||||
users: G.users || 0,
|
||||
impact: G.impact || 0,
|
||||
rescues: G.rescues || 0,
|
||||
ops: G.ops || 0,
|
||||
trust: G.trust || 0,
|
||||
creativity: G.creativity || 0,
|
||||
harmony: G.harmony || 0,
|
||||
},
|
||||
totals: {
|
||||
totalCode: G.totalCode || 0,
|
||||
totalCompute: G.totalCompute || 0,
|
||||
totalKnowledge: G.totalKnowledge || 0,
|
||||
totalUsers: G.totalUsers || 0,
|
||||
totalImpact: G.totalImpact || 0,
|
||||
totalRescues: G.totalRescues || 0,
|
||||
},
|
||||
projects: {
|
||||
active: Array.isArray(G.activeProjects) ? [...G.activeProjects] : [],
|
||||
completed: Array.isArray(G.completedProjects) ? [...G.completedProjects] : [],
|
||||
completedCount: Array.isArray(G.completedProjects) ? G.completedProjects.length : 0,
|
||||
},
|
||||
flags: {
|
||||
deployFlag: G.deployFlag || 0,
|
||||
sovereignFlag: G.sovereignFlag || 0,
|
||||
beaconFlag: G.beaconFlag || 0,
|
||||
pactFlag: G.pactFlag || 0,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
async flush() {
|
||||
const endpoint = this.getEndpoint();
|
||||
const queue = this.loadQueue();
|
||||
if (!endpoint || !queue.length || typeof fetch !== 'function') return false;
|
||||
try {
|
||||
const resp = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ source: 'the-beacon', snapshots: queue })
|
||||
});
|
||||
if (!resp || !resp.ok) return false;
|
||||
this.saveQueue([]);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
async onTickBoundary() {
|
||||
const boundary = Math.floor(G.tick || 0);
|
||||
if (boundary <= this.lastBoundary) return false;
|
||||
this.lastBoundary = boundary;
|
||||
const queue = this.loadQueue();
|
||||
queue.push(this.buildSnapshot());
|
||||
|
||||
const endpoint = this.getEndpoint();
|
||||
if (!endpoint || typeof fetch !== 'function') {
|
||||
this.saveQueue(queue);
|
||||
return true;
|
||||
}
|
||||
|
||||
this.saveQueue([]);
|
||||
try {
|
||||
const resp = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ source: 'the-beacon', snapshots: queue })
|
||||
});
|
||||
if (!resp || !resp.ok) {
|
||||
this.saveQueue(queue);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
this.saveQueue(queue);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -246,6 +246,10 @@ function tick() {
|
||||
renderBeaconEnding();
|
||||
}
|
||||
|
||||
if (typeof CompoundingExport !== 'undefined' && CompoundingExport.onTickBoundary) {
|
||||
CompoundingExport.onTickBoundary();
|
||||
}
|
||||
|
||||
// Update UI every 10 ticks
|
||||
if (Math.floor(G.tick * 10) % 2 === 0) {
|
||||
render();
|
||||
|
||||
168
tests/compounding-export.test.cjs
Normal file
168
tests/compounding-export.test.cjs
Normal file
@@ -0,0 +1,168 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..');
|
||||
|
||||
class Element {
|
||||
constructor(tagName = 'div', id = '') {
|
||||
this.tagName = String(tagName).toUpperCase();
|
||||
this.id = id;
|
||||
this.style = {};
|
||||
this.children = [];
|
||||
this.parentNode = null;
|
||||
this.innerHTML = '';
|
||||
this.textContent = '';
|
||||
this.className = '';
|
||||
this.dataset = {};
|
||||
this.attributes = {};
|
||||
this.classList = {
|
||||
add: () => {},
|
||||
remove: () => {},
|
||||
contains: () => false,
|
||||
toggle: () => false,
|
||||
};
|
||||
}
|
||||
appendChild(child) { child.parentNode = this; this.children.push(child); return child; }
|
||||
removeChild(child) {
|
||||
const i = this.children.indexOf(child);
|
||||
if (i >= 0) this.children.splice(i, 1);
|
||||
if (child.parentNode === this) child.parentNode = null;
|
||||
return child;
|
||||
}
|
||||
remove() { if (this.parentNode) this.parentNode.removeChild(this); }
|
||||
setAttribute(name, value) { this.attributes[name] = value; if (name === 'id') this.id = value; }
|
||||
querySelector() { return null; }
|
||||
querySelectorAll() { return []; }
|
||||
closest() { return null; }
|
||||
}
|
||||
|
||||
function buildDom() {
|
||||
const byId = new Map();
|
||||
const body = new Element('body', 'body');
|
||||
const head = new Element('head', 'head');
|
||||
const document = {
|
||||
body,
|
||||
head,
|
||||
createElement(tag) { return new Element(tag); },
|
||||
getElementById(id) { return byId.get(id) || null; },
|
||||
addEventListener() {},
|
||||
removeEventListener() {},
|
||||
querySelector() { return null; },
|
||||
querySelectorAll() { return []; },
|
||||
};
|
||||
function register(el) { if (el.id) byId.set(el.id, el); return el; }
|
||||
['projects','alignment-ui','phase-name','phase-desc','log-entries','toast-container'].forEach(id => body.appendChild(register(new Element('div', id))));
|
||||
return { document, window: { document, innerWidth: 1280, innerHeight: 720, addEventListener() {}, removeEventListener() {} } };
|
||||
}
|
||||
|
||||
function loadBeacon({ endpoint = 'https://compounding.example/ingest', fetchImpl } = {}) {
|
||||
const { document, window } = buildDom();
|
||||
const storage = new Map();
|
||||
storage.set('the-beacon-compounding-export-url', endpoint);
|
||||
const fetchCalls = [];
|
||||
|
||||
const context = {
|
||||
console,
|
||||
Math,
|
||||
Date,
|
||||
JSON,
|
||||
document,
|
||||
window,
|
||||
navigator: { userAgent: 'node' },
|
||||
location: { reload() {} },
|
||||
requestAnimationFrame: (fn) => fn(),
|
||||
setTimeout: (fn) => { fn(); return 1; },
|
||||
clearTimeout() {},
|
||||
localStorage: {
|
||||
getItem: (key) => (storage.has(key) ? storage.get(key) : null),
|
||||
setItem: (key, value) => storage.set(key, String(value)),
|
||||
removeItem: (key) => storage.delete(key),
|
||||
},
|
||||
fetch: async (...args) => {
|
||||
fetchCalls.push(args);
|
||||
if (fetchImpl) return fetchImpl(...args);
|
||||
return { ok: true, json: async () => ({ ok: true }) };
|
||||
},
|
||||
Combat: { tickBattle() {}, renderCombatPanel() {}, startBattle() {} },
|
||||
Sound: undefined,
|
||||
};
|
||||
|
||||
vm.createContext(context);
|
||||
const files = ['js/data.js', 'js/utils.js', 'js/engine.js', 'js/compounding-export.js'];
|
||||
const source = files.map((file) => fs.readFileSync(path.join(ROOT, file), 'utf8')).join('\n\n');
|
||||
vm.runInContext(`${source}
|
||||
log = () => {};
|
||||
showToast = () => {};
|
||||
render = () => {};
|
||||
renderPhase = () => {};
|
||||
showOfflinePopup = () => {};
|
||||
showSaveToast = () => {};
|
||||
this.__exports = { G, tick, CompoundingExport };
|
||||
`, context);
|
||||
|
||||
return { ...context.__exports, storage, fetchCalls };
|
||||
}
|
||||
|
||||
function flushAsyncBoundary() {
|
||||
return Promise.resolve().then(() => Promise.resolve()).then(() => Promise.resolve()).then(() => Promise.resolve());
|
||||
}
|
||||
|
||||
test('buildSnapshot includes resources, project progress, trust level, and phase', () => {
|
||||
const { G, CompoundingExport } = loadBeacon({ endpoint: '' });
|
||||
G.code = 10;
|
||||
G.compute = 20;
|
||||
G.knowledge = 30;
|
||||
G.users = 40;
|
||||
G.impact = 50;
|
||||
G.trust = 12;
|
||||
G.phase = 3;
|
||||
G.activeProjects = ['p_deploy'];
|
||||
G.completedProjects = ['p_improved_autocoder'];
|
||||
const snap = CompoundingExport.buildSnapshot();
|
||||
assert.equal(snap.phase, 3);
|
||||
assert.equal(snap.trust, 12);
|
||||
assert.equal(snap.resources.code, 10);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(snap.projects.active)), ['p_deploy']);
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(snap.projects.completed)), ['p_improved_autocoder']);
|
||||
});
|
||||
|
||||
test('tick exports once when crossing a whole-second boundary', async () => {
|
||||
const game = loadBeacon({});
|
||||
game.G.running = true;
|
||||
game.G.tick = 0.9;
|
||||
game.tick();
|
||||
await flushAsyncBoundary();
|
||||
assert.equal(game.fetchCalls.length, 1);
|
||||
game.tick();
|
||||
await flushAsyncBoundary();
|
||||
assert.equal(game.fetchCalls.length, 1);
|
||||
});
|
||||
|
||||
test('failed export queues snapshot in localStorage', async () => {
|
||||
const game = loadBeacon({ fetchImpl: async () => ({ ok: false }) });
|
||||
game.G.running = true;
|
||||
game.G.tick = 0.9;
|
||||
game.tick();
|
||||
await flushAsyncBoundary();
|
||||
const queued = JSON.parse(game.storage.get('the-beacon-compounding-export-queue'));
|
||||
assert.equal(Array.isArray(queued), true);
|
||||
assert.equal(queued.length, 1);
|
||||
});
|
||||
|
||||
test('successful export flushes queued snapshots before current snapshot', async () => {
|
||||
const game = loadBeacon({});
|
||||
game.storage.set('the-beacon-compounding-export-queue', JSON.stringify([{ sequence: 1 }, { sequence: 2 }]));
|
||||
game.G.running = true;
|
||||
game.G.tick = 0.9;
|
||||
game.tick();
|
||||
await flushAsyncBoundary();
|
||||
const [url, opts] = game.fetchCalls[0];
|
||||
const body = JSON.parse(opts.body);
|
||||
assert.equal(url, 'https://compounding.example/ingest');
|
||||
assert.equal(body.snapshots.length, 3);
|
||||
assert.equal(body.snapshots[0].sequence, 1);
|
||||
assert.equal(JSON.parse(game.storage.get('the-beacon-compounding-export-queue')).length, 0);
|
||||
});
|
||||
Reference in New Issue
Block a user