mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
每日签到功能
This commit is contained in:
1 parent
06847d9b19
commit
9887deca6a
7 files changed
+51
-10
No files matched your search
@@ -7,5 +7,3 @@ message.markAsRead
|
||||
Handle OAuth Map !important
|
||||
基于document的model难以lint,建议拆分
|
||||
写test! !important
|
||||
每日签到
|
||||
rp.delta
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hydrooj",
|
||||
"version": "2.12.15",
|
||||
"version": "2.12.16",
|
||||
"bin": "bin/hydrooj.js",
|
||||
"main": "dist/loader.js",
|
||||
"typings": "dist/loader.d.ts",
|
||||
|
||||
@@ -18,7 +18,7 @@ class DomainRankHandler extends Handler {
|
||||
@param('page', Types.PositiveInt, true)
|
||||
async get(domainId: string, page = 1) {
|
||||
const [dudocs, upcount, ucount] = await paginate(
|
||||
domain.getMultiInDomain(domainId).sort({ rp: -1 }),
|
||||
domain.getMultiUserInDomain(domainId).sort({ rp: -1 }),
|
||||
page,
|
||||
100,
|
||||
);
|
||||
@@ -84,7 +84,7 @@ class DomainUserHandler extends ManageHandler {
|
||||
async get({ domainId }) {
|
||||
const rudocs = {};
|
||||
const [dudocs, roles] = await Promise.all([
|
||||
domain.getMultiInDomain(domainId, {
|
||||
domain.getMultiUserInDomain(domainId, {
|
||||
$and: [
|
||||
{ role: { $ne: 'default' } },
|
||||
{ role: { $ne: null } },
|
||||
|
||||
@@ -92,6 +92,35 @@ class StatusUpdateHandler extends Handler {
|
||||
}
|
||||
}
|
||||
|
||||
class CheckInHandler extends Handler {
|
||||
async prepare() {
|
||||
const { checkincnt, lastcheckin } = this.user;
|
||||
const today = new Date();
|
||||
today.setHours(0);
|
||||
today.setMinutes(0);
|
||||
today.setSeconds(0);
|
||||
today.setMilliseconds(0);
|
||||
if (lastcheckin === today.getTime()) throw new BadRequestError("You've already checked in today!");
|
||||
this.user.checkincnt++;
|
||||
this.user.lastcheckin = today.getTime();
|
||||
await Promise.all([
|
||||
user.setById(this.user._id, { checkincnt, lastcheckin }),
|
||||
user.inc(this.user._id, 'rpdelta', Math.round(Math.sqrt(checkincnt))),
|
||||
]);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.redirect = '/';
|
||||
}
|
||||
|
||||
async post() {
|
||||
this.response.body = {
|
||||
cnt: this.user.checkincnt,
|
||||
last: this.user.lastcheckin,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class SwitchLanguageHandler extends Handler {
|
||||
@param('lang', Types.String)
|
||||
async get(domainId: string, lang: string) {
|
||||
@@ -108,6 +137,7 @@ export async function apply() {
|
||||
Route('file_upload', '/fs/upload', FileUploadHandler, PRIV.PRIV_CREATE_FILE);
|
||||
Route('status', '/status', StatusHandler);
|
||||
Route('status_update', '/status/update', StatusUpdateHandler);
|
||||
Route('check_in', '/checkin', CheckInHandler, PRIV.PRIV_USER_PROFILE);
|
||||
Route('switch_language', '/language/:lang', SwitchLanguageHandler);
|
||||
}
|
||||
|
||||
|
||||
@@ -141,7 +141,7 @@ export function setMultiUserInDomain(domainId: string, query: any, params: any)
|
||||
return collUser.updateMany({ domainId, ...query }, { $set: params });
|
||||
}
|
||||
|
||||
export function getMultiInDomain(domainId: string, query: any = {}) {
|
||||
export function getMultiUserInDomain(domainId: string, query: any = {}) {
|
||||
return collUser.find({ domainId, ...query });
|
||||
}
|
||||
|
||||
@@ -212,7 +212,7 @@ global.Hydro.model.domain = {
|
||||
setMultiUserInDomain,
|
||||
setUserInDomain,
|
||||
incUserInDomain,
|
||||
getMultiInDomain,
|
||||
getMultiUserInDomain,
|
||||
getDictUserByDomainId,
|
||||
getJoinSettings,
|
||||
getPrefixSearch,
|
||||
|
||||
@@ -95,6 +95,8 @@ AccountSetting(
|
||||
'/components/profile/backgrounds/1.jpg', 'text', 'Profile Background Image',
|
||||
'Choose the background image in your profile page.'),
|
||||
Setting('setting_storage', 'usage', null, 0, 'number', 'Userfile Usage', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
Setting('setting_storage', 'checkincnt', null, 0, 'number', 'Check In Counter', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
Setting('setting_storage', 'lastcheckin', null, 0, 'number', 'Last checkin time', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
);
|
||||
|
||||
DomainSetting(
|
||||
@@ -110,6 +112,7 @@ DomainUserSetting(
|
||||
Setting('setting_storage', 'nSubmit', null, 0, 'number', 'nSubmit', null, FLAG_HIDDEN | FLAG_DISABLED),
|
||||
Setting('setting_storage', 'nLike', null, 0, 'number', 'nLike', null, FLAG_HIDDEN | FLAG_DISABLED),
|
||||
Setting('setting_storage', 'rp', null, 1500, 'number', 'RP', null, FLAG_HIDDEN | FLAG_DISABLED),
|
||||
Setting('setting_storage', 'rpdelta', null, 0, 'number', 'RP.delta', null, FLAG_HIDDEN | FLAG_DISABLED),
|
||||
Setting('setting_storage', 'level', null, 0, 'number', 'level', null, FLAG_HIDDEN | FLAG_DISABLED),
|
||||
);
|
||||
|
||||
|
||||
@@ -84,7 +84,8 @@ async function runContest(...arg: any[]) {
|
||||
}
|
||||
|
||||
export async function calcLevel(domainId: string, report: Function) {
|
||||
const dudocs = await domain.getMultiInDomain(domainId).sort('rp', -1).toArray();
|
||||
const dudocs = await domain.getMultiUserInDomain(domainId).sort('rp', -1).toArray();
|
||||
if (!dudocs.length) return;
|
||||
let last = { rp: null };
|
||||
let rank = 0;
|
||||
let count = 0;
|
||||
@@ -97,8 +98,8 @@ export async function calcLevel(domainId: string, report: Function) {
|
||||
bulk.find({ _id: dudoc._id }).updateOne({ $set: { rank } });
|
||||
last = dudoc;
|
||||
if (count % 1000 === 0) report({ message: `#${count}: Rank ${rank}` });
|
||||
await bulk.execute();
|
||||
}
|
||||
await bulk.execute();
|
||||
if (!rank) {
|
||||
report({ message: 'No one has rp' });
|
||||
return;
|
||||
@@ -120,6 +121,11 @@ export async function calcLevel(domainId: string, report: Function) {
|
||||
|
||||
async function runInDomain(domainId: string, isSub: boolean, report: Function) {
|
||||
const udict: ND = {};
|
||||
const deltaudict: ND = {};
|
||||
const dudocs = await domain.getMultiUserInDomain(domainId).toArray();
|
||||
for (const dudoc of dudocs) {
|
||||
deltaudict[dudoc.uid] = dudoc.rpdelta || 0;
|
||||
}
|
||||
const contests = await contest.getMulti(domainId, { rated: true }, -1).sort('endAt', -1).toArray();
|
||||
await report({ message: `Found ${contests.length} contests in ${domainId}` });
|
||||
for (const i in contests) {
|
||||
@@ -146,7 +152,11 @@ async function runInDomain(domainId: string, isSub: boolean, report: Function) {
|
||||
await domain.setMultiUserInDomain(domainId, {}, { rp: 1500 });
|
||||
const tasks = [];
|
||||
for (const uid in udict) {
|
||||
tasks.push(domain.setUserInDomain(domainId, parseInt(uid, 10), { rp: udict[uid] }));
|
||||
tasks.push(
|
||||
domain.setUserInDomain(
|
||||
domainId, parseInt(uid, 10), { rp: udict[uid] + (deltaudict[uid] || 0) },
|
||||
),
|
||||
);
|
||||
}
|
||||
await Promise.all(tasks);
|
||||
await calcLevel(domainId, report);
|
||||
|
||||
Reference in new issue
Block a user