Class: Pokerstats::PokerstarsHandHistoryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pokerstats/pokerstars_hand_history_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stats = nil) ⇒ PokerstarsHandHistoryParser

Returns a new instance of PokerstarsHandHistoryParser.



82
83
84
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 82

def initialize stats=nil
  @stats = stats || HandStatistics.new
end

Class Method Details

.game(lines) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 67

def self.game(lines)
  lines.lstrip!
  return nil if lines.empty?
  case lines[/[^\n]+/].chomp
  when /PokerStars Game #([0-9]+): Tournament #([0-9]+), (\$[0-9+$]+) ([^\-]*) - Level ([IVXL]+) \((#{CASH})\/(#{CASH})\) - (.*)$/
    "PS#{$1}"
  when /PokerStars Game #([0-9]+): +([^(]*) \((#{CASH})\/(#{CASH})\) - (.*)$/
    "PS#{$1}"
  when /PokerStars Game #([0-9]+): +([^(]*) \((#{CASH})\/(#{CASH}) USD\) - (.*)$/
    "PS#{$1}"
  else
    nil
  end
end

.has_valid_header?(lines) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 63

def self.has_valid_header?(lines)
  !game(lines).nil?
end

.parse_lines(lines, stats = nil) ⇒ Object



58
59
60
61
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 58

def self.parse_lines(lines, stats=nil)
  parser = self.new(stats)
  lines.each {|line| parser.parse(line)}
end

Instance Method Details

#ignorable?(line) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 86

