Class: Sashite::Pcn::Game::Sides
- Inherits:
-
Object
- Object
- Sashite::Pcn::Game::Sides
- Defined in:
- lib/sashite/pcn/game/sides.rb,
lib/sashite/pcn/game/sides/player.rb
Overview
Represents player information for both sides of a game
Manages two Player objects (first and second) with support for player metadata, styles, and time control settings. Both players are optional and default to empty player objects.
Defined Under Namespace
Classes: Player
Constant Summary collapse
- ERROR_INVALID_FIRST =
Error messages
"first must be a hash"- ERROR_INVALID_SECOND =
"second must be a hash"
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality with another Sides object.
-
#[](index) ⇒ Player?
Access player by index.
-
#both_have_time_control? ⇒ Boolean
Check if both players have time control.
-
#complete? ⇒ Boolean
Check if both players have information.
-
#each {|player| ... } ⇒ Enumerator
Iterate over both players.
-
#elos ⇒ Array<Integer>
Get both players’ Elo ratings.
-
#empty? ⇒ Boolean
Check if no player information is present.
-
#first ⇒ Player
Get first player information.
-
#has_player?(side) ⇒ Boolean
Check if a specific side is present.
-
#hash ⇒ Integer
Hash code for use in collections.
-
#initialize(first: {}, second: {}) ⇒ Sides
constructor
Create a new Sides instance.
-
#inspect ⇒ String
String representation for debugging.
-
#map {|player| ... } ⇒ Array
Map over both players.
-
#mixed_time_control? ⇒ Boolean
Check if players have different time controls.
-
#names ⇒ Array<String>
Get both players’ names.
-
#player(side) ⇒ Player?
Get player by side.
-
#second ⇒ Player
Get second player information.
-
#styles ⇒ Array<String>
Get both players’ styles.
-
#symmetric_time_control? ⇒ Boolean
Check if both players have same time control.
-
#time_budgets ⇒ Array<Integer>
Get both players’ time budgets.
-
#time_control_description ⇒ String?
Get time control description.
-
#to_a ⇒ Array<Player>
Get array of both players.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#unlimited_game? ⇒ Boolean
Check if neither player has time control.
Constructor Details
#initialize(first: {}, second: {}) ⇒ Sides
Create a new Sides instance
65 66 67 68 69 70 71 72 73 |
# File 'lib/sashite/pcn/game/sides.rb', line 65 def initialize(first: {}, second: {}) raise ::ArgumentError, ERROR_INVALID_FIRST unless first.is_a?(::Hash) raise ::ArgumentError, ERROR_INVALID_SECOND unless second.is_a?(::Hash) @first = Player.new(**first.transform_keys(&:to_sym)) @second = Player.new(**second.transform_keys(&:to_sym)) freeze end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality with another Sides object
348 349 350 351 352 |
# File 'lib/sashite/pcn/game/sides.rb', line 348 def ==(other) return false unless other.is_a?(self.class) @first == other.first && @second == other.second end |
#[](index) ⇒ Player?
Access player by index
108 109 110 111 112 113 |
# File 'lib/sashite/pcn/game/sides.rb', line 108 def [](index) case index when 0 then @first when 1 then @second end end |
#both_have_time_control? ⇒ Boolean
Check if both players have time control
166 167 168 |
# File 'lib/sashite/pcn/game/sides.rb', line 166 def both_have_time_control? @first.has_time_control? && @second.has_time_control? end |
#complete? ⇒ Boolean
Check if both players have information
146 147 148 |
# File 'lib/sashite/pcn/game/sides.rb', line 146 def complete? !@first.empty? && !@second.empty? end |
#each {|player| ... } ⇒ Enumerator
Iterate over both players
264 265 266 267 268 269 |
# File 'lib/sashite/pcn/game/sides.rb', line 264 def each return enum_for(:each) unless block_given? yield @first yield @second end |
#elos ⇒ Array<Integer>
Get both players’ Elo ratings
226 227 228 |
# File 'lib/sashite/pcn/game/sides.rb', line 226 def elos [@first.elo, @second.elo] end |
#empty? ⇒ Boolean
Check if no player information is present
136 137 138 |
# File 'lib/sashite/pcn/game/sides.rb', line 136 def empty? @first.empty? && @second.empty? end |
#first ⇒ Player
Get first player information
83 84 85 |
# File 'lib/sashite/pcn/game/sides.rb', line 83 def first @first end |
#has_player?(side) ⇒ Boolean
Check if a specific side is present
371 372 373 374 |
# File 'lib/sashite/pcn/game/sides.rb', line 371 def has_player?(side) player = player(side) player && !player.empty? end |
#hash ⇒ Integer
Hash code for use in collections
359 360 361 |
# File 'lib/sashite/pcn/game/sides.rb', line 359 def hash [@first, @second].hash end |
#inspect ⇒ String
String representation for debugging
336 337 338 339 340 341 342 |
# File 'lib/sashite/pcn/game/sides.rb', line 336 def inspect players = [] players << "first=#{@first.name || '(empty)'}" players << "second=#{@second.name || '(empty)'}" "#<#{self.class.name} #{players.join(' ')}>" end |
#map {|player| ... } ⇒ Array
Map over both players
279 280 281 282 283 |
# File 'lib/sashite/pcn/game/sides.rb', line 279 def map(&) return enum_for(:map) unless block_given? [@first, @second].map(&) end |
#mixed_time_control? ⇒ Boolean
Check if players have different time controls
Returns true when the two players have different time control settings. This is the logical opposite of symmetric_time_control?.
206 207 208 |
# File 'lib/sashite/pcn/game/sides.rb', line 206 def mixed_time_control? !symmetric_time_control? end |
#names ⇒ Array<String>
Get both players’ names
216 217 218 |
# File 'lib/sashite/pcn/game/sides.rb', line 216 def names [@first.name, @second.name] end |
#player(side) ⇒ Player?
Get player by side
123 124 125 126 127 128 |
# File 'lib/sashite/pcn/game/sides.rb', line 123 def player(side) case side.to_sym when :first then @first when :second then @second end end |
#second ⇒ Player
Get second player information
95 96 97 |
# File 'lib/sashite/pcn/game/sides.rb', line 95 def second @second end |
#styles ⇒ Array<String>
Get both players’ styles
236 237 238 239 240 241 |
# File 'lib/sashite/pcn/game/sides.rb', line 236 def styles [ @first.style&.to_s, @second.style&.to_s ] end |
#symmetric_time_control? ⇒ Boolean
Check if both players have same time control
156 157 158 |
# File 'lib/sashite/pcn/game/sides.rb', line 156 def symmetric_time_control? @first.periods == @second.periods end |
#time_budgets ⇒ Array<Integer>
Get both players’ time budgets
249 250 251 252 253 254 |
# File 'lib/sashite/pcn/game/sides.rb', line 249 def time_budgets [ @first.initial_time_budget, @second.initial_time_budget ] end |
#time_control_description ⇒ String?
Get time control description
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/sashite/pcn/game/sides.rb', line 386 def time_control_description if unlimited_game? "Unlimited time" elsif mixed_time_control? first_tc = describe_periods(@first.periods) second_tc = describe_periods(@second.periods) "Mixed: first #{first_tc}, second #{second_tc}" elsif symmetric_time_control? tc = describe_periods(@first.periods) "#{tc} (symmetric)" else first_tc = describe_periods(@first.periods) second_tc = describe_periods(@second.periods) "First: #{first_tc}, Second: #{second_tc}" end end |
#to_a ⇒ Array<Player>
Get array of both players
291 292 293 |
# File 'lib/sashite/pcn/game/sides.rb', line 291 def to_a [@first, @second] end |
#to_h ⇒ Hash
Convert to hash representation
Returns a hash containing only non-empty player objects. If both players are empty, returns an empty hash.
326 327 328 329 330 331 |
# File 'lib/sashite/pcn/game/sides.rb', line 326 def to_h result = {} result[:first] = @first.to_h unless @first.empty? result[:second] = @second.to_h unless @second.empty? result end |
#unlimited_game? ⇒ Boolean
Check if neither player has time control
176 177 178 |
# File 'lib/sashite/pcn/game/sides.rb', line 176 def unlimited_game? @first.unlimited_time? && @second.unlimited_time? end |