Skip to content

Commit

Permalink
Fix negative filtering in stats and points
Browse files Browse the repository at this point in the history
  • Loading branch information
BGMP committed Jan 22, 2024
1 parent 7c14d59 commit 8d6a9e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions app/models/team_result_entry.rb
Expand Up @@ -2,7 +2,8 @@ class TeamResultEntry
include Mongoid::Document
include TeamLogoUploader::Attachment(:team_logo)

before_save :filter_negatives
# NOTE: This should never happen, but we cannot allow negative counts under any circumstances
before_save :filter_negative_points

embedded_in :season
embedded_in :ranking
Expand All @@ -17,7 +18,9 @@ class TeamResultEntry
validates_presence_of :name
validates_presence_of :short_name

def filter_negatives
def filter_negative_points
return if points.nil?

points = 0 if points && points < 0
end
end
10 changes: 6 additions & 4 deletions app/models/user.rb
Expand Up @@ -22,7 +22,7 @@ class User
before_create :create_stats

# NOTE: This should never happen, but we cannot allow negative counts under any circumstances
before_save :filter_negatives
before_save :filter_negative_stats

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :confirmable, :lockable,
:trackable, :omniauthable
Expand Down Expand Up @@ -110,14 +110,16 @@ def create_stats
)
end

def filter_negatives
def filter_negative_stats
return if stats.nil?

stats.race_wins = 0 if stats.race_wins && stats.race_wins < 0
stats.race_win_rate = 0 if stats.race_win_rate && stats.race_win_rate < 0
stats.race_win_rate = 0.0 if stats.race_win_rate && stats.race_win_rate < 0.0
stats.race_podiums = 0 if stats.race_podiums && stats.race_podiums < 0
stats.race_count = 0 if stats.race_count && stats.race_count < 0
stats.positions_sum = 0 if stats.positions_sum && stats.positions_sum < 0
stats.session_wins = 0 if stats.session_wins && stats.session_wins < 0
stats.session_win_rate = 0 if stats.session_win_rate && stats.session_win_rate < 0
stats.session_win_rate = 0.0 if stats.session_win_rate && stats.session_win_rate < 0.0
stats.session_podiums = 0 if stats.session_podiums && stats.session_podiums < 0
stats.session_count = 0 if stats.session_count && stats.session_count < 0
stats.average_position = 0.0 if stats.average_position && stats.average_position < 0.0
Expand Down

0 comments on commit 8d6a9e9

Please sign in to comment.