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.



27
28
29
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 27

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

Class Method Details

.has_valid_header?(lines) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 13

def self.has_valid_header?(lines)
  lines.lstrip!
  case lines[/^[^\n\r]*/]
    when /PokerStars Game #([0-9]+): Tournament #([0-9]+), (\$[0-9+$]+) ([^\-]*) - Level ([IVXL]+) \((#{CASH})\/(#{CASH})\) - (.*)$/
      true
    when /PokerStars Game #([0-9]+): +([^(]*) \((#{CASH})\/(#{CASH})\) - (.*)$/
      true
    when /PokerStars Game #([0-9]+): +([^(]*) \((#{CASH})\/(#{CASH}) USD\) - (.*)$/
      true
    else
      false
  end
end

.parse_lines(lines, stats = nil) ⇒ Object



8
9
10
11
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 8

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)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 31

def ignorable?(line)
  regular_expressions_for_ignorable_phrases = [
    /(.*): doesn't show hand/,
    /(.*) has timed out/,
    /(.*) has returned/,
    /(.*) leaves the table/,
    /(.*) joins the table at seat #[0-9]+/,
    /(.*) sits out/,
    /(.*) mucks hand/,
    /(.*) is sitting out/,    
    /(.*) is (dis)?connected/,
    /(.*) said,/, 
    /(.*) 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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pokerstats/pokerstars_hand_history_parser.rb', line 56

def parse(line)
  begin
    case line
    when /PokerStars Game #([0-9]+): Tournament #([0-9]+), (\$[0-9+$]+) ([^\-]*) - Level ([IVXL]+) \((#{CASH})\/(#{CASH})\) - (.*)$/
      @stats.update_hand :name => "PS#{$1}", :description=> "#{$2}, #{$3} #{$4}", :tournament=> $2, :sb=> $6.to_d, :bb=> $7.to_d, :played_at=> Time.parse($8), :street => :prelude
    when /PokerStars Game #([0-9]+): +([^(]*) \((#{CASH})\/(#{CASH})\) - (.*)$/
      @stats.update_hand :name => "PS#{$1}", :description=> "#{$2} (#{$3}/#{$4})", :tournament=> nil, :sb=> cash_to_d($3), :bb=> cash_to_d($4), :played_at=> Time.parse($5), :street => :prelude
    when /PokerStars Game #([0-9]+): +([^(]*) \((#{CASH})\/(#{CASH}) USD\) - (.*)$/
      @stats.update_hand :name => "PS#{$1}", :description=> "#{$2} (#{$3}/#{$4})", :tournament=> nil, :sb=> cash_to_d($3), :bb=> cash_to_d($4), :played_at=> Time.parse($5), :street => :prelude
    when /PokerStars Game #([0-9]+):/
      raise "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 "popo!"
    when /Seat ([0-9]+): (.+) \(#{CASH} in chips\)( is sitting out)?/
      @stats.register_player(:seat => $1.to_i, :screen_name => $2)
    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))
    when /Table '([0-9]+) ([0-9]+)' (.*) Seat #([0-9]+) is the button/
      @stats.register_button($4.to_i)
    when /Table '(.*)' (.*) Seat #([0-9]+) is the button/
      @stats.register_button($3.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 /Seat [0-9]+: (.*) (\((small blind)|(big blind)|(button)\) )?showed \[([^\]]+)\] and ((won) \(#{CASH}\)|(lost)) with (.*)/
    when /Seat [0-9]+: (.*) mucked \[([^\]]+)\]/
    else
      raise "invalid line for parse: #{line}" unless ignorable?(line)
    end
  rescue => e
    raise e
  end
end