big update
This commit is contained in:
90
admin/cleanup.js
Normal file
90
admin/cleanup.js
Normal file
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env node
|
||||
/* Robot Clash - Firebase cleanup. DRY-RUN by default: it only lists what it WOULD delete.
|
||||
*
|
||||
* npm install firebase-admin (once, locally)
|
||||
* node admin/cleanup.js --key serviceAccount.json # preview, deletes nothing
|
||||
* node admin/cleanup.js --key serviceAccount.json --apply # actually delete
|
||||
*
|
||||
* Options: --db <rtdb-url> --stale-min <minutes, default 10>
|
||||
* The service-account key is a SECRET: never commit or upload it (add it to .gitignore).
|
||||
*
|
||||
* Deletes (only with --apply): anonymous guest accounts (no e-mail), stale lobby/ presence,
|
||||
* and orphan saves/ ranks/ users/ nodes whose uid no longer exists in Auth.
|
||||
*/
|
||||
'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 APPLY = arg('apply', false) === true;
|
||||
const STALE_MIN = +arg('stale-min', 10);
|
||||
|
||||
if (!keyPath || keyPath === true) {
|
||||
console.error('Need --key <serviceAccount.json> (or set GOOGLE_APPLICATION_CREDENTIALS).');
|
||||
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 () => {
|
||||
console.log(APPLY ? '=== APPLY: deleting ===' : '=== DRY-RUN: nothing is deleted ===');
|
||||
|
||||
// 1) all auth users; flag anonymous (no e-mail and no linked provider)
|
||||
const uids = new Set();
|
||||
const anon = [];
|
||||
let pageToken;
|
||||
do {
|
||||
const res = await auth.listUsers(1000, pageToken);
|
||||
for (const u of res.users) {
|
||||
uids.add(u.uid);
|
||||
const registered = !!u.email || (u.providerData && u.providerData.length > 0);
|
||||
if (!registered) anon.push(u.uid);
|
||||
}
|
||||
pageToken = res.pageToken;
|
||||
} while (pageToken);
|
||||
console.log(`Auth users: ${uids.size} total, ${anon.length} anonymous/guest`);
|
||||
|
||||
// 2) stale lobby presence (no ts or older than the cutoff)
|
||||
const cutoff = Date.now() - STALE_MIN * 60000;
|
||||
const lobby = (await db.ref('lobby').get()).val() || {};
|
||||
const staleLobby = Object.entries(lobby).filter(([, v]) => !v || !v.ts || v.ts < cutoff).map(([k]) => k);
|
||||
console.log(`Lobby: ${Object.keys(lobby).length} entries, ${staleLobby.length} stale (> ${STALE_MIN} min)`);
|
||||
|
||||
// 3) orphan RTDB nodes (uid no longer exists in Auth)
|
||||
const orphans = {};
|
||||
for (const node of ['saves', 'ranks', 'users']) {
|
||||
const data = (await db.ref(node).get()).val() || {};
|
||||
orphans[node] = Object.keys(data).filter(uid => !uids.has(uid));
|
||||
console.log(`${node}: ${Object.keys(data).length} entries, ${orphans[node].length} orphan`);
|
||||
}
|
||||
|
||||
if (!APPLY) {
|
||||
console.log('\nWould delete:');
|
||||
console.log(' anonymous accounts :', anon.length);
|
||||
console.log(' stale lobby :', staleLobby.length);
|
||||
for (const node of Object.keys(orphans)) console.log(` orphan ${node.padEnd(12)}:`, orphans[node].length);
|
||||
console.log('\nRe-run with --apply to actually delete.');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
let n = 0;
|
||||
for (let i = 0; i < anon.length; i += 1000) { const r = await auth.deleteUsers(anon.slice(i, i + 1000)); n += r.successCount; }
|
||||
console.log(`Deleted ${n} anonymous accounts.`);
|
||||
for (const uid of staleLobby) await db.ref('lobby/' + uid).remove();
|
||||
console.log(`Removed ${staleLobby.length} stale lobby entries.`);
|
||||
for (const node of Object.keys(orphans)) { for (const uid of orphans[node]) await db.ref(node + '/' + uid).remove(); console.log(`Removed ${orphans[node].length} orphan ${node}.`); }
|
||||
console.log('Done.');
|
||||
process.exit(0);
|
||||
})().catch(e => { console.error(e); process.exit(1); });
|
||||
63
admin/setcoins.js
Normal file
63
admin/setcoins.js
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/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); });
|
||||
Reference in New Issue
Block a user