<body>
<?php
echo "<p>Buckyball Racing Club";
- foreach (array("competitor", "hull", "ship", "system", "station", "race", "raikogram", "entry") as $what) {
+ foreach (array("competitor", "hull", "ship", "system", "station", "race", "raikogram", "entry", "timeline") as $what) {
$title = preg_replace('/y$/', 'ie', $what);
echo " | <a href=\"?module=$what\">" . ucfirst($title) . "s</a>";
}
--- /dev/null
+<h2>Race timelines</h2>
+<?php
+
+ function ordinal($n) {
+ if (($n % 100) >= 11 && ($n % 100) <= 13) return "${n}th";
+ else return $n . array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th')[$n % 10];
+ }
+
+ function show_timeline($race_id) {
+ $rq = new CourseQuery;
+ $rq->joinWith('Course.Race');
+ $course = $rq->filterByRaceId($race_id)->find();
+ $race = $start_line = $finish_line = null;
+ foreach ($course as $leg) {
+ if (! $race) $race = $leg->getRace();
+ $station_id = $leg->getStationId();
+ if ($leg->getFinishLine()) $finish_line = $station_id;
+ if ($leg->getStartLine()) $finish_line = $station_id;
+ }
+ $allotted_time = $race->getAllottedTime();
+ echo "<h3>" . $race->getName() . "</h3>\n";
+
+ $q = new LaptimeQuery;
+ $q->joinWith('Laptime.Station');
+ $q->joinWith('Station.System');
+ $q->joinWith('Laptime.Lap');
+ $q->joinWith('Lap.Entry');
+ $q->joinWith('Entry.Ship');
+ $q->joinWith('Ship.Competitor');
+ $q->where('Entry.RaceId=?', $race_id);
+ $legs = $q->orderByArrival()->orderBy('Lap.LapNumber')->orderByStationOrder()->orderBy('Competitor.CmdrName')->orderBy('Entry.Id')->find();
+
+ foreach ($legs as $leg) {
+ $arrival = elite_time($leg->getArrival());
+ $station_order = $leg->getStationOrder();
+ $nth = ordinal($station_order);
+ $station = $leg->getStation();
+ $station_id = $station->getId();
+ $station_name = $station->getName();
+ $type = strtolower($station->getType());
+ $system = $station->getSystem();
+ $system_name = $system->getName();
+ $lap = $leg->getLap();
+ $lap_number = $lap->getLapNumber();
+ $hull_start = $lap->getHullStart();
+ $hull_end = $lap->getHullEnd();
+ $entry = $lap->getEntry();
+ $entry_id = $entry->getId();
+ $ship = $entry->getShip();
+ $ship_name = $ship->getName();
+ $competitor = $ship->getCompetitor();
+ $cmdr_name = $competitor->getCmdrName();
+
+ $verb = ($station_order > 1) ? "arrived at" : "left";
+ echo "<p>";
+ echo "CMDR <strong>$cmdr_name</strong> flying <strong>$ship_name</strong> #$entry_id $verb $nth station ";
+ if ($allotted_time) echo "of lap $lap_number, ";
+ echo "<em>$station_name</em> $type in <strong>$system_name</strong> 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";
+ echo "</p>";
+ }
+ }
+
+ function module_timeline($action) {
+ if ($action) show_timeline($action);
+ $rq = new RaceQuery;
+ $rq->joinWith('Race.Entry');
+ $races = $rq->find();
+ foreach ($races as $race) {
+ if (! count($race->getEntries())) continue;
+ $id = $race->getId();
+ $name = $race->getName();
+ /* XXX */
+ $url = preg_replace('/&action=\d+/', '', $_SERVER['REQUEST_URI']) . "&action=$id";
+ echo "<p><a href=\"$url\">$name</a></p>\n";
+ }
+ }
+
+?>