76 lines
2.4 KiB
Ruby
76 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module EuchreCamp
|
|
module Actions
|
|
module Players
|
|
class Profile < 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 partnership statistics
|
|
partnerships = EuchreCamp::Services::PartnershipTracker.get_player_partnerships(player_id)
|
|
|
|
# Get recent games (last 10)
|
|
recent_games = get_recent_games(player_id)
|
|
|
|
response.render(view,
|
|
player: player,
|
|
partnerships: partnerships,
|
|
recent_games: recent_games
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def get_recent_games(player_id)
|
|
rom = EuchreCamp::App["persistence.rom"]
|
|
|
|
# Get matches where this player participated
|
|
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 }
|
|
)
|
|
)
|
|
.order(Sequel.desc(: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]]
|
|
]
|
|
|
|
# Determine if this player won
|
|
player_on_team_1 = match[:team_1_p1_id] == player_id || match[:team_1_p2_id] == player_id
|
|
won = player_on_team_1 ? (match[:team_1_score] > match[:team_2_score]) : (match[:team_2_score] > match[:team_1_score])
|
|
|
|
{
|
|
date: match[:played_at],
|
|
team_1: team_1_players,
|
|
team_2: team_2_players,
|
|
score: "#{match[:team_1_score]}-#{match[:team_2_score]}",
|
|
won: won,
|
|
player_partner: player_on_team_1 ? team_1_players.find { |p| p[:id] != player_id } : team_2_players.find { |p| p[:id] != player_id }
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|