From ded52bd6a7060aee252fc436cf0703ef53e5f625 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 8 Jan 2016 12:29:25 -0500 Subject: [PATCH] Add races. --- lib/race.php | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- race.js | 33 ++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/lib/race.php b/lib/race.php index 5ddd25a..9e1cef3 100644 --- a/lib/race.php +++ b/lib/race.php @@ -132,7 +132,73 @@ } } + function do_race($name, $allotted_time, $posted) { + $race = new Race; + $race->setName($name); + if ($allotted_time) $race->setAllottedTime($allotted_time); + $race->save(); + + foreach ($posted as $k => $v) { + if (! preg_match('/^station(\d+)$/', $k, $m)) continue; + $station_id = $m[1]; + $leg = new Course; + $leg->setRaceId($race->getId()); + $leg->setStationId($station_id); + if ($v & Course::StartFlag()) $leg->setStartLine(true); + if ($v & Course::FinishFlag()) $leg->setFinishLine(true); + $leg->save(); + } + + return true; + } + + function add_race($name, $allotted_time) { + $sq = new StationQuery; + $sq->joinWith('Station.System'); + $stations = $sq->orderBy('System.Name')->orderByName()->find(); + + $posted = array(); + foreach ($_POST as $k => $v) { + if (! preg_match('/^station\d+/', $k, $m)) continue; + if (! $v) continue; + $posted[$k] = $v; + } + if (count($posted)) { + if (do_race($name, $allotted_time, $posted)) return true; + } + + form(); + echo "\n"; + foreach ($stations as $station) { + $system = $station->getSystem(); + $station_id = $station->getId(); + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + } + echo "
" . $station->getName() . "" . $station->getType() . "" . $system->getName() . "
\n"; + hidden('name', $name); + hidden('allotted_time', $allotted_time); + submit('add_race', 'Submit race'); + end_form(); + + return false; + } + function module_race($action) { + if ($_POST['add_race']) { + if (add_race($_POST["name"], $_POST["allotted_time"])) unset($_POST); + } + $rq = new RaceQuery; if ($action) $rq->filterById($action); $races = $rq->find(); @@ -146,7 +212,14 @@ $eq->joinWith('Ship.Hull'); $eq->filterByRaceId($id); $entries = $eq->orderByTotalDistance('desc')->orderByPenaltyTime('desc')->orderByTotalTime('desc')->find(); - if (! count($entries) && ! $action) continue; + if (! $action) { + echo "

" . $race->getLink() . "

\n"; + continue; + } + if (! count($entries)) { + echo "

No entries.

\n"; + continue; + } echo "

$name

\n"; echo "
\n"; @@ -163,6 +236,22 @@ } echo "
\n"; } + echo "
\n"; + $sq = new StationQuery; + $stations = $sq->orderByName()->find(); + if (! count($stations)) { + echo "

Add stations before adding races.

\n"; + return; + } + form(); + echo "

Add a new race: "; + input("name", $_POST["name"]); + echo "allotted time (s): "; + input("allotted_time", $_POST["allotted_time"]); + + submit("add_race", "Add"); + echo "

\n"; + end_form(); } ?> diff --git a/race.js b/race.js index de31119..a6eaf4f 100644 --- a/race.js +++ b/race.js @@ -1,3 +1,7 @@ +var start_flag = 1 << 1; +var finish_flag = 1 << 2; +var mandatory_flags = start_flag | finish_flag; + /* Copy table to clipboard. */ function copy_classification(event) { try { @@ -36,11 +40,40 @@ function toggle_classification(event) { } } +/* Check start and finish lines are set. */ +function validate() { + var flags = 0; + for (input of document.getElementsByClassName('station')) { + flags |= input.value & mandatory_flags; + } + + var submits = document.getElementsByName('add_race'); + if (submits.length) submits[0].disabled = ! (flags == mandatory_flags); +} + +/* Action of a waypoint was changed. */ +function changed_waypoint(event) { + for (input of document.getElementsByClassName('station')) { + if (input == event.target) continue; + input.value &= ~(event.target.value & mandatory_flags); + } + + validate(); +} + /* Document onload. */ function loaded() { for (classification of document.getElementsByClassName('classification')) { classification.addEventListener('dblclick', copy_classification, true); } + var waypoints = document.getElementsByClassName('station'); + if (waypoints.length) { + for (input of waypoints) { + input.addEventListener('change', changed_waypoint, true); + input.addEventListener('focusout', changed_waypoint, true); + } + validate(); + } } document.onload = loaded(); -- 2.7.4