improve tourney importing

This commit is contained in:
sqlcoder 2009-09-28 00:44:55 +01:00
parent db861292a5
commit b6ea02fd43
2 changed files with 73 additions and 26 deletions

View File

@ -1098,8 +1098,10 @@ class Database:
if self.backend == self.SQLITE: if self.backend == self.SQLITE:
c.execute("INSERT INTO TourneyTypes (id, siteId, buyin, fee) VALUES (NULL, 1, 0, 0);") c.execute("INSERT INTO TourneyTypes (id, siteId, buyin, fee) VALUES (NULL, 1, 0, 0);")
elif self.backend == self.PGSQL: elif self.backend == self.PGSQL:
c.execute("insert into TourneyTypes values (1,1,0,0,0,False,False,null,False,False,False);") c.execute("""insert into TourneyTypes(siteId, buyin, fee, maxSeats, knockout
else: ,rebuyOrAddon, speed, headsUp, shootout, matrix)
values (1, 0, 0, 0, False, False, null, False, False, False);""")
elif self.backend == self.MYSQL_INNODB:
c.execute("""insert into TourneyTypes(id, siteId, buyin, fee, maxSeats, knockout c.execute("""insert into TourneyTypes(id, siteId, buyin, fee, maxSeats, knockout
,rebuyOrAddon, speed, headsUp, shootout, matrix) ,rebuyOrAddon, speed, headsUp, shootout, matrix)
values (1, 1, 0, 0, 0, False, False, null, False, False, False);""") values (1, 1, 0, 0, 0, False, False, null, False, False, False);""")
@ -1768,6 +1770,8 @@ class Database:
#cursor.execute("SELECT id FROM HandsPlayers WHERE handId=%s AND playerId+0=%s", (hands_id, player_ids[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(cursor.fetchall()[0][0])
except: except:
err = traceback.extract_tb(sys.exc_info()[2])[-1]
print "***Error storing hand: "+err[2]+"("+str(err[1])+"): "+str(sys.exc_info()[1])
raise FpdbError( "store_hands_players_holdem_omaha_tourney error: " + str(sys.exc_value) ) raise FpdbError( "store_hands_players_holdem_omaha_tourney error: " + str(sys.exc_value) )
return result return result
@ -2000,27 +2004,40 @@ class Database:
#end def storeHudCache #end def storeHudCache
def store_tourneys(self, tourneyTypeId, siteTourneyNo, entries, prizepool, startTime): def store_tourneys(self, tourneyTypeId, siteTourneyNo, entries, prizepool, startTime):
ret = -1
try: try:
# try and create tourney record, fetch id if it already exists # try and create tourney record, fetch id if it already exists
# avoids race condition when doing the select first # avoids race condition when doing the select first
cursor = self.get_cursor()
cursor.execute("savepoint ins_tourney")
cursor.execute("""INSERT INTO Tourneys cursor.execute("""INSERT INTO Tourneys
(tourneyTypeId, siteTourneyNo, entries, prizepool, startTime) (tourneyTypeId, siteTourneyNo, entries, prizepool, startTime)
VALUES (%s, %s, %s, %s, %s)""".replace('%s', self.sql.query['placeholder']) VALUES (%s, %s, %s, %s, %s)""".replace('%s', self.sql.query['placeholder'])
,(tourneyTypeId, siteTourneyNo, entries, prizepool, startTime)) ,(tourneyTypeId, siteTourneyNo, entries, prizepool, startTime))
tmp = self.get_last_insert_id(cursor) ret = self.get_last_insert_id(cursor)
#cursor.execute("SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s", (siteTourneyNo, tourneyTypeId)) #print "created new tourneys.id:",ret
#tmp=cursor.fetchone()
#print "created new tourneys.id:",tmp
except: except:
#if str(sys.exc_value) .... not unique index error: #if str(sys.exc_value) contains 'sitetourneyno':
# raise FpdbError( "store_tourneys error: " + str(sys.exc_value) ) # raise FpdbError( "store_tourneys error: " + str(sys.exc_value) )
#else: #else:
cursor = self.get_cursor() #print "error insert tourney (%s) trying select ..." % (str(sys.exc_value),)
cursor.execute( "SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s".replace('%s', self.sql.query['placeholder']) cursor.execute("rollback to savepoint ins_tourney")
, (siteTourneyNo, tourneyTypeId) ) try:
tmp = cursor.fetchone()[0] cursor.execute( "SELECT id FROM Tourneys WHERE siteTourneyNo=%s AND tourneyTypeId+0=%s".replace('%s', self.sql.query['placeholder'])
, (siteTourneyNo, tourneyTypeId) )
rec = cursor.fetchone()
#print "select tourney result: ", rec
try:
len(rec)
ret = rec[0]
except:
print "Tourney id not found"
except:
print "Error selecting tourney id:", str(sys.exc_info()[1])
return tmp cursor.execute("release savepoint ins_tourney")
#print "store_tourneys returning", ret
return ret
#end def store_tourneys #end def store_tourneys
def store_tourneys_players(self, tourney_id, player_ids, payin_amounts, ranks, winnings): def store_tourneys_players(self, tourney_id, player_ids, payin_amounts, ranks, winnings):
@ -2034,21 +2051,31 @@ class Database:
#print "winnings:",winnings #print "winnings:",winnings
for i in xrange(len(player_ids)): for i in xrange(len(player_ids)):
try: try:
cursor.execute("savepoint ins_tplayer")
cursor.execute("""INSERT INTO TourneysPlayers cursor.execute("""INSERT INTO TourneysPlayers
(tourneyId, playerId, payinAmount, rank, winnings) VALUES (%s, %s, %s, %s, %s)""".replace('%s', self.sql.query['placeholder']), (tourneyId, playerId, payinAmount, rank, winnings) VALUES (%s, %s, %s, %s, %s)""".replace('%s', self.sql.query['placeholder']),
(tourney_id, player_ids[i], payin_amounts[i], ranks[i], winnings[i])) (tourney_id, player_ids[i], payin_amounts[i], ranks[i], winnings[i]))
tmp = self.get_last_insert_id(cursor) tmp = self.get_last_insert_id(cursor)
#print "created new tourneys_players.id:",tmp result.append(tmp)
#print "created new tourneys_players.id:", tmp
except: except:
cursor.execute("rollback to savepoint ins_tplayer")
cursor.execute("SELECT id FROM TourneysPlayers WHERE tourneyId=%s AND playerId+0=%s".replace('%s', self.sql.query['placeholder']) cursor.execute("SELECT id FROM TourneysPlayers WHERE tourneyId=%s AND playerId+0=%s".replace('%s', self.sql.query['placeholder'])
,(tourney_id, player_ids[i])) ,(tourney_id, player_ids[i]))
tmp=cursor.fetchone()[0] tmp = cursor.fetchone()
#print "tried SELECTing tourneys_players.id:",tmp #print "tried SELECTing tourneys_players.id:", tmp
result.append(tmp) try:
len(tmp)
result.append(tmp[0])
except:
print "tplayer id not found for tourney,player %s,%s" % (tourney_id, player_ids[i])
pass
except: except:
raise FpdbError( "store_tourneys_players error: " + str(sys.exc_value) ) raise FpdbError( "store_tourneys_players error: " + str(sys.exc_value) )
cursor.execute("release savepoint ins_tplayer")
#print "store_tourneys_players returning", result
return result return result
#end def store_tourneys_players #end def store_tourneys_players

