136 lines
3.7 KiB
Ruby
136 lines
3.7 KiB
Ruby
class WorldsController < ApplicationController
|
|
allow_unauthenticated_access
|
|
|
|
before_action :set_title
|
|
before_action :set_fr_lang
|
|
|
|
def set_title
|
|
@title = I18n.t("worlds.title")
|
|
@nonav = true
|
|
end
|
|
|
|
def set_fr_lang
|
|
I18n.locale = "fr"
|
|
end
|
|
|
|
def main
|
|
authorize :world_bet
|
|
|
|
@world_bets_w = WorldBet.all.order(world_score: :desc)
|
|
@world_bets_f = WorldBet.all.order(french_score: :desc)
|
|
end
|
|
|
|
def about
|
|
authorize :world_bet
|
|
end
|
|
|
|
def collab
|
|
authorize :world_bet
|
|
|
|
participant_ids = Set[]
|
|
@world_bets = WorldBet.all.each do |b|
|
|
b.bet.split(";", -1).first.split(",", -1).each do |participant_id|
|
|
participant_ids.add(participant_id)
|
|
end
|
|
b.bet.split(";", -1).last.split(",", -1).each do |participant_id|
|
|
participant_ids.add(participant_id)
|
|
end
|
|
end
|
|
|
|
@participants = WorldParticipant.includes([ :world_country ]).find(participant_ids.to_a).sort_by { |p| p.name }
|
|
end
|
|
|
|
def view
|
|
authorize :world_bet
|
|
|
|
@world_bet = WorldBet.find(params[:bet_id])
|
|
@french_participants = WorldParticipant.includes([ :world_country ]).find(@world_bet.bet.split(";", -1).last.split(",", -1).map { |x| Integer(x) })
|
|
@world_participants = WorldParticipant.includes([ :world_country ]).find(@world_bet.bet.split(";", -1).first.split(",", -1).map { |x| Integer(x) })
|
|
end
|
|
|
|
def new
|
|
authorize :world_bet
|
|
|
|
@world_bet = WorldBet.new
|
|
@world_participants = WorldParticipant.includes([ :world_country ]).all.order(:name)
|
|
france = WorldCountry.where(code: "FR").first
|
|
@french_participants = WorldParticipant.includes([ :world_country ]).where(world_country: france).order(:name)
|
|
end
|
|
|
|
def create
|
|
authorize :world_bet
|
|
|
|
|
|
@world_bet = WorldBet.new(world_bet_params)
|
|
@world_bet.score = 0
|
|
@world_bet.french_score = 0
|
|
@world_bet.world_score = 0
|
|
all_params = params.require(:world_bet)
|
|
|
|
w_set = Set[]
|
|
f_set = Set[]
|
|
@w_array = []
|
|
@f_array = []
|
|
(1..10).each do |i|
|
|
if all_params.key?("w_bet_#{i}")
|
|
p_id = all_params["w_bet_#{i}"]
|
|
w_set.add(p_id)
|
|
@w_array.push(p_id)
|
|
end
|
|
if all_params.key?("f_bet_#{i}")
|
|
p_id = all_params["f_bet_#{i}"]
|
|
f_set.add(p_id)
|
|
@f_array.push(p_id)
|
|
end
|
|
end
|
|
@world_bet.serialize_bet(@w_array, @f_array)
|
|
|
|
if w_set.length != 10 || f_set.length != 10
|
|
@world_bet.errors.add(:name, I18n.t("activerecord.errors.models.world_bet.attributes.bet.duplicate"))
|
|
end
|
|
|
|
if w_set.length == 10 && f_set.length == 10 && @world_bet.save
|
|
redirect_to "/wjpc_bets/main", notice: t("worlds.new.notice")
|
|
else
|
|
@world_participants = WorldParticipant.all.sort_by { |p| p.name }
|
|
france = WorldCountry.where(code: "FR").first
|
|
@french_participants = WorldParticipant.where(world_country: france).sort_by { |p| p.name }
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def update
|
|
authorize :world_bet
|
|
|
|
@participant = WorldParticipant.find(params[:participant_id])
|
|
if @participant.update(world_participant_params)
|
|
participants_hash = {}
|
|
WorldParticipant.all.each do |p|
|
|
if p.score != nil
|
|
participants_hash[p.id] = p.score
|
|
end
|
|
end
|
|
WorldBet.all.each do |b|
|
|
old_score = b.score
|
|
b.compute_score(participants_hash)
|
|
if b.score != old_score
|
|
b.save
|
|
end
|
|
end
|
|
redirect_back(fallback_location: root_path, notice: t("worlds.collab.notice"))
|
|
else
|
|
redirect_to "/wjpc_bets/collab", notice: t("worlds.collab.error")
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def world_bet_params
|
|
params.expect(world_bet: [ :name ])
|
|
end
|
|
|
|
def world_participant_params
|
|
params.expect(world_participant: [ :qualifier_result, :semifinal_result, :final_result ])
|
|
end
|
|
end
|