# == Schema Information # # Table name: world_bets # # id :integer not null, primary key # bet :string # french_score :integer # name :string # score :integer # world_score :integer # created_at :datetime not null # updated_at :datetime not null # class WorldBet < ApplicationRecord validates :name, presence: true, uniqueness: true validates :score, presence: true, numericality: { greater_than_or_equal_to: 0 } validates :french_score, presence: true, numericality: { greater_than_or_equal_to: 0 } validates :world_score, presence: true, numericality: { greater_than_or_equal_to: 0 } @@multiplier_array = [ 20, 15, 12, 10, 8, 6, 4, 3, 2, 1 ] @@points_array = [ 100, 70, 50, 40, 30, 26, 23, 20, 17, 15 ] def self.multiplier(i) if i < 0 or i > 9 return 0 end @@multiplier_array[i] end def self.points(i) if !i || i < 1 return 0 end if i <= 10 return @@points_array[i - 1] end if i <= 20 return 12 end if i <= 30 return 10 end if i <= 40 return 8 end if i <= 50 return 7 end if i <= 60 return 6 end if i <= 70 return 5 end if i <= 80 return 4 end if i <= 90 return 3 end if i <= 100 return 2 end if i <= 150 return 1 end 0 end def serialize_bet(w, f) self.bet = "#{w.join(',')};#{f.join(',')}" end def compute_score(participant_scores) self.score = 0 self.french_score = 0 self.world_score = 0 w = self.bet.split(";", -1).first.split(",", -1) w.each_with_index do |participant_id, i| if participant_scores.has_key?(Integer(participant_id)) self.score += participant_scores[Integer(participant_id)] * WorldBet.multiplier(i) self.world_score += participant_scores[Integer(participant_id)] * WorldBet.multiplier(i) end end f = self.bet.split(";", -1).last.split(",", -1) f.each_with_index do |participant_id, i| if participant_scores.has_key?(Integer(participant_id)) self.score += participant_scores[Integer(participant_id)] * WorldBet.multiplier(i) self.french_score += participant_scores[Integer(participant_id)] * WorldBet.multiplier(i) end end end end