goldberries_data_pipeline/process_list_afterjson.php
2024-06-28 05:07:20 -04:00

94 lines
2.7 KiB
PHP

<?php
// map a challenge's difficulty (human readable) to database difficulty id
function map_diff_id($chal) : int
{
// standard
switch ($chal->diff_id ?? -1) {
case 0:
return 2; break;
case 1:
return 5; break;
case 2:
return 8; break;
case 3:
return 11; break;
case 4:
return 14; break;
case 5:
return 15; break;
case 6:
return 16; break;
case 7:
return 17; break;
case 8:
return 19; break; // undetermined
default:
return 18; break; // standard
}
}
$obj_camps = json_decode(file_get_contents('generated_camps.json'));
$obj_players = json_decode(file_get_contents('generated_players.json'));
$id_camp = 1;
$id_map = 1;
$id_chal = 1;
$camp_str = "INSERT INTO Campaign(name, url, author_gb_name, author_gb_id) VALUES\n";
$map_str = "INSERT INTO Map(name, campaign_id) VALUES\n";
$chal_str = "INSERT INTO Challenge(map_id, objective_id, difficulty_id, requires_fc, has_fc, is_arbitrary) VALUES\n";
$sub_str = "INSERT INTO Submission(challenge_id, player_id, proof_url, is_fc, verifier_notes) VALUES\n";
$player_str = "INSERT INTO Player(name) VALUES\n";
foreach ($obj_camps as $camp_ind => &$camp) {
$camp_str .= "('" . str_replace("'", "''", $camp->name)
. "', '" . $camp->url
. "', '" . str_replace("'", "''", $camp->author_name)
. "', " . $camp->author_id
. "),\n";
foreach ($camp->maps as $map_ind => &$map) {
$map_str .= "('" . str_replace("'", "''", $map->name)
. "', " . $id_camp
. "),\n";
foreach ($map->challenges as $chal_ind => &$chal) {
if (!isset($chal->submissions)) {
continue;
}
$chal_str .= "(" . $id_map
. ", 1, " . map_diff_id($chal)
. ", " . ($chal->requires_fc ? "'1'" : "'0'")
. ", " . ($chal->has_fc ? "'1'" : "'0'")
. ", '0'),\n";
foreach ($chal->submissions as $sub_ind => &$sub) {
$sub_str .= "(" . $id_chal
. ", " . $sub->player_id
. ", '" . $sub->proof_url
. "', " .($sub->is_fc ? "'1'" : "'0'")
. ", " . (isset($sub->verifier_note) ? ("'" . str_replace("'", "''", $sub->verifier_note) . "'") : "null")
. "),\n";
}
$id_chal += 1;
}
$id_map += 1;
}
$id_camp += 1;
}
foreach ($obj_players as $player_ind => &$player) {
$player_str .= "('" . str_replace("'", "''", $player) . "'),\n";
}
$camp_str = rtrim($camp_str, "\n ,") . ";";
$map_str = rtrim($map_str, "\n ,") . ";";
$chal_str = rtrim($chal_str, "\n ,") . ";";
$sub_str = rtrim($sub_str, "\n ,") . ";";
$player_str = rtrim($player_str, "\n ,") . ";";
file_put_contents('sql_campaign.sql', $camp_str);
file_put_contents('sql_maps.sql', $map_str);
file_put_contents('sql_chals.sql', $chal_str);
file_put_contents('sql_subs.sql', $sub_str);
file_put_contents('sql_player.sql', $player_str);