33# Represents an internal practice session where an organization's own players
44# compete against each other in a controlled environment.
55#
6- # An inhouse goes through three phases:
6+ # An inhouse goes through four phases:
77# waiting — lobby open, players joining
8- # in_progress — teams balanced, games being played
8+ # draft — captain draft in progress (picks 1-2-2-2-1)
9+ # in_progress — teams set, games being played
910# done — session closed
1011#
1112class Inhouse < ApplicationRecord
13+ PICK_ORDER = %w[ blue red red blue blue red red blue ] . freeze
14+
1215 # Associations
1316 belongs_to :organization
1417 belongs_to :created_by , class_name : 'User' , foreign_key : :created_by_user_id
18+ belongs_to :blue_captain , class_name : 'Player' , optional : true
19+ belongs_to :red_captain , class_name : 'Player' , optional : true
1520 has_many :inhouse_participations , dependent : :destroy
1621 has_many :players , through : :inhouse_participations
1722
1823 # Enum
19- enum :status , { waiting : 'waiting' , in_progress : 'in_progress' , done : 'done' } , prefix : false
24+ enum :status , { waiting : 'waiting' , draft : 'draft' , in_progress : 'in_progress' , done : 'done' } , prefix : false
2025
2126 # Scopes
22- scope :active , -> { where ( status : %w[ waiting in_progress ] ) }
27+ scope :active , -> { where ( status : %w[ waiting draft in_progress ] ) }
2328 scope :history , -> { where ( status : 'done' ) }
2429 scope :recent , -> { order ( created_at : :desc ) }
2530
@@ -28,13 +33,29 @@ class Inhouse < ApplicationRecord
2833
2934 validate :valid_status_transition , on : :update
3035
36+ # Returns which team should pick next during draft ('blue' or 'red').
37+ # Returns nil if draft is not active or all picks are done.
38+ def current_pick_team
39+ return nil unless draft?
40+ return nil if draft_pick_number . nil?
41+ return nil if draft_pick_number >= PICK_ORDER . size
42+
43+ PICK_ORDER [ draft_pick_number ]
44+ end
45+
46+ # True when all 8 non-captain picks have been made.
47+ def draft_complete?
48+ draft_pick_number . to_i >= PICK_ORDER . size
49+ end
50+
3151 private
3252
3353 def valid_status_transition
3454 return unless status_changed?
3555
3656 allowed = {
37- 'waiting' => %w[ in_progress done ] ,
57+ 'waiting' => %w[ draft in_progress done ] ,
58+ 'draft' => %w[ in_progress done ] ,
3859 'in_progress' => %w[ done ] ,
3960 'done' => [ ]
4061 }
0 commit comments