View File

@ -913,32 +913,52 @@ def recogniseGametypeID(backend, db, cursor, topline, smallBlindLine, site_id, c
#end def recogniseGametypeID #end def recogniseGametypeID
def recogniseTourneyTypeId(db, siteId, tourneySiteId, buyin, fee, knockout, rebuyOrAddon): def recogniseTourneyTypeId(db, siteId, tourneySiteId, buyin, fee, knockout, rebuyOrAddon):
ret = -1
cursor = db.get_cursor() cursor = db.get_cursor()
# First we try to find the tourney itself (by its tourneySiteId) in case it has already been inserted before (by a summary file for instance) # First we try to find the tourney itself (by its tourneySiteId) in case it has already been inserted before (by a summary file for instance)
# The reason is that some tourneys may not be identified correctly in the HH toplines (especially Buy-In and Fee which are used to search/create the TourneyTypeId) # The reason is that some tourneys may not be identified correctly in the HH toplines (especially Buy-In and Fee which are used to search/create the TourneyTypeId)
#TODO: When the summary file will be dumped to BD, if the tourney is already in, Buy-In/Fee may need an update (e.g. creation of a new type and link to the Tourney) #TODO: When the summary file will be dumped to BD, if the tourney is already in, Buy-In/Fee may need an update (e.g. creation of a new type and link to the Tourney)
cursor.execute (db.sql.query['getTourneyTypeIdByTourneyNo'].replace('%s', db.sql.query['placeholder']), (tourneySiteId, siteId)) cursor.execute (db.sql.query['getTourneyTypeIdByTourneyNo'].replace('%s', db.sql.query['placeholder']), (tourneySiteId, siteId))
result=cursor.fetchone() result = cursor.fetchone()
try: try:
len(result) len(result)
ret = result[0]
except: except:
cursor.execute( """SELECT id FROM TourneyTypes cursor.execute( """SELECT id FROM TourneyTypes
WHERE siteId=%s AND buyin=%s AND fee=%s WHERE siteId=%s AND buyin=%s AND fee=%s
AND knockout=%s AND rebuyOrAddon=%s""" AND knockout=%s AND rebuyOrAddon=%s"""
, (siteId, buyin, fee, knockout, rebuyOrAddon) ) , (siteId, buyin, fee, knockout, rebuyOrAddon) )
result=cursor.fetchone() result = cursor.fetchone()
#print "tried SELECTing gametypes.id, result:",result #print "tried selecting tourneytypes.id, result:", result
try: try:
len(result) len(result)
ret = result[0]
except TypeError:#this means we need to create a new entry except TypeError:#this means we need to create a new entry
cursor.execute( """INSERT INTO TourneyTypes (siteId, buyin, fee, knockout, rebuyOrAddon) #print "insert new tourneytype record ..."
VALUES (%s, %s, %s, %s, %s)""" try:
, (siteId, buyin, fee, knockout, rebuyOrAddon) ) cursor.execute( """INSERT INTO TourneyTypes (siteId, buyin, fee, knockout, rebuyOrAddon)
result = db.get_last_insert_id(cursor) VALUES (%s, %s, %s, %s, %s)"""
, (siteId, buyin, fee, knockout, rebuyOrAddon) )
ret = db.get_last_insert_id(cursor)
except:
#print "maybe tourneytype was created since select, try selecting again ..."
cursor.execute( """SELECT id FROM TourneyTypes
WHERE siteId=%s AND buyin=%s AND fee=%s
AND knockout=%s AND rebuyOrAddon=%s"""
, (siteId, buyin, fee, knockout, rebuyOrAddon) )
result = cursor.fetchone()
try:
len(result)
ret = result[0]
except:
print "Failed to find or insert TourneyTypes record"
ret = -1 # failed to find or insert record
#print "tried selecting tourneytypes.id again, result:", result
return result[0] #print "recogniseTourneyTypeId: returning", ret
return ret
#end def recogniseTourneyTypeId #end def recogniseTourneyTypeId