acpc_dealer_data 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -81,15 +81,18 @@ class ActionMessages
81
81
  end
82
82
  end
83
83
 
84
- def self.parse_file(acpc_log_file_path, player_names, game_def_directory)
85
- File.open(acpc_log_file_path, 'r') do |file|
86
- ActionMessages.parse file, player_names, game_def_directory
84
+ class LogFile < File
85
+ end
86
+
87
+ def self.parse_file(acpc_log_file_path, player_names, game_def_directory, num_hands=nil)
88
+ LogFile.open(acpc_log_file_path, 'r') do |file|
89
+ ActionMessages.parse file, player_names, game_def_directory, num_hands
87
90
  end
88
91
  end
89
92
 
90
93
  alias_new :parse
91
94
 
92
- def initialize(acpc_log_statements, player_names, game_def_directory)
95
+ def initialize(acpc_log_statements, player_names, game_def_directory, num_hands=nil)
93
96
  @final_score = nil
94
97
  @match_def = nil
95
98
  @data = acpc_log_statements.inject([]) do |accumulating_data, log_line|
@@ -102,6 +105,7 @@ class ActionMessages
102
105
  accumulating_data.empty? ||
103
106
  accumulating_data.last.first[:state].hand_number != parsed_message[:state].hand_number
104
107
  )
108
+ break accumulating_data if accumulating_data.length == num_hands
105
109
  accumulating_data << []
106
110
  end
107
111
 
@@ -41,15 +41,18 @@ class HandResults
41
41
  end
42
42
  end
43
43
 
44
- def self.parse_file(acpc_log_file_path, player_names, game_def_directory)
45
- File.open(acpc_log_file_path, 'r') do |file|
46
- HandResults.parse file, player_names, game_def_directory
44
+ class LogFile < File
45
+ end
46
+
47
+ def self.parse_file(acpc_log_file_path, player_names, game_def_directory, num_hands=nil)
48
+ LogFile.open(acpc_log_file_path, 'r') do |file|
49
+ HandResults.parse file, player_names, game_def_directory, num_hands
47
50
  end
48
51
  end
49
52
 
50
53
  alias_new :parse
51
54
 
52
- def initialize(acpc_log_statements, player_names, game_def_directory)
55
+ def initialize(acpc_log_statements, player_names, game_def_directory, num_hands=nil)
53
56
  @final_score = nil
54
57
  @match_def = nil
55
58
 
@@ -60,6 +63,7 @@ class HandResults
60
63
  parsed_message = HandResults.parse_state(log_line)
61
64
  if parsed_message
62
65
  accumulating_data << parsed_message
66
+ break accumulating_data if accumulating_data.length == num_hands
63
67
  else
64
68
  @final_score = HandResults.parse_score(log_line) unless @final_score
65
69
  end
@@ -32,19 +32,57 @@ class PokerMatchData
32
32
  )
33
33
 
34
34
  # @returns [PokerMatchData]
35
- def self.parse_files(action_messages_file, result_messages_file, player_names, dealer_directory)
36
- parsed_action_messages = Celluloid::Future.new { ActionMessages.parse_file action_messages_file, player_names, dealer_directory }
37
- parsed_hand_results = Celluloid::Future.new { HandResults.parse_file result_messages_file, player_names, dealer_directory }
35
+ def self.parse_files(action_messages_file, result_messages_file, player_names, dealer_directory, num_hands=nil)
36
+ parsed_action_messages = Celluloid::Future.new do
37
+ ActionMessages.parse_file(
38
+ action_messages_file,
39
+ player_names,
40
+ dealer_directory,
41
+ num_hands
42
+ )
43
+ end
44
+ parsed_hand_results = Celluloid::Future.new do
45
+ HandResults.parse_file(
46
+ result_messages_file,
47
+ player_names,
48
+ dealer_directory,
49
+ num_hands
50
+ )
51
+ end
38
52
 
39
- PokerMatchData.new parsed_action_messages.value, parsed_hand_results.value, player_names, dealer_directory
53
+ PokerMatchData.new(
54
+ parsed_action_messages.value,
55
+ parsed_hand_results.value,
56
+ player_names,
57
+ dealer_directory
58
+ )
40
59
  end
