Files
Robot-Clash/server/economy.js
2026-11-18 15:18:38 +01:00

26 lines
1.1 KiB
JavaScript

/* Server-authoritative money/fame. The client can read its wallet but never set it directly;
it changes only through validated actions (buy/sell, fight result) added in step 3.
Loads the game's DOM-free logic (parts/robot/combat) so the server is the single source of truth. */
'use strict';
const RC = require('../game/robot.js'); // pulls in parts.js too, sets globalThis.RC
require('../game/combat.js');
require('../game/ai.js');
const { Wallet } = require('./db');
const clampMoney = v => Math.max(0, Math.min(1e9, Math.round(+v || 0)));
function wallet(uid) { Wallet.init.run(uid, 800); return Wallet.get.get(uid); }
function setWallet(uid, money, fame) {
const w = wallet(uid);
const m = money == null ? w.money : clampMoney(money);
const f = fame == null ? w.fame : clampMoney(fame);
Wallet.set.run(m, f, uid);
return { money: m, fame: f };
}
function addWallet(uid, dMoney, dFame) {
const w = wallet(uid);
return setWallet(uid, w.money + (+dMoney || 0), w.fame + (+dFame || 0));
}
module.exports = { RC, wallet, setWallet, addWallet, clampMoney };