feat(tournaments): add partnership tracking and tournament management

Implement comprehensive partnership tracking system and tournament
management features including round-robin, single/double elimination,
and Swiss system pairings.

Changes:
- Add partnership_games and partnership_stats tables
- Implement PartnershipTracker service for recording partnership data
- Add tournament generator with multiple formats
- Create tournament management actions and views
- Add acceptance tests for partnership tracking
- Fix PartnershipTracker to use single snapshot per player per match
- Fix round-robin test to query by round_number instead of hardcoded ID

Note: All 8 acceptance tests now passing
This commit is contained in:
2026-03-27 12:25:34 -07:00
parent cb47b9da99
commit ac858f1e6d
55 changed files with 2591 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class BracketMatchups < ::EuchreCamp::Repository[:bracket_matchups]
commands :create, update: :by_pk, delete: :by_pk
def by_event(event_id)
bracket_matchups.where(event_id: event_id).to_a
end
def by_round(round_id)
bracket_matchups.where(round_id: round_id).order(:bracket_position).to_a
end
def by_event_and_round(event_id, round_id)
bracket_matchups
.where(event_id: event_id, round_id: round_id)
.order(:bracket_position)
.to_a
end
end
end
end
+39
View File
@@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class EventParticipants < ::EuchreCamp::Repository[:event_participants]
commands :create, update: :by_pk, delete: :by_pk
def by_event(event_id)
event_participants.where(event_id: event_id).to_a
end
def players_by_event(event_id)
# Return player IDs registered for an event
event_participants
.where(event_id: event_id)
.select(:player_id)
.to_a
.map { |p| p[:player_id] }
end
def register_player(event_id, player_id, seed: nil)
create(
event_id: event_id,
player_id: player_id,
status: 'registered',
seed: seed
)
end
def remove_player(event_id, player_id)
event_participants
.where(event_id: event_id, player_id: player_id)
.delete
end
end
end
end
+27
View File
@@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class Events < ::EuchreCamp::Repository[:events]
commands :create, update: :by_pk, delete: :by_pk
def all
events.to_a
end
def by_id(id)
events.by_pk(id).one!
end
def upcoming
events.where(status: ['planned', 'active']).order(:event_date).to_a
end
def completed
events.where(status: 'completed').order(Sequel.desc(:event_date)).to_a
end
end
end
end
+30
View File
@@ -0,0 +1,30 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class PartnershipGames < ::EuchreCamp::Repository[:partnership_games]
commands :create, update: :by_pk, delete: :by_pk
def by_player(player_id)
partnership_games.where(
Sequel.|({ player_1_id: player_id }, { player_2_id: player_id })
).to_a
end
def by_match(match_id)
partnership_games.where(match_id: match_id).to_a
end
def by_partnership(player_1_id, player_2_id)
partnership_games.where(
Sequel.|(
{ player_1_id: player_1_id, player_2_id: player_2_id },
{ player_1_id: player_2_id, player_2_id: player_1_id }
)
).to_a
end
end
end
end
+51
View File
@@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class PartnershipStats < ::EuchreCamp::Repository[:partnership_stats]
commands :create, update: :by_pk
def find_or_create(player_1_id, player_2_id)
# Ensure consistent ordering (lower ID first)
p1_id = [player_1_id, player_2_id].min
p2_id = [player_1_id, player_2_id].max
stats = partnership_stats.where(player_1_id: p1_id, player_2_id: p2_id).one
return stats if stats
create(
player_1_id: p1_id,
player_2_id: p2_id,
games_played: 0,
wins: 0,
losses: 0,
total_elo_change: 0
)
end
def by_player(player_id)
partnership_stats.where(
Sequel.|({ player_1_id: player_id }, { player_2_id: player_id })
).to_a
end
def update_stats(player_1_id, player_2_id, won: false, elo_change: 0)
stats = find_or_create(player_1_id, player_2_id)
update_params = {
games_played: stats.games_played + 1,
wins: stats.wins + (won ? 1 : 0),
losses: stats.losses + (won ? 0 : 1),
total_elo_change: stats.total_elo_change + elo_change,
last_played: Time.now,
updated_at: Time.now
}
update(stats.id, update_params)
end
end
end
end
+32
View File
@@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class Teams < ::EuchreCamp::Repository[:teams]
commands :create, update: :by_pk, delete: :by_pk
def by_event(event_id)
teams.where(event_id: event_id).to_a
end
def find_or_create(event_id, player_1_id, player_2_id, team_name = nil)
existing = teams.where(
event_id: event_id,
player_1_id: [player_1_id, player_2_id],
player_2_id: [player_1_id, player_2_id]
).one
return existing if existing
create(
event_id: event_id,
player_1_id: player_1_id,
player_2_id: player_2_id,
team_name: team_name || "Team #{player_1_id}-#{player_2_id}"
)
end
end
end
end
+30
View File
@@ -0,0 +1,30 @@
# frozen_string_literal: true
require 'rom-repository'
module EuchreCamp
module Repositories
class TournamentRounds < ::EuchreCamp::Repository[:tournament_rounds]
commands :create, update: :by_pk, delete: :by_pk
def by_event(event_id)
tournament_rounds.where(event_id: event_id).order(:round_number).to_a
end
def current_round(event_id)
tournament_rounds
.where(event_id: event_id, status: 'active')
.order(Sequel.desc(:round_number))
.one
end
def next_round(event_id)
tournament_rounds
.where(event_id: event_id, status: 'pending')
.order(:round_number)
.limit(1)
.one
end
end
end
end