41
60
 
42
61
  # @returns [PokerMatchData]
43
- def self.parse(action_messages, result_messages, player_names, dealer_directory)
44
- parsed_action_messages = Celluloid::Future.new { ActionMessages.parse action_messages, player_names, dealer_directory }
45
- parsed_hand_results = Celluloid::Future.new { HandResults.parse result_messages, player_names, dealer_directory }
62
+ def self.parse(action_messages, result_messages, player_names, dealer_directory, num_hands=nil)
63
+ parsed_action_messages = Celluloid::Future.new do
64
+ ActionMessages.parse(
65
+ action_messages,
66
+ player_names,
67
+ dealer_directory,
68
+ num_hands
69
+ )
70
+ end
71
+ parsed_hand_results = Celluloid::Future.new do
72
+ HandResults.parse(
73
+ result_messages,
74
+ player_names,
75
+ dealer_directory,
76
+ num_hands
77
+ )
78
+ end
46
79
 
47
- PokerMatchData.new parsed_action_messages.value, parsed_hand_results.value, player_names, dealer_directory
80
+ PokerMatchData.new(
81
+ parsed_action_messages.value,
82
+ parsed_hand_results.value,
83
+ player_names,
84
+ dealer_directory
85
+ )
48
86
  end
49
87
 
50
88
  def initialize(parsed_action_messages, parsed_hand_results, player_names, dealer_directory)
@@ -57,8 +95,6 @@ class PokerMatchData
57
95
  end
58
96
 
59
97
  if (
60
- parsed_action_messages.final_score.nil? ||
61
- parsed_hand_results.final_score.nil? ||
62
98
  parsed_action_messages.final_score != parsed_hand_results.final_score
63
99
  )
64
100
  raise FinalScoresDoNotMatch
@@ -66,7 +102,9 @@ class PokerMatchData
66
102
 
67
103
  @match_def = parsed_hand_results.match_def
68
104
 
69
- set_chip_distribution! parsed_hand_results.final_score
105
+ if parsed_hand_results.final_score
106
+ set_chip_distribution! parsed_hand_results.final_score
107
+ end
70
108
 
71
109
  set_data! parsed_action_messages, parsed_hand_results
72
110
 
@@ -96,7 +134,7 @@ class PokerMatchData
96
134
  def all_in?(seat=@seat) @players[seat].all_in? end
97
135
  def active?(seat=@seat) @players[seat].active? end
98
136
 
99
- def for_every_hand!(num_hands=@data.length)
137
+ def for_every_hand!
100
138
  initialize_players!
101
139
 
102
140
  @data.each_index do |i|
@@ -111,11 +149,9 @@ class PokerMatchData
111
149
  end
112
150
 
113
151
  yield @hand_number
114
-
115
- break if @hand_number + 1 >= num_hands
116
152
  end
117
153
 
118
- if @hand_number == @data.length && @chip_distribution != @players.map { |p| p.chip_balance }
154
+ if @chip_distribution && @chip_distribution != @players.map { |p| p.chip_balance }
119
155
  raise PlayerDataInconsistent, "chip distribution: #{@chip_distribution}, player balances: #{@players.map { |p| p.chip_balance }}"
120
156
  end
121
157
 
@@ -1,3 +1,3 @@
1
1
  module AcpcDealerData
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -18,6 +18,7 @@ describe ActionMessages do
18
18
  @patient = nil
19
19
  @match_def = nil
20
20
  @player_names = nil
21
+ @no_final_score = false
21
22
  end
22
23
 
23
24
  describe '::parse_to_message' do
@@ -109,46 +110,96 @@ describe ActionMessages do
109
110
  end
110
111
 
111
112
  describe 'properly parses ACPC log statements' do
