From: CMDR furrycat Date: Sun, 19 Jun 2016 07:43:08 +0000 (-0400) Subject: Associate races with events in a given timeframe. X-Git-Url: http://git.furryclan.net/?a=commitdiff_plain;h=81bec737a798b3e24916eb87e78ab08b7935b943;p=furrycat%2Frace-o-matic.git Associate races with events in a given timeframe. --- diff --git a/entry.js b/entry.js index 48a7dc7..fe89209 100644 --- a/entry.js +++ b/entry.js @@ -589,6 +589,14 @@ function do_plot(table, canvas) { }); } +/* Check that entry is between the start and end times of the event. */ +function within_race_timeframe(time) { + if (! time) return false; + if (event_start_time != null && time < from_iso8601(event_start_time)) return false; + if (event_end_time != null && time < from_iso8601(event_end_time)) return false; + return true; +} + /* Check entry validity. */ function validate(table, cloned) { var valid = true; @@ -624,6 +632,10 @@ function validate(table, cloned) { input.setCustomValidity(""); no_stations_visited = false; var time = from_iso8601(real_date(text)); + if (! within_race_timeframe(time)) { + input.setCustomValidity('Outside race timeframe'); + valid = false; + } if (i == first_row(table)) first_leg_time = time; else last_leg_time = time; if (time <= last_time) { diff --git a/event.js b/event.js new file mode 100644 index 0000000..24848e9 --- /dev/null +++ b/event.js @@ -0,0 +1,16 @@ +/* Document onload. */ +function loaded() { + var today = elite_date().replace(/..:..:..$/, '00:00:00'); + for (var input of document.getElementsByTagName('input')) { + var m = input.name.match(/_time$/); + if (! m) continue; + input.autocomplete = 'off'; + input.type = 'text'; + input.pattern = '^(33[0-9][0-9]-(?:0[1-9]|1[0-2])-(?:[0-2][0-9]|3[01])) ((?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$'; + input.placeholder = today; + input.maxLength = input.size = 19; + input.id = input.name; + } +} + +document.onload = loaded(); diff --git a/lib/entry.php b/lib/entry.php index 881ffa4..5165597 100644 --- a/lib/entry.php +++ b/lib/entry.php @@ -118,10 +118,12 @@ $q = new CourseQuery; $q->joinWith("Course.Station"); $q->joinWith("Course.Race"); + $q->joinWith("Race.Event"); $q->joinWith("Station.System"); $q->findByRaceId($race_id); $course = $q->find(); $race = $course[0]->getRace(); + $event = $race->getEvent(); $allotted_time = 0 + $race->getAllottedTime(); $legs = array(); $system_ids = array(); @@ -159,6 +161,17 @@ echo "var distances = " . json_encode($distances) . ";\n"; echo "var allotted_time = $allotted_time;\n"; echo "var optional_waypoints = $waypoints;\n"; + $event_start_time = $event->getStartTime(); + if ($event_start_time) { + $event_start_time = real_time($event->getStartTime()); + $event_end_time = real_time($event->getEndTime()); + echo "var event_start_time = '$event_start_time';\n"; + echo "var event_end_time = '$event_end_time';\n"; + } + else { + echo "var event_start_time = null;\n"; + echo "var event_end_time = null;\n"; + } if ($entry) retrieve_entry($entry); if ($_POST['lap1station1']) { /* diff --git a/lib/event.php b/lib/event.php new file mode 100644 index 0000000..d8ba3f3 --- /dev/null +++ b/lib/event.php @@ -0,0 +1,82 @@ +

Events

+Missing event name!

\n"; + return false; + } + if ($start_time || $end_time) { + $real_start_time = real_time($start_time); + if (is_null($real_start_time)) { + echo "

Invalid event start time!

\n"; + return false; + } + $real_end_time = real_time($end_time); + if (is_null($real_end_time)) { + echo "

Invalid event end time!

\n"; + return false; + } + if ($real_end_time < $real_start_time) { + echo "

Event end time is earlier than start time!

\n"; + return false; + } + } + $event = new Event; + $event->setName($name); + if ($real_start_time) $event->setStartTime($real_start_time); + if ($real_end_time) $event->setEndTime($real_end_time); + try { + $event->save(); + return true; + } + catch (Exception $e) { + echo "

Error adding event: " . $e->getMessage() . "

\n"; + } + return false; + } + + function show_event($event) { + echo "

" . $event->getName() . "

\n"; + + $rq = new RaceQuery; + $races = $rq->findByEventId($event->getId()); + if (! count($races)) { + echo "

No races.

\n"; + return; + } + + foreach ($races as $race) { + echo "

" . $race->getTitleLink() . "

\n"; + } + } + + function module_event($action) { + if ($_POST['add_event']) { + if (add_event($_POST["name"], $_POST["start_time"], $_POST["end_time"])) unset($_POST); + } + + $q = new EventQuery; + if ($action) $q->filterById($action); + $events = $q->find(); + if (! count($events)) echo "

No events

\n"; + echo "
\n"; + foreach ($events as $event) { + if ($action) show_event($event); + else echo "

" . $event->getLink() . "

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

Add a new event: "; + input("name", $_POST['name']); + echo " from "; + input("start_time", $_POST['start_time']); + echo " to "; + input("end_time", $_POST['start_time']); + submit("add_event", "Add"); + echo "

