Skip to content

Commit 2662b64

Browse files
authored
Merge pull request #231 from fraz3alpha/avg-parkrun
Add nearest parkrun to average parkrun location stat
2 parents bddbefb + c0638c9 commit 2662b64

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

browser-extensions/common/js/lib/challenges.js

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,14 @@ function generate_stat_total_countries_visited(parkrun_results, geo_data) {
686686
}
687687
}
688688

689+
// Calculate average lat/lon of all parkruns completed
689690
function generate_stat_average_parkrun_location(parkrun_results, geo_data) {
690691
var lat_sum = 0
691692
var lon_sum = 0
692693
var count = 0
693694

694695
parkrun_results.forEach(function (parkrun_event) {
695-
// Work out how far the parkrunner has travelled to this location
696+
// Is this parkrun still live (know where it is), and has it got a location we can do something with?
696697
if (parkrun_event.name in geo_data.data.events) {
697698
var event_location_info = geo_data.data.events[parkrun_event.name]
698699
if (event_location_info.lat && event_location_info.lon) {
@@ -721,6 +722,81 @@ function generate_stat_average_parkrun_location(parkrun_results, geo_data) {
721722
}
722723
}
723724

725+
726+
// Calculate average parkrun location
727+
function calculate_average_parkrun_location(parkrun_results, geo_data) {
728+
var lat_sum = 0
729+
var lon_sum = 0
730+
var count = 0
731+
var lat_av = ''
732+
var lon_av = ''
733+
734+
parkrun_results.forEach(function (parkrun_event) {
735+
// Is this parkrun still live (know where it is), and has it got a location we can do something with?
736+
if (parkrun_event.name in geo_data.data.events) {
737+
var event_location_info = geo_data.data.events[parkrun_event.name]
738+
if (event_location_info.lat && event_location_info.lon) {
739+
lat_sum += parseFloat(event_location_info.lat)
740+
lon_sum += parseFloat(event_location_info.lon)
741+
count += 1
742+
}
743+
}
744+
})
745+
746+
if (count > 0) {
747+
lat_av = (lat_sum/count)
748+
lon_av = (lon_sum/count)
749+
}
750+
751+
return{
752+
"lat" : lat_av,
753+
"lon" : lon_av
754+
}
755+
}
756+
757+
// What's the URL of the parkrun's webpage?
758+
function get_parkrun_page_url(data, parkrun_name) {
759+
parkrun_page_url = undefined
760+
if (data.info.has_geo_data) {
761+
if (parkrun_name in data.geo_data.data.events) {
762+
parkrun_shortname = data.geo_data.data.events[parkrun_name].shortname
763+
country_name = data.geo_data.data.events[parkrun_name].country_name
764+
if (country_name in data.geo_data.data.countries) {
765+
domain_url = data.geo_data.data.countries[country_name].url
766+
parkrun_page_url = "https://" + domain_url + "/" + parkrun_shortname
767+
}
768+
}
769+
}
770+
return parkrun_page_url
771+
}
772+
773+
// Which is the closest parkrun to your average parkrun location?
774+
function generate_stat_average_parkrun_event(parkrun_results, geo_data) {
775+
// Calculate average parkrun for user
776+
var average_parkrun_location = calculate_average_parkrun_location(parkrun_results, geo_data)
777+
var average_parkrun_event_name = ''
778+
var average_parkrun_event_distance = undefined
779+
// For each parkrun event, get the event's information, which includes its lon and lat.
780+
$.each(geo_data.data.events, function(event_name, event_location_info) {
781+
// For each parkrun event, calculate the 3D distance between the event and the user's average parkrun location
782+
var distance = calculate_great_circle_distance(event_location_info, average_parkrun_location)
783+
// If the distance to the event from the average parkrun location is less than the distance currently stored, store the event
784+
if (average_parkrun_event_distance == undefined || distance < average_parkrun_event_distance) {
785+
average_parkrun_event_name = event_name
786+
average_parkrun_event_distance = distance
787+
}
788+
})
789+
790+
var url_link = get_parkrun_page_url(data, average_parkrun_event_name)
791+
792+
return {
793+
"display_name": "Average parkrun event",
794+
"help": "The nearest parkrun event to your average parkrun location.",
795+
"value": average_parkrun_event_name,
796+
"url": url_link
797+
}
798+
}
799+
724800
// Furthest parkrun you have run away from your home parkrun
725801
function generate_stat_furthest_travelled(parkrun_results, geo_data, home_parkrun) {
726802
furthest_travelled = {
@@ -746,10 +822,13 @@ function generate_stat_furthest_travelled(parkrun_results, geo_data, home_parkru
746822
furthest_travelled.display_name = furthest_travelled.parkrun_event.name + ", " + furthest_travelled.parkrun_event.country_name
747823
}
748824

825+
var url_link = get_parkrun_page_url(data, furthest_travelled.parkrun_event.name)
826+
749827
return {
750828
"display_name": "Furthest travelled",
751829
"help": "The furthest away parkrun you have been to (calculated from your home parkrun).",
752-
"value": furthest_travelled.display_name + ", "+ furthest_travelled.distance + "km"
830+
"value": furthest_travelled.display_name + ", "+ furthest_travelled.distance + "km",
831+
"url": url_link
753832
}
754833
}
755834

@@ -786,10 +865,13 @@ function generate_stat_nearest_event_not_done_yet(parkrun_results, geo_data, hom
786865
value = nendy.name + ", " + nendy.country_name+ " - " + Math.round(event_distances[nendy_name]) + "km away"
787866
}
788867

868+
var url_link = get_parkrun_page_url(data, nendy.name)
869+
789870
return {
790871
"display_name": "Nearest event not done yet (NENDY)",
791872
"help": "The nearest parkrun event to your home parkrun that you have not done yet.",
792-
"value": value
873+
"value": value,
874+
"url": url_link
793875
}
794876
}
795877

@@ -844,6 +926,7 @@ function generate_stats(data) {
844926
stats['total_distance_travelled'] = generate_stat_total_distance_travelled(data.parkrun_results, data.geo_data)
845927
stats['total_countries_visited'] = generate_stat_total_countries_visited(data.parkrun_results, data.geo_data)
846928
stats['average_parkrun_location'] = generate_stat_average_parkrun_location(data.parkrun_results, data.geo_data)
929+
stats['average_parkrun_event'] = generate_stat_average_parkrun_event(data.parkrun_results, data.geo_data)
847930
}
848931

849932
// Stats that need the user data available, and we are on their page (i.e. has

website/_data/stats.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ stats:
113113
towards your home parkrun. If, however, you visit all over the world, who knows
114114
where you will be on average - probably in the sea!
115115
116+
- shortname: average_parkrun_event
117+
name: Average parkrun event
118+
description: >-
119+
The closest parkrun event to your average parkrun location.
120+
In case you want to go run it.
116121
117122
- shortname: furthest_travelled
118123
name: Furthest travelled

0 commit comments

Comments
 (0)