|
| 1 | +class Api::V1::PlayersController < Api::V1::BaseController |
| 2 | + before_action :set_player, only: [:show, :update, :destroy, :stats, :matches] |
| 3 | + |
| 4 | + def index |
| 5 | + players = organization_scoped(Player).includes(:champion_pools) |
| 6 | + |
| 7 | + # Apply filters |
| 8 | + players = players.by_role(params[:role]) if params[:role].present? |
| 9 | + players = players.by_status(params[:status]) if params[:status].present? |
| 10 | + |
| 11 | + # Apply search |
| 12 | + if params[:search].present? |
| 13 | + search_term = "%#{params[:search]}%" |
| 14 | + players = players.where('summoner_name ILIKE ? OR real_name ILIKE ?', search_term, search_term) |
| 15 | + end |
| 16 | + |
| 17 | + # Pagination |
| 18 | + result = paginate(players.order(:role, :summoner_name)) |
| 19 | + |
| 20 | + render_success({ |
| 21 | + players: PlayerSerializer.render_as_hash(result[:data]), |
| 22 | + pagination: result[:pagination] |
| 23 | + }) |
| 24 | + end |
| 25 | + |
| 26 | + def show |
| 27 | + render_success({ |
| 28 | + player: PlayerSerializer.render_as_hash(@player) |
| 29 | + }) |
| 30 | + end |
| 31 | + |
| 32 | + def create |
| 33 | + player = organization_scoped(Player).new(player_params) |
| 34 | + player.organization = current_organization |
| 35 | + |
| 36 | + if player.save |
| 37 | + log_user_action( |
| 38 | + action: 'create', |
| 39 | + entity_type: 'Player', |
| 40 | + entity_id: player.id, |
| 41 | + new_values: player.attributes |
| 42 | + ) |
| 43 | + |
| 44 | + render_created({ |
| 45 | + player: PlayerSerializer.render_as_hash(player) |
| 46 | + }, message: 'Player created successfully') |
| 47 | + else |
| 48 | + render_error( |
| 49 | + message: 'Failed to create player', |
| 50 | + code: 'VALIDATION_ERROR', |
| 51 | + status: :unprocessable_entity, |
| 52 | + details: player.errors.as_json |
| 53 | + ) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def update |
| 58 | + old_values = @player.attributes.dup |
| 59 | + |
| 60 | + if @player.update(player_params) |
| 61 | + log_user_action( |
| 62 | + action: 'update', |
| 63 | + entity_type: 'Player', |
| 64 | + entity_id: @player.id, |
| 65 | + old_values: old_values, |
| 66 | + new_values: @player.attributes |
| 67 | + ) |
| 68 | + |
| 69 | + render_updated({ |
| 70 | + player: PlayerSerializer.render_as_hash(@player) |
| 71 | + }) |
| 72 | + else |
| 73 | + render_error( |
| 74 | + message: 'Failed to update player', |
| 75 | + code: 'VALIDATION_ERROR', |
| 76 | + status: :unprocessable_entity, |
| 77 | + details: @player.errors.as_json |
| 78 | + ) |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + def destroy |
| 83 | + if @player.destroy |
| 84 | + log_user_action( |
| 85 | + action: 'delete', |
| 86 | + entity_type: 'Player', |
| 87 | + entity_id: @player.id, |
| 88 | + old_values: @player.attributes |
| 89 | + ) |
| 90 | + |
| 91 | + render_deleted(message: 'Player deleted successfully') |
| 92 | + else |
| 93 | + render_error( |
| 94 | + message: 'Failed to delete player', |
| 95 | + code: 'DELETE_ERROR', |
| 96 | + status: :unprocessable_entity |
| 97 | + ) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + def stats |
| 102 | + # Get player statistics |
| 103 | + matches = @player.matches.order(game_start: :desc) |
| 104 | + recent_matches = matches.limit(20) |
| 105 | + player_stats = PlayerMatchStat.where(player: @player, match: matches) |
| 106 | + |
| 107 | + stats_data = { |
| 108 | + player: PlayerSerializer.render_as_hash(@player), |
| 109 | + overall: { |
| 110 | + total_matches: matches.count, |
| 111 | + wins: matches.victories.count, |
| 112 | + losses: matches.defeats.count, |
| 113 | + win_rate: calculate_player_win_rate(matches), |
| 114 | + avg_kda: calculate_player_avg_kda(player_stats), |
| 115 | + avg_cs: player_stats.average(:minions_killed)&.round(1) || 0, |
| 116 | + avg_vision_score: player_stats.average(:vision_score)&.round(1) || 0, |
| 117 | + avg_damage: player_stats.average(:total_damage_dealt)&.round(0) || 0 |
| 118 | + }, |
| 119 | + recent_form: { |
| 120 | + last_5_matches: calculate_recent_form(recent_matches.limit(5)), |
| 121 | + last_10_matches: calculate_recent_form(recent_matches.limit(10)) |
| 122 | + }, |
| 123 | + champion_pool: ChampionPoolSerializer.render_as_hash( |
| 124 | + @player.champion_pools.order(games_played: :desc).limit(5) |
| 125 | + ), |
| 126 | + performance_by_role: calculate_performance_by_role(player_stats) |
| 127 | + } |
| 128 | + |
| 129 | + render_success(stats_data) |
| 130 | + end |
| 131 | + |
| 132 | + def matches |
| 133 | + matches = @player.matches |
| 134 | + .includes(:player_match_stats) |
| 135 | + .order(game_start: :desc) |
| 136 | + |
| 137 | + # Filter by date range if provided |
| 138 | + if params[:start_date].present? && params[:end_date].present? |
| 139 | + matches = matches.in_date_range(params[:start_date], params[:end_date]) |
| 140 | + end |
| 141 | + |
| 142 | + result = paginate(matches) |
| 143 | + |
| 144 | + # Include player stats for each match |
| 145 | + matches_with_stats = result[:data].map do |match| |
| 146 | + player_stat = match.player_match_stats.find_by(player: @player) |
| 147 | + { |
| 148 | + match: MatchSerializer.render_as_hash(match), |
| 149 | + player_stats: player_stat ? PlayerMatchStatSerializer.render_as_hash(player_stat) : nil |
| 150 | + } |
| 151 | + end |
| 152 | + |
| 153 | + render_success({ |
| 154 | + matches: matches_with_stats, |
| 155 | + pagination: result[:pagination] |
| 156 | + }) |
| 157 | + end |
| 158 | + |
| 159 | + def import |
| 160 | + # This will be implemented when Riot API is ready |
| 161 | + render_error( |
| 162 | + message: 'Import functionality not yet implemented', |
| 163 | + code: 'NOT_IMPLEMENTED', |
| 164 | + status: :not_implemented |
| 165 | + ) |
| 166 | + end |
| 167 | + |
| 168 | + private |
| 169 | + |
| 170 | + def set_player |
| 171 | + @player = organization_scoped(Player).find(params[:id]) |
| 172 | + end |
| 173 | + |
| 174 | + def player_params |
| 175 | + params.require(:player).permit( |
| 176 | + :summoner_name, :real_name, :role, :status, :jersey_number, |
| 177 | + :birth_date, :country, :nationality, |
| 178 | + :contract_start_date, :contract_end_date, |
| 179 | + :solo_queue_tier, :solo_queue_rank, :solo_queue_lp, |
| 180 | + :solo_queue_wins, :solo_queue_losses, |
| 181 | + :flex_queue_tier, :flex_queue_rank, :flex_queue_lp, |
| 182 | + :peak_tier, :peak_rank, :peak_season, |
| 183 | + :riot_puuid, :riot_summoner_id, |
| 184 | + :twitter_handle, :twitch_channel, :instagram_handle, |
| 185 | + :notes |
| 186 | + ) |
| 187 | + end |
| 188 | + |
| 189 | + def calculate_player_win_rate(matches) |
| 190 | + return 0 if matches.empty? |
| 191 | + ((matches.victories.count.to_f / matches.count) * 100).round(1) |
| 192 | + end |
| 193 | + |
| 194 | + def calculate_player_avg_kda(stats) |
| 195 | + return 0 if stats.empty? |
| 196 | + |
| 197 | + total_kills = stats.sum(:kills) |
| 198 | + total_deaths = stats.sum(:deaths) |
| 199 | + total_assists = stats.sum(:assists) |
| 200 | + |
| 201 | + deaths = total_deaths.zero? ? 1 : total_deaths |
| 202 | + ((total_kills + total_assists).to_f / deaths).round(2) |
| 203 | + end |
| 204 | + |
| 205 | + def calculate_recent_form(matches) |
| 206 | + matches.map { |m| m.victory? ? 'W' : 'L' } |
| 207 | + end |
| 208 | + |
| 209 | + def calculate_performance_by_role(stats) |
| 210 | + stats.group(:role).select( |
| 211 | + 'role', |
| 212 | + 'COUNT(*) as games', |
| 213 | + 'AVG(kills) as avg_kills', |
| 214 | + 'AVG(deaths) as avg_deaths', |
| 215 | + 'AVG(assists) as avg_assists', |
| 216 | + 'AVG(performance_score) as avg_performance' |
| 217 | + ).map do |stat| |
| 218 | + { |
| 219 | + role: stat.role, |
| 220 | + games: stat.games, |
| 221 | + avg_kda: { |
| 222 | + kills: stat.avg_kills&.round(1) || 0, |
| 223 | + deaths: stat.avg_deaths&.round(1) || 0, |
| 224 | + assists: stat.avg_assists&.round(1) || 0 |
| 225 | + }, |
| 226 | + avg_performance: stat.avg_performance&.round(1) || 0 |
| 227 | + } |
| 228 | + end |
| 229 | + end |
| 230 | +end |
0 commit comments