Allow specifying number of optional waypoints.
authorCMDR furrycat <elite@furrycat.net>
Mon, 6 Jun 2016 10:53:02 +0000 (06:53 -0400)
committerCMDR furrycat <elite@furrycat.net>
Mon, 6 Jun 2016 10:53:02 +0000 (06:53 -0400)
Allow races where competitors must visit any n of m stations.  When creating
the race set the number of optional waypoints to n and ensure that m > n
stations are marked as waypoints.

lib/race.php
propel/schema.xml
race.js

index 15ab62f..0ab11ff 100644 (file)
     $race = new Race;
     $race->setName($name);
     if ($allotted_time) $race->setAllottedTime($allotted_time);
+    $optional = $posted['optional'];
+    if (is_int($optional) && $optional > 0) $race->setWaypoints($optional);
     $prizes = 0;
     foreach ($posted as $k => $v) {
       if (! preg_match('/^prize_(.+)$/', $k, $m)) continue;
     echo "</p>\n";
 
     echo "<h3>Waypoints</h3>\n";
+    echo "<p>Number of optional waypoints: ";
+    input("optional");
+    echo "</p>\n";
+
     echo "<table>\n";
     foreach ($stations as $station) {
       $system = $station->getSystem();
index 0377b0e..b992118 100644 (file)
     <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
     <column name="name" type="varchar" size="64" required="true"/>
     <column name="allotted_time" type="integer" required="false"/>
+    <column name="waypoints" type="integer" required="false"/>
     <column name="prizes" type="integer" required="false"/>
     <column name="colour1" type="char" size="7" required="false"/>
     <column name="colour2" type="char" size="7" required="false"/>
diff --git a/race.js b/race.js
index 786d941..018f1e6 100644 (file)
--- a/race.js
+++ b/race.js
@@ -112,6 +112,22 @@ function validate() {
         break;
       }
     }
+
+    if (valid) {
+      /* Can't have optional more stations than potential ones. */
+      for (var input of  document.getElementsByTagName('input')) {
+        if (input.name != 'optional') continue;
+        if (input.value) {
+          var optional = parseInt(input.value);
+          if (optional > stations.length) {
+            input.setCustomValidity("Too many stations marked optional");
+            valid = false;
+          }
+        }
+        if (valid) input.setCustomValidity("");
+        break;
+      }
+    }
   }
 
   var submits = document.getElementsByName('add_race');