Skip to content

Commit bddbefb

Browse files
authored
Merge pull request #230 from fraz3alpha/combine-obsessive-badges
Bundle the obsessive badges into a single table
2 parents e2c83a4 + 461486f commit bddbefb

File tree

1 file changed

+114
-36
lines changed

1 file changed

+114
-36
lines changed

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

Lines changed: 114 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,30 @@ function generate_running_challenge_data(data) {
199199
{"month": 11},
200200
],
201201
"help": "Run in each month of the year."}))
202-
challenge_data.push(challenge_in_a_year(data, {
203-
"shortname": "obsessive-bronze",
204-
"name": "Bronze Level Obsessive",
205-
"data": 30,
206-
"help": "Run 30+ parkruns in one calendar year."}))
207-
challenge_data.push(challenge_in_a_year(data, {
208-
"shortname": "obsessive-silver",
209-
"name": "Silver Level Obsessive",
210-
"data": 40,
211-
"help": "Run 40+ parkruns in one calendar year."}))
212-
challenge_data.push(challenge_in_a_year(data, {
213-
"shortname": "obsessive-gold",
214-
"name": "Gold Level Obsessive",
215-
"data": 50,
216-
"help": "Run 50+ parkruns in one calendar year."}))
202+
challenge_data.push(challenge_obsessive(data,
203+
{
204+
"shortname": "obsessive-gold",
205+
"name": "parkrun Obsessive",
206+
"help": "Run at 30+ (bronze), 40+ (silver), or 50+(gold) parkruns in a calendar year.",
207+
"stages": [
208+
{
209+
"count": 30,
210+
"name": "Bronze",
211+
"badge_icon": "runner-obsessive-bronze"
212+
},
213+
{
214+
"count": 40,
215+
"name": "Silver",
216+
"badge_icon": "runner-obsessive-silver"
217+
},
218+
{
219+
"count": 50,
220+
"name": "Gold",
221+
"badge_icon": "runner-obsessive-gold"
222+
}
223+
]
224+
}))
225+
217226
}
218227

219228
// if (data.parkrun_results && data.geo_data) {
@@ -1970,15 +1979,17 @@ function challenge_groundhog_day(data, params) {
19701979
return update_data_object(o)
19711980
}
19721981

1973-
function challenge_in_a_year(data, params) {
1982+
function challenge_obsessive(data, params) {
19741983

19751984
var parkrun_results = data.parkrun_results
1976-
var count = params.data
1985+
var stages = params.stages
19771986

19781987
var o = create_data_object(params, "runner")
19791988
o.subparts = ["1"]
19801989
o.summary_text = "0"
19811990

1991+
var badgesAwarded = []
1992+
19821993
by_year = {}
19831994

19841995
parkrun_results.forEach(function (parkrun_event) {
@@ -1993,32 +2004,99 @@ function challenge_in_a_year(data, params) {
19932004

19942005
})
19952006

2007+
// We now have an object showing us how many parkruns were attended in each year.
2008+
// Until such a time as volunteering can be allocated in this way, this just means
2009+
// running and getting a result.
2010+
2011+
console.log(by_year)
2012+
2013+
var subBadgesAvailable = []
2014+
2015+
$.each(stages, function(index, obsessiveMilestone) {
2016+
subBadgesAvailable.push({
2017+
"awarded": false
2018+
})
2019+
})
2020+
2021+
// Now lets look at each year for which we have data, and see if the different milestones
2022+
// have been reached.
19962023
Object.keys(by_year).sort().forEach(function (year) {
1997-
if (by_year[year].length >= count) {
1998-
o.subparts_detail.push({
1999-
"name": year,
2000-
"date": year,
2001-
"info": by_year[year].length,
2002-
"subpart": count+"+"
2003-
})
2004-
o.subparts_completed_count += 1
2005-
if (!o.complete) {
2006-
o.complete = true
2007-
o.completed_on = year
2008-
}
2024+
var currentBadge = undefined
2025+
$.each(stages, function(index, obsessiveMilestone) {
2026+
if (by_year[year].length >= obsessiveMilestone.count) {
2027+
currentBadge = {
2028+
"index": index,
2029+
"name": obsessiveMilestone.name,
2030+
"count": obsessiveMilestone.count,
2031+
"badge_icon": obsessiveMilestone.badge_icon
2032+
}
20092033
}
2034+
})
2035+
if (currentBadge !== undefined) {
2036+
// Award a badge for this year
2037+
o.subparts_detail.push({
2038+
"name": year,
2039+
"date": year,
2040+
"info": by_year[year].length,
2041+
"subpart": currentBadge.name+" ("+currentBadge.count+"+)",
2042+
"badge": currentBadge
2043+
})
2044+
// Ensure that we add the badge at the top
2045+
subBadgesAvailable[currentBadge.index].awarded = true
2046+
}
2047+
20102048
})
20112049

2012-
if (o.subparts_detail.length == 0) {
2050+
// Change the summary to indicate number of times completed
2051+
if (o.subparts_detail.length > 0) {
2052+
o.summary_text = "x"+o.subparts_detail.length
2053+
}
2054+
2055+
// Award the badges to be displayed at the top
2056+
$.each(subBadgesAvailable, function(index, subBadge) {
2057+
if (subBadge.awarded == true) {
2058+
badgesAwarded.push({
2059+
"name": stages[index].name,
2060+
"badge_icon": stages[index].badge_icon
2061+
})
2062+
}
2063+
})
2064+
2065+
o.badgesAwarded = badgesAwarded
2066+
2067+
2068+
// Display the badges above what the parkrunner has, so they can see
2069+
// what is still to come. i.e. if they have nothing, show all 3 badges,
2070+
// if they have bronze, show silver+gold, have silver, show gold, if
2071+
// they already have gold, they then don't need hints and that'll do :)
2072+
2073+
// Find the highest ones they have.
2074+
var maxBadgeAwarded = -1
2075+
$.each(subBadgesAvailable, function(index, subBadge) {
2076+
if (subBadge.awarded) {
2077+
maxBadgeAwarded = index
2078+
}
2079+
})
2080+
console.log("parkrunner has achieved max obsessive level of " + maxBadgeAwarded)
2081+
2082+
// Add the badge for any higher badges than the ones they have
2083+
$.each(stages, function(index, obsessiveMilestone) {
2084+
if (index > maxBadgeAwarded) {
20132085
o.subparts_detail.push({
2014-
"subpart": count+"+",
2015-
"info": "-"
2086+
// "subpart": count+"+",
2087+
"badge": {
2088+
"name": obsessiveMilestone.name,
2089+
"badge_icon": obsessiveMilestone.badge_icon
2090+
},
2091+
"subpart": obsessiveMilestone.name+" ("+obsessiveMilestone.count+"+)",
2092+
"name": "-"
20162093
})
2017-
}
2094+
}
2095+
})
20182096

2019-
// Change the summary to indicate number of times completed
2020-
if (o.subparts_completed_count > 0) {
2021-
o.summary_text = "x"+o.subparts_completed_count
2097+
// If they have been awarded the maximum badge, then give them a tick
2098+
if (maxBadgeAwarded == stages.length-1) {
2099+
o.complete = true
20222100
}
20232101

20242102
// Return an object representing this challenge

0 commit comments

Comments
 (0)