From: CMDR furrycat Date: Sun, 7 Feb 2016 14:09:13 +0000 (-0500) Subject: Record system coordinates and set race Raikogram automatically. X-Git-Url: http://git.furryclan.net/?a=commitdiff_plain;h=30dc2764ca3ec02fa9779989e3cfd91803484633;p=furrycat%2Frace-o-matic.git Record system coordinates and set race Raikogram automatically. --- diff --git a/lib/race.php b/lib/race.php index 86811b9..ab6bc47 100644 --- a/lib/race.php +++ b/lib/race.php @@ -284,6 +284,13 @@ } } + 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); @@ -295,12 +302,14 @@ $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); @@ -311,6 +320,34 @@ $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; } diff --git a/lib/system.php b/lib/system.php index 6313084..4448470 100644 --- a/lib/system.php +++ b/lib/system.php @@ -1,13 +1,18 @@

Systems

Missing system name!

\n"; return false; } + if (! is_numeric($x) || ! is_numeric($y) || ! is_numeric($z)) { + echo "

Missing system coordinates

\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 "
\n"; foreach ($systems as $system) { $system_name = $system->getName(); - echo "

$system_name

\n"; + $coords = implode(", ", $system->getCoords()); + echo "

$system_name ($coords)

\n"; } echo "
\n"; @@ -37,6 +43,12 @@ form(); echo "

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 "

\n"; end_form(); diff --git a/propel/build/classes/buckyball/System.php b/propel/build/classes/buckyball/System.php new file mode 100644 index 0000000..b077fb6 --- /dev/null +++ b/propel/build/classes/buckyball/System.php @@ -0,0 +1,25 @@ +getX(), $this->getY(), $this->getZ()); + } + + function setCoords($x, $y, $z) { + return $this->setX($x) && $this->setY($y) && $this->setZ($z); + } +} diff --git a/propel/schema.xml b/propel/schema.xml index 0fd5757..2df0d04 100644 --- a/propel/schema.xml +++ b/propel/schema.xml @@ -59,9 +59,17 @@ + + + + + + + +