From a128a26ca7f329442e335aed9e0006455e4f9872 Mon Sep 17 00:00:00 2001 From: Chris Schlaeger Date: Sun, 26 Jul 2015 20:59:44 +0200 Subject: New: More running records --- lib/postrunner/Activity.rb | 2 +- lib/postrunner/PersonalRecords.rb | 28 +++++++++++++++++++--------- lib/postrunner/RecordListPageView.rb | 3 ++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb index 3c92a5e..e0dbc89 100644 --- a/lib/postrunner/Activity.rb +++ b/lib/postrunner/Activity.rb @@ -340,7 +340,7 @@ module PostRunner # Store the found records start_time = @fit_activity.sessions[0].timestamp - if @distance_record_sport + if distance_record_sport @db.records.register_result(self, distance_record_sport, distance_record, nil, start_time) end diff --git a/lib/postrunner/PersonalRecords.rb b/lib/postrunner/PersonalRecords.rb index 66ef7e3..bfa4a57 100644 --- a/lib/postrunner/PersonalRecords.rb +++ b/lib/postrunner/PersonalRecords.rb @@ -20,10 +20,13 @@ require 'postrunner/ActivityLink' module PostRunner + # The PersonalRecords class stores the various records. Records are grouped + # by specific year or all-time records. class PersonalRecords include Fit4Ruby::Converters + # List of popular distances for each sport. SpeedRecordDistances = { 'cycling' => { 1000.0 => '1 km', @@ -39,6 +42,9 @@ module PostRunner 18000.0 => '180 km', }, 'running' => { + 400.0 => '400 m', + 500.0 => '500 m', + 800.0 => '800 m', 1000.0 => '1 km', 1609.0 => '1 mi', 2000.0 => '2 km', @@ -62,6 +68,7 @@ module PostRunner 3860.0 => '2.4 mi' }, 'walking' => { + 500.0 => '500 m', 1000.0 => '1 km', 1609.0 => '1 mi', 5000.0 => '5 km', @@ -71,6 +78,8 @@ module PostRunner } } + # The Record class stores a single speed or longest distance record. It + # also stores a reference to the Activity that contains the record. class Record include Fit4Ruby::Converters @@ -87,7 +96,7 @@ module PostRunner def to_table_row(t) t.row((@duration.nil? ? - [ 'Longest Run', '%.1f m' % @distance, '-' ] : + [ 'Longest Distance', '%.3f km' % (@distance / 1000.0), '-' ] : [ PersonalRecords::SpeedRecordDistances[@sport][@distance], secsToHMS(@duration), speedToPace(@distance / @duration) ]) + @@ -107,7 +116,7 @@ module PostRunner def initialize(sport, year) @sport = sport @year = year - @distance = nil + @distance_record = nil @speed_records = {} PersonalRecords::SpeedRecordDistances[@sport].each_key do |dist| @speed_records[dist] = nil @@ -134,8 +143,9 @@ module PostRunner end else # We have a potential distance record. - if @distance.nil? || result.distance > @distance.distance - @distance = result + if @distance_record.nil? || + @distance_record.distance < result.distance + @distance_record = result Log.info "New #{@year ? @year.to_s : 'all-time'} " + "#{result.sport} distance record: #{result.distance} m" return true @@ -146,8 +156,8 @@ module PostRunner end def delete_activity(activity) - if @distance && @distance.activity == activity - @distance = nil + if @distance_record && @distance_record.activity == activity + @distance_record = nil end PersonalRecords::SpeedRecordDistances[@sport].each_key do |dist| if @speed_records[dist] && @speed_records[dist].activity == activity @@ -158,7 +168,7 @@ module PostRunner # Return true if no Record is stored in this RecordSet object. def empty? - return false if @distance + return false if @distance_record @speed_records.each_value { |r| return false if r } true @@ -166,7 +176,7 @@ module PostRunner # Iterator for all Record objects that are stored in this data structure. def each(&block) - yield(@distance) if @distance + yield(@distance_record) if @distance_record @speed_records.each_value do |record| yield(record) if record end @@ -201,7 +211,7 @@ module PostRunner t.body records = @speed_records.values.delete_if { |r| r.nil? } - records << @distance if @distance + records << @distance_record if @distance_record records.sort { |r1, r2| r1.distance <=> r2.distance }.each do |r| r.to_table_row(t) diff --git a/lib/postrunner/RecordListPageView.rb b/lib/postrunner/RecordListPageView.rb index f1de004..01dafb3 100644 --- a/lib/postrunner/RecordListPageView.rb +++ b/lib/postrunner/RecordListPageView.rb @@ -55,7 +55,8 @@ module PostRunner ViewFrame.new("All-time #{@sport_name} Records", frame_width, @records.all_time).to_html(@doc) - @records.yearly.each do |year, record| + @records.yearly.sort{ |y1, y2| y2[0] <=> y1[0] }. + each do |year, record| next if record.empty? ViewFrame.new("#{year} #{@sport_name} Records", frame_width, record).to_html(@doc) -- cgit v1.2.3