\n"; + end_form(); + } +?> diff --git a/lib/header.php b/lib/header.php index b31c7a7..3d0d0b2 100644 --- a/lib/header.php +++ b/lib/header.php @@ -11,7 +11,7 @@ \n"; $links = array(); - foreach (array("competitor", "manufacturer", "hull", "ship", "system", "station", "race", "raikogram", "entry") as $what) { + foreach (array("competitor", "manufacturer", "hull", "ship", "system", "station", "event", "race", "raikogram", "entry") as $what) { $title = preg_replace('/y$/', 'ie', $what); $links[] = "" . ucfirst($title) . "s"; } diff --git a/lib/race.php b/lib/race.php index e8d5166..c17a3fe 100644 --- a/lib/race.php +++ b/lib/race.php @@ -437,9 +437,10 @@ return sqrt(($x * $x) + ($y * $y) + ($z * $z)); } - function do_race($name, $allotted_time, $posted) { + function do_race($name, $event_id, $allotted_time, $posted) { $race = new Race; $race->setName($name); + $race->setEventId($event_id); if ($allotted_time) $race->setAllottedTime($allotted_time); $optional = $posted['optional']; if (is_int($optional) && $optional > 0) $race->setWaypoints($optional); @@ -499,7 +500,7 @@ return true; } - function add_race($name, $allotted_time) { + function add_race($name, $event_id, $allotted_time) { $sq = new StationQuery; $sq->joinWith('Station.System'); $stations = $sq->orderBySystemId('desc')->orderByName()->find(); @@ -511,7 +512,11 @@ $posted[$k] = $v; } if (count($posted)) { - if (do_race($name, $allotted_time, $posted)) return true; + if (! $event_id) { + echo "

Race not associated with an event.

\n"; + return false; + } + if (do_race($name, $event_id, $allotted_time, $posted)) return true; } form(); @@ -818,8 +823,8 @@ function module_race($action, $args) { global $race_colours; - if ($_POST['add_race']) { - if (add_race($_POST["name"], $_POST["allotted_time"])) unset($_POST); + if (array_key_exists("add_race", $_POST)) { + if (add_race($_POST["name"], $_POST["event_id"], $_POST["allotted_time"])) unset($_POST); } $methods = array( @@ -959,7 +964,7 @@ $white_fields[] = 'Raced'; if ($allotted_time) $white_fields[] = 'Total'; if ($race->getPrizes()) $orange_fields[] = 'Award'; - entry_header($sort, $format, $type, $blue_fields, $white_fields, $orange_fields, $race->getName(), $race->getColours(true)); + entry_header($sort, $format, $type, $blue_fields, $white_fields, $orange_fields, $race->getTitle(), $race->getColours(true)); $n = $position = $rank = 1; $shown = 0; $ship_entries = array(); @@ -1308,8 +1313,22 @@ echo "

Add stations before adding races.

\n"; return; } + $eq = new EventQuery; + $events = $eq->orderByName()->find(); + if (! count($events)) { + echo "

Add events before adding races.

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

Add a new race: "; + echo "Event: \n"; input("name", $_POST["name"]); echo "allotted time (s): "; input("allotted_time", $_POST["allotted_time"]); diff --git a/propel/build/classes/buckyball/Event.php b/propel/build/classes/buckyball/Event.php new file mode 100644 index 0000000..0031c37 --- /dev/null +++ b/propel/build/classes/buckyball/Event.php @@ -0,0 +1,43 @@ +getColour1(); + $colour2 = $this->getColour2(); + if ($strict && (! $colour1 || ! $colour2)) return null; + if (! $colour1) $colour1 = self::Colours()[0]; + if (! $colour2) $colour2 = self::Colours()[1]; + return array($colour1, $colour2); + } + + function getFlag() { + $colours = $this->getColours(true); + if (! $colours) return ''; + $style = "background-image: linear-gradient(-45deg, $colours[0] 0%, $colours[0] 50%, $colours[1] 50%, $colours[1] 100%);"; + return "     "; + } + + function getLink() { + return parent::getLink() . $this->getFlag(); + } +} diff --git a/propel/build/classes/buckyball/Race.php b/propel/build/classes/buckyball/Race.php index ac1da2c..7b03044 100644 --- a/propel/build/classes/buckyball/Race.php +++ b/propel/build/classes/buckyball/Race.php @@ -24,12 +24,6 @@ class Race extends BaseRace 'average_lap' => array('fastest average lap', 'ByAverageLap', 1 << 5) ); - private static $COLOURS = array('#808080', '#000000'); - - function Colours() { - return self::$COLOURS; - } - function Prizes() { return self::$PRIZES; } @@ -91,23 +85,31 @@ class Race extends BaseRace } function getColours($strict = false) { - $colour1 = $this->getColour1(); - $colour2 = $this->getColour2(); - if ($strict && (! $colour1 || ! $colour2)) return null; - if (! $colour1) $colour1 = self::Colours()[0]; - if (! $colour2) $colour2 = self::Colours()[1]; - return array($colour1, $colour2); + return $this->getEvent()->getColours($strict); } function getFlag() { - $colours = $this->getColours(true); - if (! $colours) return ''; - $style = "background-image: linear-gradient(-45deg, $colours[0] 0%, $colours[0] 50%, $colours[1] 50%, $colours[1] 100%);"; - return "     "; + return $this->getEvent()->getFlag(); + } + + function getName() { + $name = $this->getEvent()->getName(); + $class = $this->getClassName(); + if ($class) $name .= " $class"; + return $name; + } + + function getTitle() { + $class = $this->getClassName(); + if ($class) return $class; + return $this->getEvent()->getName(); } function getLink() { return parent::getLink() . $this->getFlag(); } + function getTitleLink() { + return parent::getLink($this->getTitle()) . $this->getFlag(); + } } diff --git a/propel/schema.xml b/propel/schema.xml index 97fc9dd..b67d69b 100644 --- a/propel/schema.xml +++ b/propel/schema.xml @@ -111,6 +111,23 @@ + + + + + + + + + + + + + + + +
+ @@ -118,15 +135,18 @@ - + + - - - + + + + +