Class: HandData

Inherits:
Object
  • Object
show all
Defined in:
lib/acpc_dealer_data/hand_data.rb

Defined Under Namespace

Classes: Turn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match_def, action_data, result) ⇒ HandData

Returns a new instance of HandData.



38
39
40
41
42
43
44
45
46
47
# File 'lib/acpc_dealer_data/hand_data.rb', line 38

def initialize(match_def, action_data, result)
  @match_def = match_def
  
  set_chip_distribution! result

  set_data! action_data

  @turn_number = nil
  @seat = nil
end

Instance Attribute Details

#chip_distributionObject (readonly)

Returns the value of attribute chip_distribution.



17
18
19
# File 'lib/acpc_dealer_data/hand_data.rb', line 17

def chip_distribution
  @chip_distribution
end

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/acpc_dealer_data/hand_data.rb', line 17

def data
  @data
end

#match_defObject (readonly)

Returns the value of attribute match_def.



17
18
19
# File 'lib/acpc_dealer_data/hand_data.rb', line 17

def match_def
  @match_def
end

#seatObject (readonly)

Returns the value of attribute seat.



17
18
19
# File 'lib/acpc_dealer_data/hand_data.rb', line 17

def seat
  @seat
end

#turn_numberObject (readonly)

Returns the value of attribute turn_number.



17
18
19
# File 'lib/acpc_dealer_data/hand_data.rb', line 17

def turn_number
  @turn_number
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
52
53
# File 'lib/acpc_dealer_data/hand_data.rb', line 49

def ==(other)
  @match_def == other.match_def &&
  @chip_distribution == other.chip_distribution &&
  @data == other.data
end

#current_match_state(seat = @seat) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/acpc_dealer_data/hand_data.rb', line 67

def current_match_state(seat=@seat)
  if @turn_number
    @data[@turn_number].state_messages[seat]
  else
    nil
  end
end

#final_turn?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/acpc_dealer_data/hand_data.rb', line 100

def final_turn?
  if @turn_number
    @turn_number >= @data.length - 1
  else
    nil
  end
end

#for_every_turn!(seat = 0) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/acpc_dealer_data/hand_data.rb', line 55

def for_every_turn!(seat=0)
  @seat = seat
  @data.each_index do |i|
    @turn_number = i

    yield @turn_number
  end

  @turn_number = nil
  self
end

#last_actionObject



92
93
94
95
96
97
98
# File 'lib/acpc_dealer_data/hand_data.rb', line 92

def last_action
  if @turn_number && @turn_number != 0
    @data[@turn_number-1].action_message
  else
    nil
  end
end

#last_match_state(seat = @seat) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/acpc_dealer_data/hand_data.rb', line 84

def last_match_state(seat=@seat)
  if @turn_number && @turn_number != 0
    @data[@turn_number-1].state_messages[seat]
  else
    nil
  end
end

#next_actionActionMessage

Returns Next action in the hand to be taken in the current turn.

Returns:

  • (ActionMessage)

    Next action in the hand to be taken in the current turn.



76
77
78
79
80
81
82
# File 'lib/acpc_dealer_data/hand_data.rb', line 76

def next_action
  if @turn_number
    @data[@turn_number].action_message
  else
    nil
  end
end

#set_chip_distribution!(result) ⇒ Object (protected)



110
111
112
113
114
115
116
117
118
119
# File 'lib/acpc_dealer_data/hand_data.rb', line 110

def set_chip_distribution!(result)
  @chip_distribution = []
  result.each do |player_name, amount|
    begin
      @chip_distribution[@match_def.player_names.index(player_name.to_s)] = amount
    rescue TypeError
      raise PlayerNamesDoNotMatch
    end
  end
end

#set_data!(action_data) ⇒ Object (protected)



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/acpc_dealer_data/hand_data.rb', line 121

def set_data!(action_data)
  number_of_state_messages = @match_def.game_def.number_of_players

  @data = []
  message_number = 0
  while message_number < action_data.length
    state_messages = action_data[message_number..message_number+number_of_state_messages-1]

    assert_messages_have_no_actions state_messages

    state_messages = process_state_messages state_messages

    assert_messages_are_well_defined state_messages

    message_number += number_of_state_messages

    action_message = if action_data.in_bounds?(message_number) && 
      action_data[message_number].respond_to?(:action)

      message_number += 1
      action_data[message_number-1]
    else
      assert_message_is_from_final_turn action_data, message_number, state_messages

      nil
    end

    @data << Turn.new(state_messages, action_message)
  end
end