Add races.
authorCMDR furrycat <elite@furrycat.net>
Fri, 8 Jan 2016 17:29:25 +0000 (12:29 -0500)
committerCMDR furrycat <elite@furrycat.net>
Fri, 8 Jan 2016 17:29:25 +0000 (12:29 -0500)
lib/race.php
race.js

index 5ddd25a..9e1cef3 100644 (file)
     }
   }
 
+  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 "<table>\n";
+    foreach ($stations as $station) {
+      $system = $station->getSystem();
+      $station_id = $station->getId();
+      echo "<tr>\n";
+      echo "<th>" . $station->getName() . "</th>\n";
+      echo "<td>" . $station->getType() . "</td>\n";
+      echo "<td>" . $system->getName() . "</td>\n";
+      echo "<td><select class=\"station\" name=\"station$station_id\">";
+      option("station$station_id", 0, "");
+      option("station$station_id", Course::WaypointFlag(), "waypoint");
+      option("station$station_id", Course::WaypointFlag() | Course::StartFlag(), "start line");
+      option("station$station_id", Course::WaypointFlag() | Course::FinishFlag(), "finish line");
+      option("station$station_id", Course::WaypointFlag() | Course::StartFlag() | Course::FinishFlag(), "start/finish line");
+      echo "</select></td>\n";
+      echo "</tr>\n";
+    }
+    echo "</table>\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();
       $eq->joinWith('Ship.Hull');
       $eq->filterByRaceId($id);
       $entries = $eq->orderByTotalDistance('desc')->orderByPenaltyTime('desc')->orderByTotalTime('desc')->find();
-      if (! count($entries) && ! $action) continue;
+      if (! $action) {
+        echo "<p>" . $race->getLink() . "</p>\n";
+        continue;
+      }
+      if (! count($entries)) {
+        echo "<p>No entries.</p>\n";
+        continue;
+      }
 
       echo "<h3>$name</h3>\n";
       echo "<div class=\"classification\">\n";
       }
       echo "</div>\n";
     }
+    echo "<hr>\n";
+    $sq = new StationQuery;
+    $stations = $sq->orderByName()->find();
+    if (! count($stations)) {
+      echo "<p>Add stations before adding races.</p>\n";
+      return;
+    }
+    form();
+    echo "<p>Add a new race: ";
+    input("name", $_POST["name"]);
+    echo "allotted time (s): ";
+    input("allotted_time", $_POST["allotted_time"]);
+
+    submit("add_race", "Add");
+    echo "</p>\n";
+    end_form();
   }
 
 ?>
diff --git a/race.js b/race.js
index de31119..a6eaf4f 100644 (file)
--- 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();