Set which prizes are awarded on a per-race basis.
authorCMDR furrycat <elite@furrycat.net>
Wed, 3 Feb 2016 11:09:55 +0000 (06:09 -0500)
committerCMDR furrycat <elite@furrycat.net>
Wed, 3 Feb 2016 11:09:55 +0000 (06:09 -0500)
lib/race.php
propel/build/classes/buckyball/Race.php [new file with mode: 0644]
propel/schema.xml
style.css

index 28a4096..953ccbd 100644 (file)
       case 'row1': return array('#d9d9d9', '#d4d4d4');
       case 'row2': return array('#ebebeb', '#e6e6e6');
       case 'winner': return array('#00ad19', '#009416'); // Green 129,100,80 129,100,68
-      case 'runnerup': return array('#66cc75', '#57ad63'); // Green 128,50,80 128,50,68
-      case 'BySpeed': return array('#cc4300', '#ad3900'); // Orange 20,100,80 20,100,68
-      case 'ByAverageSpeed': return array('#cb5e29', '#ad5023'); // Orange 20,80,80 20,80,68
-      case 'ByFastestLap': return array('#3d8bcc', '#3476ad'); // Blue 207,70,80 207,70,68
-      case 'ByAverageLap': return array('#669dcc', '#5786ad'); // Blue 207,50,80 207,50,68
+      case 'runner_up': return array('#66cc75', '#57ad63'); // Green 128,50,80 128,50,68
+      case 'best_speed': return array('#cc4300', '#ad3900'); // Orange 20,100,80 20,100,68
+      case 'average_speed': return array('#cb5e29', '#ad5023'); // Orange 20,80,80 20,80,68
+      case 'fastest_lap': return array('#3d8bcc', '#3476ad'); // Blue 207,70,80 207,70,68
+      case 'average_lap': return array('#669dcc', '#5786ad'); // Blue 207,50,80 207,50,68
     }
     return null;
   }
 
