125 lines
3.9 KiB
JavaScript
125 lines
3.9 KiB
JavaScript
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;
|
|
}
|
|
}
|
|
};
|