112
- it 'from file' do
113
- skip "Not sure how to test this easily when GameDefinition needs to open a file as well"
113
+ describe 'from file' do
114
+ it 'when every hand is desired' do
115
+ init_data do |action_messages|
116
+ file_name = 'file_name'
117
+ ActionMessages::LogFile.expects(:open).with(file_name, 'r').yields(
118
+ action_messages
119
+ ).returns(
120
+ ActionMessages.parse(
121
+ action_messages,
122
+ @player_names,
123
+ AcpcDealer::DEALER_DIRECTORY
124
+ )
125
+ )
114
126
 
115
- init_data do |action_messages|
116
- file_name = 'file_name'
117
- File.expects(:open).with(file_name, 'r').yields(
118
- action_messages
119
- ).returns(
120
- ActionMessages.parse(
121
- action_messages,
127
+ @patient = ActionMessages.parse_file(
128
+ file_name,
122
129
  @player_names,
123
130
  AcpcDealer::DEALER_DIRECTORY
124
131
  )
125
- )
126
132
 
127
- @patient = ActionMessages.parse_file(
128
- file_name,
129
- @player_names,
130
- AcpcDealer::DEALER_DIRECTORY
131
- )
133
+ check_patient
134
+ end
135
+ end
136
+ it 'when a particular number of hands is desired' do
137
+ @no_final_score = true
138
+ num_hands = 1
139
+ init_data do |action_messages|
140
+ file_name = 'file_name'
141
+ ActionMessages::LogFile.expects(:open).with(file_name, 'r').yields(
142
+ action_messages
143
+ ).returns(
144
+ ActionMessages.parse(
145
+ action_messages,
146
+ @player_names,
147
+ AcpcDealer::DEALER_DIRECTORY,
148
+ num_hands
149
+ )
150
+ )
151
+
152
+ @patient = ActionMessages.parse_file(
153
+ file_name,
154
+ @player_names,
155
+ AcpcDealer::DEALER_DIRECTORY,
156
+ num_hands
157
+ )
158
+
159
+ # Fix data to check patient against
160
+ @final_score = nil
161
+ @data = @data[0..num_hands-1]
132
162
 
133
- check_patient
163
+ check_patient
164
+ end
134
165
  end
135
166
  end
136
- it 'from array' do
137
- init_data do |action_messages|
138
- @patient = ActionMessages.parse(
139
- action_messages,
140
- @player_names,
141
- AcpcDealer::DEALER_DIRECTORY
142
- )
167
+ describe 'from array' do
168
+ it 'when all hands are desired' do
169
+ init_data do |action_messages|
170
+ @patient = ActionMessages.parse(
171
+ action_messages,
172
+ @player_names,
173
+ AcpcDealer::DEALER_DIRECTORY
174
+ )
143
175
 
144
- check_patient
176
+ check_patient
177
+ end
178
+ end
179
+ it 'when a particular number of hands is desired' do
180
+ @no_final_score = true
181
+ num_hands = 1
182
+ init_data do |log_statements|
183
+ @patient = ActionMessages.parse(
184
+ log_statements,
185
+ @player_names,
186
+ AcpcDealer::DEALER_DIRECTORY,
187
+ num_hands
188
+ )
189
+
190
+ # Fix data to check patient against
191
+ @final_score = nil
192
+ @data = @data[0..num_hands-1]
193
+
194
+ check_patient
195
+ end
145
196
  end
146
197
  end
147
198
  end
148
199
 
149
200
  def check_patient
150
201
  @patient.data.must_equal @data
151
- @patient.final_score.must_equal @final_score
202
+ @patient.final_score.must_equal @final_score unless @no_final_score
152
203
  @patient.match_def.must_equal @match_def
153
204
  end
154
205
 
@@ -16,6 +16,7 @@ describe HandResults do
16
16
  @final_score = nil
17
17
  @player_names = nil
18
18
  @match_def = nil
19
+ @no_final_score = false
19
20
  end
20
21
 
21
22
  describe '::parse_state' do
@@ -56,36 +57,96 @@ describe HandResults do
56
57
  end
57
58
 
58
59
  describe 'properly parses ACPC log statements' do
59
- it 'from file' do
60
- skip "Not sure how to test this easily when GameDefinition needs to open a file as well"
60
+ describe 'from file' do
61
+ it 'when every hand is desired' do
62
+ init_data do |log_statements|
63
+ file_name = 'file_name'
64
+ HandResults::LogFile.stubs(:open).with(file_name, 'r').yields(
65
+ log_statements
66
+ ).returns(
67
+ HandResults.parse(
68
+ log_statements,
69
+ @player_names,
70
+ AcpcDealer::DEALER_DIRECTORY
71
+ )
72
+ )
61
73
 
62
- init_data do |log_statements|
63
- file_name = 'file_name'
64
- File.stubs(:open).with(file_name, 'r').yields(
65
- log_statements
66
- ).returns(HandResults.parse(log_statements))
74
+ @patient = HandResults.parse_file(
75
+ file_name,
76
+ @player_names,
77
+ AcpcDealer::DEALER_DIRECTORY
78
+ )
67
79
 
68
- @patient = HandResults.parse_file file_name
80
+ check_patient
81
+ end
82
+ end
83
+ it 'when a particular number of hands is desired' do
84
+ @no_final_score = true
85
+ num_hands = 3
86
+ init_data do |log_statements|
87
+ file_name = 'file_name'
88
+ HandResults::LogFile.stubs(:open).with(file_name, 'r').yields(
89
+ log_statements
90
+ ).returns(
91
+ HandResults.parse(
92
+ log_statements,
93
+ @player_names,
94
+ AcpcDealer::DEALER_DIRECTORY,
95
+ num_hands
96
+ )
97
+ )
98
+
99
+ @patient = HandResults.parse_file(
100
+ file_name,
101
+ @player_names,
102
+ AcpcDealer::DEALER_DIRECTORY,
103
+ num_hands
104
+ )
69
105
 
70
- check_patient
106
+ # Fix data to check patient against
107
+ @final_score = nil
108
+ @data = @data[0..num_hands-1]
109
+
110
+ check_patient
111
+ end
71
112
  end
72
113
  end
73
- it 'from array' do
74
- init_data do |log_statements|
75
- @patient = HandResults.parse(
76
- log_statements,
77
- @player_names,
78
- AcpcDealer::DEALER_DIRECTORY
79
- )
80
-
81
- check_patient
114
+ describe 'from array' do
115
+ it 'when every hand is desired' do
116
+ init_data do |log_statements|
117
+ @patient = HandResults.parse(
118
+ log_statements,
119
+ @player_names,
120
+ AcpcDealer::DEALER_DIRECTORY
121
+ )
122
+
123
+ check_patient
124
+ end
125
+ end
126
+ it 'when a particular number of hands is desired' do
127
+ @no_final_score = true
128
+ num_hands = 3
129
+ init_data do |log_statements|
130
+ @patient = HandResults.parse(
131
+ log_statements,
132
+ @player_names,
133
+ AcpcDealer::DEALER_DIRECTORY,
134
+ num_hands
135
+ )
136
+
137
+ # Fix data to check patient against
138
+ @final_score = nil
139
+ @data = @data[0..num_hands-1]
140
+
141
+ check_patient
142
+ end
82
143
  end
83
144
  end
84
145
  end
85
146
 
86
147
  def check_patient
87
148
  @patient.data.must_equal @data
88
- @patient.final_score.must_equal @final_score
149
+ @patient.final_score.must_equal @final_score unless @no_final_score
89
150
  @patient.match_def.must_equal @match_def
90
151
  end
91
152
 
@@ -22,6 +22,7 @@ describe PokerMatchData do
22
22
  @hand_number = nil
23
23
  @hand_data_list = nil
24
24
  @final_hand = nil
25
+ @no_chip_distribution = false
25
26
  end
26
27
 
27
28
  describe 'when given action and result messages' do
@@ -85,25 +86,53 @@ describe PokerMatchData do
85
86
  end
86
87
  end
87
88
  end
88
- it 'works properly on every hand' do
89
- init_data do |action_messages, result_messages|
90
-
91
- @patient = PokerMatchData.parse(
92
- action_messages,
93
- result_messages,
94
- @player_names,
95
- AcpcDealer::DEALER_DIRECTORY
96
- )
97
- @patient.seat = 0
89
+ describe 'works properly' do
90
+ it 'for every hand' do
91
+ init_data do |action_messages, result_messages|
92
+
93
+ @patient = PokerMatchData.parse(
94
+ action_messages,
95
+ result_messages,
96
+ @player_names,
97
+ AcpcDealer::DEALER_DIRECTORY
98
+ )
99
+ @patient.seat = 0
98
100
 
99
- @hand_number = 0
100
- @patient.for_every_hand! do
101
- @final_hand = @hand_number >= @hand_data_list.length - 1
102
- @patient.for_every_turn! do
103
- check_patient
101
+ @hand_number = 0
102
+ @patient.for_every_hand! do
103
+ @final_hand = @hand_number >= @hand_data_list.length - 1
104
+ @patient.for_every_turn! do
105
+ check_patient
106
+ end
107
+
108
+ @hand_number += 1
104
109
  end
110
+ end
111
+ end
112
+ it 'for a particular number of hands' do
113
+ num_hands = 1
114
+ @no_chip_distribution = true
115
+ init_data(num_hands) do |action_messages, result_messages|
116
+ @chip_distribution = nil
117
+
118
+ @patient = PokerMatchData.parse(
119
+ action_messages,
120
+ result_messages,
121
+ @player_names,
122
+ AcpcDealer::DEALER_DIRECTORY,
123
+ num_hands
124
+ )
125
+ @patient.seat = 0
126
+
127
+ @hand_number = 0
128
+ @patient.for_every_hand! do
129
+ @final_hand = @hand_number >= @hand_data_list.length - 1
130
+ @patient.for_every_turn! do
131
+ check_patient
132
+ end
105
133
 
106
- @hand_number += 1
134
+ @hand_number += 1
135
+ end
107
136
  end
108
137
  end
109
138
  end
@@ -111,13 +140,13 @@ describe PokerMatchData do
111
140
 
112
141
  def check_patient
113
142
  @patient.match_def.must_equal @match_def
114
- @patient.chip_distribution.must_equal @chip_distribution
143
+ @patient.chip_distribution.must_equal @chip_distribution unless @no_chip_distribution
115
144
  @patient.hand_number.must_equal @hand_number
116
145
  @patient.current_hand.must_equal @hand_data_list[@hand_number]
117
146
  @patient.final_hand?.must_equal @final_hand
118
147
  end
119
148
 
120
- def init_data
149
+ def init_data(num_hands=nil)
121
150
  data.each do |game, data_hash|
122
151
  @chip_distribution = data_hash[:chip_distribution]
123
152
  @match_def_line_index = data_hash[:match_def_line_index]
@@ -130,12 +159,14 @@ describe PokerMatchData do
130
159
  action_messages = ActionMessages.parse(
131
160
  data_hash[:action_messages],
132
161
  @player_names,
133
- AcpcDealer::DEALER_DIRECTORY
162
+ AcpcDealer::DEALER_DIRECTORY,
163
+ num_hands
134
164
  )
135
165
  result_messages = HandResults.parse(
136
166
  data_hash[:result_messages],
137
167
  @player_names,
138
- AcpcDealer::DEALER_DIRECTORY
168
+ AcpcDealer::DEALER_DIRECTORY,
169
+ num_hands
139
170
  )
140
171
 
141
172
  @hand_data_list = []
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acpc_dealer_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-31 00:00:00.000000000 Z
12
+ date: 2013-01-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: acpc_dealer
@@ -164,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
164
  version: '0'
165
165
  segments:
166
166
  - 0
167
- hash: 1076567299815575746
167
+ hash: -128442043826121202
168
168
  required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  none: false
170
170
  requirements:
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  version: '0'
174
174
  segments:
175
175
  - 0
176
- hash: 1076567299815575746
176
+ hash: -128442043826121202
177
177
  requirements: []
178
178
  rubyforge_project:
179
179
  rubygems_version: 1.8.24