Worlds Bets
This commit is contained in:
93
app/models/world_bet.rb
Normal file
93
app/models/world_bet.rb
Normal file
@@ -0,0 +1,93 @@
|
||||
# == 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
|
||||
24
app/models/world_country.rb
Normal file
24
app/models/world_country.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: world_countries
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# code :string not null
|
||||
# name :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
class WorldCountry < ApplicationRecord
|
||||
has_many :world_participants
|
||||
|
||||
validates :code, presence: true, uniqueness: true
|
||||
validates :name, presence: true, uniqueness: true
|
||||
|
||||
def self.add_countries(str)
|
||||
str.split(";", -1).each do |c|
|
||||
name = c.split(",", -1).first
|
||||
code = c.split(",", -1).last
|
||||
WorldCountry.create(name: name, code: code)
|
||||
end
|
||||
end
|
||||
end
|
||||
56
app/models/world_participant.rb
Normal file
56
app/models/world_participant.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: world_participants
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# final_result :integer
|
||||
# name :string not null
|
||||
# qualifier_result :integer
|
||||
# round :string not null
|
||||
# score :integer
|
||||
# semifinal_result :integer
|
||||
# username :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# world_country_id :integer not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_world_participants_on_world_country_id (world_country_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# world_country_id (world_country_id => world_countries.id)
|
||||
#
|
||||
class WorldParticipant < ApplicationRecord
|
||||
belongs_to :world_country
|
||||
|
||||
validates :name, presence: true
|
||||
validates :username, presence: true
|
||||
validates :round, inclusion: { in: %w[a b c d], message: "%{value} is not a valid round" }
|
||||
validates :qualifier_result, numericality: { greater_than_or_equal_to: 1 }, if: -> { qualifier_result.present? }
|
||||
validates :semifinal_result, numericality: { greater_than_or_equal_to: 1 }, if: -> { semifinal_result.present? }
|
||||
validates :final_result, numericality: { greater_than_or_equal_to: 1 }, if: -> { final_result.present? }
|
||||
|
||||
before_save :compute_score
|
||||
|
||||
def compute_score
|
||||
self.score = WorldBet.points(qualifier_result) + WorldBet.points(semifinal_result) * 2 + WorldBet.points(final_result) * 4
|
||||
end
|
||||
|
||||
def self.add_participants(str)
|
||||
str.split(";", -1).each do |c|
|
||||
cs = c.split(",", -1)
|
||||
if cs.length >= 4
|
||||
name = cs.first
|
||||
username = cs[1]
|
||||
country_code = cs[2]
|
||||
round = cs.last
|
||||
country = WorldCountry.where(code: country_code).first
|
||||
if country
|
||||
WorldParticipant.create(name: name, username: username, world_country: country, round: round)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user