core: user level update

This commit is contained in:
undefined committed 2021-04-19 13:41:59 +08:00
1 parent 988140c244
commit e06279e26c
3 files changed
+13 -26

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.23.5", "version": "2.23.6",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "dist/loader.js", "main": "dist/loader.js",
"typings": "dist/loader.d.ts", "typings": "dist/loader.d.ts",
+2 -13
View File
@@ -255,19 +255,8 @@ PRIV.PRIV_DEFAULT = PRIV.PRIV_USER_PROFILE
+ PRIV.PRIV_CREATE_FILE + PRIV.PRIV_CREATE_FILE
+ PRIV.PRIV_DELETE_FILE_SELF; + PRIV.PRIV_DELETE_FILE_SELF;
// [10, 1] means that people whose rank is less than 1% will get Level 10 // people whose rank is less than 1% will get Level 10
export const LEVELS = [ export const LEVELS = [100, 90, 70, 55, 40, 30, 20, 10, 5, 2, 1];
[10, 1],
[9, 2],
[8, 10],
[7, 20],
[6, 30],
[5, 40],
[4, 70],
[3, 90],
[2, 95],
[1, 100],
];
export const BUILTIN_USERS: Udoc[] = [ export const BUILTIN_USERS: Udoc[] = [
{ {
+10 -12
View File
@@ -84,36 +84,34 @@ async function runContest(...arg: any[]) {
} }
export async function calcLevel(domainId: string, report: Function) { export async function calcLevel(domainId: string, report: Function) {
const dudocs = await domain.getMultiUserInDomain(domainId).sort('rp', -1).toArray(); const ducnt = await domain.getMultiUserInDomain(domainId).count();
if (!dudocs.length) return; if (!ducnt) return;
let last = { rp: null }; let last = { rp: null };
let rank = 0; let rank = 0;
let count = 0; let count = 0;
const coll = global.Hydro.service.db.collection('domain.user'); const coll = global.Hydro.service.db.collection('domain.user');
const ducur = domain.getMultiUserInDomain(domainId).project({ rp: 1 }).sort({ rp: -1 });
let bulk = coll.initializeUnorderedBulkOp(); let bulk = coll.initializeUnorderedBulkOp();
for (const dudoc of dudocs) { while (await ducur.hasNext()) {
const dudoc = await ducur.next();
count++; count++;
if (!dudoc.rp) dudoc.rp = null; if (!dudoc.rp) dudoc.rp = null;
if (dudoc.rp !== last.rp) rank = count; if (dudoc.rp !== last.rp) rank = count;
bulk.find({ _id: dudoc._id }).updateOne({ $set: { rank } }); bulk.find({ _id: dudoc._id }).updateOne({ $set: { rank } });
last = dudoc; last = dudoc;
if (count % 1000 === 0) report({ message: `#${count}: Rank ${rank}` }); if (count % 100 === 0) report({ message: `#${count}: Rank ${rank}` });
} }
await bulk.execute(); await bulk.execute();
if (!rank) {
report({ message: 'No one has rp' });
return;
}
const levels = global.Hydro.model.builtin.LEVELS; const levels = global.Hydro.model.builtin.LEVELS;
bulk = coll.initializeUnorderedBulkOp(); bulk = coll.initializeUnorderedBulkOp();
for (let i = 0; i < levels.length; i++) { for (let i = 0; i < levels.length; i++) {
report({ message: 'Updating users levelled {0}'.format(levels[i][0]) }); report({ message: 'Updating users levelled {0}'.format(i) });
const query: FilterQuery<Udoc> = { const query: FilterQuery<Udoc> = {
domainId, domainId,
$and: [{ rank: { $lte: (levels[i][1] * rank) / 100 } }], $and: [{ rank: { $lte: (levels[i] * count) / 100 } }],
}; };
if (i < levels.length - 1) query.$and.push({ rank: { $gt: (levels[i + 1][1] * rank) / 100 } }); if (i < levels.length) query.$and.push({ rank: { $gt: (levels[i + 1] * count) / 100 } });
bulk.find(query).update({ $set: { level: levels[i][0] } }); bulk.find(query).update({ $set: { level: i } });
} }
await bulk.execute(); await bulk.execute();
} }