module Public class TournamentPagesController < SiteBaseController include Public::LiveHelper layout "marketing_live" def index @tournaments = Tournament .visible_to_public .with_attached_logo_file .includes(:club) .order(starts_on: :desc, name: :asc) end def show @tournament = Tournament.find_by!(slug: params[:slug]) unless @tournament.published? || owner_access? raise ActiveRecord::RecordNotFound end @club = @tournament.club @owner_preview = !@tournament.published? && owner_access? @owner_manage = owner_access? @tab = params[:tab].to_s.presence_in(%w[risultati tabellone]) || "risultati" load_public_content! end private def owner_access? logged_in? && @tournament.club.owned_by?(current_user) end def load_public_content! @online_paths = fetch_online_paths @matches = @tournament.matches .includes( :tournament_group, :tournament_round, :stream_sessions, home_participant: { logo_file_attachment: :blob }, away_participant: { logo_file_attachment: :blob } ) .order(Arel.sql("scheduled_at ASC NULLS LAST"), created_at: :asc) broadcasting = StreamSession .broadcasting .includes(:score_state, match: [:home_participant, :away_participant, { team: :club }]) .where(match_id: @tournament.matches.select(:id)) .order(Arel.sql("started_at DESC NULLS LAST"), created_at: :desc) .to_a @live_by_match_id = {} broadcasting.each { |session| @live_by_match_id[session.match_id] ||= session } @live_sessions = broadcasting.select(&:public_watchable?) @recordings = Recording.ready.publicly_listed .joins(stream_session: :match) .where(matches: { tournament_id: @tournament.id }) .includes(stream_session: { match: [:home_participant, :away_participant] }) .order(recorded_at: :desc) @recording_by_match_id = {} @recordings.each do |rec| match_id = rec.stream_session.match_id @recording_by_match_id[match_id] ||= rec end @groups = @tournament.groups.order(:position) @standings_by_group = @groups.index_with { |group| Tournaments::Standings.call(group) } @rounds = @tournament.rounds.order(:position) @matches_by_round = @matches.select { |match| match.tournament_round_id.present? } .group_by(&:tournament_round_id) end def fetch_online_paths Mediamtx::Client.new.online_path_names rescue Mediamtx::Client::Error, Errno::ECONNREFUSED, SocketError [] end end end