Fix cli.js for current yargs and mongoose

This commit is contained in:
Terncode committed 2026-11-29 13:26:47 +01:00
1 parent 5271f6e541
commit e5c33d6c0d
1 file changed
+95 -29
+95 -29
View File
@@ -1,18 +1,22 @@
require('./src/scripts/server/boot');
require('./dist/node/server/boot');
const mongoose = require('mongoose');
const fs = require('fs');
const _ = require('lodash');
const { decompressPony } = require('./src/scripts/common/compressPony');
const ponyInfo = require('./src/scripts/common/ponyInfo');
const db = require('./src/scripts/server/db');
const config = require('./src/scripts/server/config').config;
const argv = require('yargs').option('addrole', { type: 'array' }).option('removerole', { type: 'array' }).argv;
const {
decompressPony
} = require('./dist/node//common/compressPony');
const ponyInfo = require('./dist/node/common/ponyInfo');
const db = require('./dist/node/server/db');
const config = require('./dist/node/server/config').config;
const yargs = require('yargs');
const argv = (yargs.default || yargs)(process.argv.slice(2)).option('addrole', {
type: 'array'
}).option('removerole', {
type: 'array'
}).argv;
mongoose.connect(config.db, {
reconnectTries: Number.MAX_VALUE,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
serverSelectionTimeoutMS: 5000,
});
function iterate(query, onData, onEnd = () => process.exit()) {
@@ -41,7 +45,9 @@ function generatePonyStats() {
for (const key of setFields) {
const stats = sets[key];
const { type = 0, pattern = 0 } = pony[key] || {};
const {
type = 0, pattern = 0
} = pony[key] || {};
const row = stats.get(type) || new Map();
stats.set(type, row);
row.set(pattern, 1 + (row.get(pattern) || 0));
@@ -73,8 +79,8 @@ function generatePonyStats() {
['', '', 'pattern'],
['', '', ...patterns],
...entries(sets[key])
.sort(([a], [b]) => a - b)
.map(([key, value], i) => [i === 0 ? 'type' : '', key, ...patterns.map(pat => value.get(pat) || 0)]),
.sort(([a], [b]) => a - b)
.map(([key, value], i) => [i === 0 ? 'type' : '', key, ...patterns.map(pat => value.get(pat) || 0)]),
];
}));
@@ -103,7 +109,11 @@ function generatePonyStats() {
function fixCharacterStates() {
const config = require('./config.json');
db.Character.countDocuments({ state: { $exists: true } }, (err, total) => {
db.Character.countDocuments({
state: {
$exists: true
}
}, (err, total) => {
let migrated = 0;
let updating = 0;
let done = false;
@@ -115,11 +125,18 @@ function fixCharacterStates() {
console.log('started', total);
iterate(db.Character.find({ state: { $exists: true } }, '_id name state'), doc => {
iterate(db.Character.find({
state: {
$exists: true
}
}, '_id name state'), doc => {
const before = doc.state;
const after = {};
for (const { id, name } of config.servers) {
for (const {
id,
name
} of config.servers) {
const state = _.clone(before[name]);
if (state) {
@@ -148,7 +165,11 @@ function fixCharacterStates() {
}
updating++;
db.Character.update({ _id: doc._id }, { state: after }, e => {
db.Character.update({
_id: doc._id
}, {
state: after
}, e => {
migrated++;
updating--;
@@ -173,12 +194,20 @@ function fixCharacterStates() {
async function updateCharacterPositions() {
const servers = ['main', 'safe', 'safe-ru'];
const total = await db.Character.countDocuments({ state: { $exists: true } }).exec();
const total = await db.Character.countDocuments({
state: {
$exists: true
}
}).exec();
let done = 0;
console.log('updating', total);
iterate(db.Character.find({ state: { $exists: true } }, '_id name state'), doc => {
iterate(db.Character.find({
state: {
$exists: true
}
}, '_id name state'), doc => {
const state = doc.state;
if (state) {
@@ -193,7 +222,11 @@ async function updateCharacterPositions() {
}
if (changed) {
db.Character.updateOne({ _id: doc._id }, { state }, () => {
db.Character.updateOne({
_id: doc._id
}, {
state
}, () => {
done++;
if ((done % 10000) === 0) {
@@ -212,12 +245,20 @@ async function updateCharacterPositions() {
}
async function createPerfTestAccounts() {
const ponies = await db.Character.find({ account: '57ae2336a67f4dc52e123ed1' }).exec();
const ponies = await db.Character.find({
account: '57ae2336a67f4dc52e123ed1'
}).exec();
for (let i = 0; i < 1000; i++) {
const account = await db.Account.create({ name: `perf-${i}` });
const account = await db.Account.create({
name: `perf-${i}`
});
const char = ponies[i % ponies.length];
await db.Character.create({ account: account._id, name: `perf-${i}`, info: char.info });
await db.Character.create({
account: account._id,
name: `perf-${i}`,
info: char.info
});
}
process.exit();
@@ -265,7 +306,14 @@ async function clearOrphanedIgnores() {
let removed = 0;
iterate(db.Account.find({}, '_id').lean(), doc => ids.add(doc._id.toString()), () => {
iterate(db.Account.find({ ignores: { $exists: true, $not: { $size: 0 } } }, '_id ignores').lean(), doc => {
iterate(db.Account.find({
ignores: {
$exists: true,
$not: {
$size: 0
}
}
}, '_id ignores').lean(), doc => {
const remove = [];
if (doc.ignores) {
@@ -278,7 +326,13 @@ async function clearOrphanedIgnores() {
if (remove.length) {
removed += remove.length;
db.Account.updateOne({ _id: doc._id }, { $pull: { ignores: remove } }, err => console.log(err || 'removed'));
db.Account.updateOne({
_id: doc._id
}, {
$pull: {
ignores: remove
}
}, err => console.log(err || 'removed'));
}
}, () => {
console.log('done, removed: ', removed);
@@ -290,9 +344,21 @@ async function clearOrphanedIgnores() {
async function toggleRole(give, id, roles) {
if (typeof id === 'string' && id.length === 24 && ['superadmin', 'admin', 'mod', 'dev'].indexOf(roles) !== -1) {
if (give) {
await db.Account.updateOne({ _id: id }, { $addToSet: { roles } }).exec();
await db.Account.updateOne({
_id: id
}, {
$addToSet: {
roles
}
}).exec();
} else {
await db.Account.updateOne({ _id: id }, { $pull: { roles } }).exec();
await db.Account.updateOne({
_id: id
}, {
$pull: {
roles
}
}).exec();
}
console.log('done');
} else {
@@ -310,9 +376,9 @@ if (argv.settingsstats) {
} else if (argv.clearignores) {
clearOrphanedIgnores();
} else if (argv.addrole) {
toggleRole(true, argv.addrole[0], argv.addrole[1]);
toggleRole(true, String(argv.addrole[0]), String(argv.addrole[1]));
} else if (argv.removerole) {
toggleRole(false, argv.removerole[0], argv.removerole[1]);
toggleRole(false, String(argv.removerole[0]), String(argv.removerole[1]));
} else {
console.log('invalid arg');
process.exit();