diff --git a/app/event.php b/app/event.php index ec74f1a..ca657b0 100644 --- a/app/event.php +++ b/app/event.php @@ -412,6 +412,17 @@ public static function addNewEvent($data) file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$newid.".txt", $result, FILE_APPEND); } + // create new mappings file: result classname|coursename|courseid so we can match them up again for updates. + // may be able to use the sarjat file but don't want to touch it - I don't know what it could break in RG1 + for ($i = 1; $i < count($data->mappings); $i++) { + $class = utils::encode_rg_output($data->mappings[$i]->class); + $course = utils::encode_rg_output($data->mappings[$i]->course); + $courseid = utils::encode_rg_output($data->mappings[$i]->courseid); + + $classmapping = $class."|".$course."|".$courseid.PHP_EOL; + file_put_contents(KARTAT_DIRECTORY."mappings_".$newid.".txt", $classmapping, FILE_APPEND); + } + if ($write["status_msg"] == "") { $write["ok"] = true; $write["status_msg"] = "Event created."; @@ -441,7 +452,7 @@ public static function deleteEvent($eventid) // rename all associated files but don't worry about errors // safer than deleting them since you can always add the event again - $files = array("kilpailijat_", "kommentit_", "hajontakanta_", "merkinnat_", "radat_", "ratapisteet_", "sarjat_", "sarjojenkoodit_"); + $files = array("kilpailijat_", "kommentit_", "hajontakanta_", "merkinnat_", "radat_", "ratapisteet_", "sarjat_", "sarjojenkoodit_", "mappings_"); foreach ($files as $file) { @rename(KARTAT_DIRECTORY.$file.$eventid.".txt", KARTAT_DIRECTORY."deleted_".$file.$eventid.".txt"); } @@ -569,4 +580,4 @@ public static function fixResults($id) { utils::unlockDatabase(); } } -} \ No newline at end of file +} diff --git a/app/result.php b/app/result.php index 9af2a42..60aaad3 100644 --- a/app/result.php +++ b/app/result.php @@ -193,6 +193,209 @@ private static function isDefaultComment($comment) return false; } + public static function updateResults($eventid, $data){ + + $date = date("Y-m-dTHis"); + $write["status_msg"] = ""; + + utils::rg2log("Updating result files for ".$eventid.". Old data will have datestamp: ".$date.".txt"); + + + //Archive old kilpailijat file + rename(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", + KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt"); + + + // create new kilpailijat file: results + for ($i = 0; $i < count($data->results); $i++) { + $a = $data->results[$i]; + + // save position and status if we got them + if (isset($a->position)) { + $position = $a->position; + } else { + $position = ''; + } + if (isset($a->status)) { + $status = utils::abbreviateStatus($a->status); + } else { + $status = ''; + } + + // course provided by json is actually result class - get correct course name + // based on mapping file, and load to txt file if not "Do not save" + $coursename = ""; + $courseid = ""; + $fh = fopen(KARTAT_DIRECTORY."mappings_".$eventid.".txt", 'r'); + while ($oldrow = fgets($fh)) { + $row = explode("|", $oldrow); + if (utils::encode_rg_output($a->course) == $row[0]) { + $coursename = $row[1]; + $courseid = $row[2]; + break; + } + } + + if($coursename !== "Do not save"){ + $result = ($i + 1)."|".trim($courseid)."|".utils::encode_rg_output($coursename); + $result .= "|".utils::encode_rg_output(trim($a->name))."|".$a->starttime."|"; + // abusing dbid to save status and position + $result .= utils::encode_rg_output($a->dbid)."_#".$position."#".$status; + $result .= "|".$a->variantid."|".$a->time."|".$a->splits.PHP_EOL; + file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", $result, FILE_APPEND); + } + } + + + // Get orig and new resultid so we can replace them in comments and routes files + // 1|1|C2|Bridget Anderson|35700|121_#1#OK||00:51:36| + + $kilpailijat = array(); + + $fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt", 'r'); + while ($oldrow = fgets($fh)) { + + $old = explode("|", $oldrow); + + $fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", 'r'); + while ($newrow = fgets($fh)) { + + $new = explode("|", $newrow); + + if ($new[3] == $old[3] && $new[2] == $old[2]){ + + $row = array(); + $row["origresultid"] = $old[0]; + $row["newresultid"] = $new[0]; + $row["origcourseid"] = $old[1]; + $row["newcourseid"] = $new[1]; + $row["coursename"] = $old[2]; + $row["name"] = $old[3]; + $kilpailijat[] = $row; + + break; + } + } + } + + // Add GPS routes from old file + $fh = fopen(KARTAT_DIRECTORY."kilpailijat_".$eventid."_rm_".$date.".txt", 'r'); + while ($oldrow = fgets($fh)) { + + $old = explode("|", $oldrow); + + if (strpos($old[3], 'GPS ') !== false){ + + // Get the current Id for the non-GPS record + + foreach ($kilpailijat as $kil){ + + if (substr($old[3], 5) == $kil["name"] && + $old[2] == $kil["coursename"]){ + + $row = array(); + $row["origresultid"] = $old[0]; + $row["newresultid"] = GPS_RESULT_OFFSET + $kil["newresultid"]; + $row["origcourseid"] = $old[1]; + $row["newcourseid"] = $kil["newcourseid"]; + $row["coursename"] = $old[2]; + $row["name"] = $old[3]; + $kilpailijat[] = $row; + + $result = $row["newresultid"]."|".$row["newcourseid"]."|".$row["coursename"]; + $result .= "|".$row["name"]."|".$old[4]."|".$old[5]."|".$old[6]."|".$old[7]."|".$old[8]."|".$old[9]; + + file_put_contents(KARTAT_DIRECTORY."kilpailijat_".$eventid.".txt", $result, FILE_APPEND); + + break; + + } + } + } + } + + if (file_exists(KARTAT_DIRECTORY."kommentit_".$eventid.".txt")){ + + // Recreate kommentit file + // Replace old course id and result id with ones from new kilpailijat file + // Kommentit format: 2|34|Jake Hanson||test + + $updatedfile = array(); + $fh = fopen(KARTAT_DIRECTORY."/kommentit_".$eventid.".txt", 'r'); + while ($oldrow = fgets($fh)) { + + $olddata = explode("|", $oldrow); + + foreach ($kilpailijat as $k){ + if ($k["origresultid"] == $olddata[1]){ + + $row = $k["newcourseid"]."|".$k["newresultid"]."|".$olddata[2]."|".$olddata[3]."|".$olddata[4]; + + $updatedfile[] = $row; + + break; + + } + } + } + + //Archive old file + rename(KARTAT_DIRECTORY."kommentit_".$eventid.".txt", + KARTAT_DIRECTORY."kommentit_".$eventid."_rm_".$date.".txt"); + + //Write new file + file_put_contents(KARTAT_DIRECTORY."kommentit_".$eventid.".txt", $updatedfile); + utils::rg2log("Updated route comments file "); + } + + if (file_exists(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt")){ + + + // Recreate merkinnat file + // Replace old course id and result id with ones from new kilpaijat file + // Merkinnat format: 2|34|Jake Hanson|null|Semicolon-separated results + + $updatedfile = array(); + $fh = fopen(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", 'r'); + while ($oldrow = fgets($fh)) { + $olddata = explode("|", $oldrow); + + foreach ($kilpailijat as $k){ + if ($k["origresultid"] == $olddata[1]){ + + $row = $k["newcourseid"]."|".$k["newresultid"]."|".$olddata[2]."|".$olddata[2]."|".$olddata[4]; + + $updatedfile[] = $row; + + break; + + } + } + + } + + //Archive old file + rename(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", + KARTAT_DIRECTORY."merkinnat_".$eventid."_rm_".$date.".txt"); + + //Write updated file + file_put_contents(KARTAT_DIRECTORY."merkinnat_".$eventid.".txt", $updatedfile); + utils::rg2log("Updated routes file"); + + } + + if ($write["status_msg"] == "") { + $write["ok"] = true; + $write["status_msg"] = "Results updated."; + } else { + $write["ok"] = false; + } + utils::rg2log("Updated results file "); + return $write; + + utils::unlockDatabase(); + } + private static function sortResultsByCourseThenTime($a, $b) { if (intval($a["courseid"]) !== intval($b["courseid"])) { @@ -200,4 +403,4 @@ private static function sortResultsByCourseThenTime($a, $b) } return (intval($a["secs"]) - intval($b["secs"])); } -} \ No newline at end of file +} diff --git a/html/manager.html b/html/manager.html index f0c255f..08f0778 100644 --- a/html/manager.html +++ b/html/manager.html @@ -55,6 +55,7 @@