Skip to content

Instantly share code, notes, and snippets.

@garethtdavies
Last active July 3, 2016 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garethtdavies/b35ad5164d4f39d2009a3e35328f02d9 to your computer and use it in GitHub Desktop.
Save garethtdavies/b35ad5164d4f39d2009a3e35328f02d9 to your computer and use it in GitHub Desktop.
WTS Solo Bike Breakaway Analysis
<?php
/*
| Finds sucessful solo bike breakaways in WTS events
| Add your API key on line 11
*/
// Set some basic constants here
define("APIURL", "https://api.triathlon.org/v1/");
define("APIKEY", "YOUR_API_KEY_HERE");
define("GROUPSIZE", 1); // Number of athletes in the group
define("BREAKSIZE", 10); // Difference in seconds a group needs to be ahead to be considered a breakaway
// This function takes the cURL responses and parses the output
function getResponse($url)
{
$response = doCurl(APIURL . $url);
// Should do some error checking here to ensure you are getting a 200
$resp = json_decode($response['resp'], true);
return $resp;
}
// This function performs the cURL request for a given URL
function doCurl($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTPHEADER => array(
"apikey: " . APIKEY
)
));
$resp = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Build up a return array that includes the cURL response header
$return = array(
'code' => $code,
'resp' => $resp
);
curl_close($curl);
return $return;
}
// This function converts a hh:mm:ss to seconds so we can easily sum times
function timeToSecs($time_convert)
{
$time_converted = explode(":", $time_convert);
// Handle results that haven't been stored properly
isset($time_converted[0]) ? $time_converted[0] : $time_converted[0] = 0;
isset($time_converted[1]) ? $time_converted[1] : $time_converted[1] = 0;
isset($time_converted[2]) ? $time_converted[2] : $time_converted[2] = 0;
$total_seconds = ($time_converted[0] * 3600) + ($time_converted[1] * 60) + ($time_converted[2]);
return $total_seconds;
}
// Let's output all the formatted data directly
echo "Event Name | Program Name | Program ID | Athlete Name | Athlete ID | Athlete NOC | Position | Diff \r\n";
echo "--------------------------------------------------------------------------------------------------- \r\n";
// Start the analysis
// Get all WTS events
$events = getResponse("events?category_id=351&per_page=100");
foreach ($events['data'] as $event) {
// Loop through programs at each event - customise which programs you are interested in here
$programs = getResponse("events/{$event['event_id']}/programs?prog_filter=Elite%20Men|Elite%20Women");
foreach ($programs['data'] as $program) {
// We need to find the bike leader and also bike leader + group size
$end_of_bike_for_prog = array();
$athletes = array();
// for events that haven't happened yet but may have a program check for a result
if ($program['results'] === false)
continue;
// Get the results of the program
$results = getResponse("events/{$event['event_id']}/programs/{$program['prog_id']}/results");
foreach ($results['data']['results'] as $result) {
// We now have an individial result lets convert everything to seconds for easier analysis
$swim = timeToSecs($result['splits'][0]);
$t1 = timeToSecs($result['splits'][1]);
$bike = timeToSecs($result['splits'][2]);
// Sanity check here as sometimes splits are missing
// If any data is missing we will simply not include the result in the analysis as we have no way of knowing currently
// Sometimes 1 second is included in results to indicate DNS so we are using 2 seconds as a check to ensure all times are greater
if ($swim < 2 || $t1 < 2 || $bike < 2)
continue;
// Sum everything to get the end of bike time
$end_of_bike_time = $swim + $t1 + $bike;
// Add the split to the end of bike array so we have all end of bike splits for a program
array_push($end_of_bike_for_prog, $end_of_bike_time);
// We want to be able to reference the athlete that was the breakaway so add to athletes array using the time as the key
// This is a quick solution for the case where breakaway is 1 as will always be unique but would need to be tweaked for larger groups
$athletes[$end_of_bike_time] = array(
'athlete_id' => $result['athlete_id'],
'athlete_title' => $result['athlete_title'],
'athlete_noc' => $result['athlete_noc'],
'position' => $result['position']
);
}
// We now have all ed of bike times in an array so easy to sort and find the lowest and nth lowest
sort($end_of_bike_for_prog);
// What is the lowest time
$program_leader = $end_of_bike_for_prog[0];
// What is the time of the main pack
$program_pack = $end_of_bike_for_prog[GROUPSIZE]; // This will always be GROUPSIZE + 1 as array indexed from 0
// Was this a breakaway? If so let's echo out the relevant details
$difference = $program_pack - $program_leader;
if ($difference >= BREAKSIZE)
echo "{$event['event_title']} | {$event['event_id']} | {$program['prog_name']} | {$program['prog_id']} | {$athletes[$program_leader]['athlete_title']} | {$athletes[$program_leader]['athlete_id']} | {$athletes[$program_leader]['athlete_noc']} | {$athletes[$program_leader]['position']} | {$difference} \r\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment