Fixes for importing and HUD with the postgres db.

This commit is contained in:
Ray 2009-05-31 21:25:36 -04:00
parent f55460341d
commit 069aa025e4
5 changed files with 50 additions and 35 deletions

View File

@ -38,12 +38,15 @@ class Database:
if c.supported_databases[db_name].db_server == 'postgresql': if c.supported_databases[db_name].db_server == 'postgresql':
# psycopg2 database module for posgres via DB-API # psycopg2 database module for posgres via DB-API
import psycopg2 import psycopg2
import psycopg2.extensions
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
try: try:
self.connection = psycopg2.connect(host = c.supported_databases[db_name].db_ip, # self.connection = psycopg2.connect(host = c.supported_databases[db_name].db_ip,
user = c.supported_databases[db_name].db_user, # user = c.supported_databases[db_name].db_user,
password = c.supported_databases[db_name].db_pass, # password = c.supported_databases[db_name].db_pass,
database = c.supported_databases[db_name].db_name) # database = c.supported_databases[db_name].db_name)
self.connection = psycopg2.connect(database = c.supported_databases[db_name].db_name)
except: except:
print "Error opening database connection %s. See error log file." % (file) print "Error opening database connection %s. See error log file." % (file)
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
@ -120,7 +123,7 @@ class Database:
"""Get and return the cards for each player in the hand.""" """Get and return the cards for each player in the hand."""
cards = {} # dict of cards, the key is the seat number example: {1: 'AcQd9hTs5d'} cards = {} # dict of cards, the key is the seat number example: {1: 'AcQd9hTs5d'}
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_cards'], hand) c.execute(self.sql.query['get_cards'], (hand, ))
colnames = [desc[0] for desc in c.description] colnames = [desc[0] for desc in c.description]
for row in c.fetchall(): for row in c.fetchall():
s_dict = {} s_dict = {}
@ -133,7 +136,7 @@ class Database:
"""Get and return the community cards for the specified hand.""" """Get and return the community cards for the specified hand."""
cards = {} cards = {}
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_common_cards'], hand) c.execute(self.sql.query['get_common_cards'], (hand,))
colnames = [desc[0] for desc in c.description] colnames = [desc[0] for desc in c.description]
for row in c.fetchall(): for row in c.fetchall():
s_dict = {} s_dict = {}
@ -154,20 +157,20 @@ class Database:
# cards += "xx" # cards += "xx"
# else: # else:
# cards += ranks[d['card' + str(i) + 'Value']] + d['card' +str(i) + 'Suit'] # cards += ranks[d['card' + str(i) + 'Value']] + d['card' +str(i) + 'Suit']
cv = "card%dValue" % i cv = "card%dvalue" % i
if cv not in d or d[cv] == None: if cv not in d or d[cv] == None:
break break
elif d[cv] == 0: elif d[cv] == 0:
cards += "xx" cards += "xx"
else: else:
cs = "card%dSuit" % i cs = "card%dsuit" % i
cards = "%s%s%s" % (cards, ranks[d[cv]], d[cs]) cards = "%s%s%s" % (cards, ranks[d[cv]], d[cs])
return cards return cards
def get_action_from_hand(self, hand_no): def get_action_from_hand(self, hand_no):
action = [ [], [], [], [], [] ] action = [ [], [], [], [], [] ]
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_action_from_hand'], (hand_no)) c.execute(self.sql.query['get_action_from_hand'], (hand_no, ))
for row in c.fetchall(): for row in c.fetchall():
street = row[0] street = row[0]
act = row[1:] act = row[1:]
@ -178,7 +181,7 @@ class Database:
"""Returns a hash of winners:amount won, given a hand number.""" """Returns a hash of winners:amount won, given a hand number."""
winners = {} winners = {}
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_winners_from_hand'], (hand)) c.execute(self.sql.query['get_winners_from_hand'], (hand, ))
for row in c.fetchall(): for row in c.fetchall():
winners[row[0]] = row[1] winners[row[0]] = row[1]
return winners return winners
@ -205,7 +208,6 @@ class Database:
return stat_dict return stat_dict
def get_player_id(self, config, site, player_name): def get_player_id(self, config, site, player_name):
print "site = %s, player name = %s" % (site, player_name)
c = self.connection.cursor() c = self.connection.cursor()
c.execute(self.sql.query['get_player_id'], {'player': player_name, 'site': site}) c.execute(self.sql.query['get_player_id'], {'player': player_name, 'site': site})
row = c.fetchone() row = c.fetchone()
@ -224,19 +226,19 @@ if __name__=="__main__":
h = db_connection.get_last_hand() h = db_connection.get_last_hand()
print "last hand = ", h print "last hand = ", h
hero = db_connection.get_player_id(c, 'PokerStars', 'nutOmatic') hero = db_connection.get_player_id(c, 'Full Tilt Poker', 'PokerAscetic')
print "nutOmatic is id_player = %d" % hero print "nutOmatic is id_player = %d" % hero
stat_dict = db_connection.get_stats_from_hand(h) stat_dict = db_connection.get_stats_from_hand(h)
for p in stat_dict.keys(): for p in stat_dict.keys():
print p, " ", stat_dict[p] print p, " ", stat_dict[p]
print "nutOmatics stats:" # print "nutOmatics stats:"
stat_dict = db_connection.get_stats_from_hand(h, hero) # stat_dict = db_connection.get_stats_from_hand(h, hero)
for p in stat_dict.keys(): # for p in stat_dict.keys():
print p, " ", stat_dict[p] # print p, " ", stat_dict[p]
print "cards =", db_connection.get_cards(73525) print "cards =", db_connection.get_cards(u'1')
db_connection.close_connection db_connection.close_connection
print "press enter to continue" print "press enter to continue"

View File

@ -147,7 +147,6 @@ class HUD_main(object):
if new_hand_id == "": # blank line means quit if new_hand_id == "": # blank line means quit
self.destroy() self.destroy()
break # this thread is not always killed immediately with gtk.main_quit() break # this thread is not always killed immediately with gtk.main_quit()
# get basic info about the new hand from the db # get basic info about the new hand from the db
# if there is a db error, complain, skip hand, and proceed # if there is a db error, complain, skip hand, and proceed
try: try:
@ -168,7 +167,7 @@ class HUD_main(object):
(tour_number, tab_number) = mat_obj.group(1, 2) (tour_number, tab_number) = mat_obj.group(1, 2)
temp_key = tour_number temp_key = tour_number
else: # tourney, but can't get number and table else: # tourney, but can't get number and table
print "could not find tournamtne: skipping " print "could not find tournament: skipping "
sys.stderr.write("Could not find tournament %d in hand %d. Skipping.\n" % (int(tour_number), int(new_hand_id))) sys.stderr.write("Could not find tournament %d in hand %d. Skipping.\n" % (int(tour_number), int(new_hand_id)))
continue continue

View File

@ -237,7 +237,7 @@ class Sql:
AND HudCache.gametypeId+0 = Hands.gametypeId+0) AND HudCache.gametypeId+0 = Hands.gametypeId+0)
INNER JOIN Players ON (Players.id = HandsPlayers.PlayerId+0) INNER JOIN Players ON (Players.id = HandsPlayers.PlayerId+0)
WHERE Hands.id = %s WHERE Hands.id = %s
GROUP BY HudCache.PlayerId GROUP BY HudCache.PlayerId, Players.name, seatNo
""" """
# same as above except stats are aggregated for all blind/limit levels # same as above except stats are aggregated for all blind/limit levels
@ -312,7 +312,7 @@ class Sql:
AND gt1.limittype = gt2.limittype AND gt1.limittype = gt2.limittype
AND gt2.id = Hands.gametypeId AND gt2.id = Hands.gametypeId
AND Hands.id = %s) AND Hands.id = %s)
GROUP BY HudCache.PlayerId GROUP BY HudCache.PlayerId, Players.name, seatNo
""" """
self.query['get_players_from_hand'] = """ self.query['get_players_from_hand'] = """
@ -349,13 +349,20 @@ class Sql:
select select
seatNo AS seat_number, seatNo AS seat_number,
name AS screen_name, name AS screen_name,
card1Value, card1Suit, card1Value AS card1value,
card2Value, card2Suit, card1Suit AS card1suit,
card3Value, card3Suit, card2Value AS card2value,
card4Value, card4Suit, card2Suit AS card2suit,
card5Value, card5Suit, card3Value AS card3value,
card6Value, card6Suit, card3Suit AS card3suit,
card7Value, card7Suit card4Value AS card4value,
card4Suit AS card4suit,
card5Value AS card5value,
card5Suit AS card5suit,
card6Value AS card6value,
card6Suit AS card6suit,
card7Value AS card7value,
card7Suit AS card7suit
from HandsPlayers, Players from HandsPlayers, Players
where handID = %s and HandsPlayers.playerId = Players.id where handID = %s and HandsPlayers.playerId = Players.id
order by seatNo order by seatNo
@ -363,11 +370,16 @@ class Sql:
self.query['get_common_cards'] = """ self.query['get_common_cards'] = """
select select
card1Value, card1Suit, card1Value AS card1value,
card2Value, card2Suit, card1Suit AS card1suit,
card3Value, card3Suit, card2Value AS card2value,
card4Value, card4Suit, card2Suit AS card2suit,
card5Value, card5Suit card3Value AS card3value,
card3Suit AS card3suit,
card4Value AS card4value,
card4Suit AS card4suit,
card5Value AS card5value,
card5Suit AS card5suit
from BoardCards from BoardCards
where handId = %s where handId = %s
""" """

