# frozen_string_literal: true module EuchreCamp module Actions module Players class Schedule < EuchreCamp::Action include Deps[players_repo: 'repositories.players'] def handle(request, response) player_id = request.params[:id] player = players_repo.by_id(player_id) # Get upcoming matches for this player upcoming_matches = get_upcoming_matches(player_id) # Get active tournaments for this player active_tournaments = get_active_tournaments(player_id) # Get tournament schedule tournament_schedule = get_tournament_schedule(player_id) response.render(view, player: player, upcoming_matches: upcoming_matches, active_tournaments: active_tournaments, tournament_schedule: tournament_schedule ) end private def get_upcoming_matches(player_id) rom = EuchreCamp::App["persistence.rom"] # Get upcoming matches where this player participates matches = rom.relations[:matches] .where( Sequel.|( { team_1_p1_id: player_id }, { team_1_p2_id: player_id }, { team_2_p1_id: player_id }, { team_2_p2_id: player_id } ) ) .where(status: 'scheduled') .order(:played_at) .limit(10) .to_a # Get player names for display all_players = rom.relations[:players].to_a.to_h { |p| [p[:id], p] } matches.map do |match| team_1_players = [ all_players[match[:team_1_p1_id]], all_players[match[:team_1_p2_id]] ] team_2_players = [ all_players[match[:team_2_p1_id]], all_players[match[:team_2_p2_id]] ] player_on_team_1 = match[:team_1_p1_id] == player_id || match[:team_1_p2_id] == player_id { id: match[:id], date: match[:played_at], team_1: team_1_players, team_2: team_2_players, opponent_team: player_on_team_1 ? team_2_players : team_1_players, player_team: player_on_team_1 ? team_1_players : team_2_players, event_id: match[:event_id] } end end def get_active_tournaments(player_id) rom = EuchreCamp::App["persistence.rom"] # Get tournaments where player is participating event_ids = rom.relations[:event_participants] .where(player_id: player_id) .select(:event_id) .to_a .map { |e| e[:event_id] } return [] if event_ids.empty? # Get active tournaments rom.relations[:events] .where(id: event_ids, status: 'active') .to_a end def get_tournament_schedule(player_id) rom = EuchreCamp::App["persistence.rom"] # Get all tournaments where player participates event_ids = rom.relations[:event_participants] .where(player_id: player_id) .select(:event_id) .to_a .map { |e| e[:event_id] } return [] if event_ids.empty? # Get tournaments and their schedules tournaments = rom.relations[:events] .where(id: event_ids) .to_a tournaments.map do |tournament| # Get tournament rounds rounds = rom.relations[:tournament_rounds] .where(event_id: tournament[:id]) .order(:round_number) .to_a { tournament: tournament, rounds: rounds } end end end end end end