In the "Games" filter, 27_3draw was showing up as 273draw with the 3 underlined....
[fpdb-dooglus.git] / pyfpdb / HandHistory.py
blob9a3ba68699b85b6d6d66ce8d48df1449cb7f8216
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """HandHistory.py
5 Parses HandHistory xml files and returns requested objects.
6 """
7 # Copyright 2008-2011, Ray E. Barker
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ########################################################################
24 # Standard Library modules
25 import xml.dom.minidom
26 from xml.dom.minidom import Node
28 class HandHistory:
29 def __init__(self, xml_string, elements = ('ALL')):
31 doc = xml.dom.minidom.parseString(xml_string)
32 if elements == ('ALL'):
33 elements = ('BETTING', 'AWARDS', 'POSTS', 'PLAYERS', 'GAME')
35 if 'BETTING' in elements:
36 self.BETTING = Betting(doc.getElementsByTagName('BETTING')[0])
37 if 'AWARDS' in elements:
38 self.AWARDS = Awards (doc.getElementsByTagName('AWARDS')[0])
39 if 'POSTS' in elements:
40 self.POSTS = Posts (doc.getElementsByTagName('POSTS')[0])
41 if 'GAME' in elements:
42 self.GAME = Game (doc.getElementsByTagName('GAME')[0])
43 if 'PLAYERS' in elements:
44 self.PLAYERS = {}
45 p_n = doc.getElementsByTagName('PLAYERS')[0]
46 for p in p_n.getElementsByTagName('PLAYER'):
47 a_player = Player(p)
48 self.PLAYERS[a_player.name] = a_player
50 class Player:
51 def __init__(self, node):
52 self.name = node.getAttribute('NAME')
53 self.seat = node.getAttribute('SEAT')
54 self.stack = node.getAttribute('STACK')
55 self.showed_hand = node.getAttribute('SHOWED_HAND')
56 self.cards = node.getAttribute('CARDS')
57 self.allin = node.getAttribute('ALLIN')
58 self.sitting_out = node.getAttribute('SITTING_OUT')
59 self.hand = node.getAttribute('HAND')
60 self.start_cards = node.getAttribute('START_CARDS')
62 if self.allin == '' or \
63 self.allin == '0' or \
64 self.allin.upper() == 'FALSE': self.allin = False
65 else: self.allin = True
67 if self.sitting_out == '' or \
68 self.sitting_out == '0' or \
69 self.sitting_out.upper() == 'FALSE': self.sitting_out = False
70 else: self.sitting_out = True
72 def __str__(self):
73 temp = "%s\n seat = %s\n stack = %s\n cards = %s\n" % \
74 (self.name, self.seat, self.stack, self.cards)
75 temp = temp + " showed_hand = %s\n allin = %s\n" % \
76 (self.showed_hand, self.allin)
77 temp = temp + " hand = %s\n start_cards = %s\n" % \
78 (self.hand, self.start_cards)
79 return temp
81 class Awards:
82 def __init__(self, node):
83 self.awards = [] # just an array of award objects
84 for a in node.getElementsByTagName('AWARD'):
85 self.awards.append(Award(a))
87 def __str__(self):
88 temp = ""
89 for a in self.awards:
90 temp = temp + "%s\n" % (a)
91 return temp
93 class Award:
94 def __init__(self, node):
95 self.player = node.getAttribute('PLAYER')
96 self.amount = node.getAttribute('AMOUNT')
97 self.pot = node.getAttribute('POT')
99 def __str__(self):
100 return self.player + " won " + self.amount + " from " + self.pot
102 class Game:
103 def __init__(self, node):
104 print node
105 self.tags = {}
106 for tag in ( ('GAME_NAME', 'game_name'), ('MAX', 'max'), ('HIGHLOW', 'high_low'),
107 ('STRUCTURE', 'structure'), ('MIXED', 'mixed') ):
108 L = node.getElementsByTagName(tag[0])
109 if (not L): continue
110 print L
111 for node2 in L:
112 title = ""
113 for node3 in node2.childNodes:
114 if (node3.nodeType == Node.TEXT_NODE):
115 title +=node3.data
116 self.tags[tag[1]] = title
118 def __str__(self):
119 return "%s %s %s, (%s max), %s" % (self.tags['structure'],
120 self.tags['game_name'],
121 self.tags['game_name'],
122 self.tags['max'],
123 self.tags['game_name'])
125 class Posts:
126 def __init__(self, node):
127 self.posts = [] # just an array of post objects
128 for p in node.getElementsByTagName('POST'):
129 self.posts.append(Post(p))
131 def __str__(self):
132 temp = ""
133 for p in self.posts:
134 temp = temp + "%s\n" % (p)
135 return temp
137 class Post:
138 def __init__(self, node):
139 self.player = node.getAttribute('PLAYER')
140 self.amount = node.getAttribute('AMOUNT')
141 self.posted = node.getAttribute('POSTED')
142 self.live = node.getAttribute('LIVE')
144 def __str__(self):
145 return ("%s posted %s %s %s") % (self.player, self.amount, self.posted, self.live)
147 class Betting:
148 def __init__(self, node):
149 self.rounds = [] # a Betting object is just an array of rounds
150 for r in node.getElementsByTagName('ROUND'):
151 self.rounds.append(Round(r))
153 def __str__(self):
154 temp = ""
155 for r in self.rounds:
156 temp = temp + "%s\n" % (r)
157 return temp
159 class Round:
160 def __init__(self, node):
161 self.name = node.getAttribute('ROUND_NAME')
162 self.action = []
163 for a in node.getElementsByTagName('ACTION'):
164 self.action.append(Action(a))
166 def __str__(self):
167 temp = self.name + "\n"
168 for a in self.action:
169 temp = temp + " %s\n" % (a)
170 return temp
172 class Action:
173 def __init__(self, node):
174 self.player = node.getAttribute('PLAYER')
175 self.action = node.getAttribute('ACT')
176 self.amount = node.getAttribute('AMOUNT')
177 self.allin = node.getAttribute('ALLIN')
179 def __str__(self):
180 return self.player + " " + self.action + " " + self.amount + " " + self.allin
182 if __name__== "__main__":
183 file = open('test.xml', 'r')
184 xml_string = file.read()
185 file.close()
187 print xml_string + "\n\n\n"
188 h = HandHistory(xml_string, ('ALL'))
189 print h.GAME
190 print h.POSTS
191 print h.BETTING
192 print h.AWARDS
194 for p in h.PLAYERS.keys():
195 print h.PLAYERS[p]