From 228138934be8e4f07a28c23953e3245837277570 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 5 Feb 2016 02:55:18 -0500 Subject: [PATCH] Timeline JSON. --- lib/timeline.php | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/lib/timeline.php b/lib/timeline.php index 915edc4..64630fa 100644 --- a/lib/timeline.php +++ b/lib/timeline.php @@ -7,6 +7,31 @@ else return $n . array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th')[$n % 10]; } + function find_prizewinners($race, $entries) { + $competitors = array(); + $prizes = array(); + $last_sort_method = null; + foreach ($race->getPrizeKeys() as $p) { + $sort_method = $race->PrizeSortMethod($p); + if ($sort_method != $last_sort_method) uasort($entries, "Entry::$sort_method"); + $last_sort_method = $sort_method; + foreach ($entries as $entry) { + if ($entry->isExcluded()) continue; + $entry_id = $entry->getId(); + if (array_key_exists($entry_id, $prizes)) continue; + $competitor_id = $entry->getShip()->getCompetitorId(); + if (array_key_exists($competitor_id, $competitors)) continue; + $prizes[$entry_id] = $p; + $competitors[$competitor_id] = $entry_id; + break; + } + } + return array( + "competitors" => $competitors, + "entries" => $prizes + ); + } + function show_timeline($race_id) { $cq = new CourseQuery; $cq->joinWith('Course.Race'); @@ -40,6 +65,49 @@ echo "
\n"; $plots = array(); + $json_racers = array(); + $json_events = array(); + $eq = new EntryQuery; + $eq->joinWith('Entry.Ship'); + $eq->joinWith('Ship.Competitor'); + $eq->joinWith('Ship.Hull'); + $eq->filterByRaceId($race_id); + $entry_objects = $eq->find(); + foreach ($entry_objects as $entry) $entries[] = $entry; + $prizewinners = find_prizewinners($race, $entries); + uasort($entries, "Entry::StandardSort"); + $ranked_entries = array(); + $competitor_entries = array(); + foreach ($entries as $entry) { + if ($entry->isExcluded()) continue; + $entry_id = $entry->getId(); + $ship =$entry->getShip(); + $competitor = $ship->getCompetitor(); + $competitor_id = $competitor->getId(); + if (array_key_exists($entry_id, $prizewinners["entries"]) || (! array_key_exists($competitor_id, $prizewinners["competitors"]) && ! array_key_exists($competitor_id, $competitor_entries))) { + $competitor_entries[$competitor_id] = $entry_id; + $ranked_entries[$entry_id] = true; + $json_racer = array("join" => count($json_racers) + 1); + $json_racer["owner"] = $competitor->getCmdrName(); + $json_racer["user"] = $competitor->getForumName(); + if (! $json_racer["user"]) $json_racer["user"] = $json_racer["owner"]; + $json_racer["ship"] = $ship->getName(); + $json_racer["hull"] = $ship->getHull()->getName(); + $json_racer["lap_distance"] = $entry->getLapDistance(); + $json_racer["complete_laps"] = $entry->getCompleteLaps(); + $json_racer["allotted_time"] = $allotted_time + $entry->getBonusTime(); + $json_racer["bonus_time"] = (int) $entry->getBonusTime(); + $json_racer["raced_time"] = $entry->getRacedTime(); + $json_racer["total_time"] = $entry->getTotalTime(); + $json_racer["joker"] = (bool) $entry->usedJoker(); + $json_racer["shields"] = (bool) $entry->shields(); + $json_racer["docking_computer"] = (bool) $entry->usedDockingComputer(); + $json_racer["fsd_injection"] = (bool) $entry->usedInjection(); + $json_racer["cut_short"] = (bool) $entry->cutShort(); + $json_racers[] = $json_racer; + } + } + $q = new LaptimeQuery; $q->joinWith('Laptime.Station'); $q->joinWith('Station.System'); @@ -149,18 +217,47 @@ $verb = ($station_order > 1) ? "arrived at" : "left"; echo "

"; echo "$delta: CMDR $cmdr_name flying $hull_name $ship_name from $start_time $verb $nth station "; - if ($allotted_time) echo "of lap $lap_number, "; + if ($allotted_time) { + echo "of lap $lap_number, "; + if ($time <= $allotted_time) $period = "regulation"; + else if ($time <= $allotted_time + $entry->getBonusTime()) $period = "bonus"; + else $period = "penalty"; + } echo "$station_name $type in $system_name at $arrival"; if ($station_id == $finish_line && $station_number > 1 && is_numeric($hull_end)) echo " with $hull_end% hull remaining"; else if ($station_id == $start_line && is_numeric($hull_start)) echo " with $hull_start% hull"; if ($distance) echo " covering ${distance}Ly of total ${total_distance}Ly"; + if ($allotted_time) echo " during $period"; echo "

"; + + if (array_key_exists($entry_id, $ranked_entries)) { + $json_events[] = array( + "ship" => $ship->getName(), + "event_type" => explode(" ", $verb)[0], + "time" => $time, + "timestamp" => $arrival, + "time_period" => $period, + "location" => $station->getName(), + "lap" => $lap_number, + "leg" => $station_order, + "leg_distance" => (float) $distance, + "total_distance" => (float) $total_distance + ); + } } } echo "\n"; + + echo "

JSON

\n"; + echo "
\n";
+    echo json_encode(array(
+      "racers" => $json_racers,
+      "events" => $json_events,
+    ));
+    echo "
\n"; } function module_timeline($action, $args) { -- 2.7.4