算法评测平台前端。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

486 lines
14 KiB

7 years ago
let Problem = syzoj.model('problem');
let JudgeState = syzoj.model('judge_state');
let Article = syzoj.model('article');
let Contest = syzoj.model('contest');
let User = syzoj.model('user');
let UserPrivilege = syzoj.model('user_privilege');
7 years ago
const RatingCalculation = syzoj.model('rating_calculation');
const RatingHistory = syzoj.model('rating_history');
let ContestPlayer = syzoj.model('contest_player');
const calcRating = require('../libs/rating');
7 years ago
app.get('/admin/info', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
let allSubmissionsCount = await JudgeState.count();
6 years ago
let todaySubmissionsCount = await JudgeState.count({
submit_time: TypeORM.MoreThanOrEqual(syzoj.utils.getCurrentDate(true))
});
7 years ago
let problemsCount = await Problem.count();
let articlesCount = await Article.count();
let contestsCount = await Contest.count();
let usersCount = await User.count();
res.render('admin_info', {
allSubmissionsCount: allSubmissionsCount,
todaySubmissionsCount: todaySubmissionsCount,
problemsCount: problemsCount,
articlesCount: articlesCount,
contestsCount: contestsCount,
usersCount: usersCount
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
let configItems = {
'title': { name: '站点标题', type: String },
'google_analytics': { name: 'Google Analytics', type: String },
7 years ago
'默认参数': null,
'default.problem.time_limit': { name: '时间限制(单位:ms)', type: Number },
'default.problem.memory_limit': { name: '空间限制(单位:MiB)', type: Number },
'限制': null,
'limit.time_limit': { name: '最大时间限制(单位:ms)', type: Number },
'limit.memory_limit': { name: '最大空间限制(单位:MiB)', type: Number },
'limit.data_size': { name: '所有数据包大小(单位:byte)', type: Number },
'limit.testdata': { name: '测试数据大小(单位:byte)', type: Number },
'limit.submit_code': { name: '代码长度(单位:byte)', type: Number },
'limit.submit_answer': { name: '提交答案题目答案大小(单位:byte)', type: Number },
'limit.custom_test_input': { name: '自定义测试输入文件大小(单位:byte)', type: Number },
'limit.testdata_filecount': { name: '测试数据文件数量(单位:byte)', type: Number },
'每页显示数量': null,
'page.problem': { name: '题库', type: Number },
'page.judge_state': { name: '提交记录', type: Number },
'page.problem_statistics': { name: '题目统计', type: Number },
'page.ranklist': { name: '排行榜', type: Number },
'page.discussion': { name: '讨论', type: Number },
'page.article_comment': { name: '评论', type: Number },
'page.contest': { name: '比赛', type: Number }
7 years ago
};
app.get('/admin/config', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
for (let i in configItems) {
if (!configItems[i]) continue;
configItems[i].val = eval(`syzoj.config.${i}`);
}
res.render('admin_config', {
items: configItems
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/config', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
for (let i in configItems) {
if (!configItems[i]) continue;
if (req.body[i]) {
let val;
if (configItems[i].type === Boolean) {
val = req.body[i] === 'on';
} else if (configItems[i].type === Number) {
val = Number(req.body[i]);
7 years ago
} else {
val = req.body[i];
}
let f = new Function('val', `syzoj.config.${i} = val`);
f(val);
}
}
await syzoj.utils.saveConfig();
res.redirect(syzoj.utils.makeUrl(['admin', 'config']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.get('/admin/privilege', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
6 years ago
let a = await UserPrivilege.find();
7 years ago
let users = {};
for (let p of a) {
if (!users[p.user_id]) {
users[p.user_id] = {
6 years ago
user: await User.findById(p.user_id),
7 years ago
privileges: []
};
}
users[p.user_id].privileges.push(p.privilege);
}
res.render('admin_privilege', {
users: Object.values(users)
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/privilege', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
let data = JSON.parse(req.body.data);
for (let id in data) {
6 years ago
let user = await User.findById(id);
7 years ago
if (!user) throw new ErrorMessage(`不存在 ID 为 ${id} 的用户。`);
await user.setPrivileges(data[id]);
}
res.redirect(syzoj.utils.makeUrl(['admin', 'privilege']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
7 years ago
app.get('/admin/rating', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
6 years ago
const contests = await Contest.find({
order: {
start_time: 'DESC'
}
});
const calcs = await RatingCalculation.find({
order: {
id: 'DESC'
}
});
7 years ago
for (const calc of calcs) await calc.loadRelationships();
res.render('admin_rating', {
contests: contests,
calcs: calcs
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/rating/add', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
6 years ago
const contest = await Contest.findById(req.body.contest);
7 years ago
if (!contest) throw new ErrorMessage('无此比赛');
await contest.loadRelationships();
const newcalc = await RatingCalculation.create(contest.id);
await newcalc.save();
if (!contest.ranklist || contest.ranklist.ranklist.player_num <= 1) {
throw new ErrorMessage("比赛人数太少。");
}
const players = [];
for (let i = 1; i <= contest.ranklist.ranklist.player_num; i++) {
6 years ago
const user = await User.findById((await ContestPlayer.findById(contest.ranklist.ranklist[i])).user_id);
7 years ago
players.push({
user: user,
rank: i,
currentRating: user.rating
});
}
const newRating = calcRating(players);
for (let i = 0; i < newRating.length; i++) {
const user = newRating[i].user;
user.rating = newRating[i].currentRating;
await user.save();
const newHistory = await RatingHistory.create(newcalc.id, user.id, newRating[i].currentRating, newRating[i].rank);
7 years ago
await newHistory.save();
}
res.redirect(syzoj.utils.makeUrl(['admin', 'rating']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.post('/admin/rating/delete', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
6 years ago
const calcList = await RatingCalculation.find({
where: {
id: TypeORM.MoreThanOrEqual(req.body.calc_id)
},
order: {
id: 'DESC'
}
});
7 years ago
if (calcList.length === 0) throw new ErrorMessage('ID 不正确');
for (let i = 0; i < calcList.length; i++) {
await calcList[i].delete();
}
res.redirect(syzoj.utils.makeUrl(['admin', 'rating']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/admin/other', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
res.render('admin_other');
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
7 years ago
app.get('/admin/rejudge', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
res.render('admin_rejudge', {
form: {},
count: null
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/other', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
if (req.body.type === 'reset_count') {
6 years ago
const problems = await Problem.find();
7 years ago
for (const p of problems) {
await p.resetSubmissionCount();
}
} else if (req.body.type === 'reset_discussion') {
6 years ago
const articles = await Article.find();
for (const a of articles) {
await a.resetReplyCountAndTime();
}
} else if (req.body.type === 'reset_codelen') {
6 years ago
const submissions = await JudgeState.find();
for (const s of submissions) {
if (s.type !== 'submit-answer') {
s.code_length = s.code.length;
await s.save();
}
}
} else {
throw new ErrorMessage("操作类型不正确");
}
res.redirect(syzoj.utils.makeUrl(['admin', 'other']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
7 years ago
app.post('/admin/rejudge', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
6 years ago
let query = JudgeState.createQueryBuilder();
7 years ago
let user = await User.fromName(req.body.submitter || '');
6 years ago
if (user) {
query.andWhere('user_id = :user_id', { user_id: user.id });
} else if (req.body.submitter) {
query.andWhere('user_id = :user_id', { user_id: 0 });
}
7 years ago
let minID = parseInt(req.body.min_id);
6 years ago
if (!isNaN(minID)) query.andWhere('id >= :minID', { minID })
7 years ago
let maxID = parseInt(req.body.max_id);
6 years ago
if (!isNaN(maxID)) query.andWhere('id <= :maxID', { maxID })
7 years ago
let minScore = parseInt(req.body.min_score);
6 years ago
if (!isNaN(minScore)) query.andWhere('score >= :minScore', { minScore });
7 years ago
let maxScore = parseInt(req.body.max_score);
6 years ago
if (!isNaN(maxScore)) query.andWhere('score <= :maxScore', { maxScore });
7 years ago
let minTime = syzoj.utils.parseDate(req.body.min_time);
6 years ago
if (!isNaN(minTime)) query.andWhere('submit_time >= :minTime', { minTime: parseInt(minTime) });
7 years ago
let maxTime = syzoj.utils.parseDate(req.body.max_time);
6 years ago
if (!isNaN(maxTime)) query.andWhere('submit_time <= :maxTime', { maxTime: parseInt(maxTime) });
7 years ago
6 years ago
if (req.body.language) {
if (req.body.language === 'submit-answer') {
query.andWhere(new TypeORM.Brackets(qb => {
qb.orWhere('language = :language', { language: '' })
.orWhere('language IS NULL');
}));
} else if (req.body.language === 'non-submit-answer') {
query.andWhere('language != :language', { language: '' })
.andWhere('language IS NOT NULL');;
} else {
query.andWhere('language = :language', { language: req.body.language });
7 years ago
}
6 years ago
}
7 years ago
6 years ago
if (req.body.status) {
query.andWhere('status LIKE :status', { status: req.body.status + '%' });
}
if (req.body.problem_id) {
query.andWhere('problem_id = :problem_id', { problem_id: parseInt(req.body.problem_id) || 0 })
7 years ago
}
6 years ago
let count = await JudgeState.countQuery(query);
7 years ago
if (req.body.type === 'rejudge') {
6 years ago
let submissions = await JudgeState.queryAll(query);
7 years ago
for (let submission of submissions) {
await submission.rejudge();
}
}
res.render('admin_rejudge', {
form: req.body,
count: count
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.get('/admin/links', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
res.render('admin_links', {
links: syzoj.config.links || []
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/links', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
syzoj.config.links = JSON.parse(req.body.data);
await syzoj.utils.saveConfig();
res.redirect(syzoj.utils.makeUrl(['admin', 'links']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.get('/admin/raw', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
res.render('admin_raw', {
data: JSON.stringify(syzoj.config, null, 2)
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/raw', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
syzoj.config = JSON.parse(req.body.data);
await syzoj.utils.saveConfig();
res.redirect(syzoj.utils.makeUrl(['admin', 'raw']));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.post('/admin/restart', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
syzoj.restart();
res.render('admin_restart', {
data: JSON.stringify(syzoj.config, null, 2)
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});
app.get('/admin/serviceID', async (req, res) => {
try {
if (!res.locals.user || !res.locals.user.is_admin) throw new ErrorMessage('您没有权限进行此操作。');
res.send({
serviceID: syzoj.serviceID
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
})
}
});