diff --git a/pyfpdb/Card.py b/pyfpdb/Card.py index 69ac009b..6ab9b62d 100755 --- a/pyfpdb/Card.py +++ b/pyfpdb/Card.py @@ -24,25 +24,37 @@ def twoStartCards(value1, suit1, value2, suit2): (y+2) represents rank of second card (2=2 .. 14=Ace) If x > y then pair is suited, if x < y then unsuited""" if value1 < 2 or value2 < 2: - return(0) - if (suit1 == suit2 and value1 < value2) or (suit1 != suit2 and value2 > value1): - return(13 * (value2-2) + (value1-1)) + ret = 0 + if value1 == value2: # pairs + ret = (13 * (value2-2) + (value2-1) ) + elif suit1 == suit2: + if value1 > value2: + ret = 13 * (value1-2) + (value2-1) + else: + ret = 13 * (value2-2) + (value1-1) else: - return(13 * (value1-2) + (value2-1)) + if value1 > value2: + ret = 13 * (value2-2) + (value2-1) + else: + ret = 13 * (value1-2) + (value2-1) + +# print "twoStartCards(", value1, suit1, value2, suit2, ")=", ret + return ret def twoStartCardString(card): """ Function to convert an int representing 2 holdem hole cards (as created by twoStartCards) into a string like AQo """ - if card <= 0: - return 'xx' - else: + ret = 'xx' + if card > 0: card -= 1 s = ('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A') - x = card/13 - y = card - 13*x - if x == y: return(s[x] + s[y]) - elif x > y: return(s[x] + s[y] + 's') - else: return(s[y] + s[x] + 'o') + x = card / 13 + y = card - 13 * x + if x == y: ret = s[x] + s[y] + elif x > y: ret = s[x] + s[y] + 's' + else: ret = s[y] + s[x] + 'o' +# print "twoStartCardString(", card ,") = " + ret + return ret def fourStartCards(value1, suit1, value2, suit2, value3, suit3, value4, suit4): """ Function to convert 4 value,suit pairs into a Omaha style starting hand, diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 068f7153..8d58da50 100755 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -29,6 +29,8 @@ import traceback from datetime import datetime, date, time, timedelta from time import time, strftime import string +import re +import logging # pyGTK modules @@ -45,6 +47,116 @@ class Database: PGSQL = 3 SQLITE = 4 + # Data Structures for index and foreign key creation + # drop_code is an int with possible values: 0 - don't drop for bulk import + # 1 - drop during bulk import + # db differences: + # - note that mysql automatically creates indexes on constrained columns when + # foreign keys are created, while postgres does not. Hence the much longer list + # of indexes is required for postgres. + # all primary keys are left on all the time + # + # table column drop_code + + indexes = [ + [ ] # no db with index 0 + , [ ] # no db with index 1 + , [ # indexes for mysql (list index 2) + {'tab':'Players', 'col':'name', 'drop':0} + , {'tab':'Hands', 'col':'siteHandNo', 'drop':0} + , {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09 + , {'tab':'HandsPlayers', 'col':'handId', 'drop':0} # not needed, handled by fk + , {'tab':'HandsPlayers', 'col':'playerId', 'drop':0} # not needed, handled by fk + , {'tab':'HandsPlayers', 'col':'tourneysTypeId', 'drop':0} + , {'tab':'HandsPlayers', 'col':'tourneysPlayersId', 'drop':0} + , {'tab':'Tourneys', 'col':'siteTourneyNo', 'drop':0} + ] + , [ # indexes for postgres (list index 3) + {'tab':'Gametypes', 'col':'siteId', 'drop':0} + , {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09 + , {'tab':'Hands', 'col':'siteHandNo', 'drop':0} + , {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0} + , {'tab':'HandsPlayers', 'col':'handId', 'drop':1} + , {'tab':'HandsPlayers', 'col':'playerId', 'drop':1} + , {'tab':'HandsPlayers', 'col':'tourneysPlayersId', 'drop':0} + , {'tab':'HudCache', 'col':'gametypeId', 'drop':1} + , {'tab':'HudCache', 'col':'playerId', 'drop':0} + , {'tab':'HudCache', 'col':'tourneyTypeId', 'drop':0} + , {'tab':'Players', 'col':'siteId', 'drop':1} + , {'tab':'Players', 'col':'name', 'drop':0} + , {'tab':'Tourneys', 'col':'tourneyTypeId', 'drop':1} + , {'tab':'Tourneys', 'col':'siteTourneyNo', 'drop':0} + , {'tab':'TourneysPlayers', 'col':'playerId', 'drop':0} + , {'tab':'TourneysPlayers', 'col':'tourneyId', 'drop':0} + , {'tab':'TourneyTypes', 'col':'siteId', 'drop':0} + ] + , [ # indexes for sqlite (list index 4) + ] + ] + + foreignKeys = [ + [ ] # no db with index 0 + , [ ] # no db with index 1 + , [ # foreign keys for mysql + {'fktab':'Hands', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} + , {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} + , {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} + , {'fktab':'HandsActions', 'fkcol':'handsPlayerId', 'rtab':'HandsPlayers', 'rcol':'id', 'drop':1} + , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} + , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} + , {'fktab':'HudCache', 'fkcol':'tourneyTypeId', 'rtab':'TourneyTypes', 'rcol':'id', 'drop':1} + ] + , [ # foreign keys for postgres + {'fktab':'Hands', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} + , {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} + , {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} + , {'fktab':'HandsActions', 'fkcol':'handsPlayerId', 'rtab':'HandsPlayers', 'rcol':'id', 'drop':1} + , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} + , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} + , {'fktab':'HudCache', 'fkcol':'tourneyTypeId', 'rtab':'TourneyTypes', 'rcol':'id', 'drop':1} + ] + ] + + + # MySQL Notes: + # "FOREIGN KEY (handId) REFERENCES Hands(id)" - requires index on Hands.id + # - creates index handId on .handId + # alter table t drop foreign key fk + # alter table t add foreign key (fkcol) references tab(rcol) + # alter table t add constraint c foreign key (fkcol) references tab(rcol) + # (fkcol is used for foreigh key name) + + # mysql to list indexes: + # SELECT table_name, index_name, non_unique, column_name + # FROM INFORMATION_SCHEMA.STATISTICS + # WHERE table_name = 'tbl_name' + # AND table_schema = 'db_name' + # ORDER BY table_name, index_name, seq_in_index + # + # ALTER TABLE Tourneys ADD INDEX siteTourneyNo(siteTourneyNo) + # ALTER TABLE tab DROP INDEX idx + + # mysql to list fks: + # SELECT constraint_name, table_name, column_name, referenced_table_name, referenced_column_name + # FROM information_schema.KEY_COLUMN_USAGE + # WHERE REFERENCED_TABLE_SCHEMA = (your schema name here) + # AND REFERENCED_TABLE_NAME is not null + # ORDER BY TABLE_NAME, COLUMN_NAME; + + # this may indicate missing object + # _mysql_exceptions.OperationalError: (1025, "Error on rename of '.\\fpdb\\hands' to '.\\fpdb\\#sql2-7f0-1b' (errno: 152)") + + + # PG notes: + + # To add a foreign key constraint to a table: + # ALTER TABLE tab ADD CONSTRAINT c FOREIGN KEY (col) REFERENCES t2(col2) MATCH FULL; + # ALTER TABLE tab DROP CONSTRAINT zipchk + # + # Note: index names must be unique across a schema + # CREATE INDEX idx ON tab(col) + # DROP INDEX idx + def __init__(self, c, db_name = None, game = None, sql = None): # db_name and game not used any more print "\ncreating Database instance, sql =", sql self.fdb = fpdb_db.fpdb_db() # sets self.fdb.db self.fdb.cursor and self.fdb.sql @@ -68,19 +180,27 @@ class Database: self.sql = SQL.Sql(type = self.type, db_server = db_params['db-server']) else: self.sql = sql - + + # config while trying out new hudcache mechanism + self.use_date_in_hudcache = True + # To add to config: + self.hud_session_gap = 30 # Gap (minutes) between hands that indicates a change of session + # (hands every 2 mins for 1 hour = one session, if followed + # by a 40 minute gap and then more hands on same table that is + # a new session) self.hud_style = 'T' # A=All-time # S=Session # T=timed (last n days) # Future values may also include: # H=Hands (last n hands) - self.hud_hands = 1000 # Max number of hands from each player to use for hud stats + self.hud_hands = 2000 # Max number of hands from each player to use for hud stats self.hud_days = 30 # Max number of days from each player to use for hud stats - self.hud_session_gap = 30 # Gap (minutes) between hands that indicates a change of session - # (hands every 2 mins for 1 hour = one session, if followed - # by a 40 minute gap and then more hands on same table that is - # a new session) + + self.hud_hero_style = 'T' # Duplicate set of vars just for hero + self.hud_hero_hands = 2000 + self.hud_hero_days = 30 + self.cursor = self.fdb.cursor if self.fdb.wrongDbVersion == False: @@ -115,6 +235,7 @@ class Database: self.saveActions = False if self.import_options['saveActions'] == False else True self.connection.rollback() # make sure any locks taken so far are released + #end def __init__ # could be used by hud to change hud style def set_hud_style(self, style): @@ -327,71 +448,82 @@ class Database: return row[0] else: return None + + def get_site_id(self, site): + c = self.get_cursor() + c.execute(self.sql.query['getSiteId'], (site,)) + result = c.fetchall() + return result def get_last_insert_id(self): - return self.fdb.getLastInsertId() + try: + ret = self.fdb.getLastInsertId() + except: + print "get_last_insert_id error:", str(sys.exc_value) + return ret #stores a stud/razz hand into the database - def ring_stud(self, config, settings, db, cursor, base, category, site_hand_no, gametype_id, hand_start_time + def ring_stud(self, config, settings, base, category, site_hand_no, gametype_id, hand_start_time ,names, player_ids, start_cashes, antes, card_values, card_suits, winnings, rakes ,action_types, allIns, action_amounts, actionNos, hudImportData, maxSeats, tableName ,seatNos): - fpdb_simple.fillCardArrays(len(names), base, category, card_values, card_suits) + try: + fpdb_simple.fillCardArrays(len(names), base, category, card_values, card_suits) - hands_id = fpdb_simple.storeHands(self.backend, db, cursor, site_hand_no, gametype_id - ,hand_start_time, names, tableName, maxSeats, hudImportData - ,(None, None, None, None, None), (None, None, None, None, None)) + hands_id = self.storeHands(self.backend, site_hand_no, gametype_id + ,hand_start_time, names, tableName, maxSeats, hudImportData + ,(None, None, None, None, None), (None, None, None, None, None)) - #print "before calling store_hands_players_stud, antes:", antes - hands_players_ids = fpdb_simple.store_hands_players_stud(self.backend, db, cursor, hands_id, player_ids - ,start_cashes, antes, card_values - ,card_suits, winnings, rakes, seatNos) + #print "before calling store_hands_players_stud, antes:", antes + hands_players_ids = self.store_hands_players_stud(self.backend, hands_id, player_ids + ,start_cashes, antes, card_values + ,card_suits, winnings, rakes, seatNos) - if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': - fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametype_id, hand_start_time, player_ids, hudImportData) + if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': + self.storeHudCache(self.backend, base, category, gametype_id, hand_start_time, player_ids, hudImportData) + except: + print "ring_stud error: " + str(sys.exc_value) # in case exception doesn't get printed + raise fpdb_simple.FpdbError("ring_stud error: " + str(sys.exc_value)) - if self.saveActions: - fpdb_simple.storeActions(cursor, hands_players_ids, action_types - ,allIns, action_amounts, actionNos) return hands_id #end def ring_stud - def ring_holdem_omaha(self, config, settings, db, cursor, base, category, site_hand_no, gametype_id + def ring_holdem_omaha(self, config, settings, base, category, site_hand_no, gametype_id ,hand_start_time, names, player_ids, start_cashes, positions, card_values ,card_suits, board_values, board_suits, winnings, rakes, action_types, allIns ,action_amounts, actionNos, hudImportData, maxSeats, tableName, seatNos): """stores a holdem/omaha hand into the database""" - t0 = time() - fpdb_simple.fillCardArrays(len(names), base, category, card_values, card_suits) - t1 = time() - fpdb_simple.fill_board_cards(board_values, board_suits) - t2 = time() + try: + t0 = time() + #print "in ring_holdem_omaha" + fpdb_simple.fillCardArrays(len(names), base, category, card_values, card_suits) + t1 = time() + fpdb_simple.fill_board_cards(board_values, board_suits) + t2 = time() - hands_id = fpdb_simple.storeHands(self.backend, db, cursor, site_hand_no, gametype_id - ,hand_start_time, names, tableName, maxSeats, - hudImportData, board_values, board_suits) - #TEMPORARY CALL! - Just until all functions are migrated - t3 = time() - hands_players_ids = fpdb_simple.store_hands_players_holdem_omaha( - self.backend, db, cursor, category, hands_id, player_ids, start_cashes - , positions, card_values, card_suits, winnings, rakes, seatNos, hudImportData) - t4 = time() - #print "ring holdem, backend=%d" % backend - if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': - fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametype_id, hand_start_time, player_ids, hudImportData) - t5 = time() - t6 = time() - if self.saveActions: - fpdb_simple.storeActions(cursor, hands_players_ids, action_types, allIns, action_amounts, actionNos) - t7 = time() - #print "fills=(%4.3f) saves=(%4.3f,%4.3f,%4.3f,%4.3f)" % (t2-t0, t3-t2, t4-t3, t5-t4, t6-t5) + hands_id = self.storeHands(self.backend, site_hand_no, gametype_id + ,hand_start_time, names, tableName, maxSeats + ,hudImportData, board_values, board_suits) + #TEMPORARY CALL! - Just until all functions are migrated + t3 = time() + hands_players_ids = self.store_hands_players_holdem_omaha( + self.backend, category, hands_id, player_ids, start_cashes + , positions, card_values, card_suits, winnings, rakes, seatNos, hudImportData) + t4 = time() + if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': + self.storeHudCache(self.backend, base, category, gametype_id, hand_start_time, player_ids, hudImportData) + t5 = time() + #print "fills=(%4.3f) saves=(%4.3f,%4.3f,%4.3f)" % (t2-t0, t3-t2, t4-t3, t5-t4) + except: + print "ring_holdem_omaha error: " + str(sys.exc_value) # in case exception doesn't get printed + raise fpdb_simple.FpdbError("ring_holdem_omaha error: " + str(sys.exc_value)) return hands_id #end def ring_holdem_omaha - def tourney_holdem_omaha(self, config, settings, db, cursor, base, category, siteTourneyNo, buyin, fee, knockout + def tourney_holdem_omaha(self, config, settings, base, category, siteTourneyNo, buyin, fee, knockout ,entries, prizepool, tourney_start, payin_amounts, ranks, tourneyTypeId ,siteId #end of tourney specific params ,site_hand_no, gametype_id, hand_start_time, names, player_ids @@ -400,63 +532,417 @@ class Database: ,actionNos, hudImportData, maxSeats, tableName, seatNos): """stores a tourney holdem/omaha hand into the database""" - fpdb_simple.fillCardArrays(len(names), base, category, card_values, card_suits) - fpdb_simple.fill_board_cards(board_values, board_suits) + try: + fpdb_simple.fillCardArrays(len(names), base, category, card_values, card_suits) + fpdb_simple.fill_board_cards(board_values, board_suits) - tourney_id = fpdb_simple.store_tourneys(cursor, tourneyTypeId, siteTourneyNo, entries, prizepool, tourney_start) - tourneys_players_ids = fpdb_simple.store_tourneys_players(cursor, tourney_id, player_ids, payin_amounts, ranks, winnings) + tourney_id = self.store_tourneys(tourneyTypeId, siteTourneyNo, entries, prizepool, tourney_start) + tourneys_players_ids = self.store_tourneys_players(tourney_id, player_ids, payin_amounts, ranks, winnings) - hands_id = fpdb_simple.storeHands(self.backend, db, cursor, site_hand_no, gametype_id - ,hand_start_time, names, tableName, maxSeats) + hands_id = self.storeHands(self.backend, site_hand_no, gametype_id + ,hand_start_time, names, tableName, maxSeats + ,hudImportData, board_values, board_suits) - hands_players_ids = fpdb_simple.store_hands_players_holdem_omaha_tourney( - self.backend, db, cursor, category, hands_id, player_ids, start_cashes, positions - , card_values, card_suits, winnings, rakes, seatNos, tourneys_players_ids) + hands_players_ids = self.store_hands_players_holdem_omaha_tourney( + self.backend, category, hands_id, player_ids, start_cashes, positions + , card_values, card_suits, winnings, rakes, seatNos, tourneys_players_ids + , hudImportData) - #print "tourney holdem, backend=%d" % backend - if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': - fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametype_id, hand_start_time, player_ids, hudImportData) + #print "tourney holdem, backend=%d" % backend + if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': + self.storeHudCache(self.backend, base, category, gametype_id, hand_start_time, player_ids, hudImportData) + except: + print "tourney_holdem_omaha error: " + str(sys.exc_value) # in case exception doesn't get printed + raise fpdb_simple.FpdbError("tourney_holdem_omaha error: " + str(sys.exc_value)) - if self.saveActions: - fpdb_simple.storeActions(cursor, hands_players_ids, action_types, allIns, action_amounts, actionNos) return hands_id #end def tourney_holdem_omaha - def tourney_stud(self, config, settings, db, cursor, base, category, siteTourneyNo, buyin, fee, knockout, entries + def tourney_stud(self, config, settings, base, category, siteTourneyNo, buyin, fee, knockout, entries ,prizepool, tourneyStartTime, payin_amounts, ranks, tourneyTypeId, siteId ,siteHandNo, gametypeId, handStartTime, names, playerIds, startCashes, antes ,cardValues, cardSuits, winnings, rakes, actionTypes, allIns, actionAmounts ,actionNos, hudImportData, maxSeats, tableName, seatNos): #stores a tourney stud/razz hand into the database - fpdb_simple.fillCardArrays(len(names), base, category, cardValues, cardSuits) + try: + fpdb_simple.fillCardArrays(len(names), base, category, cardValues, cardSuits) - tourney_id = fpdb_simple.store_tourneys(cursor, tourneyTypeId, siteTourneyNo, entries, prizepool, tourneyStartTime) + tourney_id = self.store_tourneys(tourneyTypeId, siteTourneyNo, entries, prizepool, tourneyStartTime) - tourneys_players_ids = fpdb_simple.store_tourneys_players(cursor, tourney_id, playerIds, payin_amounts, ranks, winnings) + tourneys_players_ids = self.store_tourneys_players(tourney_id, playerIds, payin_amounts, ranks, winnings) - hands_id = fpdb_simple.storeHands(self.backend, db, cursor, siteHandNo, gametypeId, handStartTime, names, tableName, maxSeats) + hands_id = self.storeHands( self.backend, siteHandNo, gametypeId + , handStartTime, names, tableName, maxSeats + , hudImportData, board_values, board_suits ) - hands_players_ids = fpdb_simple.store_hands_players_stud_tourney(self.backend, db, cursor, hands_id - , playerIds, startCashes, antes, cardValues, cardSuits - , winnings, rakes, seatNos, tourneys_players_ids) + hands_players_ids = self.store_hands_players_stud_tourney(self.backend, hands_id + , playerIds, startCashes, antes, cardValues, cardSuits + , winnings, rakes, seatNos, tourneys_players_ids) - if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': - fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametypeId, hand_start_time, playerIds, hudImportData) + if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop': + self.storeHudCache(self.backend, base, category, gametypeId, hand_start_time, playerIds, hudImportData) + except: + print "tourney_stud error: " + str(sys.exc_value) # in case exception doesn't get printed + raise fpdb_simple.FpdbError("tourney_stud error: " + str(sys.exc_value)) - if self.saveActions: - fpdb_simple.storeActions(cursor, hands_players_ids, actionTypes, allIns, actionAmounts, actionNos) return hands_id #end def tourney_stud + def prepareBulkImport(self): + """Drop some indexes/foreign keys to prepare for bulk import. + Currently keeping the standalone indexes as needed to import quickly""" + stime = time() + c = self.get_cursor() + # sc: don't think autocommit=0 is needed, should already be in that mode + if self.backend == self.MYSQL_INNODB: + c.execute("SET foreign_key_checks=0") + c.execute("SET autocommit=0") + return + if self.backend == self.PGSQL: + self.connection.set_isolation_level(0) # allow table/index operations to work + for fk in self.foreignKeys[self.backend]: + if fk['drop'] == 1: + if self.backend == self.MYSQL_INNODB: + c.execute("SELECT constraint_name " + + "FROM information_schema.KEY_COLUMN_USAGE " + + #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' + "WHERE 1=1 " + + "AND table_name = %s AND column_name = %s " + + "AND referenced_table_name = %s " + + "AND referenced_column_name = %s ", + (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) + cons = c.fetchone() + print "preparebulk find fk: cons=", cons + if cons: + print "dropping mysql fk", cons[0], fk['fktab'], fk['fkcol'] + try: + c.execute("alter table " + fk['fktab'] + " drop foreign key " + cons[0]) + except: + print " drop failed: " + str(sys.exc_info()) + elif self.backend == self.PGSQL: + # DON'T FORGET TO RECREATE THEM!! + print "dropping pg fk", fk['fktab'], fk['fkcol'] + try: + # try to lock table to see if index drop will work: + # hmmm, tested by commenting out rollback in grapher. lock seems to work but + # then drop still hangs :-( does work in some tests though?? + # will leave code here for now pending further tests/enhancement ... + c.execute( "lock table %s in exclusive mode nowait" % (fk['fktab'],) ) + #print "after lock, status:", c.statusmessage + #print "alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol']) + try: + c.execute("alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol'])) + print "dropped pg fk pg fk %s_%s_fkey, continuing ..." % (fk['fktab'], fk['fkcol']) + except: + if "does not exist" not in str(sys.exc_value): + print "warning: drop pg fk %s_%s_fkey failed: %s, continuing ..." \ + % (fk['fktab'], fk['fkcol'], str(sys.exc_value).rstrip('\n') ) + except: + print "warning: constraint %s_%s_fkey not dropped: %s, continuing ..." \ + % (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n')) + else: + print "Only MySQL and Postgres supported so far" + return -1 + + for idx in self.indexes[self.backend]: + if idx['drop'] == 1: + if self.backend == self.MYSQL_INNODB: + print "dropping mysql index ", idx['tab'], idx['col'] + try: + # apparently nowait is not implemented in mysql so this just hangs if there are locks + # preventing the index drop :-( + c.execute( "alter table %s drop index %s;", (idx['tab'],idx['col']) ) + except: + print " drop index failed: " + str(sys.exc_info()) + # ALTER TABLE `fpdb`.`handsplayers` DROP INDEX `playerId`; + # using: 'HandsPlayers' drop index 'playerId' + elif self.backend == self.PGSQL: + # DON'T FORGET TO RECREATE THEM!! + print "dropping pg index ", idx['tab'], idx['col'] + try: + # try to lock table to see if index drop will work: + c.execute( "lock table %s in exclusive mode nowait" % (idx['tab'],) ) + #print "after lock, status:", c.statusmessage + try: + # table locked ok so index drop should work: + #print "drop index %s_%s_idx" % (idx['tab'],idx['col']) + c.execute( "drop index if exists %s_%s_idx" % (idx['tab'],idx['col']) ) + #print "dropped pg index ", idx['tab'], idx['col'] + except: + if "does not exist" not in str(sys.exc_value): + print "warning: drop index %s_%s_idx failed: %s, continuing ..." \ + % (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n')) + except: + print "warning: index %s_%s_idx not dropped %s, continuing ..." \ + % (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n')) + else: + print "Error: Only MySQL and Postgres supported so far" + return -1 + + if self.backend == self.PGSQL: + self.connection.set_isolation_level(1) # go back to normal isolation level + self.commit() # seems to clear up errors if there were any in postgres + ptime = time() - stime + print "prepare import took", ptime, "seconds" + #end def prepareBulkImport + + def afterBulkImport(self): + """Re-create any dropped indexes/foreign keys after bulk import""" + stime = time() + + c = self.get_cursor() + if self.backend == self.MYSQL_INNODB: + c.execute("SET foreign_key_checks=1") + c.execute("SET autocommit=1") + return + + if self.backend == self.PGSQL: + self.connection.set_isolation_level(0) # allow table/index operations to work + for fk in self.foreignKeys[self.backend]: + if fk['drop'] == 1: + if self.backend == self.MYSQL_INNODB: + c.execute("SELECT constraint_name " + + "FROM information_schema.KEY_COLUMN_USAGE " + + #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' + "WHERE 1=1 " + + "AND table_name = %s AND column_name = %s " + + "AND referenced_table_name = %s " + + "AND referenced_column_name = %s ", + (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) + cons = c.fetchone() + #print "afterbulk: cons=", cons + if cons: + pass + else: + print "creating fk ", fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol'] + try: + c.execute("alter table " + fk['fktab'] + " add foreign key (" + + fk['fkcol'] + ") references " + fk['rtab'] + "(" + + fk['rcol'] + ")") + except: + print " create fk failed: " + str(sys.exc_info()) + elif self.backend == self.PGSQL: + print "creating fk ", fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol'] + try: + c.execute("alter table " + fk['fktab'] + " add constraint " + + fk['fktab'] + '_' + fk['fkcol'] + '_fkey' + + " foreign key (" + fk['fkcol'] + + ") references " + fk['rtab'] + "(" + fk['rcol'] + ")") + except: + print " create fk failed: " + str(sys.exc_info()) + else: + print "Only MySQL and Postgres supported so far" + return -1 + + for idx in self.indexes[self.backend]: + if idx['drop'] == 1: + if self.backend == self.MYSQL_INNODB: + print "creating mysql index ", idx['tab'], idx['col'] + try: + c.execute( "alter table %s add index %s(%s)" + , (idx['tab'],idx['col'],idx['col']) ) + except: + print " create fk failed: " + str(sys.exc_info()) + elif self.backend == self.PGSQL: + # pass + # mod to use tab_col for index name? + print "creating pg index ", idx['tab'], idx['col'] + try: + print "create index %s_%s_idx on %s(%s)" % (idx['tab'], idx['col'], idx['tab'], idx['col']) + c.execute( "create index %s_%s_idx on %s(%s)" + % (idx['tab'], idx['col'], idx['tab'], idx['col']) ) + except: + print " create index failed: " + str(sys.exc_info()) + else: + print "Only MySQL and Postgres supported so far" + return -1 + + if self.backend == self.PGSQL: + self.connection.set_isolation_level(1) # go back to normal isolation level + self.commit() # seems to clear up errors if there were any in postgres + atime = time() - stime + print "After import took", atime, "seconds" + #end def afterBulkImport + + def drop_referential_integrity(self): + """Update all tables to remove foreign keys""" + + c = self.get_cursor() + c.execute(self.sql.query['list_tables']) + result = c.fetchall() + + for i in range(len(result)): + c.execute("SHOW CREATE TABLE " + result[i][0]) + inner = c.fetchall() + + for j in range(len(inner)): + # result[i][0] - Table name + # result[i][1] - CREATE TABLE parameters + #Searching for CONSTRAINT `tablename_ibfk_1` + for m in re.finditer('(ibfk_[0-9]+)', inner[j][1]): + key = "`" + inner[j][0] + "_" + m.group() + "`" + c.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key) + self.commit() + #end drop_referential_inegrity + + def recreate_tables(self): + """(Re-)creates the tables of the current DB""" + + self.drop_tables() + self.create_tables() + self.createAllIndexes() + self.commit() + print "Finished recreating tables" + #end def recreate_tables + + def create_tables(self): + #todo: should detect and fail gracefully if tables already exist. + try: + logging.debug(self.sql.query['createSettingsTable']) + c = self.get_cursor() + c.execute(self.sql.query['createSettingsTable']) + logging.debug(self.sql.query['createSitesTable']) + c.execute(self.sql.query['createSitesTable']) + c.execute(self.sql.query['createGametypesTable']) + c.execute(self.sql.query['createPlayersTable']) + c.execute(self.sql.query['createAutoratesTable']) + c.execute(self.sql.query['createHandsTable']) + c.execute(self.sql.query['createTourneyTypesTable']) + c.execute(self.sql.query['createTourneysTable']) + c.execute(self.sql.query['createTourneysPlayersTable']) + c.execute(self.sql.query['createHandsPlayersTable']) + c.execute(self.sql.query['createHandsActionsTable']) + c.execute(self.sql.query['createHudCacheTable']) + #c.execute(self.sql.query['addTourneyIndex']) + #c.execute(self.sql.query['addHandsIndex']) + #c.execute(self.sql.query['addPlayersIndex']) + self.fillDefaultData() + self.commit() + except: + print "Error creating tables: ", str(sys.exc_value) + self.rollback() + raise fpdb_simple.FpdbError( "Error creating tables " + str(sys.exc_value) ) +#end def disconnect + + def drop_tables(self): + """Drops the fpdb tables from the current db""" + + try: + c = self.get_cursor() + if(self.get_backend_name() == 'MySQL InnoDB'): + #Databases with FOREIGN KEY support need this switched of before you can drop tables + self.drop_referential_integrity() + + # Query the DB to see what tables exist + c.execute(self.sql.query['list_tables']) + for table in c: + c.execute(self.sql.query['drop_table'] + table[0]) + elif(self.get_backend_name() == 'PostgreSQL'): + self.commit()# I have no idea why this makes the query work--REB 07OCT2008 + c.execute(self.sql.query['list_tables']) + tables = c.fetchall() + for table in tables: + c.execute(self.sql.query['drop_table'] + table[0] + ' cascade') + elif(self.get_backend_name() == 'SQLite'): + c.execute(self.sql.query['list_tables']) + for table in c.fetchall(): + logging.debug(self.sql.query['drop_table'] + table[0]) + c.execute(self.sql.query['drop_table'] + table[0]) + + self.commit() + except: + print "Error dropping tables: " + str(sys.exc_value) + raise fpdb_simple.FpdbError( "Error dropping tables " + str(sys.exc_value) ) + #end def drop_tables + + def createAllIndexes(self): + """Create new indexes""" + + try: + if self.backend == self.PGSQL: + self.connection.set_isolation_level(0) # allow table/index operations to work + for idx in self.indexes[self.backend]: + if self.backend == self.MYSQL_INNODB: + print "creating mysql index ", idx['tab'], idx['col'] + try: + self.get_cursor().execute( "alter table %s add index %s(%s)" + , (idx['tab'],idx['col'],idx['col']) ) + except: + pass + elif self.backend == self.PGSQL: + # mod to use tab_col for index name? + print "creating pg index ", idx['tab'], idx['col'] + try: + print "create index %s_%s_idx on %s(%s)" % (idx['tab'], idx['col'], idx['tab'], idx['col']) + self.get_cursor().execute( "create index %s_%s_idx on %s(%s)" + % (idx['tab'], idx['col'], idx['tab'], idx['col']) ) + except: + print " ERROR! :-(" + pass + else: + print "Only MySQL and Postgres supported so far" + return -1 + if self.backend == self.PGSQL: + self.connection.set_isolation_level(1) # go back to normal isolation level + except: + print "Error creating indexes: " + str(sys.exc_value) + raise fpdb_simple.FpdbError( "Error creating indexes " + str(sys.exc_value) ) + #end def createAllIndexes + + def dropAllIndexes(self): + """Drop all standalone indexes (i.e. not including primary keys or foreign keys) + using list of indexes in indexes data structure""" + # maybe upgrade to use data dictionary?? (but take care to exclude PK and FK) + if self.backend == self.PGSQL: + self.connection.set_isolation_level(0) # allow table/index operations to work + for idx in self.indexes[self.backend]: + if self.backend == self.MYSQL_INNODB: + print "dropping mysql index ", idx['tab'], idx['col'] + try: + self.get_cursor().execute( "alter table %s drop index %s" + , (idx['tab'],idx['col']) ) + except: + pass + elif self.backend == self.PGSQL: + print "dropping pg index ", idx['tab'], idx['col'] + # mod to use tab_col for index name? + try: + self.get_cursor().execute( "drop index %s_%s_idx" + % (idx['tab'],idx['col']) ) + except: + pass + else: + print "Only MySQL and Postgres supported so far" + return -1 + if self.backend == self.PGSQL: + self.connection.set_isolation_level(1) # go back to normal isolation level + #end def dropAllIndexes + + def fillDefaultData(self): + c = self.get_cursor() + c.execute("INSERT INTO Settings (version) VALUES (118);") + c.execute("INSERT INTO Sites (name,currency) VALUES ('Full Tilt Poker', 'USD')") + c.execute("INSERT INTO Sites (name,currency) VALUES ('PokerStars', 'USD')") + c.execute("INSERT INTO Sites (name,currency) VALUES ('Everleaf', 'USD')") + c.execute("INSERT INTO Sites (name,currency) VALUES ('Win2day', 'USD')") + c.execute("INSERT INTO TourneyTypes VALUES (DEFAULT, 1, 0, 0, 0, False);") + #c.execute("""INSERT INTO TourneyTypes + # (siteId,buyin,fee,knockout,rebuyOrAddon) VALUES + # (1,0,0,0,?)""",(False,) ) + #end def fillDefaultData + def rebuild_hudcache(self): """clears hudcache and rebuilds from the individual handsplayers records""" - stime = time() - self.connection.cursor().execute(self.sql.query['clearHudCache']) - self.connection.cursor().execute(self.sql.query['rebuildHudCache']) - self.commit() - print "Rebuild hudcache took %.1f seconds" % (time() - stime,) + try: + stime = time() + self.connection.cursor().execute(self.sql.query['clearHudCache']) + self.connection.cursor().execute(self.sql.query['rebuildHudCache']) + self.commit() + print "Rebuild hudcache took %.1f seconds" % (time() - stime,) + except: + print "Error rebuilding hudcache:", str(sys.exc_value) #end def rebuild_hudcache @@ -465,14 +951,13 @@ class Database: stime = time() if self.backend == self.MYSQL_INNODB: try: - self.cursor.execute(self.sql.query['analyze']) + self.get_cursor().execute(self.sql.query['analyze']) except: - print "Error during analyze" + print "Error during analyze:", str(sys.exc_value) elif self.backend == self.PGSQL: self.connection.set_isolation_level(0) # allow vacuum to work try: - self.cursor = self.get_cursor() - self.cursor.execute(self.sql.query['analyze']) + self.get_cursor().execute(self.sql.query['analyze']) except: print "Error during analyze:", str(sys.exc_value) self.connection.set_isolation_level(1) # go back to normal isolation level @@ -481,6 +966,767 @@ class Database: print "Analyze took %.1f seconds" % (atime,) #end def analyzeDB + +# Start of Hand Writing routines. Idea is to provide a mixture of routines to store Hand data +# however the calling prog requires. Main aims: +# - existing static routines from fpdb_simple just modified + + def lock_for_insert(self): + """Lock tables in MySQL to try to speed inserts up""" + try: + self.get_cursor().execute(self.sql.query['lockForInsert']) + except: + print "Error during fdb.lock_for_insert:", str(sys.exc_value) + #end def lock_for_insert + + + def store_the_hand(self, h): + """Take a HandToWrite object and store it in the db""" + + # Following code writes hands to database and commits (or rolls back if there is an error) + try: + result = None + if h.isTourney: + ranks = map(lambda x: 0, h.names) # create an array of 0's equal to the length of names + payin_amounts = fpdb_simple.calcPayin(len(h.names), h.buyin, h.fee) + + if h.base == "hold": + result = self.tourney_holdem_omaha( + h.config, h.settings, h.base, h.category, h.siteTourneyNo, h.buyin + , h.fee, h.knockout, h.entries, h.prizepool, h.tourneyStartTime + , h.payin_amounts, h.ranks, h.tourneyTypeId, h.siteID, h.siteHandNo + , h.gametypeID, h.handStartTime, h.names, h.playerIDs, h.startCashes + , h.positions, h.cardValues, h.cardSuits, h.boardValues, h.boardSuits + , h.winnings, h.rakes, h.actionTypes, h.allIns, h.actionAmounts + , h.actionNos, h.hudImportData, h.maxSeats, h.tableName, h.seatNos) + elif h.base == "stud": + result = self.tourney_stud( + h.config, h.settings, h.base, h.category, h.siteTourneyNo + , h.buyin, h.fee, h.knockout, h.entries, h.prizepool, h.tourneyStartTime + , h.payin_amounts, h.ranks, h.tourneyTypeId, h.siteID, h.siteHandNo + , h.gametypeID, h.handStartTime, h.names, h.playerIDs, h.startCashes + , h.antes, h.cardValues, h.cardSuits, h.winnings, h.rakes, h.actionTypes + , h.allIns, h.actionAmounts, h.actionNos, h.hudImportData, h.maxSeats + , h.tableName, h.seatNos) + else: + raise fpself.simple.Fpself.rror("unrecognised category") + else: + if h.base == "hold": + result = self.ring_holdem_omaha( + h.config, h.settings, h.base, h.category, h.siteHandNo + , h.gametypeID, h.handStartTime, h.names, h.playerIDs + , h.startCashes, h.positions, h.cardValues, h.cardSuits + , h.boardValues, h.boardSuits, h.winnings, h.rakes + , h.actionTypes, h.allIns, h.actionAmounts, h.actionNos + , h.hudImportData, h.maxSeats, h.tableName, h.seatNos) + elif h.base == "stud": + result = self.ring_stud( + h.config, h.settings, h.base, h.category, h.siteHandNo, h.gametypeID + , h.handStartTime, h.names, h.playerIDs, h.startCashes, h.antes + , h.cardValues, h.cardSuits, h.winnings, h.rakes, h.actionTypes, h.allIns + , h.actionAmounts, h.actionNos, h.hudImportData, h.maxSeats, h.tableName + , h.seatNos) + else: + raise fpself.simple.Fpself.rror ("unrecognised category") + self.commit() + except: + print "Error storing hand: " + str(sys.exc_value) + self.rollback() + + return result + #end def store_the_hand + + def storeHands(self, backend, site_hand_no, gametype_id + ,hand_start_time, names, tableName, maxSeats, hudCache + ,board_values, board_suits): + + cards = [Card.cardFromValueSuit(v,s) for v,s in zip(board_values,board_suits)] + #stores into table hands: + try: + self.get_cursor().execute ("""INSERT INTO Hands + (siteHandNo, gametypeId, handStart, seats, tableName, importTime, maxSeats + ,boardcard1,boardcard2,boardcard3,boardcard4,boardcard5 + ,playersVpi, playersAtStreet1, playersAtStreet2 + ,playersAtStreet3, playersAtStreet4, playersAtShowdown + ,street0Raises, street1Raises, street2Raises + ,street3Raises, street4Raises, street1Pot + ,street2Pot, street3Pot, street4Pot + ,showdownPot + ) + VALUES + (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) + """ + , (site_hand_no, gametype_id, hand_start_time, len(names), tableName, datetime.today(), maxSeats + ,cards[0], cards[1], cards[2], cards[3], cards[4] + ,hudCache['playersVpi'], hudCache['playersAtStreet1'], hudCache['playersAtStreet2'] + ,hudCache['playersAtStreet3'], hudCache['playersAtStreet4'], hudCache['playersAtShowdown'] + ,hudCache['street0Raises'], hudCache['street1Raises'], hudCache['street2Raises'] + ,hudCache['street3Raises'], hudCache['street4Raises'], hudCache['street1Pot'] + ,hudCache['street2Pot'], hudCache['street3Pot'], hudCache['street4Pot'] + ,hudCache['showdownPot'] + )) + ret = self.get_last_insert_id() + except: + ret = -1 + raise fpdb_simple.FpdbError( "storeHands error: " + str(sys.exc_value) ) + + return ret + #end def storeHands + + def store_hands_players_holdem_omaha(self, backend, category, hands_id, player_ids, start_cashes + ,positions, card_values, card_suits, winnings, rakes, seatNos, hudCache): + result=[] + + # postgres (and others?) needs the booleans converted to ints before saving: + # (or we could just save them as boolean ... but then we can't sum them so easily in sql ???) + # NO - storing booleans for now so don't need this + #hudCacheInt = {} + #for k,v in hudCache.iteritems(): + # if k in ('wonWhenSeenStreet1', 'wonAtSD', 'totalProfit'): + # hudCacheInt[k] = v + # else: + # hudCacheInt[k] = map(lambda x: 1 if x else 0, v) + + try: + inserts = [] + for i in xrange(len(player_ids)): + card1 = Card.cardFromValueSuit(card_values[i][0], card_suits[i][0]) + card2 = Card.cardFromValueSuit(card_values[i][1], card_suits[i][1]) + + if (category=="holdem"): + startCards = Card.twoStartCards(card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1]) + card3 = None + card4 = None + elif (category=="omahahi" or category=="omahahilo"): + startCards = Card.fourStartCards(card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1] + ,card_values[i][2], card_suits[i][2], card_values[i][3], card_suits[i][3]) + card3 = Card.cardFromValueSuit(card_values[i][2], card_suits[i][2]) + card4 = Card.cardFromValueSuit(card_values[i][3], card_suits[i][3]) + else: + raise fpdb_simple.FpdbError("invalid category") + + inserts.append( ( + hands_id, player_ids[i], start_cashes[i], positions[i], 1, # tourneytypeid + card1, card2, card3, card4, startCards, + winnings[i], rakes[i], seatNos[i], hudCache['totalProfit'][i], + hudCache['street0VPI'][i], hudCache['street0Aggr'][i], + hudCache['street0_3BChance'][i], hudCache['street0_3BDone'][i], + hudCache['street1Seen'][i], hudCache['street2Seen'][i], hudCache['street3Seen'][i], + hudCache['street4Seen'][i], hudCache['sawShowdown'][i], + hudCache['street1Aggr'][i], hudCache['street2Aggr'][i], hudCache['street3Aggr'][i], hudCache['street4Aggr'][i], + hudCache['otherRaisedStreet1'][i], hudCache['otherRaisedStreet2'][i], + hudCache['otherRaisedStreet3'][i], hudCache['otherRaisedStreet4'][i], + hudCache['foldToOtherRaisedStreet1'][i], hudCache['foldToOtherRaisedStreet2'][i], + hudCache['foldToOtherRaisedStreet3'][i], hudCache['foldToOtherRaisedStreet4'][i], + hudCache['wonWhenSeenStreet1'][i], hudCache['wonAtSD'][i], + hudCache['stealAttemptChance'][i], hudCache['stealAttempted'][i], hudCache['foldBbToStealChance'][i], + hudCache['foldedBbToSteal'][i], hudCache['foldSbToStealChance'][i], hudCache['foldedSbToSteal'][i], + hudCache['street1CBChance'][i], hudCache['street1CBDone'][i], hudCache['street2CBChance'][i], hudCache['street2CBDone'][i], + hudCache['street3CBChance'][i], hudCache['street3CBDone'][i], hudCache['street4CBChance'][i], hudCache['street4CBDone'][i], + hudCache['foldToStreet1CBChance'][i], hudCache['foldToStreet1CBDone'][i], + hudCache['foldToStreet2CBChance'][i], hudCache['foldToStreet2CBDone'][i], + hudCache['foldToStreet3CBChance'][i], hudCache['foldToStreet3CBDone'][i], + hudCache['foldToStreet4CBChance'][i], hudCache['foldToStreet4CBDone'][i], + hudCache['street1CheckCallRaiseChance'][i], hudCache['street1CheckCallRaiseDone'][i], + hudCache['street2CheckCallRaiseChance'][i], hudCache['street2CheckCallRaiseDone'][i], + hudCache['street3CheckCallRaiseChance'][i], hudCache['street3CheckCallRaiseDone'][i], + hudCache['street4CheckCallRaiseChance'][i], hudCache['street4CheckCallRaiseDone'][i], + hudCache['street0Calls'][i], hudCache['street1Calls'][i], hudCache['street2Calls'][i], hudCache['street3Calls'][i], hudCache['street4Calls'][i], + hudCache['street0Bets'][i], hudCache['street1Bets'][i], hudCache['street2Bets'][i], hudCache['street3Bets'][i], hudCache['street4Bets'][i] + ) ) + self.get_cursor().executemany (""" + INSERT INTO HandsPlayers + (handId, playerId, startCash, position, tourneyTypeId, + card1, card2, card3, card4, startCards, winnings, rake, seatNo, totalProfit, + street0VPI, street0Aggr, street0_3BChance, street0_3BDone, + street1Seen, street2Seen, street3Seen, street4Seen, sawShowdown, + street1Aggr, street2Aggr, street3Aggr, street4Aggr, + otherRaisedStreet1, otherRaisedStreet2, otherRaisedStreet3, otherRaisedStreet4, + foldToOtherRaisedStreet1, foldToOtherRaisedStreet2, foldToOtherRaisedStreet3, foldToOtherRaisedStreet4, + wonWhenSeenStreet1, wonAtSD, + stealAttemptChance, stealAttempted, foldBbToStealChance, foldedBbToSteal, foldSbToStealChance, foldedSbToSteal, + street1CBChance, street1CBDone, street2CBChance, street2CBDone, + street3CBChance, street3CBDone, street4CBChance, street4CBDone, + foldToStreet1CBChance, foldToStreet1CBDone, foldToStreet2CBChance, foldToStreet2CBDone, + foldToStreet3CBChance, foldToStreet3CBDone, foldToStreet4CBChance, foldToStreet4CBDone, + street1CheckCallRaiseChance, street1CheckCallRaiseDone, street2CheckCallRaiseChance, street2CheckCallRaiseDone, + street3CheckCallRaiseChance, street3CheckCallRaiseDone, street4CheckCallRaiseChance, street4CheckCallRaiseDone, + street0Calls, street1Calls, street2Calls, street3Calls, street4Calls, + street0Bets, street1Bets, street2Bets, street3Bets, street4Bets + ) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" + ,inserts ) + result.append( self.get_last_insert_id() ) + + #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) + #result.append(cursor.fetchall()[0][0]) + result.append( self.get_last_insert_id() ) + except: + raise fpdb_simple.FpdbError( "store_hands_players_holdem_omaha error: " + str(sys.exc_value) ) + + return result + #end def store_hands_players_holdem_omaha + + def store_hands_players_stud(self, backend, hands_id, player_ids, start_cashes, antes, + card_values, card_suits, winnings, rakes, seatNos): + #stores hands_players rows for stud/razz games. returns an array of the resulting IDs + + try: + result=[] + #print "before inserts in store_hands_players_stud, antes:", antes + for i in xrange(len(player_ids)): + card1 = Card.cardFromValueSuit(card_values[i][0], card_suits[i][0]) + card2 = Card.cardFromValueSuit(card_values[i][1], card_suits[i][1]) + card3 = Card.cardFromValueSuit(card_values[i][2], card_suits[i][2]) + card4 = Card.cardFromValueSuit(card_values[i][3], card_suits[i][3]) + card5 = Card.cardFromValueSuit(card_values[i][4], card_suits[i][4]) + card6 = Card.cardFromValueSuit(card_values[i][5], card_suits[i][5]) + card7 = Card.cardFromValueSuit(card_values[i][6], card_suits[i][6]) + + self.get_cursor().execute ("""INSERT INTO HandsPlayers + (handId, playerId, startCash, ante, tourneyTypeId, + card1, card2, + card3, card4, + card5, card6, + card7, winnings, rake, seatNo) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", + (hands_id, player_ids[i], start_cashes[i], antes[i], 1, + card1, card2, + card3, card4, + card5, card6, + card7, winnings[i], rakes[i], seatNos[i])) + #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) + #result.append(cursor.fetchall()[0][0]) + result.append( self.get_last_insert_id() ) + except: + raise fpdb_simple.FpdbError( "store_hands_players_stud error: " + str(sys.exc_value) ) + + return result + #end def store_hands_players_stud + + def store_hands_players_holdem_omaha_tourney(self, backend, category, hands_id, player_ids + ,start_cashes, positions, card_values, card_suits + ,winnings, rakes, seatNos, tourneys_players_ids + ,hudCache): + #stores hands_players for tourney holdem/omaha hands + + try: + result=[] + inserts = [] + for i in xrange(len(player_ids)): + card1 = Card.cardFromValueSuit(card_values[i][0], card_suits[i][0]) + card2 = Card.cardFromValueSuit(card_values[i][1], card_suits[i][1]) + + if len(card_values[0])==2: + startCards = Card.twoStartCards(card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1]) + card3 = None + card4 = None + elif len(card_values[0])==4: + startCards = Card.fourStartCards(card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1] + ,card_values[i][2], card_suits[i][2], card_values[i][3], card_suits[i][3]) + card3 = Card.cardFromValueSuit(card_values[i][2], card_suits[i][2]) + card4 = Card.cardFromValueSuit(card_values[i][3], card_suits[i][3]) + else: + raise FpdbError ("invalid card_values length:"+str(len(card_values[0]))) + + inserts.append( (hands_id, player_ids[i], start_cashes[i], positions[i], 1, # tourneytypeid + card1, card2, card3, card4, startCards, + winnings[i], rakes[i], tourneys_players_ids[i], seatNos[i], hudCache['totalProfit'][i], + hudCache['street0VPI'][i], hudCache['street0Aggr'][i], + hudCache['street0_3BChance'][i], hudCache['street0_3BDone'][i], + hudCache['street1Seen'][i], hudCache['street2Seen'][i], hudCache['street3Seen'][i], + hudCache['street4Seen'][i], hudCache['sawShowdown'][i], + hudCache['street1Aggr'][i], hudCache['street2Aggr'][i], hudCache['street3Aggr'][i], hudCache['street4Aggr'][i], + hudCache['otherRaisedStreet1'][i], hudCache['otherRaisedStreet2'][i], + hudCache['otherRaisedStreet3'][i], hudCache['otherRaisedStreet4'][i], + hudCache['foldToOtherRaisedStreet1'][i], hudCache['foldToOtherRaisedStreet2'][i], + hudCache['foldToOtherRaisedStreet3'][i], hudCache['foldToOtherRaisedStreet4'][i], + hudCache['wonWhenSeenStreet1'][i], hudCache['wonAtSD'][i], + hudCache['stealAttemptChance'][i], hudCache['stealAttempted'][i], hudCache['foldBbToStealChance'][i], + hudCache['foldedBbToSteal'][i], hudCache['foldSbToStealChance'][i], hudCache['foldedSbToSteal'][i], + hudCache['street1CBChance'][i], hudCache['street1CBDone'][i], hudCache['street2CBChance'][i], hudCache['street2CBDone'][i], + hudCache['street3CBChance'][i], hudCache['street3CBDone'][i], hudCache['street4CBChance'][i], hudCache['street4CBDone'][i], + hudCache['foldToStreet1CBChance'][i], hudCache['foldToStreet1CBDone'][i], + hudCache['foldToStreet2CBChance'][i], hudCache['foldToStreet2CBDone'][i], + hudCache['foldToStreet3CBChance'][i], hudCache['foldToStreet3CBDone'][i], + hudCache['foldToStreet4CBChance'][i], hudCache['foldToStreet4CBDone'][i], + hudCache['street1CheckCallRaiseChance'][i], hudCache['street1CheckCallRaiseDone'][i], + hudCache['street2CheckCallRaiseChance'][i], hudCache['street2CheckCallRaiseDone'][i], + hudCache['street3CheckCallRaiseChance'][i], hudCache['street3CheckCallRaiseDone'][i], + hudCache['street4CheckCallRaiseChance'][i], hudCache['street4CheckCallRaiseDone'][i], + hudCache['street0Calls'][i], hudCache['street1Calls'][i], hudCache['street2Calls'][i], + hudCache['street3Calls'][i], hudCache['street4Calls'][i], + hudCache['street0Bets'][i], hudCache['street1Bets'][i], hudCache['street2Bets'][i], + hudCache['street3Bets'][i], hudCache['street4Bets'][i] + ) ) + + self.get_cursor().executemany (""" + INSERT INTO HandsPlayers + (handId, playerId, startCash, position, tourneyTypeId, + card1, card2, card3, card4, startCards, winnings, rake, tourneysPlayersId, seatNo, totalProfit, + street0VPI, street0Aggr, street0_3BChance, street0_3BDone, + street1Seen, street2Seen, street3Seen, street4Seen, sawShowdown, + street1Aggr, street2Aggr, street3Aggr, street4Aggr, + otherRaisedStreet1, otherRaisedStreet2, otherRaisedStreet3, otherRaisedStreet4, + foldToOtherRaisedStreet1, foldToOtherRaisedStreet2, foldToOtherRaisedStreet3, foldToOtherRaisedStreet4, + wonWhenSeenStreet1, wonAtSD, + stealAttemptChance, stealAttempted, foldBbToStealChance, foldedBbToSteal, foldSbToStealChance, foldedSbToSteal, + street1CBChance, street1CBDone, street2CBChance, street2CBDone, + street3CBChance, street3CBDone, street4CBChance, street4CBDone, + foldToStreet1CBChance, foldToStreet1CBDone, foldToStreet2CBChance, foldToStreet2CBDone, + foldToStreet3CBChance, foldToStreet3CBDone, foldToStreet4CBChance, foldToStreet4CBDone, + street1CheckCallRaiseChance, street1CheckCallRaiseDone, street2CheckCallRaiseChance, street2CheckCallRaiseDone, + street3CheckCallRaiseChance, street3CheckCallRaiseDone, street4CheckCallRaiseChance, street4CheckCallRaiseDone, + street0Calls, street1Calls, street2Calls, street3Calls, street4Calls, + street0Bets, street1Bets, street2Bets, street3Bets, street4Bets + ) + VALUES + (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" + ,inserts ) + + result.append( self.get_last_insert_id() ) + #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) + #result.append(cursor.fetchall()[0][0]) + except: + raise fpdb_simple.FpdbError( "store_hands_players_holdem_omaha_tourney error: " + str(sys.exc_value) ) + + return result + #end def store_hands_players_holdem_omaha_tourney + + def store_hands_players_stud_tourney(self, backend, hands_id, player_ids, start_cashes, + antes, card_values, card_suits, winnings, rakes, seatNos, tourneys_players_ids): + #stores hands_players for tourney stud/razz hands + + try: + result=[] + for i in xrange(len(player_ids)): + self.get_cursor().execute ("""INSERT INTO HandsPlayers + (handId, playerId, startCash, ante, + card1Value, card1Suit, card2Value, card2Suit, + card3Value, card3Suit, card4Value, card4Suit, + card5Value, card5Suit, card6Value, card6Suit, + card7Value, card7Suit, winnings, rake, tourneysPlayersId, seatNo) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, %s)""", + (hands_id, player_ids[i], start_cashes[i], antes[i], + card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1], + card_values[i][2], card_suits[i][2], card_values[i][3], card_suits[i][3], + card_values[i][4], card_suits[i][4], card_values[i][5], card_suits[i][5], + card_values[i][6], card_suits[i][6], winnings[i], rakes[i], tourneys_players_ids[i], seatNos[i])) + #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) + #result.append(cursor.fetchall()[0][0]) + result.append( self.get_last_insert_id() ) + except: + raise fpdb_simple.FpdbError( "store_hands_players_stud_tourney error: " + str(sys.exc_value) ) + + return result + #end def store_hands_players_stud_tourney + + def storeHudCache(self, backend, base, category, gametypeId, hand_start_time, playerIds, hudImportData): + """Update cached statistics. If update fails because no record exists, do an insert. + Can't use array updates here (not easily anyway) because we need to insert any rows + that don't get updated.""" + + # if (category=="holdem" or category=="omahahi" or category=="omahahilo"): + try: + if self.use_date_in_hudcache: + #print "key =", "d%02d%02d%02d " % (hand_start_time.year-2000, hand_start_time.month, hand_start_time.day) + styleKey = "d%02d%02d%02d" % (hand_start_time.year-2000, hand_start_time.month, hand_start_time.day) + else: + # hard-code styleKey as 'A000000' (all-time cache, no key) for now + styleKey = 'A000000' + + #print "storeHudCache, len(playerIds)=", len(playerIds), " len(vpip)=" \ + #, len(hudImportData['street0VPI']), " len(totprof)=", len(hudImportData['totalProfit']) + for player in xrange(len(playerIds)): + + # Set up a clean row + row=[] + row.append(0)#blank for id + row.append(gametypeId) + row.append(playerIds[player]) + row.append(len(playerIds))#seats + for i in xrange(len(hudImportData)+2): + row.append(0) + + if base=="hold": + row[4]=hudImportData['position'][player] + else: + row[4]=0 + row[5]=1 #tourneysGametypeId + row[6]+=1 #HDs + if hudImportData['street0VPI'][player]: row[7]+=1 + if hudImportData['street0Aggr'][player]: row[8]+=1 + if hudImportData['street0_3BChance'][player]: row[9]+=1 + if hudImportData['street0_3BDone'][player]: row[10]+=1 + if hudImportData['street1Seen'][player]: row[11]+=1 + if hudImportData['street2Seen'][player]: row[12]+=1 + if hudImportData['street3Seen'][player]: row[13]+=1 + if hudImportData['street4Seen'][player]: row[14]+=1 + if hudImportData['sawShowdown'][player]: row[15]+=1 + if hudImportData['street1Aggr'][player]: row[16]+=1 + if hudImportData['street2Aggr'][player]: row[17]+=1 + if hudImportData['street3Aggr'][player]: row[18]+=1 + if hudImportData['street4Aggr'][player]: row[19]+=1 + if hudImportData['otherRaisedStreet1'][player]: row[20]+=1 + if hudImportData['otherRaisedStreet2'][player]: row[21]+=1 + if hudImportData['otherRaisedStreet3'][player]: row[22]+=1 + if hudImportData['otherRaisedStreet4'][player]: row[23]+=1 + if hudImportData['foldToOtherRaisedStreet1'][player]: row[24]+=1 + if hudImportData['foldToOtherRaisedStreet2'][player]: row[25]+=1 + if hudImportData['foldToOtherRaisedStreet3'][player]: row[26]+=1 + if hudImportData['foldToOtherRaisedStreet4'][player]: row[27]+=1 + if hudImportData['wonWhenSeenStreet1'][player]!=0.0: row[28]+=hudImportData['wonWhenSeenStreet1'][player] + if hudImportData['wonAtSD'][player]!=0.0: row[29]+=hudImportData['wonAtSD'][player] + if hudImportData['stealAttemptChance'][player]: row[30]+=1 + if hudImportData['stealAttempted'][player]: row[31]+=1 + if hudImportData['foldBbToStealChance'][player]: row[32]+=1 + if hudImportData['foldedBbToSteal'][player]: row[33]+=1 + if hudImportData['foldSbToStealChance'][player]: row[34]+=1 + if hudImportData['foldedSbToSteal'][player]: row[35]+=1 + + if hudImportData['street1CBChance'][player]: row[36]+=1 + if hudImportData['street1CBDone'][player]: row[37]+=1 + if hudImportData['street2CBChance'][player]: row[38]+=1 + if hudImportData['street2CBDone'][player]: row[39]+=1 + if hudImportData['street3CBChance'][player]: row[40]+=1 + if hudImportData['street3CBDone'][player]: row[41]+=1 + if hudImportData['street4CBChance'][player]: row[42]+=1 + if hudImportData['street4CBDone'][player]: row[43]+=1 + + if hudImportData['foldToStreet1CBChance'][player]: row[44]+=1 + if hudImportData['foldToStreet1CBDone'][player]: row[45]+=1 + if hudImportData['foldToStreet2CBChance'][player]: row[46]+=1 + if hudImportData['foldToStreet2CBDone'][player]: row[47]+=1 + if hudImportData['foldToStreet3CBChance'][player]: row[48]+=1 + if hudImportData['foldToStreet3CBDone'][player]: row[49]+=1 + if hudImportData['foldToStreet4CBChance'][player]: row[50]+=1 + if hudImportData['foldToStreet4CBDone'][player]: row[51]+=1 + + #print "player=", player + #print "len(totalProfit)=", len(hudImportData['totalProfit']) + if hudImportData['totalProfit'][player]: + row[52]+=hudImportData['totalProfit'][player] + + if hudImportData['street1CheckCallRaiseChance'][player]: row[53]+=1 + if hudImportData['street1CheckCallRaiseDone'][player]: row[54]+=1 + if hudImportData['street2CheckCallRaiseChance'][player]: row[55]+=1 + if hudImportData['street2CheckCallRaiseDone'][player]: row[56]+=1 + if hudImportData['street3CheckCallRaiseChance'][player]: row[57]+=1 + if hudImportData['street3CheckCallRaiseDone'][player]: row[58]+=1 + if hudImportData['street4CheckCallRaiseChance'][player]: row[59]+=1 + if hudImportData['street4CheckCallRaiseDone'][player]: row[60]+=1 + + # Try to do the update first: + cursor = self.get_cursor() + num = cursor.execute("""UPDATE HudCache + SET HDs=HDs+%s, street0VPI=street0VPI+%s, street0Aggr=street0Aggr+%s, + street0_3BChance=street0_3BChance+%s, street0_3BDone=street0_3BDone+%s, + street1Seen=street1Seen+%s, street2Seen=street2Seen+%s, street3Seen=street3Seen+%s, + street4Seen=street4Seen+%s, sawShowdown=sawShowdown+%s, + street1Aggr=street1Aggr+%s, street2Aggr=street2Aggr+%s, street3Aggr=street3Aggr+%s, + street4Aggr=street4Aggr+%s, otherRaisedStreet1=otherRaisedStreet1+%s, + otherRaisedStreet2=otherRaisedStreet2+%s, otherRaisedStreet3=otherRaisedStreet3+%s, + otherRaisedStreet4=otherRaisedStreet4+%s, + foldToOtherRaisedStreet1=foldToOtherRaisedStreet1+%s, foldToOtherRaisedStreet2=foldToOtherRaisedStreet2+%s, + foldToOtherRaisedStreet3=foldToOtherRaisedStreet3+%s, foldToOtherRaisedStreet4=foldToOtherRaisedStreet4+%s, + wonWhenSeenStreet1=wonWhenSeenStreet1+%s, wonAtSD=wonAtSD+%s, stealAttemptChance=stealAttemptChance+%s, + stealAttempted=stealAttempted+%s, foldBbToStealChance=foldBbToStealChance+%s, + foldedBbToSteal=foldedBbToSteal+%s, + foldSbToStealChance=foldSbToStealChance+%s, foldedSbToSteal=foldedSbToSteal+%s, + street1CBChance=street1CBChance+%s, street1CBDone=street1CBDone+%s, street2CBChance=street2CBChance+%s, + street2CBDone=street2CBDone+%s, street3CBChance=street3CBChance+%s, + street3CBDone=street3CBDone+%s, street4CBChance=street4CBChance+%s, street4CBDone=street4CBDone+%s, + foldToStreet1CBChance=foldToStreet1CBChance+%s, foldToStreet1CBDone=foldToStreet1CBDone+%s, + foldToStreet2CBChance=foldToStreet2CBChance+%s, foldToStreet2CBDone=foldToStreet2CBDone+%s, + foldToStreet3CBChance=foldToStreet3CBChance+%s, + foldToStreet3CBDone=foldToStreet3CBDone+%s, foldToStreet4CBChance=foldToStreet4CBChance+%s, + foldToStreet4CBDone=foldToStreet4CBDone+%s, totalProfit=totalProfit+%s, + street1CheckCallRaiseChance=street1CheckCallRaiseChance+%s, + street1CheckCallRaiseDone=street1CheckCallRaiseDone+%s, street2CheckCallRaiseChance=street2CheckCallRaiseChance+%s, + street2CheckCallRaiseDone=street2CheckCallRaiseDone+%s, street3CheckCallRaiseChance=street3CheckCallRaiseChance+%s, + street3CheckCallRaiseDone=street3CheckCallRaiseDone+%s, street4CheckCallRaiseChance=street4CheckCallRaiseChance+%s, + street4CheckCallRaiseDone=street4CheckCallRaiseDone+%s + WHERE gametypeId+0=%s + AND playerId=%s + AND activeSeats=%s + AND position=%s + AND tourneyTypeId+0=%s + AND styleKey=%s + """, (row[6], row[7], row[8], row[9], row[10], + row[11], row[12], row[13], row[14], row[15], + row[16], row[17], row[18], row[19], row[20], + row[21], row[22], row[23], row[24], row[25], + row[26], row[27], row[28], row[29], row[30], + row[31], row[32], row[33], row[34], row[35], + row[36], row[37], row[38], row[39], row[40], + row[41], row[42], row[43], row[44], row[45], + row[46], row[47], row[48], row[49], row[50], + row[51], row[52], row[53], row[54], row[55], + row[56], row[57], row[58], row[59], row[60], + row[1], row[2], row[3], str(row[4]), row[5], styleKey)) + # Test statusmessage to see if update worked, do insert if not + #print "storehud2, upd num =", num + if ( (backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") + or (backend == self.MYSQL_INNODB and num == 0) ): + #print "playerid before insert:",row[2]," num = ", num + cursor.execute("""INSERT INTO HudCache + (gametypeId, playerId, activeSeats, position, tourneyTypeId, styleKey, + HDs, street0VPI, street0Aggr, street0_3BChance, street0_3BDone, + street1Seen, street2Seen, street3Seen, street4Seen, sawShowdown, + street1Aggr, street2Aggr, street3Aggr, street4Aggr, otherRaisedStreet1, + otherRaisedStreet2, otherRaisedStreet3, otherRaisedStreet4, foldToOtherRaisedStreet1, foldToOtherRaisedStreet2, + foldToOtherRaisedStreet3, foldToOtherRaisedStreet4, wonWhenSeenStreet1, wonAtSD, stealAttemptChance, + stealAttempted, foldBbToStealChance, foldedBbToSteal, foldSbToStealChance, foldedSbToSteal, + street1CBChance, street1CBDone, street2CBChance, street2CBDone, street3CBChance, + street3CBDone, street4CBChance, street4CBDone, foldToStreet1CBChance, foldToStreet1CBDone, + foldToStreet2CBChance, foldToStreet2CBDone, foldToStreet3CBChance, foldToStreet3CBDone, foldToStreet4CBChance, + foldToStreet4CBDone, totalProfit, street1CheckCallRaiseChance, street1CheckCallRaiseDone, street2CheckCallRaiseChance, + street2CheckCallRaiseDone, street3CheckCallRaiseChance, street3CheckCallRaiseDone, street4CheckCallRaiseChance, street4CheckCallRaiseDone) + VALUES (%s, %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s, + %s, %s, %s, %s, %s)""" + , (row[1], row[2], row[3], row[4], row[5], styleKey, row[6], row[7], row[8], row[9], row[10] + ,row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20] + ,row[21], row[22], row[23], row[24], row[25], row[26], row[27], row[28], row[29], row[30] + ,row[31], row[32], row[33], row[34], row[35], row[36], row[37], row[38], row[39], row[40] + ,row[41], row[42], row[43], row[44], row[45], row[46], row[47], row[48], row[49], row[50] + ,row[51], row[52], row[53], row[54], row[55], row[56], row[57], row[58], row[59], row[60]) ) + #print "hopefully inserted hud data line: ", cursor.statusmessage + # message seems to be "INSERT 0 1" + else: + #print "updated(2) hud data line" + pass + # else: + # print "todo: implement storeHudCache for stud base" + + except: + raise fpdb_simple.FpdbError( "storeHudCache error: " + str(sys.exc_value) ) + + #end def storeHudCache + + def store_tourneys(self, tourneyTypeId, siteTourneyNo, entries, prizepool, startTime): + try: + cursor = self.get_cursor() + cursor.execute("SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s", (siteTourneyNo, tourneyTypeId)) + tmp=cursor.fetchone() + #print "tried SELECTing tourneys.id, result:",tmp + + try: + len(tmp) + except TypeError:#means we have to create new one + cursor.execute("""INSERT INTO Tourneys + (tourneyTypeId, siteTourneyNo, entries, prizepool, startTime) + VALUES (%s, %s, %s, %s, %s)""", (tourneyTypeId, siteTourneyNo, entries, prizepool, startTime)) + cursor.execute("SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s", (siteTourneyNo, tourneyTypeId)) + tmp=cursor.fetchone() + #print "created new tourneys.id:",tmp + except: + raise fpdb_simple.FpdbError( "store_tourneys error: " + str(sys.exc_value) ) + + return tmp[0] + #end def store_tourneys + + def store_tourneys_players(self, tourney_id, player_ids, payin_amounts, ranks, winnings): + try: + result=[] + cursor = self.get_cursor() + #print "in store_tourneys_players. tourney_id:",tourney_id + #print "player_ids:",player_ids + #print "payin_amounts:",payin_amounts + #print "ranks:",ranks + #print "winnings:",winnings + for i in xrange(len(player_ids)): + cursor.execute("SELECT id FROM TourneysPlayers WHERE tourneyId=%s AND playerId+0=%s", (tourney_id, player_ids[i])) + tmp=cursor.fetchone() + #print "tried SELECTing tourneys_players.id:",tmp + + try: + len(tmp) + except TypeError: + cursor.execute("""INSERT INTO TourneysPlayers + (tourneyId, playerId, payinAmount, rank, winnings) VALUES (%s, %s, %s, %s, %s)""", + (tourney_id, player_ids[i], payin_amounts[i], ranks[i], winnings[i])) + + cursor.execute("SELECT id FROM TourneysPlayers WHERE tourneyId=%s AND playerId+0=%s", + (tourney_id, player_ids[i])) + tmp=cursor.fetchone() + #print "created new tourneys_players.id:",tmp + result.append(tmp[0]) + except: + raise fpdb_simple.FpdbError( "store_tourneys_players error: " + str(sys.exc_value) ) + + return result + #end def store_tourneys_players + + +# Class used to hold all the data needed to write a hand to the db +# mainParser() in fpdb_parse_logic.py creates one of these and then passes it to + +class HandToWrite: + + def __init__(self, finished = False): # db_name and game not used any more + try: + self.finished = finished + self.config = None + self.settings = None + self.base = None + self.category = None + self.siteTourneyNo = None + self.buyin = None + self.fee = None + self.knockout = None + self.entries = None + self.prizepool = None + self.tourneyStartTime = None + self.isTourney = None + self.tourneyTypeId = None + self.siteID = None + self.siteHandNo = None + self.gametypeID = None + self.handStartTime = None + self.names = None + self.playerIDs = None + self.startCashes = None + self.positions = None + self.antes = None + self.cardValues = None + self.cardSuits = None + self.boardValues = None + self.boardSuits = None + self.winnings = None + self.rakes = None + self.actionTypes = None + self.allIns = None + self.actionAmounts = None + self.actionNos = None + self.hudImportData = None + self.maxSeats = None + self.tableName = None + self.seatNos = None + except: + print "htw.init error: " + str(sys.exc_info()) + raise + # end def __init__ + + def set_all( self, config, settings, base, category, siteTourneyNo, buyin + , fee, knockout, entries, prizepool, tourneyStartTime + , isTourney, tourneyTypeId, siteID, siteHandNo + , gametypeID, handStartTime, names, playerIDs, startCashes + , positions, antes, cardValues, cardSuits, boardValues, boardSuits + , winnings, rakes, actionTypes, allIns, actionAmounts + , actionNos, hudImportData, maxSeats, tableName, seatNos): + + try: + self.config = config + self.settings = settings + self.base = base + self.category = category + self.siteTourneyNo = siteTourneyNo + self.buyin = buyin + self.fee = fee + self.knockout = knockout + self.entries = entries + self.prizepool = prizepool + self.tourneyStartTime = tourneyStartTime + self.isTourney = isTourney + self.tourneyTypeId = tourneyTypeId + self.siteID = siteID + self.siteHandNo = siteHandNo + self.gametypeID = gametypeID + self.handStartTime = handStartTime + self.names = names + self.playerIDs = playerIDs + self.startCashes = startCashes + self.positions = positions + self.antes = antes + self.cardValues = cardValues + self.cardSuits = cardSuits + self.boardValues = boardValues + self.boardSuits = boardSuits + self.winnings = winnings + self.rakes = rakes + self.actionTypes = actionTypes + self.allIns = allIns + self.actionAmounts = actionAmounts + self.actionNos = actionNos + self.hudImportData = hudImportData + self.maxSeats = maxSeats + self.tableName = tableName + self.seatNos = seatNos + except: + print "htw.set_all error: " + str(sys.exc_info()) + raise + # end def set_hand + + def set_ring_holdem_omaha( self, config, settings, base, category, siteHandNo + , gametypeID, handStartTime, names, playerIDs + , startCashes, positions, cardValues, cardSuits + , boardValues, boardSuits, winnings, rakes + , actionTypes, allIns, actionAmounts, actionNos + , hudImportData, maxSeats, tableName, seatNos ): + self.config = config + self.settings = settings + self.base = base + self.category = category + self.siteHandNo = siteHandNo + self.gametypeID = gametypeID + self.handStartTime = handStartTime + self.names = names + self.playerIDs = playerIDs + self.startCashes = startCashes + self.positions = positions + self.cardValues = cardValues + self.cardSuits = cardSuits + self.boardValues = boardValues + self.boardSuits = boardSuits + self.winnings = winnings + self.rakes = rakes + self.actionTypes = actionTypes + self.allIns = allIns + self.actionAmounts = actionAmounts + self.actionNos = actionNos + self.hudImportData = hudImportData + self.maxSeats = maxSeats + self.tableName = tableName + self.seatNos = seatNos + # end def set_ring_holdem_omaha + + def send_ring_holdem_omaha(self, db): + result = db.ring_holdem_omaha( + self.config, self.settings, self.base, self.category, self.siteHandNo + , self.gametypeID, self.handStartTime, self.names, self.playerIDs + , self.startCashes, self.positions, self.cardValues, self.cardSuits + , self.boardValues, self.boardSuits, self.winnings, self.rakes + , self.actionTypes, self.allIns, self.actionAmounts, self.actionNos + , self.hudImportData, self.maxSeats, self.tableName, self.seatNos) + # end def send_ring_holdem_omaha + + def get_finished(self): + return( self.finished ) + # end def get_finished + + def get_siteHandNo(self): + return( self.siteHandNo ) + # end def get_siteHandNo + + if __name__=="__main__": c = Configuration.Config() diff --git a/pyfpdb/FpdbSQLQueries.py b/pyfpdb/FpdbSQLQueries.py index 7a5f0ff5..b47f45eb 100644 --- a/pyfpdb/FpdbSQLQueries.py +++ b/pyfpdb/FpdbSQLQueries.py @@ -30,849 +30,6 @@ class FpdbSQLQueries: self.query = {} self.dbname = db - ################################ - # List tables - ################################ - if(self.dbname == 'MySQL InnoDB'): - self.query['list_tables'] = """SHOW TABLES""" - elif(self.dbname == 'PostgreSQL'): - self.query['list_tables'] = """SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'""" - elif(self.dbname == 'SQLite'): - self.query['list_tables'] = """SELECT name FROM sqlite_master - WHERE type='table' - ORDER BY name;""" - - ################################################################## - # Drop Tables - MySQL, PostgreSQL and SQLite all share same syntax - ################################################################## - - if(self.dbname == 'MySQL InnoDB') or (self.dbname == 'PostgreSQL') or (self.dbname == 'SQLite'): - self.query['drop_table'] = """DROP TABLE IF EXISTS """ - - - ################################ - # Create Tables - ################################ - - ################################ - # Create Settings - ################################ - if(self.dbname == 'MySQL InnoDB'): - self.query['createSettingsTable'] = """CREATE TABLE Settings ( - version SMALLINT NOT NULL) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createSettingsTable'] = """CREATE TABLE Settings (version SMALLINT)""" - - elif(self.dbname == 'SQLite'): - self.query['createSettingsTable'] = """CREATE TABLE Settings - (version INTEGER) """ - - - ################################ - # Create Sites - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createSitesTable'] = """CREATE TABLE Sites ( - id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - name varchar(32) NOT NULL, - currency char(3) NOT NULL) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createSitesTable'] = """CREATE TABLE Sites ( - id SERIAL, PRIMARY KEY (id), - name varchar(32), - currency char(3))""" - elif(self.dbname == 'SQLite'): - self.query['createSitesTable'] = """CREATE TABLE Sites ( - id INTEGER PRIMARY KEY, - name TEXT NOT NULL, - currency TEXT NOT NULL)""" - - - ################################ - # Create Gametypes - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createGametypesTable'] = """CREATE TABLE Gametypes ( - id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - siteId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (siteId) REFERENCES Sites(id), - type char(4) NOT NULL, - base char(4) NOT NULL, - category varchar(9) NOT NULL, - limitType char(2) NOT NULL, - hiLo char(1) NOT NULL, - smallBlind int, - bigBlind int, - smallBet int NOT NULL, - bigBet int NOT NULL) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createGametypesTable'] = """CREATE TABLE Gametypes ( - id SERIAL, PRIMARY KEY (id), - siteId INTEGER, FOREIGN KEY (siteId) REFERENCES Sites(id), - type char(4), - base char(4), - category varchar(9), - limitType char(2), - hiLo char(1), - smallBlind int, - bigBlind int, - smallBet int, - bigBet int)""" - elif(self.dbname == 'SQLite'): - self.query['createGametypesTable'] = """CREATE TABLE GameTypes ( - id INTEGER PRIMARY KEY, - siteId INTEGER, - type TEXT, - base TEXT, - category TEXT, - limitType TEXT, - hiLo TEXT, - smallBlind INTEGER, - bigBlind INTEGER, - smallBet INTEGER, - bigBet INTEGER, - FOREIGN KEY(siteId) REFERENCES Sites(id) ON DELETE CASCADE)""" - - - ################################ - # Create Players - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createPlayersTable'] = """CREATE TABLE Players ( - id INT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - name VARCHAR(32) CHARACTER SET utf8 NOT NULL, - siteId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (siteId) REFERENCES Sites(id), - comment text, - commentTs DATETIME) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createPlayersTable'] = """CREATE TABLE Players ( - id SERIAL, PRIMARY KEY (id), - name VARCHAR(32), - siteId INTEGER, FOREIGN KEY (siteId) REFERENCES Sites(id), - comment text, - commentTs timestamp without time zone)""" - elif(self.dbname == 'SQLite'): - self.query['createPlayersTable'] = """CREATE TABLE Players ( - id INTEGER PRIMARY KEY, - name TEXT, - siteId INTEGER, - comment TEXT, - commentTs BLOB, - FOREIGN KEY(siteId) REFERENCES Sites(id) ON DELETE CASCADE)""" - - - ################################ - # Create Autorates - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createAutoratesTable'] = """CREATE TABLE Autorates ( - id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), - gametypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), - description varchar(50) NOT NULL, - shortDesc char(8) NOT NULL, - ratingTime DATETIME NOT NULL, - handCount int NOT NULL) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createAutoratesTable'] = """CREATE TABLE Autorates ( - id BIGSERIAL, PRIMARY KEY (id), - playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), - gametypeId INT, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), - description varchar(50), - shortDesc char(8), - ratingTime timestamp without time zone, - handCount int)""" - elif(self.dbname == 'SQLite'): - self.query['createAutoratesTable'] = """ """ - - - ################################ - # Create Hands - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createHandsTable'] = """CREATE TABLE Hands ( - id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - tableName VARCHAR(20) NOT NULL, - siteHandNo BIGINT NOT NULL, - gametypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), - handStart DATETIME NOT NULL, - importTime DATETIME NOT NULL, - seats TINYINT NOT NULL, - maxSeats TINYINT NOT NULL, - boardcard1 smallint, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ - boardcard2 smallint, - boardcard3 smallint, - boardcard4 smallint, - boardcard5 smallint, - texture smallint, - playersVpi SMALLINT NOT NULL, /* num of players vpi */ - playersAtStreet1 SMALLINT NOT NULL, /* num of players seeing flop/street4 */ - playersAtStreet2 SMALLINT NOT NULL, - playersAtStreet3 SMALLINT NOT NULL, - playersAtStreet4 SMALLINT NOT NULL, - playersAtShowdown SMALLINT NOT NULL, - street0Raises TINYINT NOT NULL, /* num small bets paid to see flop/street4, including blind */ - street1Raises TINYINT NOT NULL, /* num small bets paid to see turn/street5 */ - street2Raises TINYINT NOT NULL, /* num big bets paid to see river/street6 */ - street3Raises TINYINT NOT NULL, /* num big bets paid to see sd/street7 */ - street4Raises TINYINT NOT NULL, /* num big bets paid to see showdown */ - street1Pot INT, /* pot size at flop/street4 */ - street2Pot INT, /* pot size at turn/street5 */ - street3Pot INT, /* pot size at river/street6 */ - street4Pot INT, /* pot size at sd/street7 */ - showdownPot INT, /* pot size at sd/street7 */ - comment TEXT, - commentTs DATETIME) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createHandsTable'] = """CREATE TABLE Hands ( - id BIGSERIAL, PRIMARY KEY (id), - tableName VARCHAR(20) NOT NULL, - siteHandNo BIGINT NOT NULL, - gametypeId INT NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), - handStart timestamp without time zone NOT NULL, - importTime timestamp without time zone NOT NULL, - seats SMALLINT NOT NULL, - maxSeats SMALLINT NOT NULL, - boardcard1 smallint, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ - boardcard2 smallint, - boardcard3 smallint, - boardcard4 smallint, - boardcard5 smallint, - texture smallint, - playersVpi SMALLINT NOT NULL, /* num of players vpi */ - playersAtStreet1 SMALLINT NOT NULL, /* num of players seeing flop/street4 */ - playersAtStreet2 SMALLINT NOT NULL, - playersAtStreet3 SMALLINT NOT NULL, - playersAtStreet4 SMALLINT NOT NULL, - playersAtShowdown SMALLINT NOT NULL, - street0Raises SMALLINT NOT NULL, /* num small bets paid to see flop/street4, including blind */ - street1Raises SMALLINT NOT NULL, /* num small bets paid to see turn/street5 */ - street2Raises SMALLINT NOT NULL, /* num big bets paid to see river/street6 */ - street3Raises SMALLINT NOT NULL, /* num big bets paid to see sd/street7 */ - street4Raises SMALLINT NOT NULL, /* num big bets paid to see showdown */ - street1Pot INT, /* pot size at flop/street4 */ - street2Pot INT, /* pot size at turn/street5 */ - street3Pot INT, /* pot size at river/street6 */ - street4Pot INT, /* pot size at sd/street7 */ - showdownPot INT, /* pot size at sd/street7 */ - comment TEXT, - commentTs timestamp without time zone)""" - elif(self.dbname == 'SQLite'): - self.query['createHandsTable'] = """CREATE TABLE Hands ( - id INTEGER PRIMARY KEY, - tableName TEXT(20), - siteHandNo INTEGER, - gametypeId INTEGER, - handStart BLOB, - importTime BLOB, - seats INTEGER, - maxSeats INTEGER, - comment TEXT, - commentTs BLOB, - FOREIGN KEY(gametypeId) REFERENCES Gametypes(id) ON DELETE CASCADE)""" - - - ################################ - # Create TourneyTypes - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createTourneyTypesTable'] = """CREATE TABLE TourneyTypes ( - id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - siteId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (siteId) REFERENCES Sites(id), - buyin INT NOT NULL, - fee INT NOT NULL, - knockout INT NOT NULL, - rebuyOrAddon BOOLEAN NOT NULL) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createTourneyTypesTable'] = """CREATE TABLE TourneyTypes ( - id SERIAL, PRIMARY KEY (id), - siteId INT, FOREIGN KEY (siteId) REFERENCES Sites(id), - buyin INT, - fee INT, - knockout INT, - rebuyOrAddon BOOLEAN)""" - elif(self.dbname == 'SQLite'): - self.query['createTourneyTypesTable'] = """ """ - - - ################################ - # Create Tourneys - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( - id INT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), - siteTourneyNo BIGINT NOT NULL, - entries INT NOT NULL, - prizepool INT NOT NULL, - startTime DATETIME NOT NULL, - comment TEXT, - commentTs DATETIME) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( - id SERIAL, PRIMARY KEY (id), - tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), - siteTourneyNo BIGINT, - entries INT, - prizepool INT, - startTime timestamp without time zone, - comment TEXT, - commentTs timestamp without time zone)""" - elif(self.dbname == 'SQLite'): - self.query['createTourneysTable'] = """CREATE TABLE TourneyTypes ( - id INTEGER PRIMARY KEY, - siteId INTEGER, - buyin INTEGER, - fee INTEGER, - knockout INTEGER, - rebuyOrAddon BOOL, - FOREIGN KEY(siteId) REFERENCES Sites(id) ON DELETE CASCADE)""" - - ################################ - # Create HandsPlayers - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createHandsPlayersTable'] = """CREATE TABLE HandsPlayers ( - id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - handId BIGINT UNSIGNED NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id), - playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), - startCash INT NOT NULL, - position CHAR(1), - seatNo SMALLINT NOT NULL, - - card1 smallint NOT NULL, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ - card2 smallint NOT NULL, - card3 smallint, - card4 smallint, - card5 smallint, - card6 smallint, - card7 smallint, - startCards smallint, - - ante INT, - winnings int NOT NULL, - rake int NOT NULL, - totalProfit INT NOT NULL, - comment text, - commentTs DATETIME, - tourneysPlayersId BIGINT UNSIGNED, - tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), - - wonWhenSeenStreet1 FLOAT NOT NULL, - wonWhenSeenStreet2 FLOAT, - wonWhenSeenStreet3 FLOAT, - wonWhenSeenStreet4 FLOAT, - wonAtSD FLOAT NOT NULL, - - street0VPI BOOLEAN NOT NULL, - street0Aggr BOOLEAN NOT NULL, - street0_3BChance BOOLEAN NOT NULL, - street0_3BDone BOOLEAN NOT NULL, - street0_4BChance BOOLEAN, - street0_4BDone BOOLEAN, - other3BStreet0 BOOLEAN, - other4BStreet0 BOOLEAN, - - street1Seen BOOLEAN NOT NULL, - street2Seen BOOLEAN NOT NULL, - street3Seen BOOLEAN NOT NULL, - street4Seen BOOLEAN NOT NULL, - sawShowdown BOOLEAN NOT NULL, - - street1Aggr BOOLEAN NOT NULL, - street2Aggr BOOLEAN NOT NULL, - street3Aggr BOOLEAN NOT NULL, - street4Aggr BOOLEAN NOT NULL, - - otherRaisedStreet0 BOOLEAN, - otherRaisedStreet1 BOOLEAN NOT NULL, - otherRaisedStreet2 BOOLEAN NOT NULL, - otherRaisedStreet3 BOOLEAN NOT NULL, - otherRaisedStreet4 BOOLEAN NOT NULL, - foldToOtherRaisedStreet0 BOOLEAN, - foldToOtherRaisedStreet1 BOOLEAN NOT NULL, - foldToOtherRaisedStreet2 BOOLEAN NOT NULL, - foldToOtherRaisedStreet3 BOOLEAN NOT NULL, - foldToOtherRaisedStreet4 BOOLEAN NOT NULL, - - stealAttemptChance BOOLEAN NOT NULL, - stealAttempted BOOLEAN NOT NULL, - foldBbToStealChance BOOLEAN NOT NULL, - foldedBbToSteal BOOLEAN NOT NULL, - foldSbToStealChance BOOLEAN NOT NULL, - foldedSbToSteal BOOLEAN NOT NULL, - - street1CBChance BOOLEAN NOT NULL, - street1CBDone BOOLEAN NOT NULL, - street2CBChance BOOLEAN NOT NULL, - street2CBDone BOOLEAN NOT NULL, - street3CBChance BOOLEAN NOT NULL, - street3CBDone BOOLEAN NOT NULL, - street4CBChance BOOLEAN NOT NULL, - street4CBDone BOOLEAN NOT NULL, - - foldToStreet1CBChance BOOLEAN NOT NULL, - foldToStreet1CBDone BOOLEAN NOT NULL, - foldToStreet2CBChance BOOLEAN NOT NULL, - foldToStreet2CBDone BOOLEAN NOT NULL, - foldToStreet3CBChance BOOLEAN NOT NULL, - foldToStreet3CBDone BOOLEAN NOT NULL, - foldToStreet4CBChance BOOLEAN NOT NULL, - foldToStreet4CBDone BOOLEAN NOT NULL, - - street1CheckCallRaiseChance BOOLEAN NOT NULL, - street1CheckCallRaiseDone BOOLEAN NOT NULL, - street2CheckCallRaiseChance BOOLEAN NOT NULL, - street2CheckCallRaiseDone BOOLEAN NOT NULL, - street3CheckCallRaiseChance BOOLEAN NOT NULL, - street3CheckCallRaiseDone BOOLEAN NOT NULL, - street4CheckCallRaiseChance BOOLEAN NOT NULL, - street4CheckCallRaiseDone BOOLEAN NOT NULL, - - street0Calls TINYINT, - street1Calls TINYINT, - street2Calls TINYINT, - street3Calls TINYINT, - street4Calls TINYINT, - street0Bets TINYINT, - street1Bets TINYINT, - street2Bets TINYINT, - street3Bets TINYINT, - street4Bets TINYINT, - street0Raises TINYINT, - street1Raises TINYINT, - street2Raises TINYINT, - street3Raises TINYINT, - street4Raises TINYINT, - - actionString VARCHAR(15), - - FOREIGN KEY (tourneysPlayersId) REFERENCES TourneysPlayers(id)) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createHandsPlayersTable'] = """CREATE TABLE HandsPlayers ( - id BIGSERIAL, PRIMARY KEY (id), - handId BIGINT NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id), - playerId INT NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), - startCash INT NOT NULL, - position CHAR(1), - seatNo SMALLINT NOT NULL, - - card1 smallint NOT NULL, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ - card2 smallint NOT NULL, - card3 smallint, - card4 smallint, - card5 smallint, - card6 smallint, - card7 smallint, - startCards smallint, - - ante INT, - winnings int NOT NULL, - rake int NOT NULL, - totalProfit INT NOT NULL, - comment text, - commentTs timestamp without time zone, - tourneysPlayersId BIGINT, - tourneyTypeId INT NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), - - wonWhenSeenStreet1 FLOAT NOT NULL, - wonWhenSeenStreet2 FLOAT, - wonWhenSeenStreet3 FLOAT, - wonWhenSeenStreet4 FLOAT, - wonAtSD FLOAT NOT NULL, - - street0VPI BOOLEAN NOT NULL, - street0Aggr BOOLEAN NOT NULL, - street0_3BChance BOOLEAN NOT NULL, - street0_3BDone BOOLEAN NOT NULL, - street0_4BChance BOOLEAN, - street0_4BDone BOOLEAN, - other3BStreet0 BOOLEAN, - other4BStreet0 BOOLEAN, - - street1Seen BOOLEAN NOT NULL, - street2Seen BOOLEAN NOT NULL, - street3Seen BOOLEAN NOT NULL, - street4Seen BOOLEAN NOT NULL, - sawShowdown BOOLEAN NOT NULL, - - street1Aggr BOOLEAN NOT NULL, - street2Aggr BOOLEAN NOT NULL, - street3Aggr BOOLEAN NOT NULL, - street4Aggr BOOLEAN NOT NULL, - - otherRaisedStreet0 BOOLEAN, - otherRaisedStreet1 BOOLEAN NOT NULL, - otherRaisedStreet2 BOOLEAN NOT NULL, - otherRaisedStreet3 BOOLEAN NOT NULL, - otherRaisedStreet4 BOOLEAN NOT NULL, - foldToOtherRaisedStreet0 BOOLEAN, - foldToOtherRaisedStreet1 BOOLEAN NOT NULL, - foldToOtherRaisedStreet2 BOOLEAN NOT NULL, - foldToOtherRaisedStreet3 BOOLEAN NOT NULL, - foldToOtherRaisedStreet4 BOOLEAN NOT NULL, - - stealAttemptChance BOOLEAN NOT NULL, - stealAttempted BOOLEAN NOT NULL, - foldBbToStealChance BOOLEAN NOT NULL, - foldedBbToSteal BOOLEAN NOT NULL, - foldSbToStealChance BOOLEAN NOT NULL, - foldedSbToSteal BOOLEAN NOT NULL, - - street1CBChance BOOLEAN NOT NULL, - street1CBDone BOOLEAN NOT NULL, - street2CBChance BOOLEAN NOT NULL, - street2CBDone BOOLEAN NOT NULL, - street3CBChance BOOLEAN NOT NULL, - street3CBDone BOOLEAN NOT NULL, - street4CBChance BOOLEAN NOT NULL, - street4CBDone BOOLEAN NOT NULL, - - foldToStreet1CBChance BOOLEAN NOT NULL, - foldToStreet1CBDone BOOLEAN NOT NULL, - foldToStreet2CBChance BOOLEAN NOT NULL, - foldToStreet2CBDone BOOLEAN NOT NULL, - foldToStreet3CBChance BOOLEAN NOT NULL, - foldToStreet3CBDone BOOLEAN NOT NULL, - foldToStreet4CBChance BOOLEAN NOT NULL, - foldToStreet4CBDone BOOLEAN NOT NULL, - - street1CheckCallRaiseChance BOOLEAN NOT NULL, - street1CheckCallRaiseDone BOOLEAN NOT NULL, - street2CheckCallRaiseChance BOOLEAN NOT NULL, - street2CheckCallRaiseDone BOOLEAN NOT NULL, - street3CheckCallRaiseChance BOOLEAN NOT NULL, - street3CheckCallRaiseDone BOOLEAN NOT NULL, - street4CheckCallRaiseChance BOOLEAN NOT NULL, - street4CheckCallRaiseDone BOOLEAN NOT NULL, - - street0Calls SMALLINT, - street1Calls SMALLINT, - street2Calls SMALLINT, - street3Calls SMALLINT, - street4Calls SMALLINT, - street0Bets SMALLINT, - street1Bets SMALLINT, - street2Bets SMALLINT, - street3Bets SMALLINT, - street4Bets SMALLINT, - street0Raises SMALLINT, - street1Raises SMALLINT, - street2Raises SMALLINT, - street3Raises SMALLINT, - street4Raises SMALLINT, - - actionString VARCHAR(15), - - FOREIGN KEY (tourneysPlayersId) REFERENCES TourneysPlayers(id))""" - elif(self.dbname == 'SQLite'): - self.query['createHandsPlayersTable'] = """ """ - - - ################################ - # Create TourneysPlayers - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createTourneysPlayersTable'] = """CREATE TABLE TourneysPlayers ( - id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - tourneyId INT UNSIGNED NOT NULL, FOREIGN KEY (tourneyId) REFERENCES Tourneys(id), - playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), - payinAmount INT NOT NULL, - rank INT NOT NULL, - winnings INT NOT NULL, - comment TEXT, - commentTs DATETIME) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createTourneysPlayersTable'] = """CREATE TABLE TourneysPlayers ( - id BIGSERIAL, PRIMARY KEY (id), - tourneyId INT, FOREIGN KEY (tourneyId) REFERENCES Tourneys(id), - playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), - payinAmount INT, - rank INT, - winnings INT, - comment TEXT, - commentTs timestamp without time zone)""" - elif(self.dbname == 'SQLite'): - self.query['createTourneysPlayersTable'] = """ """ - - - ################################ - # Create HandsActions - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( - id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - handsPlayerId BIGINT UNSIGNED NOT NULL, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), - street SMALLINT NOT NULL, - actionNo SMALLINT NOT NULL, - action CHAR(5) NOT NULL, - allIn BOOLEAN NOT NULL, - amount INT NOT NULL, - comment TEXT, - commentTs DATETIME) - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( - id BIGSERIAL, PRIMARY KEY (id), - handsPlayerId BIGINT, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), - street SMALLINT, - actionNo SMALLINT, - action CHAR(5), - allIn BOOLEAN, - amount INT, - comment TEXT, - commentTs timestamp without time zone)""" - elif(self.dbname == 'SQLite'): - self.query['createHandsActionsTable'] = """ """ - - - ################################ - # Create HudCache - ################################ - - if(self.dbname == 'MySQL InnoDB'): - self.query['createHudCacheTable'] = """CREATE TABLE HudCache ( - id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), - gametypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), - playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), - activeSeats SMALLINT NOT NULL, - position CHAR(1), - tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), - styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ - HDs INT NOT NULL, - - wonWhenSeenStreet1 FLOAT NOT NULL, - wonWhenSeenStreet2 FLOAT, - wonWhenSeenStreet3 FLOAT, - wonWhenSeenStreet4 FLOAT, - wonAtSD FLOAT NOT NULL, - - street0VPI INT NOT NULL, - street0Aggr INT NOT NULL, - street0_3BChance INT NOT NULL, - street0_3BDone INT NOT NULL, - street0_4BChance INT, - street0_4BDone INT, - other3BStreet0 INT, - other4BStreet0 INT, - - street1Seen INT NOT NULL, - street2Seen INT NOT NULL, - street3Seen INT NOT NULL, - street4Seen INT NOT NULL, - sawShowdown INT NOT NULL, - - street1Aggr INT NOT NULL, - street2Aggr INT NOT NULL, - street3Aggr INT NOT NULL, - street4Aggr INT NOT NULL, - - otherRaisedStreet0 INT, - otherRaisedStreet1 INT NOT NULL, - otherRaisedStreet2 INT NOT NULL, - otherRaisedStreet3 INT NOT NULL, - otherRaisedStreet4 INT NOT NULL, - foldToOtherRaisedStreet0 INT, - foldToOtherRaisedStreet1 INT NOT NULL, - foldToOtherRaisedStreet2 INT NOT NULL, - foldToOtherRaisedStreet3 INT NOT NULL, - foldToOtherRaisedStreet4 INT NOT NULL, - - stealAttemptChance INT NOT NULL, - stealAttempted INT NOT NULL, - foldBbToStealChance INT NOT NULL, - foldedBbToSteal INT NOT NULL, - foldSbToStealChance INT NOT NULL, - foldedSbToSteal INT NOT NULL, - - street1CBChance INT NOT NULL, - street1CBDone INT NOT NULL, - street2CBChance INT NOT NULL, - street2CBDone INT NOT NULL, - street3CBChance INT NOT NULL, - street3CBDone INT NOT NULL, - street4CBChance INT NOT NULL, - street4CBDone INT NOT NULL, - - foldToStreet1CBChance INT NOT NULL, - foldToStreet1CBDone INT NOT NULL, - foldToStreet2CBChance INT NOT NULL, - foldToStreet2CBDone INT NOT NULL, - foldToStreet3CBChance INT NOT NULL, - foldToStreet3CBDone INT NOT NULL, - foldToStreet4CBChance INT NOT NULL, - foldToStreet4CBDone INT NOT NULL, - - totalProfit INT NOT NULL, - - street1CheckCallRaiseChance INT NOT NULL, - street1CheckCallRaiseDone INT NOT NULL, - street2CheckCallRaiseChance INT NOT NULL, - street2CheckCallRaiseDone INT NOT NULL, - street3CheckCallRaiseChance INT NOT NULL, - street3CheckCallRaiseDone INT NOT NULL, - street4CheckCallRaiseChance INT NOT NULL, - street4CheckCallRaiseDone INT NOT NULL, - - street0Calls INT, - street1Calls INT, - street2Calls INT, - street3Calls INT, - street4Calls INT, - street0Bets INT, - street1Bets INT, - street2Bets INT, - street3Bets INT, - street4Bets INT, - street0Raises INT, - street1Raises INT, - street2Raises INT, - street3Raises INT, - street4Raises INT) - - ENGINE=INNODB""" - elif(self.dbname == 'PostgreSQL'): - self.query['createHudCacheTable'] = """CREATE TABLE HudCache ( - id BIGSERIAL, PRIMARY KEY (id), - gametypeId INT, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), - playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), - activeSeats SMALLINT, - position CHAR(1), - tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), - styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ - HDs INT, - - wonWhenSeenStreet1 FLOAT NOT NULL, - wonWhenSeenStreet2 FLOAT, - wonWhenSeenStreet3 FLOAT, - wonWhenSeenStreet4 FLOAT, - wonAtSD FLOAT NOT NULL, - - street0VPI INT NOT NULL, - street0Aggr INT, - street0_3BChance INT NOT NULL, - street0_3BDone INT NOT NULL, - street0_4BChance INT, - street0_4BDone INT, - other3BStreet0 INT, - other4BStreet0 INT, - - street1Seen INT, - street2Seen INT, - street3Seen INT, - street4Seen INT, - sawShowdown INT, - street1Aggr INT, - street2Aggr INT, - street3Aggr INT, - street4Aggr INT, - - otherRaisedStreet0 INT, - otherRaisedStreet1 INT, - otherRaisedStreet2 INT, - otherRaisedStreet3 INT, - otherRaisedStreet4 INT, - foldToOtherRaisedStreet0 INT, - foldToOtherRaisedStreet1 INT, - foldToOtherRaisedStreet2 INT, - foldToOtherRaisedStreet3 INT, - foldToOtherRaisedStreet4 INT, - - stealAttemptChance INT, - stealAttempted INT, - foldBbToStealChance INT, - foldedBbToSteal INT, - foldSbToStealChance INT, - foldedSbToSteal INT, - - street1CBChance INT, - street1CBDone INT, - street2CBChance INT, - street2CBDone INT, - street3CBChance INT, - street3CBDone INT, - street4CBChance INT, - street4CBDone INT, - - foldToStreet1CBChance INT, - foldToStreet1CBDone INT, - foldToStreet2CBChance INT, - foldToStreet2CBDone INT, - foldToStreet3CBChance INT, - foldToStreet3CBDone INT, - foldToStreet4CBChance INT, - foldToStreet4CBDone INT, - - totalProfit INT, - - street1CheckCallRaiseChance INT, - street1CheckCallRaiseDone INT, - street2CheckCallRaiseChance INT, - street2CheckCallRaiseDone INT, - street3CheckCallRaiseChance INT, - street3CheckCallRaiseDone INT, - street4CheckCallRaiseChance INT, - street4CheckCallRaiseDone INT, - - street0Calls INT, - street1Calls INT, - street2Calls INT, - street3Calls INT, - street4Calls INT, - street0Bets INT, - street1Bets INT, - street2Bets INT, - street3Bets INT, - street4Bets INT, - street0Raises INT, - street1Raises INT, - street2Raises INT, - street3Raises INT, - street4Raises INT) - """ - elif(self.dbname == 'SQLite'): - self.query['createHudCacheTable'] = """ """ - - if(self.dbname == 'MySQL InnoDB'): - self.query['addTourneyIndex'] = """ALTER TABLE Tourneys ADD INDEX siteTourneyNo(siteTourneyNo)""" - elif(self.dbname == 'PostgreSQL'): - self.query['addTourneyIndex'] = """CREATE INDEX siteTourneyNo ON Tourneys (siteTourneyNo)""" - elif(self.dbname == 'SQLite'): - self.query['addHandsIndex'] = """ """ - - if(self.dbname == 'MySQL InnoDB'): - self.query['addHandsIndex'] = """ALTER TABLE Hands ADD INDEX siteHandNo(siteHandNo)""" - elif(self.dbname == 'PostgreSQL'): - self.query['addHandsIndex'] = """CREATE INDEX siteHandNo ON Hands (siteHandNo)""" - elif(self.dbname == 'SQLite'): - self.query['addHandsIndex'] = """ """ - - if(self.dbname == 'MySQL InnoDB'): - self.query['addPlayersIndex'] = """ALTER TABLE Players ADD INDEX name(name)""" - elif(self.dbname == 'PostgreSQL'): - self.query['addPlayersIndex'] = """CREATE INDEX name ON Players (name)""" - elif(self.dbname == 'SQLite'): - self.query['addPlayersIndex'] = """ """ - if(self.dbname == 'MySQL InnoDB' or self.dbname == 'PostgreSQL'): self.query['set tx level'] = """SET SESSION TRANSACTION diff --git a/pyfpdb/GuiBulkImport.py b/pyfpdb/GuiBulkImport.py index 6829195d..f363be6c 100755 --- a/pyfpdb/GuiBulkImport.py +++ b/pyfpdb/GuiBulkImport.py @@ -34,6 +34,7 @@ import Configuration class GuiBulkImport(): + # not used def import_dir(self): """imports a directory, non-recursive. todo: move this to fpdb_import so CLI can use it""" @@ -80,12 +81,13 @@ class GuiBulkImport(): ttime = time() - starttime if ttime == 0: ttime = 1 - print 'GuiBulkImport.import_dir done: Stored: %d \tDuplicates: %d \tPartial: %d \tErrors: %d in %s seconds - %d/sec'\ + print 'GuiBulkImport.load done: Stored: %d \tDuplicates: %d \tPartial: %d \tErrors: %d in %s seconds - %d/sec'\ % (stored, dups, partial, errs, ttime, stored / ttime) self.importer.clearFileList() self.lab_info.set_text("Import finished") except: + print "bulkimport.loadclicked error: "+str(sys.exc_value) pass self.settings['global_lock'].release() else: @@ -95,10 +97,10 @@ class GuiBulkImport(): """returns the vbox of this thread""" return self.vbox - def __init__(self, settings, config): + def __init__(self, settings, config, sql = None): self.settings = settings self.config = config - self.importer = fpdb_import.Importer(self, self.settings, config) + self.importer = fpdb_import.Importer(self, self.settings, config, sql) self.vbox = gtk.VBox(False, 0) self.vbox.show() @@ -262,7 +264,8 @@ def main(argv=None): else: #Do something useful importer = fpdb_import.Importer(False,settings, config) - importer.setDropIndexes("auto") + # importer.setDropIndexes("auto") + importer.setDropIndexes("don't drop") importer.setFailOnError(options.failOnError) importer.addBulkImportImportFileOrDir(os.path.expanduser(options.filename), site=options.filtername) importer.setCallHud(False) diff --git a/pyfpdb/Hand.py b/pyfpdb/Hand.py index 6eb1d9b4..4e7d4bdf 100644 --- a/pyfpdb/Hand.py +++ b/pyfpdb/Hand.py @@ -1323,31 +1323,19 @@ class Pot(object): # Total pot $124.30 Main pot $98.90. Side pot $23.40. | Rake $2 def __str__(self): + if self.sym is None: + self.sym = "C" if self.total is None: print "call Pot.end() before printing pot total" # NB if I'm sure end() is idempotent, call it here. raise FpdbParseError - -# TODO: This really neeads to be a loop to handle multiple side pots - if len(self.pots) == 1: # (only use Total pot) - return "Total pot %s%.2f" % (self.sym, self.total,) - elif len(self.pots) == 2: - return "Total pot %s%.2f Main pot %s%.2f. Side pot %s%2.f." % (self.sym, self.total, self.sym, self.pots[0], self.sym, self.pots[1]) - elif len(self.pots) == 3: - return "Total pot %s%.2f Main pot $%.2f. Side pot-1 %s%2.2f. Side pot-2 %s%.2f." % (self.sym, self.total, self.sym, self.pots[0], self.sym, self.pots[1], self.sym, self.pots[2]) - elif len(self.pots) == 0: - # no small blind and walk in bb (hopefully) - return "Total pot %s%.2f" % (self.sym, self.total,) - else: - return ("too many pots.. no small blind and walk in bb?. self.pots: %s" %(self.pots)) - # I don't know stars format for a walk in the bb when sb doesn't post. - # The thing to do here is raise a Hand error like fpdb import does and file it into errors.txt - - - - - + ret = "Total pot %s%.2f" % (self.sym, self.total) + if len(self.pots) < 2: + return ret; + ret += " Main pot %s%.2f" % (self.sym, self.pots[0]) + + return ret + ''.join([ (" Side pot %s%.2f." % (self.sym, self.pots[x]) ) for x in xrange(1, len(self.pots)) ]) def assemble(cnxn, handid): c = cnxn.cursor() diff --git a/pyfpdb/SQL.py b/pyfpdb/SQL.py index b75a17dc..9b480628 100644 --- a/pyfpdb/SQL.py +++ b/pyfpdb/SQL.py @@ -161,7 +161,847 @@ class Sql: # Support for the Free Poker DataBase = fpdb http://fpdb.sourceforge.net/ # if type == 'fpdb': - + + ################################ + # List tables + ################################ + print "db_server =", db_server + if db_server == 'mysql': + self.query['list_tables'] = """SHOW TABLES""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['list_tables'] = """SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['list_tables'] = """SELECT name FROM sqlite_master + WHERE type='table' + ORDER BY name;""" + + ################################################################## + # Drop Tables - MySQL, PostgreSQL and SQLite all share same syntax + ################################################################## + + self.query['drop_table'] = """DROP TABLE IF EXISTS """ + + + ################################ + # Create Settings + ################################ + if db_server == 'mysql': + self.query['createSettingsTable'] = """CREATE TABLE Settings ( + version SMALLINT NOT NULL) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createSettingsTable'] = """CREATE TABLE Settings (version SMALLINT)""" + + elif db_server == 'sqlite': # what is the correct value here? + self.query['createSettingsTable'] = """CREATE TABLE Settings + (version INTEGER) """ + + + ################################ + # Create Sites + ################################ + + if db_server == 'mysql': + self.query['createSitesTable'] = """CREATE TABLE Sites ( + id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + name varchar(32) NOT NULL, + currency char(3) NOT NULL) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createSitesTable'] = """CREATE TABLE Sites ( + id SERIAL, PRIMARY KEY (id), + name varchar(32), + currency char(3))""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createSitesTable'] = """CREATE TABLE Sites ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + currency TEXT NOT NULL)""" + + + ################################ + # Create Gametypes + ################################ + + if db_server == 'mysql': + self.query['createGametypesTable'] = """CREATE TABLE Gametypes ( + id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + siteId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (siteId) REFERENCES Sites(id), + type char(4) NOT NULL, + base char(4) NOT NULL, + category varchar(9) NOT NULL, + limitType char(2) NOT NULL, + hiLo char(1) NOT NULL, + smallBlind int, + bigBlind int, + smallBet int NOT NULL, + bigBet int NOT NULL) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createGametypesTable'] = """CREATE TABLE Gametypes ( + id SERIAL, PRIMARY KEY (id), + siteId INTEGER, FOREIGN KEY (siteId) REFERENCES Sites(id), + type char(4), + base char(4), + category varchar(9), + limitType char(2), + hiLo char(1), + smallBlind int, + bigBlind int, + smallBet int, + bigBet int)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createGametypesTable'] = """CREATE TABLE GameTypes ( + id INTEGER PRIMARY KEY, + siteId INTEGER, + type TEXT, + base TEXT, + category TEXT, + limitType TEXT, + hiLo TEXT, + smallBlind INTEGER, + bigBlind INTEGER, + smallBet INTEGER, + bigBet INTEGER, + FOREIGN KEY(siteId) REFERENCES Sites(id) ON DELETE CASCADE)""" + + + ################################ + # Create Players + ################################ + + if db_server == 'mysql': + self.query['createPlayersTable'] = """CREATE TABLE Players ( + id INT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + name VARCHAR(32) CHARACTER SET utf8 NOT NULL, + siteId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (siteId) REFERENCES Sites(id), + comment text, + commentTs DATETIME) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createPlayersTable'] = """CREATE TABLE Players ( + id SERIAL, PRIMARY KEY (id), + name VARCHAR(32), + siteId INTEGER, FOREIGN KEY (siteId) REFERENCES Sites(id), + comment text, + commentTs timestamp without time zone)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createPlayersTable'] = """CREATE TABLE Players ( + id INTEGER PRIMARY KEY, + name TEXT, + siteId INTEGER, + comment TEXT, + commentTs BLOB, + FOREIGN KEY(siteId) REFERENCES Sites(id) ON DELETE CASCADE)""" + + + ################################ + # Create Autorates + ################################ + + if db_server == 'mysql': + self.query['createAutoratesTable'] = """CREATE TABLE Autorates ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), + gametypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), + description varchar(50) NOT NULL, + shortDesc char(8) NOT NULL, + ratingTime DATETIME NOT NULL, + handCount int NOT NULL) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createAutoratesTable'] = """CREATE TABLE Autorates ( + id BIGSERIAL, PRIMARY KEY (id), + playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), + gametypeId INT, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), + description varchar(50), + shortDesc char(8), + ratingTime timestamp without time zone, + handCount int)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createAutoratesTable'] = """ """ + + + ################################ + # Create Hands + ################################ + + if db_server == 'mysql': + self.query['createHandsTable'] = """CREATE TABLE Hands ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + tableName VARCHAR(20) NOT NULL, + siteHandNo BIGINT NOT NULL, + gametypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), + handStart DATETIME NOT NULL, + importTime DATETIME NOT NULL, + seats TINYINT NOT NULL, + maxSeats TINYINT NOT NULL, + boardcard1 smallint, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ + boardcard2 smallint, + boardcard3 smallint, + boardcard4 smallint, + boardcard5 smallint, + texture smallint, + playersVpi SMALLINT NOT NULL, /* num of players vpi */ + playersAtStreet1 SMALLINT NOT NULL, /* num of players seeing flop/street4 */ + playersAtStreet2 SMALLINT NOT NULL, + playersAtStreet3 SMALLINT NOT NULL, + playersAtStreet4 SMALLINT NOT NULL, + playersAtShowdown SMALLINT NOT NULL, + street0Raises TINYINT NOT NULL, /* num small bets paid to see flop/street4, including blind */ + street1Raises TINYINT NOT NULL, /* num small bets paid to see turn/street5 */ + street2Raises TINYINT NOT NULL, /* num big bets paid to see river/street6 */ + street3Raises TINYINT NOT NULL, /* num big bets paid to see sd/street7 */ + street4Raises TINYINT NOT NULL, /* num big bets paid to see showdown */ + street1Pot INT, /* pot size at flop/street4 */ + street2Pot INT, /* pot size at turn/street5 */ + street3Pot INT, /* pot size at river/street6 */ + street4Pot INT, /* pot size at sd/street7 */ + showdownPot INT, /* pot size at sd/street7 */ + comment TEXT, + commentTs DATETIME) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createHandsTable'] = """CREATE TABLE Hands ( + id BIGSERIAL, PRIMARY KEY (id), + tableName VARCHAR(20) NOT NULL, + siteHandNo BIGINT NOT NULL, + gametypeId INT NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), + handStart timestamp without time zone NOT NULL, + importTime timestamp without time zone NOT NULL, + seats SMALLINT NOT NULL, + maxSeats SMALLINT NOT NULL, + boardcard1 smallint, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ + boardcard2 smallint, + boardcard3 smallint, + boardcard4 smallint, + boardcard5 smallint, + texture smallint, + playersVpi SMALLINT NOT NULL, /* num of players vpi */ + playersAtStreet1 SMALLINT NOT NULL, /* num of players seeing flop/street4 */ + playersAtStreet2 SMALLINT NOT NULL, + playersAtStreet3 SMALLINT NOT NULL, + playersAtStreet4 SMALLINT NOT NULL, + playersAtShowdown SMALLINT NOT NULL, + street0Raises SMALLINT NOT NULL, /* num small bets paid to see flop/street4, including blind */ + street1Raises SMALLINT NOT NULL, /* num small bets paid to see turn/street5 */ + street2Raises SMALLINT NOT NULL, /* num big bets paid to see river/street6 */ + street3Raises SMALLINT NOT NULL, /* num big bets paid to see sd/street7 */ + street4Raises SMALLINT NOT NULL, /* num big bets paid to see showdown */ + street1Pot INT, /* pot size at flop/street4 */ + street2Pot INT, /* pot size at turn/street5 */ + street3Pot INT, /* pot size at river/street6 */ + street4Pot INT, /* pot size at sd/street7 */ + showdownPot INT, /* pot size at sd/street7 */ + comment TEXT, + commentTs timestamp without time zone)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createHandsTable'] = """CREATE TABLE Hands ( + id INTEGER PRIMARY KEY, + tableName TEXT(20), + siteHandNo INTEGER, + gametypeId INTEGER, + handStart BLOB, + importTime BLOB, + seats INTEGER, + maxSeats INTEGER, + comment TEXT, + commentTs BLOB, + FOREIGN KEY(gametypeId) REFERENCES Gametypes(id) ON DELETE CASCADE)""" + + + ################################ + # Create TourneyTypes + ################################ + + if db_server == 'mysql': + self.query['createTourneyTypesTable'] = """CREATE TABLE TourneyTypes ( + id SMALLINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + siteId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (siteId) REFERENCES Sites(id), + buyin INT NOT NULL, + fee INT NOT NULL, + knockout INT NOT NULL, + rebuyOrAddon BOOLEAN NOT NULL) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createTourneyTypesTable'] = """CREATE TABLE TourneyTypes ( + id SERIAL, PRIMARY KEY (id), + siteId INT, FOREIGN KEY (siteId) REFERENCES Sites(id), + buyin INT, + fee INT, + knockout INT, + rebuyOrAddon BOOLEAN)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createTourneyTypesTable'] = """ """ + + + ################################ + # Create Tourneys + ################################ + + if db_server == 'mysql': + self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( + id INT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), + siteTourneyNo BIGINT NOT NULL, + entries INT NOT NULL, + prizepool INT NOT NULL, + startTime DATETIME NOT NULL, + comment TEXT, + commentTs DATETIME) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createTourneysTable'] = """CREATE TABLE Tourneys ( + id SERIAL, PRIMARY KEY (id), + tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), + siteTourneyNo BIGINT, + entries INT, + prizepool INT, + startTime timestamp without time zone, + comment TEXT, + commentTs timestamp without time zone)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createTourneysTable'] = """CREATE TABLE TourneyTypes ( + id INTEGER PRIMARY KEY, + siteId INTEGER, + buyin INTEGER, + fee INTEGER, + knockout INTEGER, + rebuyOrAddon BOOL, + FOREIGN KEY(siteId) REFERENCES Sites(id) ON DELETE CASCADE)""" + + ################################ + # Create HandsPlayers + ################################ + + if db_server == 'mysql': + self.query['createHandsPlayersTable'] = """CREATE TABLE HandsPlayers ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + handId BIGINT UNSIGNED NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id), + playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), + startCash INT NOT NULL, + position CHAR(1), + seatNo SMALLINT NOT NULL, + + card1 smallint NOT NULL, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ + card2 smallint NOT NULL, + card3 smallint, + card4 smallint, + card5 smallint, + card6 smallint, + card7 smallint, + startCards smallint, + + ante INT, + winnings int NOT NULL, + rake int NOT NULL, + totalProfit INT NOT NULL, + comment text, + commentTs DATETIME, + tourneysPlayersId BIGINT UNSIGNED, + tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), + + wonWhenSeenStreet1 FLOAT NOT NULL, + wonWhenSeenStreet2 FLOAT, + wonWhenSeenStreet3 FLOAT, + wonWhenSeenStreet4 FLOAT, + wonAtSD FLOAT NOT NULL, + + street0VPI BOOLEAN NOT NULL, + street0Aggr BOOLEAN NOT NULL, + street0_3BChance BOOLEAN NOT NULL, + street0_3BDone BOOLEAN NOT NULL, + street0_4BChance BOOLEAN, + street0_4BDone BOOLEAN, + other3BStreet0 BOOLEAN, + other4BStreet0 BOOLEAN, + + street1Seen BOOLEAN NOT NULL, + street2Seen BOOLEAN NOT NULL, + street3Seen BOOLEAN NOT NULL, + street4Seen BOOLEAN NOT NULL, + sawShowdown BOOLEAN NOT NULL, + + street1Aggr BOOLEAN NOT NULL, + street2Aggr BOOLEAN NOT NULL, + street3Aggr BOOLEAN NOT NULL, + street4Aggr BOOLEAN NOT NULL, + + otherRaisedStreet0 BOOLEAN, + otherRaisedStreet1 BOOLEAN NOT NULL, + otherRaisedStreet2 BOOLEAN NOT NULL, + otherRaisedStreet3 BOOLEAN NOT NULL, + otherRaisedStreet4 BOOLEAN NOT NULL, + foldToOtherRaisedStreet0 BOOLEAN, + foldToOtherRaisedStreet1 BOOLEAN NOT NULL, + foldToOtherRaisedStreet2 BOOLEAN NOT NULL, + foldToOtherRaisedStreet3 BOOLEAN NOT NULL, + foldToOtherRaisedStreet4 BOOLEAN NOT NULL, + + stealAttemptChance BOOLEAN NOT NULL, + stealAttempted BOOLEAN NOT NULL, + foldBbToStealChance BOOLEAN NOT NULL, + foldedBbToSteal BOOLEAN NOT NULL, + foldSbToStealChance BOOLEAN NOT NULL, + foldedSbToSteal BOOLEAN NOT NULL, + + street1CBChance BOOLEAN NOT NULL, + street1CBDone BOOLEAN NOT NULL, + street2CBChance BOOLEAN NOT NULL, + street2CBDone BOOLEAN NOT NULL, + street3CBChance BOOLEAN NOT NULL, + street3CBDone BOOLEAN NOT NULL, + street4CBChance BOOLEAN NOT NULL, + street4CBDone BOOLEAN NOT NULL, + + foldToStreet1CBChance BOOLEAN NOT NULL, + foldToStreet1CBDone BOOLEAN NOT NULL, + foldToStreet2CBChance BOOLEAN NOT NULL, + foldToStreet2CBDone BOOLEAN NOT NULL, + foldToStreet3CBChance BOOLEAN NOT NULL, + foldToStreet3CBDone BOOLEAN NOT NULL, + foldToStreet4CBChance BOOLEAN NOT NULL, + foldToStreet4CBDone BOOLEAN NOT NULL, + + street1CheckCallRaiseChance BOOLEAN NOT NULL, + street1CheckCallRaiseDone BOOLEAN NOT NULL, + street2CheckCallRaiseChance BOOLEAN NOT NULL, + street2CheckCallRaiseDone BOOLEAN NOT NULL, + street3CheckCallRaiseChance BOOLEAN NOT NULL, + street3CheckCallRaiseDone BOOLEAN NOT NULL, + street4CheckCallRaiseChance BOOLEAN NOT NULL, + street4CheckCallRaiseDone BOOLEAN NOT NULL, + + street0Calls TINYINT, + street1Calls TINYINT, + street2Calls TINYINT, + street3Calls TINYINT, + street4Calls TINYINT, + street0Bets TINYINT, + street1Bets TINYINT, + street2Bets TINYINT, + street3Bets TINYINT, + street4Bets TINYINT, + street0Raises TINYINT, + street1Raises TINYINT, + street2Raises TINYINT, + street3Raises TINYINT, + street4Raises TINYINT, + + actionString VARCHAR(15), + + FOREIGN KEY (tourneysPlayersId) REFERENCES TourneysPlayers(id)) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createHandsPlayersTable'] = """CREATE TABLE HandsPlayers ( + id BIGSERIAL, PRIMARY KEY (id), + handId BIGINT NOT NULL, FOREIGN KEY (handId) REFERENCES Hands(id), + playerId INT NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), + startCash INT NOT NULL, + position CHAR(1), + seatNo SMALLINT NOT NULL, + + card1 smallint NOT NULL, /* 0=none, 1-13=2-Ah 14-26=2-Ad 27-39=2-Ac 40-52=2-As */ + card2 smallint NOT NULL, + card3 smallint, + card4 smallint, + card5 smallint, + card6 smallint, + card7 smallint, + startCards smallint, + + ante INT, + winnings int NOT NULL, + rake int NOT NULL, + totalProfit INT NOT NULL, + comment text, + commentTs timestamp without time zone, + tourneysPlayersId BIGINT, + tourneyTypeId INT NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), + + wonWhenSeenStreet1 FLOAT NOT NULL, + wonWhenSeenStreet2 FLOAT, + wonWhenSeenStreet3 FLOAT, + wonWhenSeenStreet4 FLOAT, + wonAtSD FLOAT NOT NULL, + + street0VPI BOOLEAN NOT NULL, + street0Aggr BOOLEAN NOT NULL, + street0_3BChance BOOLEAN NOT NULL, + street0_3BDone BOOLEAN NOT NULL, + street0_4BChance BOOLEAN, + street0_4BDone BOOLEAN, + other3BStreet0 BOOLEAN, + other4BStreet0 BOOLEAN, + + street1Seen BOOLEAN NOT NULL, + street2Seen BOOLEAN NOT NULL, + street3Seen BOOLEAN NOT NULL, + street4Seen BOOLEAN NOT NULL, + sawShowdown BOOLEAN NOT NULL, + + street1Aggr BOOLEAN NOT NULL, + street2Aggr BOOLEAN NOT NULL, + street3Aggr BOOLEAN NOT NULL, + street4Aggr BOOLEAN NOT NULL, + + otherRaisedStreet0 BOOLEAN, + otherRaisedStreet1 BOOLEAN NOT NULL, + otherRaisedStreet2 BOOLEAN NOT NULL, + otherRaisedStreet3 BOOLEAN NOT NULL, + otherRaisedStreet4 BOOLEAN NOT NULL, + foldToOtherRaisedStreet0 BOOLEAN, + foldToOtherRaisedStreet1 BOOLEAN NOT NULL, + foldToOtherRaisedStreet2 BOOLEAN NOT NULL, + foldToOtherRaisedStreet3 BOOLEAN NOT NULL, + foldToOtherRaisedStreet4 BOOLEAN NOT NULL, + + stealAttemptChance BOOLEAN NOT NULL, + stealAttempted BOOLEAN NOT NULL, + foldBbToStealChance BOOLEAN NOT NULL, + foldedBbToSteal BOOLEAN NOT NULL, + foldSbToStealChance BOOLEAN NOT NULL, + foldedSbToSteal BOOLEAN NOT NULL, + + street1CBChance BOOLEAN NOT NULL, + street1CBDone BOOLEAN NOT NULL, + street2CBChance BOOLEAN NOT NULL, + street2CBDone BOOLEAN NOT NULL, + street3CBChance BOOLEAN NOT NULL, + street3CBDone BOOLEAN NOT NULL, + street4CBChance BOOLEAN NOT NULL, + street4CBDone BOOLEAN NOT NULL, + + foldToStreet1CBChance BOOLEAN NOT NULL, + foldToStreet1CBDone BOOLEAN NOT NULL, + foldToStreet2CBChance BOOLEAN NOT NULL, + foldToStreet2CBDone BOOLEAN NOT NULL, + foldToStreet3CBChance BOOLEAN NOT NULL, + foldToStreet3CBDone BOOLEAN NOT NULL, + foldToStreet4CBChance BOOLEAN NOT NULL, + foldToStreet4CBDone BOOLEAN NOT NULL, + + street1CheckCallRaiseChance BOOLEAN NOT NULL, + street1CheckCallRaiseDone BOOLEAN NOT NULL, + street2CheckCallRaiseChance BOOLEAN NOT NULL, + street2CheckCallRaiseDone BOOLEAN NOT NULL, + street3CheckCallRaiseChance BOOLEAN NOT NULL, + street3CheckCallRaiseDone BOOLEAN NOT NULL, + street4CheckCallRaiseChance BOOLEAN NOT NULL, + street4CheckCallRaiseDone BOOLEAN NOT NULL, + + street0Calls SMALLINT, + street1Calls SMALLINT, + street2Calls SMALLINT, + street3Calls SMALLINT, + street4Calls SMALLINT, + street0Bets SMALLINT, + street1Bets SMALLINT, + street2Bets SMALLINT, + street3Bets SMALLINT, + street4Bets SMALLINT, + street0Raises SMALLINT, + street1Raises SMALLINT, + street2Raises SMALLINT, + street3Raises SMALLINT, + street4Raises SMALLINT, + + actionString VARCHAR(15), + + FOREIGN KEY (tourneysPlayersId) REFERENCES TourneysPlayers(id))""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createHandsPlayersTable'] = """ """ + + + ################################ + # Create TourneysPlayers + ################################ + + if db_server == 'mysql': + self.query['createTourneysPlayersTable'] = """CREATE TABLE TourneysPlayers ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + tourneyId INT UNSIGNED NOT NULL, FOREIGN KEY (tourneyId) REFERENCES Tourneys(id), + playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), + payinAmount INT NOT NULL, + rank INT NOT NULL, + winnings INT NOT NULL, + comment TEXT, + commentTs DATETIME) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createTourneysPlayersTable'] = """CREATE TABLE TourneysPlayers ( + id BIGSERIAL, PRIMARY KEY (id), + tourneyId INT, FOREIGN KEY (tourneyId) REFERENCES Tourneys(id), + playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), + payinAmount INT, + rank INT, + winnings INT, + comment TEXT, + commentTs timestamp without time zone)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createTourneysPlayersTable'] = """ """ + + + ################################ + # Create HandsActions + ################################ + + if db_server == 'mysql': + self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + handsPlayerId BIGINT UNSIGNED NOT NULL, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), + street SMALLINT NOT NULL, + actionNo SMALLINT NOT NULL, + action CHAR(5) NOT NULL, + allIn BOOLEAN NOT NULL, + amount INT NOT NULL, + comment TEXT, + commentTs DATETIME) + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createHandsActionsTable'] = """CREATE TABLE HandsActions ( + id BIGSERIAL, PRIMARY KEY (id), + handsPlayerId BIGINT, FOREIGN KEY (handsPlayerId) REFERENCES HandsPlayers(id), + street SMALLINT, + actionNo SMALLINT, + action CHAR(5), + allIn BOOLEAN, + amount INT, + comment TEXT, + commentTs timestamp without time zone)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['createHandsActionsTable'] = """ """ + + + ################################ + # Create HudCache + ################################ + + if db_server == 'mysql': + self.query['createHudCacheTable'] = """CREATE TABLE HudCache ( + id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL, PRIMARY KEY (id), + gametypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), + playerId INT UNSIGNED NOT NULL, FOREIGN KEY (playerId) REFERENCES Players(id), + activeSeats SMALLINT NOT NULL, + position CHAR(1), + tourneyTypeId SMALLINT UNSIGNED NOT NULL, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), + styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ + HDs INT NOT NULL, + + wonWhenSeenStreet1 FLOAT NOT NULL, + wonWhenSeenStreet2 FLOAT, + wonWhenSeenStreet3 FLOAT, + wonWhenSeenStreet4 FLOAT, + wonAtSD FLOAT NOT NULL, + + street0VPI INT NOT NULL, + street0Aggr INT NOT NULL, + street0_3BChance INT NOT NULL, + street0_3BDone INT NOT NULL, + street0_4BChance INT, + street0_4BDone INT, + other3BStreet0 INT, + other4BStreet0 INT, + + street1Seen INT NOT NULL, + street2Seen INT NOT NULL, + street3Seen INT NOT NULL, + street4Seen INT NOT NULL, + sawShowdown INT NOT NULL, + + street1Aggr INT NOT NULL, + street2Aggr INT NOT NULL, + street3Aggr INT NOT NULL, + street4Aggr INT NOT NULL, + + otherRaisedStreet0 INT, + otherRaisedStreet1 INT NOT NULL, + otherRaisedStreet2 INT NOT NULL, + otherRaisedStreet3 INT NOT NULL, + otherRaisedStreet4 INT NOT NULL, + foldToOtherRaisedStreet0 INT, + foldToOtherRaisedStreet1 INT NOT NULL, + foldToOtherRaisedStreet2 INT NOT NULL, + foldToOtherRaisedStreet3 INT NOT NULL, + foldToOtherRaisedStreet4 INT NOT NULL, + + stealAttemptChance INT NOT NULL, + stealAttempted INT NOT NULL, + foldBbToStealChance INT NOT NULL, + foldedBbToSteal INT NOT NULL, + foldSbToStealChance INT NOT NULL, + foldedSbToSteal INT NOT NULL, + + street1CBChance INT NOT NULL, + street1CBDone INT NOT NULL, + street2CBChance INT NOT NULL, + street2CBDone INT NOT NULL, + street3CBChance INT NOT NULL, + street3CBDone INT NOT NULL, + street4CBChance INT NOT NULL, + street4CBDone INT NOT NULL, + + foldToStreet1CBChance INT NOT NULL, + foldToStreet1CBDone INT NOT NULL, + foldToStreet2CBChance INT NOT NULL, + foldToStreet2CBDone INT NOT NULL, + foldToStreet3CBChance INT NOT NULL, + foldToStreet3CBDone INT NOT NULL, + foldToStreet4CBChance INT NOT NULL, + foldToStreet4CBDone INT NOT NULL, + + totalProfit INT NOT NULL, + + street1CheckCallRaiseChance INT NOT NULL, + street1CheckCallRaiseDone INT NOT NULL, + street2CheckCallRaiseChance INT NOT NULL, + street2CheckCallRaiseDone INT NOT NULL, + street3CheckCallRaiseChance INT NOT NULL, + street3CheckCallRaiseDone INT NOT NULL, + street4CheckCallRaiseChance INT NOT NULL, + street4CheckCallRaiseDone INT NOT NULL, + + street0Calls INT, + street1Calls INT, + street2Calls INT, + street3Calls INT, + street4Calls INT, + street0Bets INT, + street1Bets INT, + street2Bets INT, + street3Bets INT, + street4Bets INT, + street0Raises INT, + street1Raises INT, + street2Raises INT, + street3Raises INT, + street4Raises INT) + + ENGINE=INNODB""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['createHudCacheTable'] = """CREATE TABLE HudCache ( + id BIGSERIAL, PRIMARY KEY (id), + gametypeId INT, FOREIGN KEY (gametypeId) REFERENCES Gametypes(id), + playerId INT, FOREIGN KEY (playerId) REFERENCES Players(id), + activeSeats SMALLINT, + position CHAR(1), + tourneyTypeId INT, FOREIGN KEY (tourneyTypeId) REFERENCES TourneyTypes(id), + styleKey CHAR(7) NOT NULL, /* 1st char is style (A/T/H/S), other 6 are the key */ + HDs INT, + + wonWhenSeenStreet1 FLOAT NOT NULL, + wonWhenSeenStreet2 FLOAT, + wonWhenSeenStreet3 FLOAT, + wonWhenSeenStreet4 FLOAT, + wonAtSD FLOAT NOT NULL, + + street0VPI INT NOT NULL, + street0Aggr INT, + street0_3BChance INT NOT NULL, + street0_3BDone INT NOT NULL, + street0_4BChance INT, + street0_4BDone INT, + other3BStreet0 INT, + other4BStreet0 INT, + + street1Seen INT, + street2Seen INT, + street3Seen INT, + street4Seen INT, + sawShowdown INT, + street1Aggr INT, + street2Aggr INT, + street3Aggr INT, + street4Aggr INT, + + otherRaisedStreet0 INT, + otherRaisedStreet1 INT, + otherRaisedStreet2 INT, + otherRaisedStreet3 INT, + otherRaisedStreet4 INT, + foldToOtherRaisedStreet0 INT, + foldToOtherRaisedStreet1 INT, + foldToOtherRaisedStreet2 INT, + foldToOtherRaisedStreet3 INT, + foldToOtherRaisedStreet4 INT, + + stealAttemptChance INT, + stealAttempted INT, + foldBbToStealChance INT, + foldedBbToSteal INT, + foldSbToStealChance INT, + foldedSbToSteal INT, + + street1CBChance INT, + street1CBDone INT, + street2CBChance INT, + street2CBDone INT, + street3CBChance INT, + street3CBDone INT, + street4CBChance INT, + street4CBDone INT, + + foldToStreet1CBChance INT, + foldToStreet1CBDone INT, + foldToStreet2CBChance INT, + foldToStreet2CBDone INT, + foldToStreet3CBChance INT, + foldToStreet3CBDone INT, + foldToStreet4CBChance INT, + foldToStreet4CBDone INT, + + totalProfit INT, + + street1CheckCallRaiseChance INT, + street1CheckCallRaiseDone INT, + street2CheckCallRaiseChance INT, + street2CheckCallRaiseDone INT, + street3CheckCallRaiseChance INT, + street3CheckCallRaiseDone INT, + street4CheckCallRaiseChance INT, + street4CheckCallRaiseDone INT, + + street0Calls INT, + street1Calls INT, + street2Calls INT, + street3Calls INT, + street4Calls INT, + street0Bets INT, + street1Bets INT, + street2Bets INT, + street3Bets INT, + street4Bets INT, + street0Raises INT, + street1Raises INT, + street2Raises INT, + street3Raises INT, + street4Raises INT) + """ + elif db_server == 'sqlite': # what is the correct value here? + self.query['createHudCacheTable'] = """ """ + + if db_server == 'mysql': + self.query['addTourneyIndex'] = """ALTER TABLE Tourneys ADD INDEX siteTourneyNo(siteTourneyNo)""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['addTourneyIndex'] = """CREATE INDEX siteTourneyNo ON Tourneys (siteTourneyNo)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['addHandsIndex'] = """ """ + + if db_server == 'mysql': + self.query['addHandsIndex'] = """ALTER TABLE Hands ADD INDEX siteHandNo(siteHandNo)""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['addHandsIndex'] = """CREATE INDEX siteHandNo ON Hands (siteHandNo)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['addHandsIndex'] = """ """ + + if db_server == 'mysql': + self.query['addPlayersIndex'] = """ALTER TABLE Players ADD INDEX name(name)""" + elif db_server == 'postgresql': # what is the correct value here? + self.query['addPlayersIndex'] = """CREATE INDEX name ON Players (name)""" + elif db_server == 'sqlite': # what is the correct value here? + self.query['addPlayersIndex'] = """ """ + + self.query['get_last_hand'] = "select max(id) from Hands" self.query['get_player_id'] = """ @@ -171,6 +1011,8 @@ class Sql: and Players.SiteId = Sites.id """ + self.query['getSiteId'] = """SELECT id from Sites where name = %s""" + self.query['get_stats_from_hand'] = """ SELECT hc.playerId AS player_id, hp.seatNo AS seat, @@ -745,7 +1587,7 @@ class Sql: ,upper(gt.limitType) ,s.name """ - #elif(self.dbname == 'SQLite'): + #elif db_server == 'sqlite': # what is the correct value here? # self.query['playerDetailedStats'] = """ """ if db_server == 'mysql': @@ -957,7 +1799,7 @@ class Sql: ) hprof2 on hprof2.gtId = stats.gtId order by stats.base, stats.limittype, stats.bigBlindDesc desc """ - #elif(self.dbname == 'SQLite'): + #elif db_server == 'sqlite': # what is the correct value here? # self.query['playerStats'] = """ """ if db_server == 'mysql': @@ -1232,7 +2074,7 @@ class Sql: order by stats.category, stats.limitType, stats.bigBlindDesc desc , cast(stats.PlPosition as smallint) """ - #elif(self.dbname == 'SQLite'): + #elif db_server == 'sqlite': # what is the correct value here? # self.query['playerStatsByPosition'] = """ """ self.query['getRingProfitAllHandsPlayerIdSite'] = """ @@ -1556,12 +2398,21 @@ class Sql: if db_server == 'mysql': self.query['analyze'] = """ - analyze table autorates, gametypes, hands, handsplayers, hudcache, players - , settings, sites, tourneys, tourneysplayers, tourneytypes + analyze table Autorates, GameTypes, Hands, HandsPlayers, Hudcache, Players + , Settings, Sites, Tourneys, TourneysPlayers, TourneyTypes """ else: # assume postgres self.query['analyze'] = "vacuum analyze" + if db_server == 'mysql': + self.query['lockForInsert'] = """ + lock tables Hands write, HandsPlayers write, HandsActions write, Players write + , HudCache write, GameTypes write, Sites write, Tourneys write + , TourneysPlayers write, TourneyTypes write, Autorates write + """ + else: # assume postgres + self.query['lockForInsert'] = "" + if __name__== "__main__": # just print the default queries and exit s = Sql(game = 'razz', type = 'ptracks') diff --git a/pyfpdb/fpdb.py b/pyfpdb/fpdb.py index 691adae9..b3162239 100755 --- a/pyfpdb/fpdb.py +++ b/pyfpdb/fpdb.py @@ -232,7 +232,7 @@ class fpdb: # ToDo: lock all other tables so that lock doesn't have to be released # self.release_global_lock() # lock_released = True - self.db.fdb.recreate_tables() + self.db.recreate_tables() #else: # for other dbs use same connection as holds global lock # self.fdb_lock.fdb.recreate_tables() @@ -243,6 +243,24 @@ class fpdb: #if not lock_released: self.release_global_lock() #end def dia_recreate_tables + + def dia_recreate_hudcache(self, widget, data=None): + if self.obtain_global_lock(): + try: + dia_confirm = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_WARNING, buttons=(gtk.BUTTONS_YES_NO), message_format="Confirm recreating HUD cache") + diastring = "Please confirm that you want to re-create the HUD cache." + dia_confirm.format_secondary_text(diastring) + + response = dia_confirm.run() + dia_confirm.destroy() + if response == gtk.RESPONSE_YES: + self.db.rebuild_hudcache() + elif response == gtk.REPSONSE_NO: + print 'User cancelled rebuilding hud cache' + except: + pass + self.release_global_lock() + def dia_regression_test(self, widget, data=None): print "todo: implement dia_regression_test" @@ -306,6 +324,7 @@ class fpdb: + @@ -344,6 +363,7 @@ class fpdb: ('createdb', None, 'Create or Delete _Database (todo)', None, 'Create or Delete Database', self.dia_create_del_database), ('createuser', None, 'Create or Delete _User (todo)', None, 'Create or Delete User', self.dia_create_del_user), ('createtabs', None, 'Create or Recreate _Tables', None, 'Create or Recreate Tables ', self.dia_recreate_tables), + ('rebuildhudcache', None, 'Rebuild HUD Cache', None, 'Rebuild HUD Cache', self.dia_recreate_hudcache), ('stats', None, '_Statistics (todo)', None, 'View Database Statistics', self.dia_database_stats), ('sessions', None, 'Sessions', None, 'View Sessions', self.dia_database_sessions), ('help', None, '_Help'), @@ -410,7 +430,6 @@ class fpdb: self.status_bar.set_text("Status: Connected to %s database named %s on host %s" % (self.db.get_backend_name(),self.db.fdb.database, self.db.fdb.host)) # Database connected to successfully, load queries to pass on to other classes - self.querydict = FpdbSQLQueries.FpdbSQLQueries(self.db.get_backend_name()) self.db.connection.rollback() #end def load_profile @@ -457,7 +476,7 @@ class fpdb: def tab_bulk_import(self, widget, data=None): """opens a tab for bulk importing""" #print "start of tab_bulk_import" - new_import_thread=GuiBulkImport.GuiBulkImport(self.settings, self.config) + new_import_thread = GuiBulkImport.GuiBulkImport(self.settings, self.config, self.sql) self.threads.append(new_import_thread) bulk_tab=new_import_thread.get_vbox() self.add_and_display_tab(bulk_tab, "Bulk Import") diff --git a/pyfpdb/fpdb_db.py b/pyfpdb/fpdb_db.py index 99724d7e..a92625fd 100644 --- a/pyfpdb/fpdb_db.py +++ b/pyfpdb/fpdb_db.py @@ -33,112 +33,6 @@ class fpdb_db: self.db = None self.cursor = None self.sql = {} - - # Data Structures for index and foreign key creation - # drop_code is an int with possible values: 0 - don't drop for bulk import - # 1 - drop during bulk import - # db differences: - # - note that mysql automatically creates indexes on constrained columns when - # foreign keys are created, while postgres does not. Hence the much longer list - # of indexes is required for postgres. - # all primary keys are left on all the time - # - # table column drop_code - - self.indexes = [ - [ ] # no db with index 0 - , [ ] # no db with index 1 - , [ # indexes for mysql (list index 2) - {'tab':'Players', 'col':'name', 'drop':0} - , {'tab':'Hands', 'col':'siteHandNo', 'drop':0} - , {'tab':'Tourneys', 'col':'siteTourneyNo', 'drop':0} - ] - , [ # indexes for postgres (list index 3) - {'tab':'Boardcards', 'col':'handId', 'drop':0} - , {'tab':'Gametypes', 'col':'siteId', 'drop':0} - , {'tab':'Hands', 'col':'gametypeId', 'drop':0} # mct 22/3/09 - , {'tab':'Hands', 'col':'siteHandNo', 'drop':0} - , {'tab':'HandsActions', 'col':'handsPlayerId', 'drop':0} - , {'tab':'HandsPlayers', 'col':'handId', 'drop':1} - , {'tab':'HandsPlayers', 'col':'playerId', 'drop':1} - , {'tab':'HandsPlayers', 'col':'tourneysPlayersId', 'drop':0} - , {'tab':'HudCache', 'col':'gametypeId', 'drop':1} - , {'tab':'HudCache', 'col':'playerId', 'drop':0} - , {'tab':'HudCache', 'col':'tourneyTypeId', 'drop':0} - , {'tab':'Players', 'col':'siteId', 'drop':1} - , {'tab':'Players', 'col':'name', 'drop':0} - , {'tab':'Tourneys', 'col':'tourneyTypeId', 'drop':1} - , {'tab':'Tourneys', 'col':'siteTourneyNo', 'drop':0} - , {'tab':'TourneysPlayers', 'col':'playerId', 'drop':0} - , {'tab':'TourneysPlayers', 'col':'tourneyId', 'drop':0} - , {'tab':'TourneyTypes', 'col':'siteId', 'drop':0} - ] - , [ # indexes for sqlite (list index 4) - ] - ] - - self.foreignKeys = [ - [ ] # no db with index 0 - , [ ] # no db with index 1 - , [ # foreign keys for mysql - {'fktab':'Hands', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} - , {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} - , {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} - , {'fktab':'HandsActions', 'fkcol':'handsPlayerId', 'rtab':'HandsPlayers', 'rcol':'id', 'drop':1} - , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} - , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} - , {'fktab':'HudCache', 'fkcol':'tourneyTypeId', 'rtab':'TourneyTypes', 'rcol':'id', 'drop':1} - ] - , [ # foreign keys for postgres - {'fktab':'Hands', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} - , {'fktab':'HandsPlayers', 'fkcol':'handId', 'rtab':'Hands', 'rcol':'id', 'drop':1} - , {'fktab':'HandsPlayers', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':1} - , {'fktab':'HandsActions', 'fkcol':'handsPlayerId', 'rtab':'HandsPlayers', 'rcol':'id', 'drop':1} - , {'fktab':'HudCache', 'fkcol':'gametypeId', 'rtab':'Gametypes', 'rcol':'id', 'drop':1} - , {'fktab':'HudCache', 'fkcol':'playerId', 'rtab':'Players', 'rcol':'id', 'drop':0} - , {'fktab':'HudCache', 'fkcol':'tourneyTypeId', 'rtab':'TourneyTypes', 'rcol':'id', 'drop':1} - ] - ] - - - # MySQL Notes: - # "FOREIGN KEY (handId) REFERENCES Hands(id)" - requires index on Hands.id - # - creates index handId on .handId - # alter table t drop foreign key fk - # alter table t add foreign key (fkcol) references tab(rcol) - # alter table t add constraint c foreign key (fkcol) references tab(rcol) - # (fkcol is used for foreigh key name) - - # mysql to list indexes: - # SELECT table_name, index_name, non_unique, column_name - # FROM INFORMATION_SCHEMA.STATISTICS - # WHERE table_name = 'tbl_name' - # AND table_schema = 'db_name' - # ORDER BY table_name, index_name, seq_in_index - # - # ALTER TABLE Tourneys ADD INDEX siteTourneyNo(siteTourneyNo) - # ALTER TABLE tab DROP INDEX idx - - # mysql to list fks: - # SELECT constraint_name, table_name, column_name, referenced_table_name, referenced_column_name - # FROM information_schema.KEY_COLUMN_USAGE - # WHERE REFERENCED_TABLE_SCHEMA = (your schema name here) - # AND REFERENCED_TABLE_NAME is not null - # ORDER BY TABLE_NAME, COLUMN_NAME; - - # this may indicate missing object - # _mysql_exceptions.OperationalError: (1025, "Error on rename of '.\\fpdb\\hands' to '.\\fpdb\\#sql2-7f0-1b' (errno: 152)") - - - # PG notes: - - # To add a foreign key constraint to a table: - # ALTER TABLE tab ADD CONSTRAINT c FOREIGN KEY (col) REFERENCES t2(col2) MATCH FULL; - # ALTER TABLE tab DROP CONSTRAINT zipchk - # - # Note: index names must be unique across a schema - # CREATE INDEX idx ON tab(col) - # DROP INDEX idx #end def __init__ def do_connect(self, config=None): @@ -244,75 +138,6 @@ class fpdb_db: #print "started fpdb_db.reconnect" self.disconnect(due_to_error) self.connect(self.backend, self.host, self.database, self.user, self.password) - - def create_tables(self): - #todo: should detect and fail gracefully if tables already exist. - logging.debug(self.sql.query['createSettingsTable']) - self.cursor.execute(self.sql.query['createSettingsTable']) - logging.debug(self.sql.query['createSitesTable']) - self.cursor.execute(self.sql.query['createSitesTable']) - self.cursor.execute(self.sql.query['createGametypesTable']) - self.cursor.execute(self.sql.query['createPlayersTable']) - self.cursor.execute(self.sql.query['createAutoratesTable']) - self.cursor.execute(self.sql.query['createHandsTable']) - self.cursor.execute(self.sql.query['createTourneyTypesTable']) - self.cursor.execute(self.sql.query['createTourneysTable']) - self.cursor.execute(self.sql.query['createTourneysPlayersTable']) - self.cursor.execute(self.sql.query['createHandsPlayersTable']) - self.cursor.execute(self.sql.query['createHandsActionsTable']) - self.cursor.execute(self.sql.query['createHudCacheTable']) - #self.cursor.execute(self.sql.query['addTourneyIndex']) - #self.cursor.execute(self.sql.query['addHandsIndex']) - #self.cursor.execute(self.sql.query['addPlayersIndex']) - self.fillDefaultData() - self.db.commit() -#end def disconnect - - def drop_tables(self): - """Drops the fpdb tables from the current db""" - - if(self.get_backend_name() == 'MySQL InnoDB'): - #Databases with FOREIGN KEY support need this switched of before you can drop tables - self.drop_referential_integrity() - - # Query the DB to see what tables exist - self.cursor.execute(self.sql.query['list_tables']) - for table in self.cursor: - self.cursor.execute(self.sql.query['drop_table'] + table[0]) - elif(self.get_backend_name() == 'PostgreSQL'): - self.db.commit()# I have no idea why this makes the query work--REB 07OCT2008 - self.cursor.execute(self.sql.query['list_tables']) - tables = self.cursor.fetchall() - for table in tables: - self.cursor.execute(self.sql.query['drop_table'] + table[0] + ' cascade') - elif(self.get_backend_name() == 'SQLite'): - self.cursor.execute(self.sql.query['list_tables']) - for table in self.cursor.fetchall(): - logging.debug(self.sql.query['drop_table'] + table[0]) - self.cursor.execute(self.sql.query['drop_table'] + table[0]) - - self.db.commit() - #end def drop_tables - - def drop_referential_integrity(self): - """Update all tables to remove foreign keys""" - - self.cursor.execute(self.sql.query['list_tables']) - result = self.cursor.fetchall() - - for i in range(len(result)): - self.cursor.execute("SHOW CREATE TABLE " + result[i][0]) - inner = self.cursor.fetchall() - - for j in range(len(inner)): - # result[i][0] - Table name - # result[i][1] - CREATE TABLE parameters - #Searching for CONSTRAINT `tablename_ibfk_1` - for m in re.finditer('(ibfk_[0-9]+)', inner[j][1]): - key = "`" + inner[j][0] + "_" + m.group() + "`" - self.cursor.execute("ALTER TABLE " + inner[j][0] + " DROP FOREIGN KEY " + key) - self.db.commit() - #end drop_referential_inegrity def get_backend_name(self): """Returns the name of the currently used backend""" @@ -329,275 +154,40 @@ class fpdb_db: def get_db_info(self): return (self.host, self.database, self.user, self.password) #end def get_db_info - - def fillDefaultData(self): - self.cursor.execute("INSERT INTO Settings (version) VALUES (118);") - self.cursor.execute("INSERT INTO Sites (name,currency) VALUES ('Full Tilt Poker', 'USD')") - self.cursor.execute("INSERT INTO Sites (name,currency) VALUES ('PokerStars', 'USD')") - self.cursor.execute("INSERT INTO Sites (name,currency) VALUES ('Everleaf', 'USD')") - self.cursor.execute("INSERT INTO Sites (name,currency) VALUES ('Win2day', 'USD')") - self.cursor.execute("INSERT INTO TourneyTypes VALUES (DEFAULT, 1, 0, 0, 0, False);") - #self.cursor.execute("""INSERT INTO TourneyTypes - # (siteId,buyin,fee,knockout,rebuyOrAddon) VALUES - # (1,0,0,0,?)""",(False,) ) - #end def fillDefaultData - - def recreate_tables(self): - """(Re-)creates the tables of the current DB""" - - self.drop_tables() - self.create_tables() - self.createAllIndexes() - self.db.commit() - print "Finished recreating tables" - #end def recreate_tables - - def prepareBulkImport(self): - """Drop some indexes/foreign keys to prepare for bulk import. - Currently keeping the standalone indexes as needed to import quickly""" - stime = time() - if self.backend == self.PGSQL: - self.db.set_isolation_level(0) # allow table/index operations to work - for fk in self.foreignKeys[self.backend]: - if fk['drop'] == 1: - if self.backend == self.MYSQL_INNODB: - self.cursor.execute("SELECT constraint_name " + - "FROM information_schema.KEY_COLUMN_USAGE " + - #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' - "WHERE 1=1 " + - "AND table_name = %s AND column_name = %s " + - "AND referenced_table_name = %s " + - "AND referenced_column_name = %s ", - (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) - cons = self.cursor.fetchone() - #print "preparebulk: cons=", cons - if cons: - print "dropping mysql fk", cons[0], fk['fktab'], fk['fkcol'] - try: - self.cursor.execute("alter table " + fk['fktab'] + " drop foreign key " + cons[0]) - except: - pass - elif self.backend == self.PGSQL: - # DON'T FORGET TO RECREATE THEM!! - print "dropping pg fk", fk['fktab'], fk['fkcol'] - try: - # try to lock table to see if index drop will work: - # hmmm, tested by commenting out rollback in grapher. lock seems to work but - # then drop still hangs :-( does work in some tests though?? - # will leave code here for now pending further tests/enhancement ... - self.cursor.execute( "lock table %s in exclusive mode nowait" % (fk['fktab'],) ) - #print "after lock, status:", self.cursor.statusmessage - #print "alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol']) - try: - self.cursor.execute("alter table %s drop constraint %s_%s_fkey" % (fk['fktab'], fk['fktab'], fk['fkcol'])) - print "dropped pg fk pg fk %s_%s_fkey, continuing ..." % (fk['fktab'], fk['fkcol']) - except: - if "does not exist" not in str(sys.exc_value): - print "warning: drop pg fk %s_%s_fkey failed: %s, continuing ..." \ - % (fk['fktab'], fk['fkcol'], str(sys.exc_value).rstrip('\n') ) - except: - print "warning: constraint %s_%s_fkey not dropped: %s, continuing ..." \ - % (fk['fktab'],fk['fkcol'], str(sys.exc_value).rstrip('\n')) - else: - print "Only MySQL and Postgres supported so far" - return -1 - - for idx in self.indexes[self.backend]: - if idx['drop'] == 1: - if self.backend == self.MYSQL_INNODB: - print "dropping mysql index ", idx['tab'], idx['col'] - try: - # apparently nowait is not implemented in mysql so this just hands if there are locks - # preventing the index drop :-( - self.cursor.execute( "alter table %s drop index %s", (idx['tab'],idx['col']) ) - except: - pass - elif self.backend == self.PGSQL: - # DON'T FORGET TO RECREATE THEM!! - print "dropping pg index ", idx['tab'], idx['col'] - try: - # try to lock table to see if index drop will work: - self.cursor.execute( "lock table %s in exclusive mode nowait" % (idx['tab'],) ) - #print "after lock, status:", self.cursor.statusmessage - try: - # table locked ok so index drop should work: - #print "drop index %s_%s_idx" % (idx['tab'],idx['col']) - self.cursor.execute( "drop index if exists %s_%s_idx" % (idx['tab'],idx['col']) ) - #print "dropped pg index ", idx['tab'], idx['col'] - except: - if "does not exist" not in str(sys.exc_value): - print "warning: drop index %s_%s_idx failed: %s, continuing ..." \ - % (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n')) - except: - print "warning: index %s_%s_idx not dropped %s, continuing ..." \ - % (idx['tab'],idx['col'], str(sys.exc_value).rstrip('\n')) - else: - print "Error: Only MySQL and Postgres supported so far" - return -1 - - if self.backend == self.PGSQL: - self.db.set_isolation_level(1) # go back to normal isolation level - self.db.commit() # seems to clear up errors if there were any in postgres - ptime = time() - stime - print "prepare import took", ptime, "seconds" - #end def prepareBulkImport - - def afterBulkImport(self): - """Re-create any dropped indexes/foreign keys after bulk import""" - stime = time() - if self.backend == self.PGSQL: - self.db.set_isolation_level(0) # allow table/index operations to work - for fk in self.foreignKeys[self.backend]: - if fk['drop'] == 1: - if self.backend == self.MYSQL_INNODB: - self.cursor.execute("SELECT constraint_name " + - "FROM information_schema.KEY_COLUMN_USAGE " + - #"WHERE REFERENCED_TABLE_SCHEMA = 'fpdb' - "WHERE 1=1 " + - "AND table_name = %s AND column_name = %s " + - "AND referenced_table_name = %s " + - "AND referenced_column_name = %s ", - (fk['fktab'], fk['fkcol'], fk['rtab'], fk['rcol']) ) - cons = self.cursor.fetchone() - print "afterbulk: cons=", cons - if cons: - pass - else: - print "creating fk ", fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol'] - try: - self.cursor.execute("alter table " + fk['fktab'] + " add foreign key (" - + fk['fkcol'] + ") references " + fk['rtab'] + "(" - + fk['rcol'] + ")") - except: - pass - elif self.backend == self.PGSQL: - print "creating fk ", fk['fktab'], fk['fkcol'], "->", fk['rtab'], fk['rcol'] - try: - self.cursor.execute("alter table " + fk['fktab'] + " add constraint " - + fk['fktab'] + '_' + fk['fkcol'] + '_fkey' - + " foreign key (" + fk['fkcol'] - + ") references " + fk['rtab'] + "(" + fk['rcol'] + ")") - except: - pass - else: - print "Only MySQL and Postgres supported so far" - return -1 - - for idx in self.indexes[self.backend]: - if idx['drop'] == 1: - if self.backend == self.MYSQL_INNODB: - print "creating mysql index ", idx['tab'], idx['col'] - try: - self.cursor.execute( "alter table %s add index %s(%s)" - , (idx['tab'],idx['col'],idx['col']) ) - except: - pass - elif self.backend == self.PGSQL: - # pass - # mod to use tab_col for index name? - print "creating pg index ", idx['tab'], idx['col'] - try: - print "create index %s_%s_idx on %s(%s)" % (idx['tab'], idx['col'], idx['tab'], idx['col']) - self.cursor.execute( "create index %s_%s_idx on %s(%s)" - % (idx['tab'], idx['col'], idx['tab'], idx['col']) ) - except: - print " ERROR! :-(" - pass - else: - print "Only MySQL and Postgres supported so far" - return -1 - - if self.backend == self.PGSQL: - self.db.set_isolation_level(1) # go back to normal isolation level - self.db.commit() # seems to clear up errors if there were any in postgres - atime = time() - stime - print "after import took", atime, "seconds" - #end def afterBulkImport - - def createAllIndexes(self): - """Create new indexes""" - if self.backend == self.PGSQL: - self.db.set_isolation_level(0) # allow table/index operations to work - for idx in self.indexes[self.backend]: - if self.backend == self.MYSQL_INNODB: - print "creating mysql index ", idx['tab'], idx['col'] - try: - self.cursor.execute( "alter table %s add index %s(%s)" - , (idx['tab'],idx['col'],idx['col']) ) - except: - pass - elif self.backend == self.PGSQL: - # mod to use tab_col for index name? - print "creating pg index ", idx['tab'], idx['col'] - try: - print "create index %s_%s_idx on %s(%s)" % (idx['tab'], idx['col'], idx['tab'], idx['col']) - self.cursor.execute( "create index %s_%s_idx on %s(%s)" - % (idx['tab'], idx['col'], idx['tab'], idx['col']) ) - except: - print " ERROR! :-(" - pass - else: - print "Only MySQL and Postgres supported so far" - return -1 - if self.backend == self.PGSQL: - self.db.set_isolation_level(1) # go back to normal isolation level - #end def createAllIndexes - - def dropAllIndexes(self): - """Drop all standalone indexes (i.e. not including primary keys or foreign keys) - using list of indexes in indexes data structure""" - # maybe upgrade to use data dictionary?? (but take care to exclude PK and FK) - if self.backend == self.PGSQL: - self.db.set_isolation_level(0) # allow table/index operations to work - for idx in self.indexes[self.backend]: - if self.backend == self.MYSQL_INNODB: - print "dropping mysql index ", idx['tab'], idx['col'] - try: - self.cursor.execute( "alter table %s drop index %s" - , (idx['tab'],idx['col']) ) - except: - pass - elif self.backend == self.PGSQL: - print "dropping pg index ", idx['tab'], idx['col'] - # mod to use tab_col for index name? - try: - self.cursor.execute( "drop index %s_%s_idx" - % (idx['tab'],idx['col']) ) - except: - pass - else: - print "Only MySQL and Postgres supported so far" - return -1 - if self.backend == self.PGSQL: - self.db.set_isolation_level(1) # go back to normal isolation level - #end def dropAllIndexes def getLastInsertId(self): - if self.backend == self.MYSQL_INNODB: - ret = self.db.insert_id() - if ret < 1 or ret > 999999999: - print "getLastInsertId(): problem fetching insert_id? ret=", ret - ret = -1 - elif self.backend == self.PGSQL: - # some options: - # currval(hands_id_seq) - use name of implicit seq here - # lastval() - still needs sequences set up? - # insert ... returning is useful syntax (but postgres specific?) - # see rules (fancy trigger type things) - self.cursor.execute ("SELECT lastval()") - row = self.cursor.fetchone() - if not row: - print "getLastInsertId(%s): problem fetching lastval? row=" % seq, row + try: + if self.backend == self.MYSQL_INNODB: + ret = self.db.insert_id() + if ret < 1 or ret > 999999999: + print "getLastInsertId(): problem fetching insert_id? ret=", ret + ret = -1 + elif self.backend == self.PGSQL: + # some options: + # currval(hands_id_seq) - use name of implicit seq here + # lastval() - still needs sequences set up? + # insert ... returning is useful syntax (but postgres specific?) + # see rules (fancy trigger type things) + c = self.db.cursor() + ret = c.execute ("SELECT lastval()") + row = c.fetchone() + if not row: + print "getLastInsertId(%s): problem fetching lastval? row=" % seq, row + ret = -1 + else: + ret = row[0] + elif self.backend == fpdb_db.SQLITE: + # don't know how to do this in sqlite + print "getLastInsertId(): not coded for sqlite yet" ret = -1 else: - ret = row[0] - elif self.backend == fpdb_db.SQLITE: - # don't know how to do this in sqlite - print "getLastInsertId(): not coded for sqlite yet" - ret = -1 - else: - print "getLastInsertId(): unknown backend ", self.backend + print "getLastInsertId(): unknown backend ", self.backend + ret = -1 + except: ret = -1 + print "getLastInsertId error:", str(sys.exc_value), " ret =", ret + raise fpdb_simple.FpdbError( "getLastInsertId error: " + str(sys.exc_value) ) + return ret def storeHand(self, p): diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index f13e333e..ed87731b 100644 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -54,14 +54,14 @@ except: class Importer: - def __init__(self, caller, settings, config): + def __init__(self, caller, settings, config, sql = None): """Constructor""" self.settings = settings self.caller = caller self.config = config + self.sql = sql + self.database = None # database will be the main db interface eventually - self.fdb = None # fdb may disappear or just hold the simple db connection - self.cursor = None self.filelist = {} self.dirlist = {} self.siteIds = {} @@ -78,13 +78,10 @@ class Importer: self.settings.setdefault("minPrint", 30) self.settings.setdefault("handCount", 0) - self.database = Database.Database(self.config) # includes .connection and .sql variables - self.fdb = fpdb_db.fpdb_db() # sets self.fdb.db self.fdb.cursor and self.fdb.sql - self.fdb.do_connect(self.config) - self.fdb.db.rollback() # make sure all locks are released + self.database = Database.Database(self.config, sql = self.sql) # includes .connection and .sql variables self.NEWIMPORT = False - self.allow_hudcache_rebuild = True; + self.allow_hudcache_rebuild = False #Set functions def setCallHud(self, value): @@ -123,8 +120,7 @@ class Importer: self.filelist[filename] = [site] + [filter] if site not in self.siteIds: # Get id from Sites table in DB - self.fdb.cursor.execute(self.fdb.sql.query['getSiteId'], (site,)) - result = self.fdb.cursor.fetchall() + result = self.database.get_site_id(site) if len(result) == 1: self.siteIds[site] = result[0][0] else: @@ -178,9 +174,9 @@ class Importer: self.settings['dropHudCache'] = self.calculate_auto2(25.0, 500.0) # returns "drop"/"don't drop" if self.settings['dropIndexes'] == 'drop': - self.fdb.prepareBulkImport() + self.database.prepareBulkImport() else: - print "No need drop indexes." + print "No need to drop indexes." #print "dropInd =", self.settings['dropIndexes'], " dropHudCache =", self.settings['dropHudCache'] totstored = 0 totdups = 0 @@ -196,10 +192,10 @@ class Importer: toterrors += errors tottime += ttime if self.settings['dropIndexes'] == 'drop': - self.fdb.afterBulkImport() + self.database.afterBulkImport() else: - print "No need rebuild indexes." - if self.settings['dropHudCache'] == 'drop': + print "No need to rebuild indexes." + if self.allow_hudcache_rebuild and self.settings['dropHudCache'] == 'drop': self.database.rebuild_hudcache() else: print "No need to rebuild hudcache." @@ -212,7 +208,7 @@ class Importer: if len(self.filelist) == 1: return "don't drop" if 'handsInDB' not in self.settings: try: - tmpcursor = self.fdb.db.cursor() + tmpcursor = self.database.get_cursor() tmpcursor.execute("Select count(1) from Hands;") self.settings['handsInDB'] = tmpcursor.fetchone()[0] except: @@ -235,7 +231,7 @@ class Importer: # get number of hands in db if 'handsInDB' not in self.settings: try: - tmpcursor = self.fdb.db.cursor() + tmpcursor = self.database.get_cursor() tmpcursor.execute("Select count(1) from Hands;") self.settings['handsInDB'] = tmpcursor.fetchone()[0] except: @@ -299,12 +295,13 @@ class Importer: self.addToDirList = {} self.removeFromFileList = {} - self.fdb.db.rollback() + self.database.rollback() #rulog.writelines(" finished\n") #rulog.close() # This is now an internal function that should not be called directly. def import_file_dict(self, file, site, filter): + #print "import_file_dict" if os.path.isdir(file): self.addToDirList[file] = [site] + [filter] return @@ -323,24 +320,6 @@ class Importer: filter_name = filter.replace("ToFpdb", "") -# Example code for using threads & queues: (maybe for obj and import_fpdb_file??) -#def worker(): -# while True: -# item = q.get() -# do_work(item) -# q.task_done() -# -#q = Queue() -#for i in range(num_worker_threads): -# t = Thread(target=worker) -# t.setDaemon(True) -# t.start() -# -#for item in source(): -# q.put(item) -# -#q.join() # block until all tasks are done - mod = __import__(filter) obj = getattr(mod, filter_name, None) if callable(obj): @@ -368,6 +347,7 @@ class Importer: def import_fpdb_file(self, file, site): + #print "import_fpdb_file" starttime = time() last_read_hand = 0 loc = 0 @@ -397,6 +377,8 @@ class Importer: self.pos_in_file[file] = inputFile.tell() inputFile.close() + #self.database.lock_for_insert() # should be ok when using one thread + try: # sometimes we seem to be getting an empty self.lines, in which case, we just want to return. firstline = self.lines[0] except: @@ -438,10 +420,10 @@ class Importer: self.hand=hand try: - handsId = fpdb_parse_logic.mainParser( self.settings, self.fdb + handsId = fpdb_parse_logic.mainParser( self.settings , self.siteIds[site], category, hand , self.config, self.database ) - self.fdb.db.commit() + self.database.commit() stored += 1 if self.callHud: @@ -451,23 +433,23 @@ class Importer: self.caller.pipe_to_hud.stdin.write("%s" % (handsId) + os.linesep) except fpdb_simple.DuplicateError: duplicates += 1 - self.fdb.db.rollback() + self.database.rollback() except (ValueError), fe: errors += 1 self.printEmailErrorMessage(errors, file, hand) if (self.settings['failOnError']): - self.fdb.db.commit() #dont remove this, in case hand processing was cancelled. + self.database.commit() #dont remove this, in case hand processing was cancelled. raise else: - self.fdb.db.rollback() + self.database.rollback() except (fpdb_simple.FpdbError), fe: errors += 1 self.printEmailErrorMessage(errors, file, hand) - self.fdb.db.rollback() + self.database.rollback() if self.settings['failOnError']: - self.fdb.db.commit() #dont remove this, in case hand processing was cancelled. + self.database.commit() #dont remove this, in case hand processing was cancelled. raise if self.settings['minPrint']: @@ -494,7 +476,7 @@ class Importer: print "failed to read a single hand from file:", inputFile handsId=0 #todo: this will cause return of an unstored hand number if the last hand was error - self.fdb.db.commit() + self.database.commit() self.handsId=handsId return (stored, duplicates, partial, errors, ttime) diff --git a/pyfpdb/fpdb_parse_logic.py b/pyfpdb/fpdb_parse_logic.py index bb361f63..fd2d6796 100644 --- a/pyfpdb/fpdb_parse_logic.py +++ b/pyfpdb/fpdb_parse_logic.py @@ -15,19 +15,25 @@ #In the "official" distribution you can find the license in #agpl-3.0.txt in the docs folder of the package. -#methods that are specific to holdem but not trivial +#parses an in-memory fpdb hand history and calls db routine to store it + +import sys import fpdb_simple import Database +from time import time, strftime + #parses a holdem hand -def mainParser(settings, fdb, siteID, category, hand, config, db = None): +def mainParser(settings, siteID, category, hand, config, db = None): + #print "mainparser" + # fdb is not used now - to be removed ... + + t0 = time() + #print "mainparser" backend = settings['db-backend'] if db == None: - #This is redundant - hopefully fdb will be a Database object in an iteration soon db = Database.Database(c = config, sql = None) - else: - db = db category = fpdb_simple.recogniseCategory(hand[0]) base = "hold" if category == "holdem" or category == "omahahi" or category == "omahahilo" else "stud" @@ -50,9 +56,8 @@ def mainParser(settings, fdb, siteID, category, hand, config, db = None): if line[-2:] == "$0": continue smallBlindLine = i break - #print "small blind line:",smallBlindLine - gametypeID = fpdb_simple.recogniseGametypeID(backend, fdb.db, fdb.cursor, hand[0], hand[smallBlindLine], siteID, category, isTourney) + gametypeID = fpdb_simple.recogniseGametypeID(backend, db, db.get_cursor(), hand[0], hand[smallBlindLine], siteID, category, isTourney) if isTourney: siteTourneyNo = fpdb_simple.parseTourneyNo(hand[0]) buyin = fpdb_simple.parseBuyin(hand[0]) @@ -63,9 +68,20 @@ def mainParser(settings, fdb, siteID, category, hand, config, db = None): tourneyStartTime= handStartTime #todo: read tourney start time rebuyOrAddon = fpdb_simple.isRebuyOrAddon(hand[0]) - tourneyTypeId = fpdb_simple.recogniseTourneyTypeId(fdb.cursor, siteID, buyin, fee, knockout, rebuyOrAddon) + tourneyTypeId = fpdb_simple.recogniseTourneyTypeId(db.get_cursor(), siteID, buyin, fee, knockout, rebuyOrAddon) + else: + siteTourneyNo = -1 + buyin = -1 + fee = -1 + entries = -1 + prizepool = -1 + knockout = 0 + tourneyStartTime= None + rebuyOrAddon = -1 - fpdb_simple.isAlreadyInDB(fdb.cursor, gametypeID, siteHandNo) + tourneyTypeId = 1 + + fpdb_simple.isAlreadyInDB(db.get_cursor(), gametypeID, siteHandNo) hand = fpdb_simple.filterCrap(hand, isTourney) @@ -79,7 +95,7 @@ def mainParser(settings, fdb, siteID, category, hand, config, db = None): seatLines.append(line) names = fpdb_simple.parseNames(seatLines) - playerIDs = fpdb_simple.recognisePlayerIDs(fdb.cursor, names, siteID) + playerIDs = fpdb_simple.recognisePlayerIDs(db.get_cursor(), names, siteID) # inserts players as needed tmp = fpdb_simple.parseCashesAndSeatNos(seatLines) startCashes = tmp['startCashes'] seatNos = tmp['seatNos'] @@ -126,8 +142,9 @@ def mainParser(settings, fdb, siteID, category, hand, config, db = None): fpdb_simple.convertBlindBet(actionTypes, actionAmounts) fpdb_simple.checkPositions(positions) - fdb.cursor.execute("SELECT limitType FROM Gametypes WHERE id=%s",(gametypeID, )) - limit_type = fdb.cursor.fetchone()[0] + c = db.get_cursor() + c.execute("SELECT limitType FROM Gametypes WHERE id=%s",(gametypeID, )) + limit_type = c.fetchone()[0] fpdb_simple.convert3B4B(category, limit_type, actionTypes, actionAmounts) totalWinnings = sum(winnings) @@ -143,49 +160,27 @@ def mainParser(settings, fdb, siteID, category, hand, config, db = None): , allIns, actionTypeByNo, winnings, totalWinnings, None , actionTypes, actionAmounts, antes) - if isTourney: - ranks = map(lambda x: 0, names) # create an array of 0's equal to the length of names - payin_amounts = fpdb_simple.calcPayin(len(names), buyin, fee) - - if base == "hold": - result = db.tourney_holdem_omaha( - config, settings, fdb.db, fdb.cursor, base, category, siteTourneyNo, buyin - , fee, knockout, entries, prizepool, tourneyStartTime - , payin_amounts, ranks, tourneyTypeId, siteID, siteHandNo - , gametypeID, handStartTime, names, playerIDs, startCashes - , positions, cardValues, cardSuits, boardValues, boardSuits - , winnings, rakes, actionTypes, allIns, actionAmounts - , actionNos, hudImportData, maxSeats, tableName, seatNos) - elif base == "stud": - result = db.tourney_stud( - config, settings, fdb.db, fdb.cursor, base, category, siteTourneyNo - , buyin, fee, knockout, entries, prizepool, tourneyStartTime - , payin_amounts, ranks, tourneyTypeId, siteID, siteHandNo - , gametypeID, handStartTime, names, playerIDs, startCashes - , antes, cardValues, cardSuits, winnings, rakes, actionTypes - , allIns, actionAmounts, actionNos, hudImportData, maxSeats - , tableName, seatNos) - else: - raise fpdb_simple.FpdbError("unrecognised category") - else: - if base == "hold": - result = db.ring_holdem_omaha( - config, settings, fdb.db, fdb.cursor, base, category, siteHandNo - , gametypeID, handStartTime, names, playerIDs - , startCashes, positions, cardValues, cardSuits - , boardValues, boardSuits, winnings, rakes - , actionTypes, allIns, actionAmounts, actionNos - , hudImportData, maxSeats, tableName, seatNos) - elif base == "stud": - result = db.ring_stud( - config, settings, fdb.db, fdb.cursor, base, category, siteHandNo, gametypeID - , handStartTime, names, playerIDs, startCashes, antes - , cardValues, cardSuits, winnings, rakes, actionTypes, allIns - , actionAmounts, actionNos, hudImportData, maxSeats, tableName - , seatNos) - else: - raise fpdb_simple.FpdbError ("unrecognised category") - fdb.db.commit() + #print "parse: hand data prepared" # only reads up to here apart from inserting new players + try: + db.commit() # need to commit new players as different db connection used + # for other writes. maybe this will change maybe not ... + except: + print "parse: error during commit: " + str(sys.exc_value) + + + # save data structures in a HandToWrite instance and then insert into database: + htw = Database.HandToWrite() + htw.set_all( config, settings, base, category, siteTourneyNo, buyin + , fee, knockout, entries, prizepool, tourneyStartTime + , isTourney, tourneyTypeId, siteID, siteHandNo + , gametypeID, handStartTime, names, playerIDs, startCashes + , positions, antes, cardValues, cardSuits, boardValues, boardSuits + , winnings, rakes, actionTypes, allIns, actionAmounts + , actionNos, hudImportData, maxSeats, tableName, seatNos) + result = db.store_the_hand(htw) + + t9 = time() + #print "parse and save=(%4.3f)" % (t9-t0) return result #end def mainParser diff --git a/pyfpdb/fpdb_simple.py b/pyfpdb/fpdb_simple.py index 3e862254..6584dd19 100644 --- a/pyfpdb/fpdb_simple.py +++ b/pyfpdb/fpdb_simple.py @@ -37,9 +37,6 @@ MYSQL_INNODB = 2 PGSQL = 3 SQLITE = 4 -# config while trying out new hudcache mechanism -use_date_in_hudcache = True - class DuplicateError(Exception): def __init__(self, value): self.value = value @@ -986,7 +983,7 @@ def recogniseGametypeID(backend, db, cursor, topline, smallBlindLine, site_id, c #AND limitType=%s AND smallBlind=%s AND bigBlind=%s", (site_id, type, category, limit_type, small_bet, big_bet)) #result=(db.insert_id(),) - result=(getLastInsertId(backend,db,cursor),) + result=(db.get_last_insert_id(),) return result[0] #end def recogniseGametypeID @@ -1122,282 +1119,6 @@ def splitRake(winnings, rakes, totalRake): rakes[i]=totalRake*winPortion #end def splitRake -def storeActions(cursor, handsPlayersIds, actionTypes, allIns, actionAmounts, actionNos): -#stores into table hands_actions - #print "start of storeActions, actionNos:",actionNos - #print " action_amounts:",action_amounts - inserts = [] - for i in xrange(len(actionTypes)): #iterate through streets - for j in xrange(len(actionTypes[i])): #iterate through names - for k in xrange(len(actionTypes[i][j])): #iterate through individual actions of that player on that street - # Add inserts into a list and let - inserts = inserts + [(handsPlayersIds[j], i, actionNos[i][j][k], actionTypes[i][j][k], allIns[i][j][k], actionAmounts[i][j][k])] - - cursor.executemany("INSERT INTO HandsActions (handsPlayerId, street, actionNo, action, allIn, amount) VALUES (%s, %s, %s, %s, %s, %s)", inserts) -#end def storeActions - -def storeHands(backend, conn, cursor, site_hand_no, gametype_id - ,hand_start_time, names, tableName, maxSeats, hudCache, - board_values, board_suits): - cards = [Card.cardFromValueSuit(v,s) for v,s in zip(board_values,board_suits)] -#stores into table hands: - cursor.execute ("""INSERT INTO Hands - (siteHandNo, gametypeId, handStart, seats, tableName, importTime, maxSeats - ,boardcard1,boardcard2,boardcard3,boardcard4,boardcard5 - ,playersVpi, playersAtStreet1, playersAtStreet2 - ,playersAtStreet3, playersAtStreet4, playersAtShowdown - ,street0Raises, street1Raises, street2Raises - ,street3Raises, street4Raises, street1Pot - ,street2Pot, street3Pot, street4Pot - ,showdownPot - ) - VALUES - (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) - """ - , (site_hand_no, gametype_id, hand_start_time, len(names), tableName, datetime.datetime.today(), maxSeats - ,cards[0], cards[1], cards[2], cards[3], cards[4] - ,hudCache['playersVpi'], hudCache['playersAtStreet1'], hudCache['playersAtStreet2'] - ,hudCache['playersAtStreet3'], hudCache['playersAtStreet4'], hudCache['playersAtShowdown'] - ,hudCache['street0Raises'], hudCache['street1Raises'], hudCache['street2Raises'] - ,hudCache['street3Raises'], hudCache['street4Raises'], hudCache['street1Pot'] - ,hudCache['street2Pot'], hudCache['street3Pot'], hudCache['street4Pot'] - ,hudCache['showdownPot'] - )) - return getLastInsertId(backend, conn, cursor) -#end def storeHands - -def store_hands_players_holdem_omaha(backend, conn, cursor, category, hands_id, player_ids, start_cashes - ,positions, card_values, card_suits, winnings, rakes, seatNos, hudCache): - result=[] - - # postgres (and others?) needs the booleans converted to ints before saving: - # (or we could just save them as boolean ... but then we can't sum them so easily in sql ???) - # NO - storing booleans for now so don't need this - #hudCacheInt = {} - #for k,v in hudCache.iteritems(): - # if k in ('wonWhenSeenStreet1', 'wonAtSD', 'totalProfit'): - # hudCacheInt[k] = v - # else: - # hudCacheInt[k] = map(lambda x: 1 if x else 0, v) - - if (category=="holdem"): - for i in xrange(len(player_ids)): - startCards = Card.twoStartCards(card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1]) - card1 = Card.cardFromValueSuit(card_values[i][0], card_suits[i][0]) - card2 = Card.cardFromValueSuit(card_values[i][1], card_suits[i][1]) - cursor.execute (""" -INSERT INTO HandsPlayers -(handId, playerId, startCash, position, tourneyTypeId, - card1, card2, startCards, winnings, rake, seatNo, totalProfit, - street0VPI, street0Aggr, street0_3BChance, street0_3BDone, - street1Seen, street2Seen, street3Seen, street4Seen, sawShowdown, - street1Aggr, street2Aggr, street3Aggr, street4Aggr, - otherRaisedStreet1, otherRaisedStreet2, otherRaisedStreet3, otherRaisedStreet4, - foldToOtherRaisedStreet1, foldToOtherRaisedStreet2, foldToOtherRaisedStreet3, foldToOtherRaisedStreet4, - wonWhenSeenStreet1, wonAtSD, - stealAttemptChance, stealAttempted, foldBbToStealChance, foldedBbToSteal, foldSbToStealChance, foldedSbToSteal, - street1CBChance, street1CBDone, street2CBChance, street2CBDone, - street3CBChance, street3CBDone, street4CBChance, street4CBDone, - foldToStreet1CBChance, foldToStreet1CBDone, foldToStreet2CBChance, foldToStreet2CBDone, - foldToStreet3CBChance, foldToStreet3CBDone, foldToStreet4CBChance, foldToStreet4CBDone, - street1CheckCallRaiseChance, street1CheckCallRaiseDone, street2CheckCallRaiseChance, street2CheckCallRaiseDone, - street3CheckCallRaiseChance, street3CheckCallRaiseDone, street4CheckCallRaiseChance, street4CheckCallRaiseDone, - street0Calls, street1Calls, street2Calls, street3Calls, street4Calls, - street0Bets, street1Bets, street2Bets, street3Bets, street4Bets -) -VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s)""", - (hands_id, player_ids[i], start_cashes[i], positions[i], 1, # tourneytypeid - card1, card2, startCards, - winnings[i], rakes[i], seatNos[i], hudCache['totalProfit'][i], - hudCache['street0VPI'][i], hudCache['street0Aggr'][i], - hudCache['street0_3BChance'][i], hudCache['street0_3BDone'][i], - hudCache['street1Seen'][i], hudCache['street2Seen'][i], hudCache['street3Seen'][i], - hudCache['street4Seen'][i], hudCache['sawShowdown'][i], - hudCache['street1Aggr'][i], hudCache['street2Aggr'][i], hudCache['street3Aggr'][i], hudCache['street4Aggr'][i], - hudCache['otherRaisedStreet1'][i], hudCache['otherRaisedStreet2'][i], - hudCache['otherRaisedStreet3'][i], hudCache['otherRaisedStreet4'][i], - hudCache['foldToOtherRaisedStreet1'][i], hudCache['foldToOtherRaisedStreet2'][i], - hudCache['foldToOtherRaisedStreet3'][i], hudCache['foldToOtherRaisedStreet4'][i], - hudCache['wonWhenSeenStreet1'][i], hudCache['wonAtSD'][i], - hudCache['stealAttemptChance'][i], hudCache['stealAttempted'][i], hudCache['foldBbToStealChance'][i], - hudCache['foldedBbToSteal'][i], hudCache['foldSbToStealChance'][i], hudCache['foldedSbToSteal'][i], - hudCache['street1CBChance'][i], hudCache['street1CBDone'][i], hudCache['street2CBChance'][i], hudCache['street2CBDone'][i], - hudCache['street3CBChance'][i], hudCache['street3CBDone'][i], hudCache['street4CBChance'][i], hudCache['street4CBDone'][i], - hudCache['foldToStreet1CBChance'][i], hudCache['foldToStreet1CBDone'][i], - hudCache['foldToStreet2CBChance'][i], hudCache['foldToStreet2CBDone'][i], - hudCache['foldToStreet3CBChance'][i], hudCache['foldToStreet3CBDone'][i], - hudCache['foldToStreet4CBChance'][i], hudCache['foldToStreet4CBDone'][i], - hudCache['street1CheckCallRaiseChance'][i], hudCache['street1CheckCallRaiseDone'][i], - hudCache['street2CheckCallRaiseChance'][i], hudCache['street2CheckCallRaiseDone'][i], - hudCache['street3CheckCallRaiseChance'][i], hudCache['street3CheckCallRaiseDone'][i], - hudCache['street4CheckCallRaiseChance'][i], hudCache['street4CheckCallRaiseDone'][i], - hudCache['street0Calls'][i], hudCache['street1Calls'][i], hudCache['street2Calls'][i], hudCache['street3Calls'][i], hudCache['street4Calls'][i], - hudCache['street0Bets'][i], hudCache['street1Bets'][i], hudCache['street2Bets'][i], hudCache['street3Bets'][i], hudCache['street4Bets'][i] - ) ) - #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId=%s", (hands_id, player_ids[i])) - #result.append(cursor.fetchall()[0][0]) - result.append( getLastInsertId(backend, conn, cursor) ) - elif (category=="omahahi" or category=="omahahilo"): - for i in xrange(len(player_ids)): - startCards = Card.fourStartCards(card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1], card_values[i][2], card_suits[i][2], card_values[i][3], card_suits[i][3]) - card1 = Card.cardFromValueSuit(card_values[i][0], card_suits[i][0]) - card2 = Card.cardFromValueSuit(card_values[i][1], card_suits[i][1]) - card3 = Card.cardFromValueSuit(card_values[i][2], card_suits[i][2]) - card4 = Card.cardFromValueSuit(card_values[i][3], card_suits[i][3]) - cursor.execute ("""INSERT INTO HandsPlayers -(handId, playerId, startCash, position, tourneyTypeId, - card1, card2, card3, card4, winnings, rake, seatNo, totalProfit, - street0VPI, street0Aggr, street0_3BChance, street0_3BDone, - street1Seen, street2Seen, street3Seen, street4Seen, sawShowdown, - street1Aggr, street2Aggr, street3Aggr, street4Aggr, - otherRaisedStreet1, otherRaisedStreet2, otherRaisedStreet3, otherRaisedStreet4, - foldToOtherRaisedStreet1, foldToOtherRaisedStreet2, foldToOtherRaisedStreet3, foldToOtherRaisedStreet4, - wonWhenSeenStreet1, wonAtSD, - stealAttemptChance, stealAttempted, foldBbToStealChance, foldedBbToSteal, foldSbToStealChance, foldedSbToSteal, - street1CBChance, street1CBDone, street2CBChance, street2CBDone, - street3CBChance, street3CBDone, street4CBChance, street4CBDone, - foldToStreet1CBChance, foldToStreet1CBDone, foldToStreet2CBChance, foldToStreet2CBDone, - foldToStreet3CBChance, foldToStreet3CBDone, foldToStreet4CBChance, foldToStreet4CBDone, - street1CheckCallRaiseChance, street1CheckCallRaiseDone, street2CheckCallRaiseChance, street2CheckCallRaiseDone, - street3CheckCallRaiseChance, street3CheckCallRaiseDone, street4CheckCallRaiseChance, street4CheckCallRaiseDone, - street0Calls, street1Calls, street2Calls, street3Calls, street4Calls, - street0Bets, street1Bets, street2Bets, street3Bets, street4Bets -) -VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, - %s, %s, %s, %s, %s, %s, %s, %s)""", - (hands_id, player_ids[i], start_cashes[i], positions[i], 1, # tourneytypeid - card1, card2, card3, card4, - winnings[i], rakes[i], seatNos[i], hudCache['totalProfit'][i], - hudCache['street0VPI'][i], hudCache['street0Aggr'][i], - hudCache['street0_3BChance'][i], hudCache['street0_3BDone'][i], - hudCache['street1Seen'][i], hudCache['street2Seen'][i], hudCache['street3Seen'][i], - hudCache['street4Seen'][i], hudCache['sawShowdown'][i], - hudCache['street1Aggr'][i], hudCache['street2Aggr'][i], hudCache['street3Aggr'][i], hudCache['street4Aggr'][i], - hudCache['otherRaisedStreet1'][i], hudCache['otherRaisedStreet2'][i], - hudCache['otherRaisedStreet3'][i], hudCache['otherRaisedStreet4'][i], - hudCache['foldToOtherRaisedStreet1'][i], hudCache['foldToOtherRaisedStreet2'][i], - hudCache['foldToOtherRaisedStreet3'][i], hudCache['foldToOtherRaisedStreet4'][i], - hudCache['wonWhenSeenStreet1'][i], hudCache['wonAtSD'][i], - hudCache['stealAttemptChance'][i], hudCache['stealAttempted'][i], hudCache['foldBbToStealChance'][i], - hudCache['foldedBbToSteal'][i], hudCache['foldSbToStealChance'][i], hudCache['foldedSbToSteal'][i], - hudCache['street1CBChance'][i], hudCache['street1CBDone'][i], hudCache['street2CBChance'][i], hudCache['street2CBDone'][i], - hudCache['street3CBChance'][i], hudCache['street3CBDone'][i], hudCache['street4CBChance'][i], hudCache['street4CBDone'][i], - hudCache['foldToStreet1CBChance'][i], hudCache['foldToStreet1CBDone'][i], - hudCache['foldToStreet2CBChance'][i], hudCache['foldToStreet2CBDone'][i], - hudCache['foldToStreet3CBChance'][i], hudCache['foldToStreet3CBDone'][i], - hudCache['foldToStreet4CBChance'][i], hudCache['foldToStreet4CBDone'][i], - hudCache['street1CheckCallRaiseChance'][i], hudCache['street1CheckCallRaiseDone'][i], - hudCache['street2CheckCallRaiseChance'][i], hudCache['street2CheckCallRaiseDone'][i], - hudCache['street3CheckCallRaiseChance'][i], hudCache['street3CheckCallRaiseDone'][i], - hudCache['street4CheckCallRaiseChance'][i], hudCache['street4CheckCallRaiseDone'][i], - hudCache['street0Calls'][i], hudCache['street1Calls'][i], hudCache['street2Calls'][i], hudCache['street3Calls'][i], hudCache['street4Calls'][i], - hudCache['street0Bets'][i], hudCache['street1Bets'][i], hudCache['street2Bets'][i], hudCache['street3Bets'][i], hudCache['street4Bets'][i] - ) ) - #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) - #result.append(cursor.fetchall()[0][0]) - result.append( getLastInsertId(backend, conn, cursor) ) - else: - raise FpdbError("invalid category") - return result -#end def store_hands_players_holdem_omaha - -def store_hands_players_stud(backend, conn, cursor, hands_id, player_ids, start_cashes, antes, - card_values, card_suits, winnings, rakes, seatNos): -#stores hands_players rows for stud/razz games. returns an array of the resulting IDs - result=[] - #print "before inserts in store_hands_players_stud, antes:", antes - for i in xrange(len(player_ids)): - card1 = Card.cardFromValueSuit(card_values[i][0], card_suits[i][0]) - card2 = Card.cardFromValueSuit(card_values[i][1], card_suits[i][1]) - card3 = Card.cardFromValueSuit(card_values[i][2], card_suits[i][2]) - card4 = Card.cardFromValueSuit(card_values[i][3], card_suits[i][3]) - card5 = Card.cardFromValueSuit(card_values[i][4], card_suits[i][4]) - card6 = Card.cardFromValueSuit(card_values[i][5], card_suits[i][5]) - card7 = Card.cardFromValueSuit(card_values[i][6], card_suits[i][6]) - - cursor.execute ("""INSERT INTO HandsPlayers -(handId, playerId, startCash, ante, tourneyTypeId, -card1, card2, -card3, card4, -card5, card6, -card7, winnings, rake, seatNo) -VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", - (hands_id, player_ids[i], start_cashes[i], antes[i], 1, - card1, card2, - card3, card4, - card5, card6, - card7, winnings[i], rakes[i], seatNos[i])) - #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) - #result.append(cursor.fetchall()[0][0]) - result.append( getLastInsertId(backend, conn, cursor) ) - return result -#end def store_hands_players_stud - -def store_hands_players_holdem_omaha_tourney(backend, conn, cursor, category, hands_id, player_ids - ,start_cashes, positions, card_values, card_suits - , winnings, rakes, seatNos, tourneys_players_ids): - #stores hands_players for tourney holdem/omaha hands - result=[] - for i in xrange(len(player_ids)): - if len(card_values[0])==2: - cursor.execute ("""INSERT INTO HandsPlayers -(handId, playerId, startCash, position, -card1Value, card1Suit, card2Value, card2Suit, -winnings, rake, tourneysPlayersId, seatNo) -VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", - (hands_id, player_ids[i], start_cashes[i], positions[i], - card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1], - winnings[i], rakes[i], tourneys_players_ids[i], seatNos[i])) - elif len(card_values[0])==4: - cursor.execute ("""INSERT INTO HandsPlayers -(handId, playerId, startCash, position, -card1Value, card1Suit, card2Value, card2Suit, -card3Value, card3Suit, card4Value, card4Suit, -winnings, rake, tourneysPlayersId, seatNo) -VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", - (hands_id, player_ids[i], start_cashes[i], positions[i], - card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1], - card_values[i][2], card_suits[i][2], card_values[i][3], card_suits[i][3], - winnings[i], rakes[i], tourneys_players_ids[i], seatNos[i])) - else: - raise FpdbError ("invalid card_values length:"+str(len(card_values[0]))) - #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) - #result.append(cursor.fetchall()[0][0]) - result.append( getLastInsertId(backend, conn, cursor) ) - - return result -#end def store_hands_players_holdem_omaha_tourney - -def store_hands_players_stud_tourney(backend, conn, cursor, hands_id, player_ids, start_cashes, - antes, card_values, card_suits, winnings, rakes, seatNos, tourneys_players_ids): -#stores hands_players for tourney stud/razz hands - result=[] - for i in xrange(len(player_ids)): - cursor.execute ("""INSERT INTO HandsPlayers -(handId, playerId, startCash, ante, -card1Value, card1Suit, card2Value, card2Suit, -card3Value, card3Suit, card4Value, card4Suit, -card5Value, card5Suit, card6Value, card6Suit, -card7Value, card7Suit, winnings, rake, tourneysPlayersId, seatNo) -VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, %s)""", - (hands_id, player_ids[i], start_cashes[i], antes[i], - card_values[i][0], card_suits[i][0], card_values[i][1], card_suits[i][1], - card_values[i][2], card_suits[i][2], card_values[i][3], card_suits[i][3], - card_values[i][4], card_suits[i][4], card_values[i][5], card_suits[i][5], - card_values[i][6], card_suits[i][6], winnings[i], rakes[i], tourneys_players_ids[i], seatNos[i])) - #cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[i])) - #result.append(cursor.fetchall()[0][0]) - result.append( getLastInsertId(backend, conn, cursor) ) - return result -#end def store_hands_players_stud_tourney - def generateHudCacheData(player_ids, base, category, action_types, allIns, actionTypeByNo ,winnings, totalWinnings, positions, actionTypes, actionAmounts, antes): """calculates data for the HUD during import. IMPORTANT: if you change this method make @@ -2105,230 +1826,3 @@ def generateFoldToCB(street, playerIDs, didStreetCB, streetCBDone, foldToStreetC if action[1]=="fold": foldToStreetCBDone[player]=True #end def generateFoldToCB - -def storeHudCache(backend, cursor, base, category, gametypeId, hand_start_time, playerIds, hudImportData): - """Modified version aiming for more speed ...""" -# if (category=="holdem" or category=="omahahi" or category=="omahahilo"): - if use_date_in_hudcache: - #print "key =", "d%02d%02d%02d " % (hand_start_time.year-2000, hand_start_time.month, hand_start_time.day) - styleKey = "d%02d%02d%02d" % (hand_start_time.year-2000, hand_start_time.month, hand_start_time.day) - else: - # hard-code styleKey as 'A000000' (all-time cache, no key) for now - styleKey = 'A000000' - - #print "storeHudCache2, len(playerIds)=", len(playerIds), " len(vpip)=" \ - #, len(hudImportData['street0VPI']), " len(totprof)=", len(hudImportData['totalProfit']) - for player in xrange(len(playerIds)): - - # Set up a clean row - row=[] - row.append(0)#blank for id - row.append(gametypeId) - row.append(playerIds[player]) - row.append(len(playerIds))#seats - for i in xrange(len(hudImportData)+2): - row.append(0) - - if base=="hold": - row[4]=hudImportData['position'][player] - else: - row[4]=0 - row[5]=1 #tourneysGametypeId - row[6]+=1 #HDs - if hudImportData['street0VPI'][player]: row[7]+=1 - if hudImportData['street0Aggr'][player]: row[8]+=1 - if hudImportData['street0_3BChance'][player]: row[9]+=1 - if hudImportData['street0_3BDone'][player]: row[10]+=1 - if hudImportData['street1Seen'][player]: row[11]+=1 - if hudImportData['street2Seen'][player]: row[12]+=1 - if hudImportData['street3Seen'][player]: row[13]+=1 - if hudImportData['street4Seen'][player]: row[14]+=1 - if hudImportData['sawShowdown'][player]: row[15]+=1 - if hudImportData['street1Aggr'][player]: row[16]+=1 - if hudImportData['street2Aggr'][player]: row[17]+=1 - if hudImportData['street3Aggr'][player]: row[18]+=1 - if hudImportData['street4Aggr'][player]: row[19]+=1 - if hudImportData['otherRaisedStreet1'][player]: row[20]+=1 - if hudImportData['otherRaisedStreet2'][player]: row[21]+=1 - if hudImportData['otherRaisedStreet3'][player]: row[22]+=1 - if hudImportData['otherRaisedStreet4'][player]: row[23]+=1 - if hudImportData['foldToOtherRaisedStreet1'][player]: row[24]+=1 - if hudImportData['foldToOtherRaisedStreet2'][player]: row[25]+=1 - if hudImportData['foldToOtherRaisedStreet3'][player]: row[26]+=1 - if hudImportData['foldToOtherRaisedStreet4'][player]: row[27]+=1 - if hudImportData['wonWhenSeenStreet1'][player]!=0.0: row[28]+=hudImportData['wonWhenSeenStreet1'][player] - if hudImportData['wonAtSD'][player]!=0.0: row[29]+=hudImportData['wonAtSD'][player] - if hudImportData['stealAttemptChance'][player]: row[30]+=1 - if hudImportData['stealAttempted'][player]: row[31]+=1 - if hudImportData['foldBbToStealChance'][player]: row[32]+=1 - if hudImportData['foldedBbToSteal'][player]: row[33]+=1 - if hudImportData['foldSbToStealChance'][player]: row[34]+=1 - if hudImportData['foldedSbToSteal'][player]: row[35]+=1 - - if hudImportData['street1CBChance'][player]: row[36]+=1 - if hudImportData['street1CBDone'][player]: row[37]+=1 - if hudImportData['street2CBChance'][player]: row[38]+=1 - if hudImportData['street2CBDone'][player]: row[39]+=1 - if hudImportData['street3CBChance'][player]: row[40]+=1 - if hudImportData['street3CBDone'][player]: row[41]+=1 - if hudImportData['street4CBChance'][player]: row[42]+=1 - if hudImportData['street4CBDone'][player]: row[43]+=1 - - if hudImportData['foldToStreet1CBChance'][player]: row[44]+=1 - if hudImportData['foldToStreet1CBDone'][player]: row[45]+=1 - if hudImportData['foldToStreet2CBChance'][player]: row[46]+=1 - if hudImportData['foldToStreet2CBDone'][player]: row[47]+=1 - if hudImportData['foldToStreet3CBChance'][player]: row[48]+=1 - if hudImportData['foldToStreet3CBDone'][player]: row[49]+=1 - if hudImportData['foldToStreet4CBChance'][player]: row[50]+=1 - if hudImportData['foldToStreet4CBDone'][player]: row[51]+=1 - - #print "player=", player - #print "len(totalProfit)=", len(hudImportData['totalProfit']) - if hudImportData['totalProfit'][player]: - row[52]+=hudImportData['totalProfit'][player] - - if hudImportData['street1CheckCallRaiseChance'][player]: row[53]+=1 - if hudImportData['street1CheckCallRaiseDone'][player]: row[54]+=1 - if hudImportData['street2CheckCallRaiseChance'][player]: row[55]+=1 - if hudImportData['street2CheckCallRaiseDone'][player]: row[56]+=1 - if hudImportData['street3CheckCallRaiseChance'][player]: row[57]+=1 - if hudImportData['street3CheckCallRaiseDone'][player]: row[58]+=1 - if hudImportData['street4CheckCallRaiseChance'][player]: row[59]+=1 - if hudImportData['street4CheckCallRaiseDone'][player]: row[60]+=1 - - # Try to do the update first: - num = cursor.execute("""UPDATE HudCache -SET HDs=HDs+%s, street0VPI=street0VPI+%s, street0Aggr=street0Aggr+%s, - street0_3BChance=street0_3BChance+%s, street0_3BDone=street0_3BDone+%s, - street1Seen=street1Seen+%s, street2Seen=street2Seen+%s, street3Seen=street3Seen+%s, - street4Seen=street4Seen+%s, sawShowdown=sawShowdown+%s, - street1Aggr=street1Aggr+%s, street2Aggr=street2Aggr+%s, street3Aggr=street3Aggr+%s, - street4Aggr=street4Aggr+%s, otherRaisedStreet1=otherRaisedStreet1+%s, - otherRaisedStreet2=otherRaisedStreet2+%s, otherRaisedStreet3=otherRaisedStreet3+%s, - otherRaisedStreet4=otherRaisedStreet4+%s, - foldToOtherRaisedStreet1=foldToOtherRaisedStreet1+%s, foldToOtherRaisedStreet2=foldToOtherRaisedStreet2+%s, - foldToOtherRaisedStreet3=foldToOtherRaisedStreet3+%s, foldToOtherRaisedStreet4=foldToOtherRaisedStreet4+%s, - wonWhenSeenStreet1=wonWhenSeenStreet1+%s, wonAtSD=wonAtSD+%s, stealAttemptChance=stealAttemptChance+%s, - stealAttempted=stealAttempted+%s, foldBbToStealChance=foldBbToStealChance+%s, - foldedBbToSteal=foldedBbToSteal+%s, - foldSbToStealChance=foldSbToStealChance+%s, foldedSbToSteal=foldedSbToSteal+%s, - street1CBChance=street1CBChance+%s, street1CBDone=street1CBDone+%s, street2CBChance=street2CBChance+%s, - street2CBDone=street2CBDone+%s, street3CBChance=street3CBChance+%s, - street3CBDone=street3CBDone+%s, street4CBChance=street4CBChance+%s, street4CBDone=street4CBDone+%s, - foldToStreet1CBChance=foldToStreet1CBChance+%s, foldToStreet1CBDone=foldToStreet1CBDone+%s, - foldToStreet2CBChance=foldToStreet2CBChance+%s, foldToStreet2CBDone=foldToStreet2CBDone+%s, - foldToStreet3CBChance=foldToStreet3CBChance+%s, - foldToStreet3CBDone=foldToStreet3CBDone+%s, foldToStreet4CBChance=foldToStreet4CBChance+%s, - foldToStreet4CBDone=foldToStreet4CBDone+%s, totalProfit=totalProfit+%s, - street1CheckCallRaiseChance=street1CheckCallRaiseChance+%s, - street1CheckCallRaiseDone=street1CheckCallRaiseDone+%s, street2CheckCallRaiseChance=street2CheckCallRaiseChance+%s, - street2CheckCallRaiseDone=street2CheckCallRaiseDone+%s, street3CheckCallRaiseChance=street3CheckCallRaiseChance+%s, - street3CheckCallRaiseDone=street3CheckCallRaiseDone+%s, street4CheckCallRaiseChance=street4CheckCallRaiseChance+%s, - street4CheckCallRaiseDone=street4CheckCallRaiseDone+%s -WHERE gametypeId+0=%s -AND playerId=%s -AND activeSeats=%s -AND position=%s -AND tourneyTypeId+0=%s -AND styleKey=%s - """, (row[6], row[7], row[8], row[9], row[10], - row[11], row[12], row[13], row[14], row[15], - row[16], row[17], row[18], row[19], row[20], - row[21], row[22], row[23], row[24], row[25], - row[26], row[27], row[28], row[29], row[30], - row[31], row[32], row[33], row[34], row[35], - row[36], row[37], row[38], row[39], row[40], - row[41], row[42], row[43], row[44], row[45], - row[46], row[47], row[48], row[49], row[50], - row[51], row[52], row[53], row[54], row[55], - row[56], row[57], row[58], row[59], row[60], - row[1], row[2], row[3], str(row[4]), row[5], styleKey)) - # Test statusmessage to see if update worked, do insert if not - #print "storehud2, upd num =", num - if ( (backend == PGSQL and cursor.statusmessage != "UPDATE 1") - or (backend == MYSQL_INNODB and num == 0) ): - #print "playerid before insert:",row[2]," num = ", num - cursor.execute("""INSERT INTO HudCache -(gametypeId, playerId, activeSeats, position, tourneyTypeId, styleKey, -HDs, street0VPI, street0Aggr, street0_3BChance, street0_3BDone, -street1Seen, street2Seen, street3Seen, street4Seen, sawShowdown, -street1Aggr, street2Aggr, street3Aggr, street4Aggr, otherRaisedStreet1, -otherRaisedStreet2, otherRaisedStreet3, otherRaisedStreet4, foldToOtherRaisedStreet1, foldToOtherRaisedStreet2, -foldToOtherRaisedStreet3, foldToOtherRaisedStreet4, wonWhenSeenStreet1, wonAtSD, stealAttemptChance, -stealAttempted, foldBbToStealChance, foldedBbToSteal, foldSbToStealChance, foldedSbToSteal, -street1CBChance, street1CBDone, street2CBChance, street2CBDone, street3CBChance, -street3CBDone, street4CBChance, street4CBDone, foldToStreet1CBChance, foldToStreet1CBDone, -foldToStreet2CBChance, foldToStreet2CBDone, foldToStreet3CBChance, foldToStreet3CBDone, foldToStreet4CBChance, -foldToStreet4CBDone, totalProfit, street1CheckCallRaiseChance, street1CheckCallRaiseDone, street2CheckCallRaiseChance, -street2CheckCallRaiseDone, street3CheckCallRaiseChance, street3CheckCallRaiseDone, street4CheckCallRaiseChance, street4CheckCallRaiseDone) -VALUES (%s, %s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s, -%s, %s, %s, %s, %s)""" - , (row[1], row[2], row[3], row[4], row[5], styleKey, row[6], row[7], row[8], row[9], row[10] - ,row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20] - ,row[21], row[22], row[23], row[24], row[25], row[26], row[27], row[28], row[29], row[30] - ,row[31], row[32], row[33], row[34], row[35], row[36], row[37], row[38], row[39], row[40] - ,row[41], row[42], row[43], row[44], row[45], row[46], row[47], row[48], row[49], row[50] - ,row[51], row[52], row[53], row[54], row[55], row[56], row[57], row[58], row[59], row[60]) ) - #print "hopefully inserted hud data line: ", cursor.statusmessage - # message seems to be "INSERT 0 1" - else: - #print "updated(2) hud data line" - pass -# else: -# print "todo: implement storeHudCache for stud base" -#end def storeHudCache2 - -def store_tourneys(cursor, tourneyTypeId, siteTourneyNo, entries, prizepool, startTime): - cursor.execute("SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s", (siteTourneyNo, tourneyTypeId)) - tmp=cursor.fetchone() - #print "tried SELECTing tourneys.id, result:",tmp - - try: - len(tmp) - except TypeError:#means we have to create new one - cursor.execute("""INSERT INTO Tourneys -(tourneyTypeId, siteTourneyNo, entries, prizepool, startTime) -VALUES (%s, %s, %s, %s, %s)""", (tourneyTypeId, siteTourneyNo, entries, prizepool, startTime)) - cursor.execute("SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s", (siteTourneyNo, tourneyTypeId)) - tmp=cursor.fetchone() - #print "created new tourneys.id:",tmp - return tmp[0] -#end def store_tourneys - -def store_tourneys_players(cursor, tourney_id, player_ids, payin_amounts, ranks, winnings): - result=[] - #print "in store_tourneys_players. tourney_id:",tourney_id - #print "player_ids:",player_ids - #print "payin_amounts:",payin_amounts - #print "ranks:",ranks - #print "winnings:",winnings - for i in xrange(len(player_ids)): - cursor.execute("SELECT id FROM TourneysPlayers WHERE tourneyId=%s AND playerId+0=%s", (tourney_id, player_ids[i])) - tmp=cursor.fetchone() - #print "tried SELECTing tourneys_players.id:",tmp - - try: - len(tmp) - except TypeError: - cursor.execute("""INSERT INTO TourneysPlayers -(tourneyId, playerId, payinAmount, rank, winnings) VALUES (%s, %s, %s, %s, %s)""", - (tourney_id, player_ids[i], payin_amounts[i], ranks[i], winnings[i])) - - cursor.execute("SELECT id FROM TourneysPlayers WHERE tourneyId=%s AND playerId+0=%s", - (tourney_id, player_ids[i])) - tmp=cursor.fetchone() - #print "created new tourneys_players.id:",tmp - result.append(tmp[0]) - return result -#end def store_tourneys_players