core: fix lang preferences when changed lang list

This commit is contained in:
undefined committed 2021-03-10 22:02:44 +08:00
1 parent 391eb96b22
commit e7780c56bb
7 files changed
+29 -40

No files matched your search

+2 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.20.16", "version": "2.20.17",
"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",
@@ -30,6 +30,7 @@
"mongodb": "^3.6.4", "mongodb": "^3.6.4",
"nodemailer": "^6.5.0", "nodemailer": "^6.5.0",
"p-queue": "^6.6.2", "p-queue": "^6.6.2",
"reflect-metadata": "^0.1.13",
"serialize-javascript": "^5.0.1", "serialize-javascript": "^5.0.1",
"sockjs": "^0.3.20", "sockjs": "^0.3.20",
"superagent": "^6.1.0", "superagent": "^6.1.0",
+8
View File
@@ -1,4 +1,5 @@
import { inspect } from 'util'; import { inspect } from 'util';
import * as yaml from 'js-yaml';
import * as judge from './judge'; import * as judge from './judge';
import { ValidationError } from '../error'; import { ValidationError } from '../error';
import * as check from '../check'; import * as check from '../check';
@@ -28,6 +29,13 @@ function set(key: string, value: any) {
if (!Number.isSafeInteger(+value)) throw new ValidationError(key); if (!Number.isSafeInteger(+value)) throw new ValidationError(key);
return +value; return +value;
} }
if (s.subType === 'yaml') {
try {
yaml.load(value);
} catch (e) {
throw new ValidationError(key);
}
}
return value; return value;
} }
return undefined; return undefined;
+3 -7
View File
@@ -48,6 +48,7 @@ export interface Setting {
range: [string, string][] | Record<string, string>, range: [string, string][] | Record<string, string>,
value: any, value: any,
type: string, type: string,
subType?: string,
name: string, name: string,
desc: string, desc: string,
flag: number, flag: number,
@@ -180,19 +181,14 @@ declare module './model/problem' {
nSubmit: number, nSubmit: number,
nAccept: number, nAccept: number,
tag: string[], tag: string[],
/** @deprecated */
category?: string[],
data: FileInfo[], data: FileInfo[],
additional_file: FileInfo[], additional_file: FileInfo[],
hidden: boolean, hidden: boolean,
acMsg?: string, acMsg?: string,
html?: boolean, html?: boolean,
difficulty?: number, difficulty?: number,
difficultyAlgo?: number, /** @deprecated */
difficultyAdmin?: number, category?: string[],
difficultySetting?: any,
/** @deprecated */ /** @deprecated */
config: any, config: any,
} }
+1
View File
@@ -1,3 +1,4 @@
import 'reflect-metadata';
/* eslint-disable import/first */ /* eslint-disable import/first */
/* eslint-disable no-continue */ /* eslint-disable no-continue */
/* eslint-disable import/no-dynamic-require */ /* eslint-disable import/no-dynamic-require */
-18
View File
@@ -42,9 +42,6 @@ export namespace Pdoc {
config: {}, config: {},
acMsg: '', acMsg: '',
difficulty: 0, difficulty: 0,
difficultyAlgo: 0,
difficultyAdmin: 0,
difficultySetting: null,
})); }));
export function create(docId?: number, pid?: string) { 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[] = [ export const PROJECTION_LIST: Pdoc.Field[] = [
'_id', 'domainId', 'docType', 'docId', 'pid', '_id', 'domainId', 'docType', 'docId', 'pid',
'owner', 'title', 'nSubmit', 'nAccept', 'difficulty', 'owner', 'title', 'nSubmit', 'nAccept', 'difficulty',
@@ -246,11 +233,6 @@ global.Hydro.model.problem = {
PROJECTION_LIST, PROJECTION_LIST,
PROJECTION_PUBLIC, PROJECTION_PUBLIC,
SETTING_DIFFICULTY_ADMIN,
SETTING_DIFFICULTY_ALGORITHM,
SETTING_DIFFICULTY_AVERAGE,
SETTING_DIFFICULTY_RANGE,
add, add,
inc, inc,
get, get,
+13 -1
View File
@@ -44,9 +44,11 @@ export const Setting = (
family: string, key: string, value: any = null, family: string, key: string, value: any = null,
type: SettingType = 'text', name = '', desc = '', flag = 0, type: SettingType = 'text', name = '', desc = '', flag = 0,
): _Setting => { ): _Setting => {
let subType = '';
if (type === 'yaml' && typeof value !== 'string') { if (type === 'yaml' && typeof value !== 'string') {
value = yaml.dump(value); value = yaml.dump(value);
type = 'textarea'; type = 'textarea';
subType = 'yaml';
} }
return { return {
family, family,
@@ -55,6 +57,7 @@ export const Setting = (
name, name,
desc, desc,
flag, flag,
subType,
type: typeof type === 'object' ? 'select' : type, type: typeof type === 'object' ? 'select' : type,
range: typeof type === 'object' ? type : null, range: typeof type === 'object' ? type : null,
}; };
@@ -181,15 +184,24 @@ SystemSetting(
bus.once('app/started', async () => { bus.once('app/started', async () => {
logger.debug('Ensuring settings'); logger.debug('Ensuring settings');
const system = global.Hydro.model.system;
for (const setting of SYSTEM_SETTINGS) { for (const setting of SYSTEM_SETTINGS) {
if (setting.value) { if (setting.value) {
const current = await global.Hydro.service.db.collection('system').findOne({ _id: setting.key }); const current = await global.Hydro.service.db.collection('system').findOne({ _id: setting.key });
if (!current || current.value == null || current.value === '') { if (!current || current.value == null || current.value === '') {
// @ts-ignore // @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 = { global.Hydro.model.setting = {
+2 -13
View File
@@ -1,4 +1,3 @@
import { Pdoc } from '../interface';
import * as problem from '../model/problem'; import * as problem from '../model/problem';
export const description = 'Caculate the difficulty of a 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); 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({ export async function run({
domainId, pid, domainId, pid,
}) { }) {
const pdoc = await problem.get(domainId, pid); const pdoc = await problem.get(domainId, pid);
const difficultyAlgo = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept); const difficulty = difficultyAlgorithm(pdoc.nSubmit, pdoc.nAccept);
const difficulty = _get_difficulty(pdoc, difficultyAlgo); return await problem.edit(domainId, pdoc.docId, { difficulty });
return await problem.edit(domainId, pdoc.docId, { difficulty, difficultyAlgo });
} }
export const validate = { export const validate = {