chore(web): add upgrader for #21

This commit is contained in:
Baoshuo Ren 2022-12-05 17:31:22 +08:00
parent 586df479f6
commit 83ffb72577
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -0,0 +1,43 @@
<?php
return function ($type) {
if ($type == 'up') {
DB::init();
$problems = DB::selectAll("select * from problems");
foreach ($problems as $info) {
$problem = new UOJProblem($info);
$difficulty = -1;
$extra_config = $problem->getExtraConfig();
if (isset($extra_config['difficulty'])) {
$old_difficulty = (float)$extra_config['difficulty'];
$difficulty = (int)(3.0 * $old_difficulty + 5) * 100;
$difficulty = (function ($search, $arr) {
$closest = null;
foreach ($arr as $item) {
if ($closest === null || abs($search - $closest) > abs($item - $search)) {
$closest = $item;
}
}
return $closest;
})($difficulty, UOJProblem::$difficulty);
}
DB::update([
"update problems",
"set", [
"difficulty" => $difficulty,
],
"where", [
"id" => $problem->info['id'],
]
]);
echo "Problem: {$problem->info['id']} ({$difficulty})\n";
}
}
};