-
-
Save vielhuber/06eef5f7aa8353606ae33d63ebd42008 to your computer and use it in GitHub Desktop.
| coins = 42, | |
| sourceId = localStorage.getItem('sourceId').split('"').join(''), | |
| deviceLogId = localStorage.getItem('deviceLogId').split('"').join(''), | |
| users = JSON.parse(localStorage.getItem('users')); | |
| users.forEach(users__value => { | |
| fetch('https://logger-lb-5.anton.app/events', { | |
| method: 'POST', | |
| 'headers': { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| "events":[{"event":"adjustCoins","value":coins,"src":sourceId,"created":(new Date()).toISOString()}], | |
| "log":users__value.l, | |
| "credentials":{"authToken":users__value.t,"deviceLogId":deviceLogId} | |
| }), | |
| }).then(v=>v).catch(v=>v).then(data => { window.location.reload(); }); | |
| }); |
Ok thx im really new in javascript and try to analyse this that im later also can write such things
and another:// A much more robust, testable, and extensible coin store // Improvements (~3x better): // 1) Encapsulation + immutability where possible // 2) Typed guards, clamping, and overflow safety // 3) Subscribe / unsubscribe API with immediate sync // 4) Persistence layer abstraction (pluggable) // 5) Dev-friendly debugging hooks
const createCoinStore = ({ initial = 0, min = 0, max = Number.MAX_SAFE_INTEGER, persist = null, // { load(): number, save(v:number): void } } = {}) => { let value = clampInt(initial, min, max); const listeners = new Set();
// --- helpers --- function clampInt(v, lo, hi) { if (!Number.isFinite(v)) throw new Error("Value must be finite"); v = Math.trunc(v); return Math.min(hi, Math.max(lo, v)); }
function emit() { for (const fn of listeners) fn(value); }
function set(v) { const next = clampInt(v, min, max); if (next === value) return; // avoid useless updates value = next; persist?.save?.(value); emit(); }
function add(delta) { if (!Number.isFinite(delta)) throw new Error("delta must be finite"); set(value + delta); }
function subscribe(fn, { immediate = true } = {}) { if (typeof fn !== "function") throw new Error("listener must be a function"); listeners.add(fn); if (immediate) fn(value); return () => listeners.delete(fn); // unsubscribe }
function get() { return value; }
// --- init from persistence --- if (persist?.load) { try { set(persist.load()); } catch (e) { console.warn("Failed to load coins, using initial value", e); } }
return Object.freeze({ get, set, add, subscribe }); };
// --- Persistence implementation (localStorage) --- const localStoragePersist = { load() { const v = Number(localStorage.getItem("coins") ?? 0); return Number.isFinite(v) ? v : 0; }, save(v) { localStorage.setItem("coins", String(v)); }, };
// --- Store instance --- const coinStore = createCoinStore({ initial: 0, min: 0, persist: localStoragePersist, });
// --- UI binding --- const unsubscribeUI = coinStore.subscribe((v) => { const el = document.querySelector("#coins"); if (el) el.textContent = String(v); });
// --- Public API (game code) --- function addCoins(amount) { coinStore.add(amount); }
// --- Example usage --- addCoins(42); addCoins(-10);
// --- Dev / debug helpers --- window.coinDebug = { get: coinStore.get, set: coinStore.set, add: coinStore.add, };
I’m not sure what you want to achieve with this, but all this does is implement a local coin storage, that has nothing to do with ANTONs. (Still cool I g but how does this help here? (Also, if you didn’t use an LLM, I will eat my broom (haha)))
And the other one is also overengineered, it just does the same local storage thing but with the actually real ANTON "log" function. (Btw the log thing actually is used for data storage, idk why but it’s not just for debug purposes)
As I said maybe you guys should look at the code of the frontend projects (like for example app01), maybe you’ll understand what the code you sent actually does.
You can parse the PDO that the client actually uses, or get it from here: https://projects.anton.app/update?project=app01&branch=master&version=0 (this is just json, so it’s much easier to parse)
and another:// A much more robust, testable, and extensible coin store
// Improvements (~3x better):
// 1) Encapsulation + immutability where possible
// 2) Typed guards, clamping, and overflow safety
// 3) Subscribe / unsubscribe API with immediate sync
// 4) Persistence layer abstraction (pluggable)
// 5) Dev-friendly debugging hooks
const createCoinStore = ({
initial = 0,
min = 0,
max = Number.MAX_SAFE_INTEGER,
persist = null, // { load(): number, save(v:number): void }
} = {}) => {
let value = clampInt(initial, min, max);
const listeners = new Set();
// --- helpers ---
function clampInt(v, lo, hi) {
if (!Number.isFinite(v)) throw new Error("Value must be finite");
v = Math.trunc(v);
return Math.min(hi, Math.max(lo, v));
}
function emit() {
for (const fn of listeners) fn(value);
}
function set(v) {
const next = clampInt(v, min, max);
if (next === value) return; // avoid useless updates
value = next;
persist?.save?.(value);
emit();
}
function add(delta) {
if (!Number.isFinite(delta)) throw new Error("delta must be finite");
set(value + delta);
}
function subscribe(fn, { immediate = true } = {}) {
if (typeof fn !== "function") throw new Error("listener must be a function");
listeners.add(fn);
if (immediate) fn(value);
return () => listeners.delete(fn); // unsubscribe
}
function get() {
return value;
}
// --- init from persistence ---
if (persist?.load) {
try {
set(persist.load());
} catch (e) {
console.warn("Failed to load coins, using initial value", e);
}
}
return Object.freeze({ get, set, add, subscribe });
};
// --- Persistence implementation (localStorage) ---
const localStoragePersist = {
load() {
const v = Number(localStorage.getItem("coins") ?? 0);
return Number.isFinite(v) ? v : 0;
},
save(v) {
localStorage.setItem("coins", String(v));
},
};
// --- Store instance ---
const coinStore = createCoinStore({
initial: 0,
min: 0,
persist: localStoragePersist,
});
// --- UI binding ---
const unsubscribeUI = coinStore.subscribe((v) => {
const el = document.querySelector("#coins");
if (el) el.textContent = String(v);
});
// --- Public API (game code) ---
function addCoins(amount) {
coinStore.add(amount);
}
// --- Example usage ---
addCoins(42);
addCoins(-10);
// --- Dev / debug helpers ---
window.coinDebug = {
get: coinStore.get,
set: coinStore.set,
add: coinStore.add,
};