-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdRatingContestFromJson.js
88 lines (78 loc) · 2.61 KB
/
updRatingContestFromJson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
global.loggerCategory = 'updRatingContestFromJson';
const fs = require('fs-extra');
const path = require('path');
const { logger } = require('./utils/logger');
const { getOjSqlAgent } = require('./utils/sql');
const { runMain } = require('./utils/misc');
const { query } = getOjSqlAgent();
const username2userIdMap = {};
async function queryOne(sql, params) {
const res = await query(sql + ' LIMIT 1', params);
if (res && res[0]) {
return res[0];
}
return null;
}
async function findUserIdByUsername(username) {
if (username2userIdMap[username]) {
return username2userIdMap[username];
}
const res = await queryOne('SELECT user_id FROM user where binary user_name=?', [username]);
if (res) {
username2userIdMap[username] = res.user_id;
return res.user_id;
}
return null;
}
async function updRatingContest(contestId) {
logger.info(`contestId: ${contestId}`);
let res;
const ratingUntil = fs.readJsonSync(path.join(__dirname, 'data', 'rating', `sdut_rating_info_until_${contestId}.json`));
const ratingChange = fs.readJsonSync(path.join(__dirname, 'data', 'rating', `sdut_rating_changes_${contestId}.json`));
const usedRatingUntil = {};
for (const username of Object.keys(ratingUntil)) {
const r = ratingUntil[username];
const userId = await findUserIdByUsername(username);
usedRatingUntil[userId] = {
rating: r.rating,
ratingHistory: r.contests.map(c => ({
contest: {
contestId: c.cid,
title: c.contest_name,
},
rank: c.rank,
rating: c.rating,
ratingChange: c.rating_change,
date: c.date,
})),
};
}
const usedRatingChange = {};
for (const contestUserId of Object.keys(ratingChange)) {
const r = ratingChange[contestUserId];
const username = r.user_name;
const userId = await findUserIdByUsername(username);
usedRatingChange[userId] = {
rank: r.rank,
oldRating: r.old_rating,
newRating: r.new_rating,
ratingChange: r.rating_change,
};
}
fs.writeFileSync('conv1.json', JSON.stringify(usedRatingUntil, null, ' '));
fs.writeFileSync('conv2.json', JSON.stringify(usedRatingChange, null, ' '));
// 更新 DB
await query(`INSERT INTO rating_contest SET contest_id=?, rating_until=?, rating_change=?, created_at=NOW(), updated_at=NOW()`, [
contestId,
JSON.stringify(usedRatingUntil),
JSON.stringify(usedRatingChange),
]);
}
async function main() {
logger.info('[updRatingContest.start]');
const contestIds = (process.argv[2] || '').split(',');
for (const contestId of contestIds) {
await updRatingContest(contestId);
}
}
runMain(main);