From 80eee3844eddda0f4b20810a20651916c0631a82 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Wed, 3 Feb 2016 06:09:55 -0500 Subject: [PATCH] Set which prizes are awarded on a per-race basis. --- lib/race.php | 155 ++++++++++++++------------------ propel/build/classes/buckyball/Race.php | 87 ++++++++++++++++++ propel/schema.xml | 1 + style.css | 2 +- 4 files changed, 154 insertions(+), 91 deletions(-) create mode 100644 propel/build/classes/buckyball/Race.php diff --git a/lib/race.php b/lib/race.php index 28a4096..953ccbd 100644 --- a/lib/race.php +++ b/lib/race.php @@ -10,59 +10,49 @@ 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 "\n"; @@ -126,15 +116,11 @@ } } - 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" @@ -253,16 +239,13 @@ $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'; @@ -273,8 +256,8 @@ echo "\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); @@ -287,8 +270,8 @@ 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]"; @@ -304,6 +287,14 @@ $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) { @@ -337,8 +328,20 @@ if (do_race($name, $allotted_time, $posted)) return true; } - echo "
\n"; form(); + echo "
\n"; + echo "

Prizes

\n"; + echo "

"; + foreach (Race::PrizeKeys() as $p) { + $description = ucfirst(Race::PrizeDescription($p)); + $name = "prize_$p"; + echo "\n"; + } + echo "

\n"; + + echo "

Waypoints

\n"; echo "
\n"; foreach ($stations as $station) { $system = $station->getSystem(); @@ -369,58 +372,25 @@ 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; } @@ -463,7 +433,7 @@ 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(), @@ -632,6 +602,11 @@ } echo "

$name

\n"; + if ($race->getPrizes()) { + $prizes = array(); + foreach ($race->getPrizeKeys() as $p) $prizes[] = $race->PrizeDescription($p); + echo "

Prizes for " . implode(", ", $prizes) . ".

\n"; + } echo "
\n"; echo "
\n"; echo "
\n"; @@ -681,7 +656,7 @@ } show_entry($entry, $allotted_time, $p, $r, $s, $n++, $type, $prizewinners); } - entry_footer($type); + entry_footer($race, $type); echo "\n"; } echo "\n"; diff --git a/propel/build/classes/buckyball/Race.php b/propel/build/classes/buckyball/Race.php new file mode 100644 index 0000000..80ffa05 --- /dev/null +++ b/propel/build/classes/buckyball/Race.php @@ -0,0 +1,87 @@ + 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; + } + +} diff --git a/propel/schema.xml b/propel/schema.xml index c325060..0fd5757 100644 --- a/propel/schema.xml +++ b/propel/schema.xml @@ -93,6 +93,7 @@ + diff --git a/style.css b/style.css index 7f566dd..05b57a0 100644 --- 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; } -- 2.7.4