2 # -*- coding: utf-8 -*-
4 #Copyright 2009-2011 Stephane Alessio
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt.
18 """parses and stores summary sections from e.g. eMail or summary files"""
21 _
= L10n
.get_translation()
23 # TODO: check to keep only the needed modules
31 from decimal_wrapper
import Decimal
34 from copy
import deepcopy
35 from Exceptions
import *
42 log
= logging
.getLogger("parser")
44 class TourneySummary(object):
46 ################################################################
48 UPS
= {'a':'A', 't':'T', 'j':'J', 'q':'Q', 'k':'K', 'S':'s', 'C':'c', 'H':'h', 'D':'d'} # SAL- TO KEEP ??
49 LCS
= {'H':'h', 'D':'d', 'C':'c', 'S':'s'} # SAL- TO KEEP ??
50 SYMBOL
= {'USD': '$', 'EUR': u
'$', 'T$': '', 'play': ''}
51 MS
= {'horse' : 'HORSE', '8game' : '8-Game', 'hose' : 'HOSE', 'ha': 'HA'}
52 SITEIDS
= {'Fulltilt':1, 'Full Tilt Poker':1, 'PokerStars':2, 'Everleaf':3, 'Win2day':4, 'OnGame':5, 'UltimateBet':6, 'Betfair':7, 'Absolute':8, 'PartyPoker':9, 'Winamax':14 }
55 def __init__(self
, db
, config
, siteName
, summaryText
, builtFrom
= "HHC"):
58 self
.siteName
= siteName
59 self
.siteId
= self
.SITEIDS
[siteName
]
61 self
.summaryText
= summaryText
62 self
.tourneyName
= None
63 self
.tourneyTypeId
= None
75 self
.prizepool
= 0 # Make it a dict in order to deal (eventually later) with non-money winnings : {'MONEY' : amount, 'OTHER' : Value ??}
82 self
.isShootout
= False
83 self
.matrixMatchId
= None # For Matrix tourneys : 1-4 => match tables (traditionnal), 0 => Positional winnings info
84 self
.matrixIdProcessed
= None
85 self
.subTourneyBuyin
= None
86 self
.subTourneyFee
= None
87 self
.rebuyChips
= None
88 self
.addOnChips
= None
91 self
.totalRebuyCount
= None
92 self
.totalAddOnCount
= None
94 self
.tourneyComment
= None
97 self
.isSatellite
= False
98 self
.isDoubleOrNothing
= False
101 self
.addedCurrency
= None
102 self
.gametype
= {'category':None, 'limitType':None}
104 self
.commentTs
= None
106 # Collections indexed by player names
108 self
.tourneysPlayersIds
= {}
111 self
.winningsCurrency
= {}
112 self
.rebuyCounts
= {}
113 self
.addOnCounts
= {}
116 # currency symbol for this summary
119 if builtFrom
=="IMAP":
123 self
.db
= Database
.Database(config
)
126 self
.insertOrUpdate()
131 vars = ( (_("SITE"), self
.siteName
),
132 (_("START TIME"), self
.startTime
),
133 (_("END TIME"), self
.endTime
),
134 (_("TOURNEY NAME"), self
.tourneyName
),
135 (_("TOURNEY NO"), self
.tourNo
),
136 (_("TOURNEY TYPE ID"), self
.tourneyTypeId
),
137 (_("TOURNEY ID"), self
.tourneyId
),
138 (_("BUYIN"), self
.buyin
),
139 (_("FEE"), self
.fee
),
140 (_("CURRENCY"), self
.currency
),
141 (_("HERO"), self
.hero
),
142 (_("MAXSEATS"), self
.maxseats
),
143 (_("ENTRIES"), self
.entries
),
144 (_("SPEED"), self
.speed
),
145 (_("PRIZE POOL"), self
.prizepool
),
146 (_("STARTING CHIP COUNT"), self
.buyInChips
),
147 (_("MIXED"), self
.mixed
),
148 (_("REBUY"), self
.isRebuy
),
149 (_("ADDON"), self
.isAddOn
),
150 (_("KO"), self
.isKO
),
151 (_("MATRIX"), self
.isMatrix
),
152 (_("MATRIX ID PROCESSED"), self
.matrixIdProcessed
),
153 (_("SHOOTOUT"), self
.isShootout
),
154 (_("MATRIX MATCH ID"), self
.matrixMatchId
),
155 (_("SUB TOURNEY BUY IN"), self
.subTourneyBuyin
),
156 (_("SUB TOURNEY FEE"), self
.subTourneyFee
),
157 (_("REBUY CHIPS"), self
.rebuyChips
),
158 (_("ADDON CHIPS"), self
.addOnChips
),
159 (_("REBUY COST"), self
.rebuyCost
),
160 (_("ADDON COST"), self
.addOnCost
),
161 (_("TOTAL REBUYS"), self
.totalRebuyCount
),
162 (_("TOTAL ADDONS"), self
.totalAddOnCount
),
163 (_("KO BOUNTY"), self
.koBounty
),
164 (_("TOURNEY COMMENT"), self
.tourneyComment
),
165 (_("SNG"), self
.isSng
),
166 (_("SATELLITE"), self
.isSatellite
),
167 (_("DOUBLE OR NOTHING"), self
.isDoubleOrNothing
),
168 (_("GUARANTEE"), self
.guarantee
),
169 (_("ADDED"), self
.added
),
170 (_("ADDED CURRENCY"), self
.addedCurrency
),
171 (_("COMMENT"), self
.comment
),
172 (_("COMMENT TIMESTAMP"), self
.commentTs
)
175 structs
= ( (_("PLAYER IDS"), self
.playerIds
),
176 (_("PLAYERS"), self
.players
),
177 (_("TOURNEYS PLAYERS IDS"), self
.tourneysPlayersIds
),
178 (_("RANKS"), self
.ranks
),
179 (_("WINNINGS"), self
.winnings
),
180 (_("WINNINGS CURRENCY"), self
.winningsCurrency
),
181 (_("COUNT REBUYS"), self
.rebuyCounts
),
182 (_("COUNT ADDONS"), self
.addOnCounts
),
183 (_("NB OF KO"), self
.koCounts
)
186 for (name
, var
) in vars:
187 str = str + "\n%s = " % name
+ pprint
.pformat(var
)
189 for (name
, struct
) in structs
:
190 str = str + "\n%s =\n" % name
+ pprint
.pformat(struct
, 4)
194 def parseSummary(self
): abstract
195 """should fill the class variables with the parsed information"""
197 def getSummaryText(self
):
198 return self
.summaryText
200 def insertOrUpdate(self
):
201 # First : check all needed info is filled in the object, especially for the initial select
204 # Some identified issues for tourneys already in the DB (which occurs when the HH file is parsed and inserted before the Summary)
205 # Be careful on updates that could make the HH import not match the tourney inserted from a previous summary import !!
206 # BuyIn/Fee can be at 0/0 => match may not be easy
207 # Only one existinf Tourney entry for Matrix Tourneys, but multiple Summary files
208 # Starttime may not match the one in the Summary file : HH = time of the first Hand / could be slighltly different from the one in the summary file
209 # Note: If the TourneyNo could be a unique id .... this would really be a relief to deal with matrix matches ==> Ask on the IRC / Ask Fulltilt ??
211 self
.playerIds
= self
.db
.getSqlPlayerIDs(self
.players
, self
.siteId
)
212 #for player in self.players:
213 # id=self.db.get_player_id(self.config, self.siteName, player)
215 # id=self.db.insertPlayer(unicode(player), self.siteId)
216 # self.playerIds.update({player:id})
218 #print "TS.insert players",self.players,"playerIds",self.playerIds
220 self
.buyinCurrency
=self
.currency
221 self
.dbid_pids
=self
.playerIds
#TODO:rename this field in Hand so this silly renaming can be removed
223 #print "TS.self before starting insert",self
224 self
.tourneyTypeId
= self
.db
.getSqlTourneyTypeIDs(self
)
225 self
.tourneyId
= self
.db
.createOrUpdateTourney(self
)
226 self
.tourneysPlayersIds
= self
.db
.createOrUpdateTourneysPlayers(self
)
229 logging
.debug(_("Tourney Insert/Update done"))
231 # TO DO : Return what has been done (tourney created, updated, nothing)
232 # ?? stored = 1 if tourney is fully created / duplicates = 1, if everything was already here and correct / partial=1 if some things were already here (between tourney, tourneysPlayers and handsPlayers)
233 # if so, prototypes may need changes to know what has been done or make some kind of dict in Tourney object that could be updated during the insert process to store that information
239 return (stored
, duplicates
, partial
, errors
, ttime
)
242 def addPlayer(self
, rank
, name
, winnings
, winningsCurrency
, rebuyCount
, addOnCount
, koCount
):
244 Adds a player to the tourney, and initialises data structures indexed by player.
245 rank (int) indicating the finishing rank (can be -1 if unknown)
246 name (string) player name
247 winnings (int) the money the player ended the tourney with (can be 0, or -1 if unknown)
249 log
.debug(_("addPlayer: rank:%s - name : '%s' - Winnings (%s)") % (rank
, name
, winnings
))
250 self
.players
.append(name
)
252 self
.ranks
.update( { name
: rank
} )
253 self
.winnings
.update( { name
: winnings
} )
254 self
.winningsCurrency
.update( { name
: winningsCurrency
} )
256 self
.ranks
.update( { name
: None } )
257 self
.winnings
.update( { name
: None } )
258 self
.winningsCurrency
.update( { name
: None } )
260 self
.rebuyCounts
.update( {name
: Decimal(rebuyCount
) } )
262 self
.rebuyCounts
.update( {name
: None } )
265 self
.addOnCounts
.update( {name
: Decimal(addOnCount
) } )
267 self
.addOnCounts
.update( {name
: None } )
270 self
.koCounts
.update( {name
: Decimal(koCount
) } )
272 self
.koCounts
.update( {name
: None } )
275 def incrementPlayerWinnings(self
, name
, additionnalWinnings
):
276 log
.debug(_("incrementPlayerWinnings: name : '%s' - Add Winnings (%s)") % (name
, additionnalWinnings
))
278 if self
.winnings
.has_key(name
):
279 oldWins
= self
.winnings
[name
]
281 self
.players
.append([-1, name
, 0])
283 self
.winnings
[name
] = oldWins
+ Decimal(additionnalWinnings
)
285 def checkPlayerExists(self
,player
):
286 if player
not in [p
[1] for p
in self
.players
]:
287 print "checkPlayerExists", player
, "fail"
290 def updateSessionsCache(self
, sc
, gsc
, tz
, doinsert
):
291 self
.heros
= self
.db
.getHeroIds(self
.dbid_pids
, self
.siteName
)
292 sc
= self
.db
.prepSessionsCache(self
.tourNo
, self
.dbid_pids
, self
.startTime
, sc
, self
.heros
, doinsert
)
294 gsc
= self
.db
.storeSessionsCache(self
.tourNo
, self
.dbid_pids
, self
.startTime
, {'type': 'summary'}
295 ,None, self
.assembleInfo(), sc
, gsc
, tz
, self
.heros
, doinsert
)
298 def assembleInfo(self
):
300 info
['tourneyTypeId'] = self
.tourneyTypeId
301 info
['winnings'] = self
.winnings
302 info
['winningsCurrency'] = self
.winningsCurrency
303 info
['buyinCurrency'] = self
.buyinCurrency
304 info
['buyin'] = self
.buyin
305 info
['fee'] = self
.fee
308 def writeSummary(self
, fh
=sys
.__stdout
__):
309 print >>fh
, "Override me"
311 def printSummary(self
):
312 self
.writeSummary(sys
.stdout
)