def ignorable?(line)
  regular_expressions_for_ignorable_phrases = [
    /(.*) has timed out/,
    /(.*) has returned/,
    /(.*) leaves the table/,
    /(.*) joins the table at seat #[0-9]+/,
    /(.*) sits out/,
    /(.*) is sitting out/,    
    /(.*) is (dis)?connected/,
    /(.*) said,/, 
    /(.*): doesn't show hand/,        
    /(.*) will be allowed to play after the button/,
    /(.*) was removed from the table for failing to post/,
    /(.*) re-buys and receives (.*) chips for (.*)/,
    /Seat [0-9]+: (.*) \(((small)|(big)) blind\) folded on the Flop/,
    /Seat [0-9]+: (.*) folded on the ((Flop)|(Turn)|(River))/,
    /Seat [0-9]+: (.*) folded before Flop \(didn't bet\)/,
    /Seat [0-9]+: (.*) (\((small blind)|(big blind)|(button)\) )?folded before Flop( \(didn't bet\))?/,
    /Seat [0-9]+: (.*) (\((small blind)|(big blind)|(button)\) )?collected (.*)/,
    /^\s*$/    
  ]
  regular_expressions_for_ignorable_phrases.any?{|re| re =~ line }
end

#parse(line) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 110

def parse(line)
  case line
  when /PokerStars Game #([0-9]+): Tournament #([0-9]+), (#{CASH})\+(#{CASH}) Hold'em (Pot |No |)Limit - Level ([IVXL]+) \((#{CASH})\/(#{CASH})\) - (.*)$/
    @stats.update_hand :name => "PS#{$1}", :description=> "#{$2}, #{$3}+#{$4} Hold'em #{$5}Limit", :tournament=> $2, 
        :sb=> $7.to_d, :bb=> $8.to_d, :ante => "0.0".to_d, 
        :played_at=> PokerstarsTimeStringConverter.new($9).as_utc_datetime,
        :street => :prelude, :board => "", :max_players => 0, :number_players => 0, :table_name => "",
        :game_type => "Hold'em", :limit_type => "#{$5}Limit", :stakes_type => cash_to_d($3)
  when /PokerStars Game #([0-9]+): Tournament #([0-9]+), (Freeroll|(#{CASH})\+(#{CASH}) *(USD)?) +Hold'em (Pot |No |)Limit -.*Level ([IVXL]+) \((#{CASH})\/(#{CASH})\) - (.*)$/
    @stats.update_hand :name => "PS#{$1}", :description=> "#{$2}, #{$3} Hold'em #{$7}Limit", :tournament=> $2, 
        :sb=> $9.to_d, :bb=> $10.to_d, :ante => "0.0".to_d, 
        :played_at=> PokerstarsTimeStringConverter.new($11).as_utc_datetime,
        :street => :prelude, :board => "", :max_players => 0, :number_players => 0, :table_name => "",
        :game_type => "Hold'em", :limit_type => "#{$7}Limit", :stakes_type => cash_to_d($4)            
  when /PokerStars Game #([0-9]+): Tournament #([0-9]+), (Freeroll|(#{CASH}).*(FPP)) *(USD)? +Hold'em (Pot |No |)Limit -.*Level ([IVXL]+) \((#{CASH})\/(#{CASH})\) - (.*)$/
    @stats.update_hand :name => "PS#{$1}", :description=> "#{$2}, #{$3} Hold'em #{$7}Limit", :tournament=> $2, 
        :sb=> $9.to_d, :bb=> $10.to_d, :ante => "0.0".to_d, 
        :played_at=> PokerstarsTimeStringConverter.new($11).as_utc_datetime,
        :street => :prelude, :board => "", :max_players => 0, :number_players => 0, :table_name => "",
        :game_type => "Hold'em", :limit_type => "#{$7}Limit", :stakes_type => "0".to_d
  when /PokerStars Game #([0-9]+): +([^(]*)Hold'em (No |Pot |)Limit \((#{CASH})\/(#{CASH})\) - (.*)$/
    @stats.update_hand :name => "PS#{$1}", :description=> "#{$2}Hold'em #{$3}Limit (#{$4}/#{$5})", :tournament=> nil, 
        :sb=> cash_to_d($4), :bb=> cash_to_d($5), :ante => "0.0".to_d, 
        :played_at=> PokerstarsTimeStringConverter.new($6).as_utc_datetime,
        :street => :prelude, :board => "", :max_players => 0, :number_players => 0, :table_name => "",
        :game_type => "Hold'em", :limit_type => "#{$3}Limit", :stakes_type => cash_to_d($5)
  when /PokerStars Game #([0-9]+): +([^(]*)Hold'em (No |Pot |)Limit \((#{CASH})\/(#{CASH}) USD\) - (.*)$/
    @stats.update_hand :name => "PS#{$1}", :description=> "#{$2}Hold'em #{$3}LImit (#{$4}/#{$5})", :tournament=> nil, 
        :sb=> cash_to_d($4), :bb=> cash_to_d($5), :ante => "0.0".to_d, 
        :played_at=> PokerstarsTimeStringConverter.new($6).as_utc_datetime,
        :street => :prelude, :board => "", :max_players => 0, :number_players => 0, :table_name => "",
        :game_type => "Hold'em", :limit_type => "#{$3}Limit", :stakes_type => cash_to_d($5)
  when /PokerStars Game #([0-9]+):/
    raise HandHistoryParseError, "invalid hand record: #{line}"
  when /\*\*\* HOLE CARDS \*\*\*/
    @stats.register_button(@stats.button)
    @stats.update_hand :street => :preflop
  when /\*\*\* FLOP \*\*\* \[(.*)\]/
    @stats.update_hand :street => :flop
  when /\*\*\* TURN \*\*\* \[([^\]]*)\] \[([^\]]*)\]/
    @stats.update_hand :street => :turn
  when /\*\*\* RIVER \*\*\* \[([^\]]*)\] \[([^\]]*)\]/
    @stats.update_hand :street => :river
  when /\*\*\* SHOW DOWN \*\*\*/
    @stats.update_hand :street => :showdown
  when /\*\*\* SUMMARY \*\*\*/
    @stats.update_hand :street => :summary
  when /Dealt to ([^)]+) \[([^\]]+)\]/
    @stats.register_action($1, 'dealt', :result => :cards, :data => $2)
  when /(.*): shows \[(.*)\]/
    @stats.register_action($1, 'shows', :result => :cards, :data => $2)
  when /Board \[(.*)\]/
    @stats.update_hand :board => $1
  when /Total pot (#{CASH}) (((Main)|(Side)) pot(-[0-9]+)? (#{CASH}). )*\| Rake (#{CASH})/
    @stats.update_hand(:total_pot => cash_to_d($1), :rake => cash_to_d($8))
  when /Total pot (#{CASH}) Main pot (#{CASH}).( Side pot-[0-9]+ (#{CASH}).)* | Rake (#{CASH})/
    raise HandHistoryParseError, "popo!"
  when /Seat ([0-9]+): (.+) \((#{CASH}) in chips\)( is sitting out)?/
    @stats.register_player(:seat => $1.to_i, :screen_name => $2, :starting_stack => cash_to_d($3))
  when /(.*): posts ((small)|(big)|(small \& big)) blind(s)? (#{CASH})/
    @stats.register_action($1, 'posts', :result => :post, :amount => cash_to_d($7))
  when /(.*): posts the ante (#{CASH})/
    @stats.register_action($1, 'antes', :result => :post, :amount => cash_to_d($2))
    @stats.update_hand(:ante => [cash_to_d($2), @stats.hand_information(:ante)].max)
  # when /Table '([0-9]+) ([0-9]+)' ([0-9]+)-max Seat #([0-9]+) is the button/
  #   @stats.register_button($4.to_i)
  when /Table '(.*)' ([0-9]+)-max Seat #([0-9]+) is the button/
    @stats.register_button($3.to_i)
    @stats.update_hand(:table_name => $1, :max_players => $2.to_i)
  when /Uncalled bet \((.*)\) returned to (.*)/
    @stats.register_action($2, 'return', :result => :win, :amount => cash_to_d($1))
  when /(.+): ((folds)|(checks))/
    @stats.register_action($1, $2, :result => :neutral)
  when /(.+): ((calls)|(bets)) ((#{CASH})( and is all-in)?)?$/
    @stats.register_action($1, $2, :result => :pay, :amount => cash_to_d($6))
  when /(.+): raises (#{CASH}) to (#{CASH})( and is all-in)?$/      
    @stats.register_action($1, 'raises', :result => :pay_to, :amount => cash_to_d($3))
  when /(.*) collected (.*) from ((side )|(main ))?pot/
    @stats.register_action($1, "wins", :result => :win, :amount => cash_to_d($2))
  when /(.*): mucks hand/
    @stats.register_action($1, 'mucks', :result => :neutral)
  when /Seat [0-9]+: (.*) (\((small blind)|(big blind)|(button)\) )?showed \[([^\]]+)\] and ((won) \(#{CASH}\)|(lost)) with (.*)/
  when /Seat [0-9]+: (.*) mucked \[([^\]]+)\]/
  else
    raise HandHistoryParseError, "invalid line for parse: #{line}" unless ignorable?(line)
  end
end