Record system coordinates and set race Raikogram automatically.
authorCMDR furrycat <elite@furrycat.net>
Sun, 7 Feb 2016 14:09:13 +0000 (09:09 -0500)
committerCMDR furrycat <elite@furrycat.net>
Sun, 7 Feb 2016 14:09:13 +0000 (09:09 -0500)
lib/race.php
lib/system.php
propel/build/classes/buckyball/System.php [new file with mode: 0644]
propel/schema.xml

index 86811b9..ab6bc47 100644 (file)
     }
   }
 
+  function distance($coords1, $coords2) {
+    $x = $coords2[0] - $coords1[0];
+    $y = $coords2[1] - $coords1[1];
+    $z = $coords2[2] - $coords1[2];
+    return sqrt(($x * $x) + ($y * $y) + ($z * $z));
+  }
+
   function do_race($name, $allotted_time, $posted) {
     $race = new Race;
     $race->setName($name);
       $prizes |= $race->PrizeValue($p);
     }
     if ($race->hasRunnerUp()) $prizes |= $race->PrizeValue('winner');
-    $race->setPrizes($prizes);
+    if ($prizes) $race->setPrizes($prizes);
     $race->save();
 
+    $station_ids = array();
     foreach ($posted as $k => $v) {
       if (! preg_match('/^station(\d+)$/', $k, $m)) continue;
       $station_id = $m[1];
+      $station_ids[] = $station_id;
       $leg = new Course;
       $leg->setRaceId($race->getId());
       $leg->setStationId($station_id);
       $leg->save();
     }
 
+    $sq = new StationQuery;
+    $stations = $sq->findById($station_ids);
+    $system_ids = array();
+    foreach ($stations as $station) $system_ids[] = $station->getSystemId();
+    $sq = new SystemQuery;
+    $systems = $sq->findById($system_ids);
+    $coords = array();
+    foreach ($systems as $system) $coords[$system->getId()] = $system->getCoords();
+
+    /* Populate Raikogram. */
+    foreach ($system_ids as $system_id1) {
+      foreach ($system_ids as $system_id2) {
+        if ($system_id1 >= $system_id2) continue;
+        $system1 = min($system_id1, $system_id2);
+        $system2 = max($system_id1, $system_id2);
+        $rq = new RaikogramQuery;
+        $rq->filterBySystem1($system1);
+        $rq->filterBySystem2($system2);
+        if ($rq->findOne()) continue;
+        $raikogram = new Raikogram;
+        $raikogram->setSystem1($system1);
+        $raikogram->setSystem2($system2);
+        $distance = distance($coords[$system1], $coords[$system2]);
+        $raikogram->setDistance(format_distance($distance));
+        $raikogram->save();
+      }
+    }
+
     return true;
   }
 
index 6313084..4448470 100644 (file)
@@ -1,13 +1,18 @@
 <h2>Systems</h2>
 <?php
 
-  function add_system($system_name) {
+  function add_system($system_name, $x, $y, $z) {
     if (! $system_name) {
       echo "<p>Missing system name!</p>\n";
       return false;
     }
+    if (! is_numeric($x) || ! is_numeric($y) || ! is_numeric($z)) {
+      echo "<p>Missing system coordinates</p>\n";
+      return false;
+    }
     $system = new System;
     $system->setName($system_name);
+    $system->setCoords($x, $y, $z);
     try {
       $system->save();
       return true;
@@ -20,7 +25,7 @@
 
   function module_system($action, $args) {
     if ($_POST['add_system']) {
-      if (add_system($_POST["system_name"])) unset($_POST);
+      if (add_system($_POST["system_name"], $_POST['x'], $_POST['y'], $_POST['z'])) unset($_POST);
     }
 
     $q = new SystemQuery;
@@ -29,7 +34,8 @@
     echo "<div id=\"systems\">\n";
     foreach ($systems as $system) {
       $system_name = $system->getName();
-      echo "<p><strong>$system_name</strong></p>\n";
+      $coords = implode(", ", $system->getCoords());
+      echo "<p><strong>$system_name</strong> ($coords)</p>\n";
     }
     echo "</div>\n";
 
     form();
     echo "<p>Add a new system: ";
     input("system_name", $_POST['system_name']);
+    echo " X:";
+    input("x", $_POST['x']);
+    echo " Y:";
+    input("y", $_POST['y']);
+    echo " Z:";
+    input("z", $_POST['z']);
     submit("add_system", "Add");
     echo "</p>\n";
     end_form();
diff --git a/propel/build/classes/buckyball/System.php b/propel/build/classes/buckyball/System.php
new file mode 100644 (file)
index 0000000..b077fb6
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+
+
+/**
+ * Skeleton subclass for representing a row from the 'system' table.
+ *
+ *
+ *
+ * You should add additional methods to this class to meet the
+ * application requirements.  This class will only be generated as
+ * long as it does not already exist in the output directory.
+ *
+ * @package    propel.generator.buckyball
+ */
+class System extends BaseSystem
+{
+  function getCoords() {
+    return array($this->getX(), $this->getY(), $this->getZ());
+  }
+
+  function setCoords($x, $y, $z) {
+    return $this->setX($x) && $this->setY($y) && $this->setZ($z);
+  }
+}
index 0fd5757..2df0d04 100644 (file)
     </vendor>
     <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
     <column name="name" type="varchar" size="64" required="true"/>
+    <column name="x" type="float" required="true"/>
+    <column name="y" type="float" required="true"/>
+    <column name="z" type="float" required="true"/>
     <unique name="name">
       <unique-column name="name"/>
     </unique>
+    <unique name="coords">
+      <unique-column name="x"/>
+      <unique-column name="y"/>
+      <unique-column name="z"/>
+    </unique>
   </table>
 
   <!-- Spacestation -->