mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
core: fix lang preferences when changed lang list
This commit is contained in:
1 parent
391eb96b22
commit
e7780c56bb
7 files changed
+29
-40
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "hydrooj",
|
||||
"version": "2.20.16",
|
||||
"version": "2.20.17",
|
||||
"bin": "bin/hydrooj.js",
|
||||
"main": "dist/loader.js",
|
||||
"typings": "dist/loader.d.ts",
|
||||
@@ -30,6 +30,7 @@
|
||||
"mongodb": "^3.6.4",
|
||||
"nodemailer": "^6.5.0",
|
||||
"p-queue": "^6.6.2",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"serialize-javascript": "^5.0.1",
|
||||
"sockjs": "^0.3.20",
|
||||
"superagent": "^6.1.0",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { inspect } from 'util';
|
||||
import * as yaml from 'js-yaml';
|
||||
import * as judge from './judge';
|
||||
import { ValidationError } from '../error';
|
||||
import * as check from '../check';
|
||||
@@ -28,6 +29,13 @@ function set(key: string, value: any) {
|
||||
if (!Number.isSafeInteger(+value)) throw new ValidationError(key);
|
||||
return +value;
|
||||
}
|
||||
if (s.subType === 'yaml') {
|
||||
try {
|
||||
yaml.load(value);
|
||||
} catch (e) {
|
||||
throw new ValidationError(key);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
return undefined;
|
||||
|
||||
@@ -48,6 +48,7 @@ export interface Setting {
|
||||
range: [string, string][] | Record<string, string>,
|
||||
value: any,
|
||||
type: string,
|
||||
subType?: string,
|
||||
name: string,
|
||||
desc: string,
|
||||
flag: number,
|
||||
@@ -180,19 +181,14 @@ declare module './model/problem' {
|
||||
nSubmit: number,
|
||||
nAccept: number,
|
||||
tag: string[],
|
||||
/** @deprecated */
|
||||
category?: string[],
|
||||
data: FileInfo[],
|
||||
additional_file: FileInfo[],
|
||||
hidden: boolean,
|
||||
acMsg?: string,
|
||||
html?: boolean,
|
||||
|
||||
difficulty?: number,
|
||||
difficultyAlgo?: number,
|
||||
difficultyAdmin?: number,
|
||||
difficultySetting?: any,
|
||||
|
||||
/** @deprecated */
|
||||
category?: string[],
|
||||
/** @deprecated */
|
||||
config: any,
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'reflect-metadata';
|
||||
/* eslint-disable import/first */
|
||||
/* eslint-disable no-continue */
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
|
||||
@@ -42,9 +42,6 @@ export namespace Pdoc {
|
||||
config: {},
|
||||
acMsg: '',
|
||||
difficulty: 0,
|
||||
difficultyAlgo: 0,
|
||||
difficultyAdmin: 0,
|
||||
difficultySetting: null,
|
||||
}));
|
||||
|
||||
export function create(docId?: number, pid?: string) {
|
||||
@@ -56,16 +53,6 @@ export namespace Pdoc {
|
||||
}
|
||||
}
|
||||
|
||||
export const SETTING_DIFFICULTY_ALGORITHM = 0;
|
||||
export const SETTING_DIFFICULTY_ADMIN = 1;
|
||||
export const SETTING_DIFFICULTY_AVERAGE = 2;
|
||||
|
||||
export const SETTING_DIFFICULTY_RANGE = [
|
||||
[SETTING_DIFFICULTY_ALGORITHM, 'Use algorithm calculated'],
|
||||
[SETTING_DIFFICULTY_ADMIN, 'Use admin specificed'],
|
||||
[SETTING_DIFFICULTY_AVERAGE, 'Use average of above'],
|
||||
];
|
||||
|
||||
export const PROJECTION_LIST: Pdoc.Field[] = [
|
||||
'_id', 'domainId', 'docType', 'docId', 'pid',
|
||||
'owner', 'title', 'nSubmit', 'nAccept', 'difficulty',
|
||||
@@ -246,11 +233,6 @@ global.Hydro.model.problem = {
|
||||
PROJECTION_LIST,
|
||||
PROJECTION_PUBLIC,
|
||||
|
||||
SETTING_DIFFICULTY_ADMIN,
|
||||
SETTING_DIFFICULTY_ALGORITHM,
|
||||
SETTING_DIFFICULTY_AVERAGE,
|
||||
SETTING_DIFFICULTY_RANGE,
|
||||
|
||||
add,
|
||||
inc,
|
||||
get,
|
||||
|
||||
@@ -44,9 +44,11 @@ export const Setting = (
|
||||
family: string, key: string, value: any = null,
|
||||
type: SettingType = 'text', name = '', desc = '', flag = 0,
|
||||
): _Setting => {
|
||||
let subType = '';
|
||||
if (type === 'yaml' && typeof value !== 'string') {
|
||||
value = yaml.dump(value);
|
||||
type = 'textarea';
|
||||
subType = 'yaml';
|
||||
}
|
||||
return {
|
||||
family,
|
||||
@@ -55,6 +57,7 @@ export const Setting = (
|
||||
name,
|
||||
desc,
|
||||
flag,
|
||||
subType,
|
||||
type: typeof type === 'object' ? 'select' : type,
|
||||
range: typeof type === 'object' ? type : null,
|
||||
};
|
||||
@@ -181,15 +184,24 @@ SystemSetting(
|
||||
|
||||
bus.once('app/started', async () => {
|
||||
logger.debug('Ensuring settings');
|
||||
const system = global.Hydro.model.system;
|
||||
for (const setting of SYSTEM_SETTINGS) {
|
||||
if (setting.value) {
|
||||
const current = await global.Hydro.service.db.collection('system').findOne({ _id: setting.key });
|
||||
if (!current || current.value == null || current.value === '') {
|
||||
// @ts-ignore
|
||||
await global.Hydro.model.system.set(setting.key, setting.value);
|
||||
await system.set(setting.key, setting.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
SETTINGS_BY_KEY['codeLang'].range = yaml.load(system.get('lang.texts')) as any;
|
||||
} catch (e) { /* Ignore */ }
|
||||
});
|
||||
|
||||
bus.on('system/setting', (args) => {
|
||||
if (!args.lang?.texts) return;
|
||||
SETTINGS_BY_KEY['lang.texts'].value = args.lang.texts;
|
||||
});
|
||||
|
||||
global.Hydro.model.setting = {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { Pdoc } from '../interface';
|
||||
import * as problem from '../model/problem';
|
||||
|
||||
export const description = 'Caculate the difficulty of a problem';
|
||||
@@ -47,22 +46,12 @@ function difficultyAlgorithm(nSubmit: number, nAccept: number) {
|
||||
return Math.max(ans, 1);
|
||||
}
|
||||
|
||||
function _get_difficulty(pdoc: Pdoc, calculatedDifficulty: number) {
|
||||
const setting = pdoc.difficultySetting;
|
||||
if (setting === problem.SETTING_DIFFICULTY_ALGORITHM) return calculatedDifficulty;
|
||||
if (setting === problem.SETTING_DIFFICULTY_ADMIN) return pdoc.difficultyAdmin;
|
||||
if (setting === problem.SETTING_DIFFICULTY_AVERAGE && pdoc.difficultyAdmin !== null) {
|
||||
return Math.round((calculatedDifficulty + pdoc.difficultyAdmin) / 2);
|
||||
} return calculatedDifficulty;
|
||||
}
|
||||
|
||||
export async function run({
|
||||
domainId, pid,
|
||||
}) {
|
||||
const pdoc = await problem.get(domainId, pid);
|
||||
const difficultyAlgo = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept);
|
||||
const difficulty = _get_difficulty(pdoc, difficultyAlgo);
|
||||
return await problem.edit(domainId, pdoc.docId, { difficulty, difficultyAlgo });
|
||||
const difficulty = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept);
|
||||
return await problem.edit(domainId, pdoc.docId, { difficulty });
|
||||
}
|
||||
|
||||
export const validate = {
|
||||
|
||||
Reference in new issue
Block a user