Files
Robot-Clash/admin/setcoins.js

64 lines
2.8 KiB
JavaScript
Raw Normal View History

2026-11-18 15:18:38 +01:00
#!/usr/bin/env node
/* Robot Clash - set a player's coins (and optionally fame) in the cloud save. For testing.
*
* npm install firebase-admin
* node admin/setcoins.js --key serviceAccount.json --name SN09 --coins 2000000
* node admin/setcoins.js --key serviceAccount.json --email me@x.de --coins 2000000 --fame 50000
* node admin/setcoins.js --key serviceAccount.json --uid <uid> --coins 2000000
*
* It MERGES money/fame + a fresh ts into saves/<uid>, so the game takes it on next login (newer ts wins)
* and the rest of the save (robots, parts) stays intact. The account must have synced at least once
* (logged in after the cloud-save update) so a full save already exists.
*/
'use strict';
const admin = require('firebase-admin');
const fs = require('fs');
function arg(name, def) {
const i = process.argv.indexOf('--' + name);
if (i < 0) return def;
const next = process.argv[i + 1];
return next && !next.startsWith('--') ? next : true;
}
const keyPath = arg('key', process.env.GOOGLE_APPLICATION_CREDENTIALS);
const DB = arg('db', 'https://robot-clash-aa37b-default-rtdb.firebaseio.com');
const coins = arg('coins', undefined);
const fame = arg('fame', undefined);
let uid = arg('uid', undefined);
const email = arg('email', undefined);
const name = arg('name', undefined);
if (!keyPath || keyPath === true) { console.error('Need --key <serviceAccount.json>.'); process.exit(1); }
if (coins === undefined && fame === undefined) { console.error('Need --coins <n> and/or --fame <n>.'); process.exit(1); }
admin.initializeApp({
credential: admin.credential.cert(JSON.parse(fs.readFileSync(keyPath, 'utf8'))),
databaseURL: DB,
});
const auth = admin.auth();
const db = admin.database();
(async () => {
if (!uid && email) { uid = (await auth.getUserByEmail(email)).uid; }
if (!uid && name) {
// match the display name in users/<uid>/name (set on register/login)
const users = (await db.ref('users').get()).val() || {};
const hit = Object.entries(users).find(([, v]) => v && v.name === name);
if (hit) uid = hit[0];
if (!uid) { console.error(`No account found with name "${name}" in users/.`); process.exit(1); }
}
if (!uid) { console.error('Need one of --uid / --email / --name.'); process.exit(1); }
const snap = await db.ref('saves/' + uid).get();
if (!snap.exists()) {
console.error(`saves/${uid} does not exist yet. Let that account log in once (after the cloud-save update), then re-run.`);
process.exit(1);
}
const patch = { ts: Date.now() };
if (coins !== undefined) patch.money = +coins;
if (fame !== undefined) patch.fame = +fame;
await db.ref('saves/' + uid).update(patch);
console.log(`Updated saves/${uid}:`, patch, '\nThe player gets it on their next login/refresh.');
process.exit(0);
})().catch(e => { console.error(e); process.exit(1); });