-  function entry_bgcolor($row, $column, $prize_entry = null) {
+  function entry_bgcolor($row, $column, $prize_name = null) {
     $blue = colour('blue');
     $white = colour('white');
     $orange = colour('orange');
     $colours = array(
       /* Header. */
-      array_merge($blue, $blue, $blue, $blue, $blue, $white, $white, $white, $orange, $orange),
+      array_merge($blue, $blue, $blue, $blue, $blue, $white, $white, $white, $orange, $orange, $orange),
       /* Rows. */
       colour('row1'),
       colour('row2'),
-      /* Winners. */
-      colour('winner'),
-      colour('runnerup'),
-      colour('BySpeed'),
-      colour('ByAverageSpeed'),
-      colour('ByFastestLap'),
-      colour('ByAverageLap')
     );
-    if (! is_null($prize_entry)) $row_index = $prize_entry + 3;
+    $row_index = $row % 2;
+    if (! is_null($prize_name)) $colours = array(colour($prize_name), colour($prize_name));
     else if (! $row) $row_index = 0;
-    else $row_index = 1 + ($row % 2);
+    else $row_index++;
     $column_index = $column % count($colours[$row_index]);
     return $colours[$row_index][$column_index];
   }
 
-  function entry_fgcolor($row, $column, $prize_entry = null) {
+  function entry_fgcolor($row, $column, $prize_name = null) {
     $white = array('#ffffff');
     $blue = array_splice(colour('blue'), 1);
     $orange = array_splice(colour('orange'), 0, 1);
     $colours = array(
       /* Header. */
-      array_merge($white, $white, $white, $white, $white, $white, $white, $white, $white, $white, $blue, $blue, $blue,$blue, $blue,  $blue, $white, $white, $white),
+      array_merge($white, $white, $white, $white, $white, $white, $white, $white, $white, $white, $blue, $blue, $blue,$blue, $blue,  $blue, $white, $white, $white, $white),
       /* Rows. */
       $blue,
       $orange,
       /* Winners. */
       $white,
       array('#ebebeb'),
-      $white,
-      array('#ebebeb'),
-      $white,
-      array('#ebebeb'),
     );
-    if (! is_null($prize_entry)) $row_index = $prize_entry + 3;
+    if (! is_null($prize_name)) $row_index = 3 + ($row % 2);
     else if (! $row) $row_index = 0;
     else $row_index = 1 + ($row % 2);
     $column_index = $column % count($colours[$row_index]);
@@ -90,7 +80,7 @@
   }
 
   function entry_header($type) {
-    $fields = array('P', 'R', 'S', 'E', 'CMDR name', 'Forum name', 'Ship name', 'Ship type', 'Distance', 'Penalty', 'Lap distance', 'Laps', 'Allotted', 'Bonus', 'Raced', 'Total', 'Best speed', 'Average speed', 'Fastest lap', 'Average lap');
+    $fields = array('P', 'R', 'S', 'E', 'CMDR name', 'Forum name', 'Ship name', 'Ship type', 'Distance', 'Penalty', 'Lap distance', 'Laps', 'Allotted', 'Bonus', 'Raced', 'Total', 'Best speed', 'Average speed', 'Fastest lap', 'Average lap', 'Award');
     $small_fields = array('Distance', 'Penalty', 'Lap distance', 'Fastest lap', 'Average lap', 'Average speed');
     if ($type == 'html') {
       echo "<table class=\"entries\">\n";
     }
   }
 
-  function entry_footer($type) {
-    $keys = array(
-      'winner' => 'Race winner',
-      'runnerup' => 'Runner-up',
-      'BySpeed' => 'Best single lap speed',
-      'ByAverageSpeed' => 'Best average speed',
-      'ByFastestLap' => 'Fastest single lap',
-      'ByAverageLap' => 'Fastest average lap',
-    );
+  function entry_footer($race, $type) {
+    $keys = array();
+    foreach ($race->getPrizeKeys() as $p) {
+      $keys[$p] = ucfirst($race->PrizeDescription($p));
+    }
     $rows = array(
       'orange' => "Competitor's best non-prizewinning entry",
       'blue' => "Competitor's other entries"
     $fields[] = format_speed($entry->getAverageSpeed());
     $fields[] = format_time($entry->getFastestLap());
     $fields[] = format_time($entry->getAverageLap());
-
-    $prize_entry = null;
-    $n = 0;
-    foreach ($prizewinners as $ignored => $id) {
-      if ($entry->getId() == $id) {
-        $prize_entry = $n;
-        break;
-      }
-      $n++;
+    $prize_name = null;
+    foreach ($prizewinners as $name => $id) {
+      if ($entry->getId() != $id) continue;
+      $prize_name = $name;
+      break;
     }
+    $fields[] = (is_null($prize_name)) ? '' : ucfirst(Race::PrizeDescription($prize_name));
 
     $formatting = array();
     if ($entry->usedDockingComputer()) $formatting[] = 'i';
       echo "<tr>\n";
       $column = 0;
       foreach ($fields as $field) {
-        $bgcolor = entry_bgcolor($row, $column, $prize_entry);
-        $fgcolor = entry_fgcolor($foreground, $column, $prize_entry);
+        $bgcolor = entry_bgcolor($row, $column, $prize_name);
+        $fgcolor = entry_fgcolor($foreground, $column, $prize_name);
         if (is_array($field)) $display = $field[1];
         else $display = $field;
         $text = entry_formatting($display, $formatting, $type);
       echo "[tr]\n";
       $column = 0;
       foreach ($fields as $field) {
-        $bgcolor = entry_bgcolor($row, $column, $prize_entry);
-        $fgcolor = entry_fgcolor($foreground, $column, $prize_entry);
+        $bgcolor = entry_bgcolor($row, $column, $prize_name);
+        $fgcolor = entry_fgcolor($foreground, $column, $prize_name);
         $display = (is_array($field)) ? $field[0] : $field;
         $text = entry_formatting($display, $formatting, $type);
         echo "[td=\"bgcolor: $bgcolor\"][color=$fgcolor]";
     $race = new Race;
     $race->setName($name);
     if ($allotted_time) $race->setAllottedTime($allotted_time);
+    $prizes = 0;
+    foreach ($posted as $k => $v) {
+      if (! preg_match('/^prize_(.+)$/', $k, $m)) continue;
+      $p = $m[1];
+      $prizes |= $race->PrizeValue($p);
+    }
+    if ($race->hasRunnerUp()) $prizes |= $race->PrizeValue('winner');
+    $race->setPrizes($prizes);
     $race->save();
 
     foreach ($posted as $k => $v) {
       if (do_race($name, $allotted_time, $posted)) return true;
     }
 
-    echo "<div id=\"waypoints\">\n";
     form();
+    echo "<div id=\"waypoints\">\n";
+    echo "<h3>Prizes</h3>\n";
+    echo "<p>";
+    foreach (Race::PrizeKeys() as $p) {
+      $description = ucfirst(Race::PrizeDescription($p));
+      $name = "prize_$p";
+      echo "<label><input type=\"checkbox\" name=\"$name\"";
+      if ($_POST[$name]) echo " checked=\"checked\"";
+      echo "> $description</label>\n";
+    }
+    echo "</p>\n";
+
+    echo "<h3>Waypoints</h3>\n";
     echo "<table>\n";
     foreach ($stations as $station) {
       $system = $station->getSystem();
     return false;
   }
 
-  function find_prizewinners($entries, $allotted_time) {
-    $sorted = array(
-      'ByBestSpeed' => null,
-      'ByAverageSpeed' => null,
-      'ByFastestLap' => null,
-      'ByAverageLap' => null
-    );
-    $ret = array_merge(
-      array('leader' => null, 'runnerup' => null),
-      $sorted
-    );
-
+  function find_prizewinners($race, $entries) {
+    $ret = array();
     $competitors = array();
-
-    uasort($entries, "Entry::StandardSort");
-
-    /* Leader and runner-up. */
-    foreach ($entries as $entry) {
-      if ($entry->isExcluded()) continue;
-      $entry_id = $entry->getId();
-      if (in_array($entry_id, array_values($ret))) continue;
-      $competitor_id = $entry->getShip()->getCompetitorId();
-      if (in_array($competitor_id, $competitors)) continue;
-      if (is_null($ret['leader'])) {
-        $ret['leader'] = $entry_id;
-        $competitors[] = $competitor_id;
-      }
-      else if (is_null($ret['runnerup'])) {
-        $ret['runnerup'] = $entry_id;
-        $competitors[] = $competitor_id;
-        break;
-      }
-    }
-
-    /* Standard races only have standard classification. */
-    if (! $allotted_time) return $ret;
-
-    /* Other prizes. */
-    foreach (array_keys($sorted) as $key) {
-      uasort($entries, "Entry::$key");
+    $last_sort_method = null;
+    foreach ($race->getPrizeKeys() as $p) {
+      $sort_method = $race->PrizeSortMethod($p);
+      if ($sort_method != $last_sort_method) uasort($entries, "Entry::$sort_method");
+      $last_sort_method = $sort_method;
       foreach ($entries as $entry) {
         if ($entry->isExcluded()) continue;
         $entry_id = $entry->getId();
         if (in_array($entry_id, array_values($ret))) continue;
         $competitor_id = $entry->getShip()->getCompetitorId();
         if (in_array($competitor_id, $competitors)) continue;
-        $ret[$key] = $entry_id;
+        $ret[$p] = $entry_id;
         $competitors[] = $competitor_id;
         break;
       }
     }
-
     return $ret;
   }
 
       if (count($entry_objects)) {
         $entries = array();
         foreach ($entry_objects as $entry) $entries[] = $entry;
-        $prizewinners = find_prizewinners($entries, $allotted_time);
+        $prizewinners = find_prizewinners($race, $entries);
         $plots = array(
           "cmdr" => array(
             "values" => array(),
       }
 
       echo "<h3>$name</h3>\n";
+      if ($race->getPrizes()) {
+        $prizes = array();
+        foreach ($race->getPrizeKeys() as $p) $prizes[] = $race->PrizeDescription($p);
+        echo "<p>Prizes for " . implode(", ", $prizes) . ".</p>\n";
+      }
       echo "<div id=\"plot_distribution\"></div>\n";
       echo "<div id=\"plot_legs\"></div>\n";
       echo "<div id=\"plot_speeds\"></div>\n";
           }
           show_entry($entry, $allotted_time, $p, $r, $s, $n++, $type, $prizewinners);
         }
-        entry_footer($type);
+        entry_footer($race, $type);
         echo "</div>\n";
       }
       echo "</div>\n";
diff --git a/propel/build/classes/buckyball/Race.php b/propel/build/classes/buckyball/Race.php
new file mode 100644 (file)
index 0000000..80ffa05
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+
+
+
+/**
+ * Skeleton subclass for representing a row from the 'race' 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 Race extends BaseRace
+{
+  private static $PRIZES = array(
+    'winner' => array('race winner', 'StandardSort', 1 << 0),
+    'runner_up' => array('runner-up', 'StandardSort', 1 << 1),
+    'best_speed' => array('best single lap speed', 'ByBestSpeed', 1 << 2),
+    'average_speed' => array('best average speed', 'ByAverageSpeed', 1 << 3),
+    'fastest_lap' => array('fastest single lap', 'ByFastestLap', 1 << 4),
+    'average_lap' => array('fastest average lap', 'ByAverageLap', 1 << 5)
+  );
+
+  function Prizes() {
+    return self::$PRIZES;
+  }
+
+  function PrizeKeys() {
+    return array_keys(self::Prizes());
+  }
+
+  function Prize($p) {
+    return self::Prizes()[$p];
+  }
+
+  function PrizeDescription($p) {
+    return self::Prize($p)[0];
+  }
+
+  function PrizeSortMethod($p) {
+    return self::Prize($p)[1];
+  }
+
+  function PrizeValue($p) {
+    return self::Prize($p)[2];
+  }
+
+  function hasPrize($p) {
+    return $this->getPrizes() & $this->PrizeValue($p);
+  }
+
+  function hasWinner() {
+    return $this->hasPrize('winner');
+  }
+
+  function hasRunnerUp() {
+    return $this->hasPrize('runner_up');
+  }
+
+  function hasBestSpeed() {
+    return $this->hasPrize('best_speed');
+  }
+
+  function hasAverageSpeed() {
+    return $this->hasPrize('average_speed');
+  }
+
+  function hasFastestLap() {
+    return $this->hasPrize('fastest_lap');
+  }
+
+  function hasAverageLap() {
+    return $this->hasPrize('average_lap');
+  }
+
+  function getPrizeKeys() {
+    $ret = array();
+    foreach ($this->PrizeKeys() as $p) {
+      if ($this->hasPrize($p)) $ret[] = $p;
+    }
+    return $ret;
+  }
+
+}
index c325060..0fd5757 100644 (file)
@@ -93,6 +93,7 @@
     <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="prizes" type="integer" required="false"/>
     <unique name="name">
       <unique-column name="name"/>
     </unique>
index 7f566dd..05b57a0 100644 (file)
--- a/style.css
+++ b/style.css
@@ -7,7 +7,7 @@ body { font-family: 'Sintony'; font-weight: 400; }
 #laps { overflow-x: auto; }
 #laps table { background-color: rgba(20, 50, 100, 0.2); }
 #laps th, #laps td { padding-left: 1em; }
-#laps label:nth-child(even) { padding-top: 0.5em; padding-bottom: 0.5em; background-color: rgba(128, 128, 128, 0.1); }
+label:nth-child(even) { padding-top: 0.5em; padding-bottom: 0.5em; background-color: rgba(128, 128, 128, 0.1); }
 #laps p[class*='order']:after { background-image: url(images/locked.gif); background-position: left center; position: relative; padding-left: 16px; background-repeat: no-repeat; content: ""; }
 #laps input:disabled { background-color: transparent; color: black; }
 th { text-align: left; }