From c4c3b76aeda422238ca2b260b325382a3865877c Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Mon, 18 Jan 2016 05:26:09 -0500 Subject: [PATCH] Classification. --- lib/race.php | 172 +++++++++++++++++++++++++++---- propel/build/classes/buckyball/Entry.php | 24 +++++ 2 files changed, 175 insertions(+), 21 deletions(-) diff --git a/lib/race.php b/lib/race.php index 9e1cef3..4509942 100644 --- a/lib/race.php +++ b/lib/race.php @@ -1,38 +1,78 @@

Races

'); + else if ($type == 'bbcode') $tags = array('[', ']'); + return sprintf("%s%s%s", open_tags($formatting, $tags), $text, close_tags($formatting, $tags)); + } + function entry_header($type) { - $fields = array('P', 'E', 'CMDR name', 'Forum name', 'Ship type', 'Ship name', 'Distance', 'Penalty', 'Lap distance', 'Laps', 'Allotted', 'Bonus', 'Raced', 'Total', 'Fastest lap', 'Average lap', 'Best speed', 'Average speed'); + $fields = array('P', 'R', 'S', 'E', 'CMDR name', 'Forum name', 'Ship name', 'Ship type', 'Distance', 'Penalty', 'Lap distance', 'Laps', 'Allotted', 'Bonus', 'Raced', 'Total', 'Fastest lap', 'Average lap', 'Best speed', 'Average speed'); $small_fields = array('Distance', 'Penalty', 'Allotted', 'Lap distance', 'Fastest lap', 'Average lap', 'Average speed'); if ($type == 'html') { echo "\n"; @@ -78,10 +118,12 @@ } } - function show_entry($entry, $allotted_time, $position, $row, $type) { + function show_entry($entry, $allotted_time, $position, $rank, $ship_rank, $row, $type, $prizewinners) { $fields = array(); $endorsements = $entry->getEndorsements(); $fields[] = $position; + $fields[] = $rank; + $fields[] = $ship_rank; $fields[] = endorsement_string($endorsements); $ship = $entry->getShip(); $competitor = $ship->getCompetitor(); @@ -91,8 +133,8 @@ if (! $forum_name) $forum_name = $cmdr_name; $fields[] = $forum_name; $hull = $ship->getHull(); - $fields[] = $hull->getName(); $fields[] = $ship->getName(); + $fields[] = $hull->getName(); $fields[] = format_distance($entry->getTotalDistance()); $fields[] = format_time($entry->getPenaltyTime()); $fields[] = format_distance($entry->getLapDistance()); @@ -106,13 +148,28 @@ $fields[] = format_speed($entry->getBestSpeed()); $fields[] = format_speed($entry->getAverageSpeed()); + $prize_entry = null; + $n = 0; + foreach ($prizewinners as $ignored => $id) { + if ($entry->getId() == $id) { + $prize_entry = $n; + break; + } + $n++; + } + + $formatting = array(); + if (endorsement('d', $entry->getEndorsements())) $formatting[] = 'i'; + if (endorsement('j', $entry->getEndorsements())) $formatting[] = 'b'; + if ($type == 'html') { echo "\n"; $column = 0; foreach ($fields as $field) { - $bgcolor = entry_bgcolor($row, $column); - $fgcolor = entry_fgcolor($row, $column); - echo "\n"; + $bgcolor = entry_bgcolor($row, $column, $prize_entry); + $fgcolor = entry_fgcolor($row, $column, $prize_entry); + $text = entry_formatting($field, $formatting, $type); + echo "\n"; $column++; } echo "\n"; @@ -121,10 +178,11 @@ echo "[tr]\n"; $column = 0; foreach ($fields as $field) { - $bgcolor = entry_bgcolor($row, $column); - $fgcolor = entry_fgcolor($row, $column); + $bgcolor = entry_bgcolor($row, $column, $prize_entry); + $fgcolor = entry_fgcolor($row, $column, $prize_entry); + $text = entry_formatting($field, $formatting, $type); echo "[td=\"bgcolor: $bgcolor\"][color=$fgcolor]"; - echo $field; + echo $text; echo "[/color][/td]\n"; $column++; } @@ -194,6 +252,50 @@ 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 + ); + + /* Leader and runner-up. */ + foreach ($entries as $entry) { + if (endorsement('x', $entry->getEndorsements())) continue; + $entry_id = $entry->getId(); + if (in_array($entry_id, array_values($ret))) continue; + if (is_null($ret['leader'])) $ret['leader'] = $entry_id; + else if (is_null($ret['runnerup'])) { + $ret['runnerup'] = $entry_id; + break; + } + } + + /* Standard races only have standard classification. */ + if (! $allotted_time) return $ret; + + $copy = array(); + foreach ($entries as $entry) $copy[] = $entry; + + /* Other prizes. */ + foreach (array_keys($sorted) as $key) { + uasort($copy, "Entry::$key"); + foreach ($copy as $entry) { + if (endorsement('x', $entry->getEndorsements())) continue; + $entry_id = $entry->getId(); + if (in_array($entry_id, array_values($ret))) continue; + $ret[$key] = $entry_id; + } + } + + return $ret; + } + function module_race($action) { if ($_POST['add_race']) { if (add_race($_POST["name"], $_POST["allotted_time"])) unset($_POST); @@ -211,7 +313,15 @@ $eq->joinWith('Ship.Competitor'); $eq->joinWith('Ship.Hull'); $eq->filterByRaceId($id); - $entries = $eq->orderByTotalDistance('desc')->orderByPenaltyTime('desc')->orderByTotalTime('desc')->find(); + /* + Ordering by total distance then penalty time will classify racers + appropriately in endurance races, while having no effect on dashes. + Ordering by total time will classify racers in dashes while not + affecting endurance races. + Ordering by bonus time will allow a racer who didn't use the joker + to win a tiebreak with one who did. + */ + $entries = $eq->orderByTotalDistance('desc')->orderByPenaltyTime('desc')->orderByTotalTime()->orderByBonusTime()->find(); if (! $action) { echo "

" . $race->getLink() . "

\n"; continue; @@ -220,16 +330,36 @@ echo "

No entries.

\n"; continue; } + $prizewinners = find_prizewinners($entries, $allotted_time); echo "

$name

\n"; echo "
\n"; foreach (array('html', 'bbcode') as $type) { echo "
\n"; entry_header($type); - $n = $position = 1; + $n = $position = $rank = 1; + $ship_entries = array(); + $competitor_entries = array(); foreach ($entries as $entry) { - $endorsements = $entry->getEndorsements(); - show_entry($entry, $allotted_time, endorsement('x', $endorsements) ? '-' : $position++, $n++, $type); + $p = $r = $s = ''; + if (! endorsement('x', $entry->getEndorsements())) { + $competitor = $entry->getShip()->getCompetitor(); + $name = $competitor->getForumName(); + if (! $name) $name = $competitor->getCmdrName(); + if (array_key_exists($name, $competitor_entries)) $competitor_entries[$name]++; + else { + $competitor_entries[$name] = 1; + $r = count($competitor_entries); + } + + $hull_id = $entry->getShip()->getHull()->getId(); + if (array_key_exists($hull_id, $ship_entries)) $ship_entries[$hull_id]++; + else $ship_entries[$hull_id] = 1; + + $p = $position++; + $s = count($ship_entries[$hull_id]); + } + show_entry($entry, $allotted_time, $p, $r, $s, $n++, $type, $prizewinners); } entry_footer($type); echo "
\n"; diff --git a/propel/build/classes/buckyball/Entry.php b/propel/build/classes/buckyball/Entry.php index 056458f..070ea7c 100644 --- a/propel/build/classes/buckyball/Entry.php +++ b/propel/build/classes/buckyball/Entry.php @@ -15,6 +15,30 @@ */ class Entry extends BaseEntry { + function ByBestSpeed($a, $b) { + $A = $a->getBestSpeed(); $B = $b->getBestSpeed(); + if ($A == $B) return 0; + return $A < $B ? -1 : 1; + } + + function ByAverageSpeed($a, $b) { + $A = $a->getAverageSpeed(); $B = $b->getAverageSpeed(); + if ($A == $B) return 0; + return $A < $B ? -1 : 1; + } + + function ByFastestLap($a, $b) { + $A = $a->getFastestLap(); $B = $b->getFastestLap(); + if ($A == $B) return 0; + return $A < $B ? -1 : 1; + } + + function ByAverageLap($a, $b) { + $A = $a->getAverageLap(); $B = $b->getAverageLap(); + if ($A == $B) return 0; + return $A < $B ? -1 : 1; + } + function getName() { return sprintf("entry %d", $this->getId()); } -- 2.7.4
$field$text