View File

@ -62,9 +62,10 @@ class fpdb_db:
self.db=MySQLdb.connect(host = host, user = user, passwd = password, db = database, use_unicode=True) self.db=MySQLdb.connect(host = host, user = user, passwd = password, db = database, use_unicode=True)
elif backend==self.PGSQL: elif backend==self.PGSQL:
import psycopg2 import psycopg2
import psycopg2.extensions
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
# If DB connection is made over TCP, then the variables # If DB connection is made over TCP, then the variables
# host, user and password are required # host, user and password are required
print "host=%s user=%s pass=%s." % (host, user, password)
if self.host and self.user and self.password: if self.host and self.user and self.password:
self.db = psycopg2.connect(host = host, self.db = psycopg2.connect(host = host,
user = user, user = user,
@ -75,6 +76,7 @@ class fpdb_db:
# flat out wrong # flat out wrong
else: else:
self.db = psycopg2.connect(database = database) self.db = psycopg2.connect(database = database)
# self.db.set_client_encoding('UNICODE')
else: else:
raise fpdb_simple.FpdbError("unrecognised database backend:"+backend) raise fpdb_simple.FpdbError("unrecognised database backend:"+backend)
self.cursor=self.db.cursor() self.cursor=self.db.cursor()

View File

@ -1410,7 +1410,7 @@ def recognisePlayerIDs(cursor, names, site_id):
if len(ids) != len(names): if len(ids) != len(names):
notfound = [n for n in names if n not in ids] # make list of names not in database notfound = [n for n in names if n not in ids] # make list of names not in database
if notfound: # insert them into database if notfound: # insert them into database
cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", (notfound)) cursor.executemany("INSERT INTO Players (name, siteId) VALUES (%s, "+str(site_id)+")", [(n,) for n in notfound])
q2 = "SELECT name,id FROM Players WHERE name=%s" % " OR name=".join(["%s" for n in notfound]) q2 = "SELECT name,id FROM Players WHERE name=%s" % " OR name=".join(["%s" for n in notfound])
cursor.execute(q2, notfound) # get their new ids cursor.execute(q2, notfound) # get their new ids
tmp = dict(cursor.fetchall()) tmp = dict(cursor.fetchall())