From 5013a873ea0b5e27331886b1ef47603bfff51a52 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 5 Feb 2016 09:32:26 -0500 Subject: [PATCH] Graphs respect filtering. --- lib/race.php | 309 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 156 insertions(+), 153 deletions(-) diff --git a/lib/race.php b/lib/race.php index 7229679..86811b9 100644 --- a/lib/race.php +++ b/lib/race.php @@ -464,157 +464,6 @@ $entries_by_id[$entry->getId()] = $entry; } $prizewinners = find_prizewinners($race, $entries); - $plots = array( - "cmdr" => array( - "values" => array(), - "labels" => array(), - "text" => "By pilot", - "textinfo" => "label+percent", - "name" => "CMDR", - "scalegroup" => "entries", - "type" => "pie", - "domain" => array( - "x" => array(0, .23), - "y" => array(0, .48) - ) - ), - "hull" => array( - "values" => array(), - "labels" => array(), - "name" => "Ship", - "text" => "By ship", - "textinfo" => "label+percent", - "scalegroup" => "entries", - "type" => "pie", - "domain" => array( - "x" => array(.61, .73), - "y" => array(0, .48) - ) - ), - "lap_distance" => array( - "values" => array(), - "labels" => array(), - "name" => "Lap distance", - "text" => "By lap distance", - "textinfo" => "label+percent", - "scalegroup" => "entries", - "type" => "pie", - "domain" => array( - "x" => array(.35, .48), - "y" => array(.51, 1) - ) - ), - "lap_count" => array( - "values" => array(), - "labels" => array(), - "name" => "Complete laps", - "text" => "By lap count", - "textinfo" => "label+percent", - "scalegroup" => "entries", - "type" => "pie", - "domain" => array( - "x" => array(.75, 1), - "y" => array(.51, 1) - ) - ) - ); - $cmdrs = array(); - $hulls = array(); - $lap_counts = array(); - $lap_distances = array(); - foreach ($entries as $entry) { - $ship = $entry->getShip(); - $cmdr_name = $ship->getCompetitor()->getCmdrName(); - $hull_name = $ship->getHull()->getName(); - $lap_count = $entry->getCompleteLaps(); - $lap_count = sprintf("%d lap%s", $lap_count, ($lap_count == 1) ? "" : "s"); - $lap_distance = format_distance($entry->getLapDistance()) . "Ly"; - if (! in_array($cmdr_name, array_keys($cmdrs))) $cmdrs[$cmdr_name] = 0; - $cmdrs[$cmdr_name]++; - if (! in_array($hull_name, array_keys($hulls))) $hulls[$hull_name] = 0; - $hulls[$hull_name]++; - if (! in_array($lap_count, array_keys($lap_counts))) $lap_counts[$lap_count] = 0; - $lap_counts[$lap_count]++; - if (! in_array($lap_distance, array_keys($lap_distances))) $lap_distances[$lap_distance] = 0; - $lap_distances[$lap_distance]++; - } - foreach ($cmdrs as $cmdr_name => $count) { - $plots["cmdr"]["values"][] = $count; - $plots["cmdr"]["labels"][] = $cmdr_name; - } - $plots["cmdr"]["pull"] = pull($plots["cmdr"]["values"]); - foreach ($hulls as $hull_name => $count) { - $plots["hull"]["values"][] = $count; - $plots["hull"]["labels"][] = $hull_name; - } - $plots["hull"]["pull"] = pull($plots["hull"]["values"]); - foreach ($lap_counts as $lap_count => $count) { - $plots["lap_count"]["values"][] = $count; - $plots["lap_count"]["labels"][] = $lap_count; - } - $plots["lap_count"]["pull"] = pull($plots["lap_count"]["values"]); - foreach ($lap_distances as $lap_distance => $count) { - $plots["lap_distance"]["values"][] = $count; - $plots["lap_distance"]["labels"][] = $lap_distance; - } - $plots["lap_distance"]["pull"] = pull($plots["lap_distance"]["values"]); - - $legs = array( - "leg_times" => array( - "x" => array(), - "y" => array(), - "name" => "Leg times", - "type" => "bar" - ) - ); - $speeds = array( - "leg_speed" => array( - "x" => array(), - "y" => array(), - "name" => "Leg speed", - "type" => "bar" - ) - ); - $laps = array( - "fastest_laps" => array( - "x" => array(), - "y" => array(), - "name" => "Fastest race lap", - "type" => "bar" - ) - ); - $dbh = Propel::getConnection(); - /* Average station to station speed. */ - $sth = $dbh->prepare("select s1.name as station1, s2.name as station2, avg(timestampdiff(second, l1.arrival, l2.arrival)) as delta from laptime l1, laptime l2, station s1, station s2, lap, entry, course where l1.lap_id=l2.lap_id and l1.station_id=s1.id and l2.station_id=s2.id and l1.lap_id=lap.id and lap.entry_id=entry.id and course.race_id=entry.race_id and l2.station_order=l1.station_order+1 and entry.race_id=:race_id group by station1, station2 order by delta"); - $sth->execute(array(":race_id" => $id)); - while ($row = $sth->fetch()) { - $legs["leg_times"]["x"][] = sprintf("%s - %s", $row["station1"], $row["station2"]); - $legs["leg_times"]["y"][] = (int) $row["delta"]; - } - /* Average station to station speed. */ - $sth = $dbh->prepare("select s1.name as station1, s2.name as station2, avg((distance / (timestampdiff(second, l1.arrival, l2.arrival) / 3600))) as speed from laptime l1, laptime l2, station s1, station s2, lap, entry, course, raikogram where l1.lap_id=l2.lap_id and l1.station_id=s1.id and l2.station_id=s2.id and l1.lap_id=lap.id and lap.entry_id=entry.id and system1=(select case when s1.system_idexecute(array(":race_id" => $id)); - while ($row = $sth->fetch()) { - $speeds["leg_speed"]["x"][] = sprintf("%s - %s", $row["station1"], $row["station2"]); - $speeds["leg_speed"]["y"][] = (float) $row["speed"]; - } - /* Average time per lap. */ - //$sth = $dbh->prepare("select lap_number, avg(delta) from (select lap_number, timestampdiff(second, min(arrival), max(arrival)) as delta from laptime, lap, entry where laptime.lap_id=lap.id and lap.entry_id=entry.id and lap_number<=entry.complete_laps and race_id=:race_id group by entry_id, lap_number) as aggregate_laps group by lap_number"); - //$sth->execute(array(":race_id" => $id)); - /* Fastest individual lap. */ - $sth = $dbh->prepare("select lap_number, count(*) as n from (select lap_number, delta from (select entry_id, lap_number, timestampdiff(second, min(arrival), max(arrival)) as delta from laptime, lap, entry where laptime.lap_id=lap.id and lap.entry_id=entry.id and lap_number<=entry.complete_laps and race_id=:race_id group by entry_id, lap_number order by entry_id, delta) as aggregate_laps group by entry_id) as aggregate_fastest group by lap_number"); - $sth->execute(array(":race_id" => $id)); - while ($row = $sth->fetch()) { - $laps["fastest_laps"]["x"][] = "Lap " . $row["lap_number"]; - $laps["fastest_laps"]["y"][] = (int) $row["n"]; - } - echo "\n"; - uasort($entries, "Entry::$sort_method"); } else { @@ -665,14 +514,16 @@ } echo "

Show: " . implode(" // ", $uri_list) . "

\n"; + $filtered_entries = array(); foreach (array('html', 'bbcode') as $type) { echo "
\n"; - entry_header($format, $type); + entry_header($sort, $format, $type); $n = $position = $rank = 1; $ship_entries = array(); $competitor_entries = array(); $competitor_hull_entries = array(); $excluded_competitors = array(); + $filter = (! count($filtered_entries)); foreach ($entries as $entry) { $entry_id = $entry->getId(); $p = $r = $s = ''; @@ -750,12 +601,164 @@ $r = (array_key_exists($name, $excluded_competitors)) ? 2 : 1; $excluded_competitors[$name] = 1; } - show_entry($format, $entry, $allotted_time, $p, $r, $s, $n++, $type, $prize_name); + show_entry($sort, $format, $entry, $allotted_time, $p, $r, $s, $n++, $type, $prize_name); + if ($filter) $filtered_entries[] = $entry; } entry_footer($race, $type); echo "
\n"; } echo "\n"; + + $plots = array( + "cmdr" => array( + "values" => array(), + "labels" => array(), + "text" => "By pilot", + "textinfo" => "label+percent", + "name" => "CMDR", + "scalegroup" => "entries", + "type" => "pie", + "domain" => array( + "x" => array(0, .23), + "y" => array(0, .48) + ) + ), + "hull" => array( + "values" => array(), + "labels" => array(), + "name" => "Ship", + "text" => "By ship", + "textinfo" => "label+percent", + "scalegroup" => "entries", + "type" => "pie", + "domain" => array( + "x" => array(.61, .73), + "y" => array(0, .48) + ) + ), + "lap_distance" => array( + "values" => array(), + "labels" => array(), + "name" => "Lap distance", + "text" => "By lap distance", + "textinfo" => "label+percent", + "scalegroup" => "entries", + "type" => "pie", + "domain" => array( + "x" => array(.35, .48), + "y" => array(.51, 1) + ) + ), + "lap_count" => array( + "values" => array(), + "labels" => array(), + "name" => "Complete laps", + "text" => "By lap count", + "textinfo" => "label+percent", + "scalegroup" => "entries", + "type" => "pie", + "domain" => array( + "x" => array(.75, 1), + "y" => array(.51, 1) + ) + ) + ); + $cmdrs = array(); + $hulls = array(); + $lap_counts = array(); + $lap_distances = array(); + foreach ($filtered_entries as $entry) { + $ship = $entry->getShip(); + $cmdr_name = $ship->getCompetitor()->getCmdrName(); + $hull_name = $ship->getHull()->getName(); + $lap_count = $entry->getCompleteLaps(); + $lap_count = sprintf("%d lap%s", $lap_count, ($lap_count == 1) ? "" : "s"); + $lap_distance = format_distance($entry->getLapDistance()) . "Ly"; + if (! in_array($cmdr_name, array_keys($cmdrs))) $cmdrs[$cmdr_name] = 0; + $cmdrs[$cmdr_name]++; + if (! in_array($hull_name, array_keys($hulls))) $hulls[$hull_name] = 0; + $hulls[$hull_name]++; + if (! in_array($lap_count, array_keys($lap_counts))) $lap_counts[$lap_count] = 0; + $lap_counts[$lap_count]++; + if (! in_array($lap_distance, array_keys($lap_distances))) $lap_distances[$lap_distance] = 0; + $lap_distances[$lap_distance]++; + } + foreach ($cmdrs as $cmdr_name => $count) { + $plots["cmdr"]["values"][] = $count; + $plots["cmdr"]["labels"][] = $cmdr_name; + } + $plots["cmdr"]["pull"] = pull($plots["cmdr"]["values"]); + foreach ($hulls as $hull_name => $count) { + $plots["hull"]["values"][] = $count; + $plots["hull"]["labels"][] = $hull_name; + } + $plots["hull"]["pull"] = pull($plots["hull"]["values"]); + foreach ($lap_counts as $lap_count => $count) { + $plots["lap_count"]["values"][] = $count; + $plots["lap_count"]["labels"][] = $lap_count; + } + $plots["lap_count"]["pull"] = pull($plots["lap_count"]["values"]); + foreach ($lap_distances as $lap_distance => $count) { + $plots["lap_distance"]["values"][] = $count; + $plots["lap_distance"]["labels"][] = $lap_distance; + } + $plots["lap_distance"]["pull"] = pull($plots["lap_distance"]["values"]); + + $legs = array( + "leg_times" => array( + "x" => array(), + "y" => array(), + "name" => "Leg times", + "type" => "bar" + ) + ); + $speeds = array( + "leg_speed" => array( + "x" => array(), + "y" => array(), + "name" => "Leg speed", + "type" => "bar" + ) + ); + $laps = array( + "fastest_laps" => array( + "x" => array(), + "y" => array(), + "name" => "Fastest race lap", + "type" => "bar" + ) + ); + $dbh = Propel::getConnection(); + /* Average station to station speed. */ + $sth = $dbh->prepare("select s1.name as station1, s2.name as station2, avg(timestampdiff(second, l1.arrival, l2.arrival)) as delta from laptime l1, laptime l2, station s1, station s2, lap, entry, course where l1.lap_id=l2.lap_id and l1.station_id=s1.id and l2.station_id=s2.id and l1.lap_id=lap.id and lap.entry_id=entry.id and course.race_id=entry.race_id and l2.station_order=l1.station_order+1 and entry.race_id=:race_id group by station1, station2 order by delta"); + $sth->execute(array(":race_id" => $id)); + while ($row = $sth->fetch()) { + $legs["leg_times"]["x"][] = sprintf("%s - %s", $row["station1"], $row["station2"]); + $legs["leg_times"]["y"][] = (int) $row["delta"]; + } + /* Average station to station speed. */ + $sth = $dbh->prepare("select s1.name as station1, s2.name as station2, avg((distance / (timestampdiff(second, l1.arrival, l2.arrival) / 3600))) as speed from laptime l1, laptime l2, station s1, station s2, lap, entry, course, raikogram where l1.lap_id=l2.lap_id and l1.station_id=s1.id and l2.station_id=s2.id and l1.lap_id=lap.id and lap.entry_id=entry.id and system1=(select case when s1.system_idexecute(array(":race_id" => $id)); + while ($row = $sth->fetch()) { + $speeds["leg_speed"]["x"][] = sprintf("%s - %s", $row["station1"], $row["station2"]); + $speeds["leg_speed"]["y"][] = (float) $row["speed"]; + } + /* Average time per lap. */ + //$sth = $dbh->prepare("select lap_number, avg(delta) from (select lap_number, timestampdiff(second, min(arrival), max(arrival)) as delta from laptime, lap, entry where laptime.lap_id=lap.id and lap.entry_id=entry.id and lap_number<=entry.complete_laps and race_id=:race_id group by entry_id, lap_number) as aggregate_laps group by lap_number"); + //$sth->execute(array(":race_id" => $id)); + /* Fastest individual lap. */ + $sth = $dbh->prepare("select lap_number, count(*) as n from (select lap_number, delta from (select entry_id, lap_number, timestampdiff(second, min(arrival), max(arrival)) as delta from laptime, lap, entry where laptime.lap_id=lap.id and lap.entry_id=entry.id and lap_number<=entry.complete_laps and race_id=:race_id group by entry_id, lap_number order by entry_id, delta) as aggregate_laps group by entry_id) as aggregate_fastest group by lap_number"); + $sth->execute(array(":race_id" => $id)); + while ($row = $sth->fetch()) { + $laps["fastest_laps"]["x"][] = "Lap " . $row["lap_number"]; + $laps["fastest_laps"]["y"][] = (int) $row["n"]; + } + echo "\n"; } echo "
\n"; $sq = new StationQuery; -- 2.7.4