2008-08-19 00:53:25 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Database.py
|
|
|
|
|
|
|
|
Create and manage the database objects.
|
|
|
|
"""
|
|
|
|
# Copyright 2008, Ray E. Barker
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
# postmaster -D /var/lib/pgsql/data
|
|
|
|
|
|
|
|
# Standard Library modules
|
2008-09-15 22:31:55 +02:00
|
|
|
import sys
|
2008-10-04 22:43:50 +02:00
|
|
|
import traceback
|
2009-05-28 00:34:10 +02:00
|
|
|
from datetime import datetime, date, time, timedelta
|
2009-07-21 23:26:23 +02:00
|
|
|
from time import time, strftime
|
2009-06-02 00:27:56 +02:00
|
|
|
import string
|
2008-08-19 00:53:25 +02:00
|
|
|
|
|
|
|
# pyGTK modules
|
|
|
|
|
|
|
|
# FreePokerTools modules
|
2009-06-18 00:03:43 +02:00
|
|
|
import fpdb_db
|
2009-06-19 22:15:52 +02:00
|
|
|
import fpdb_simple
|
2008-08-19 00:53:25 +02:00
|
|
|
import Configuration
|
|
|
|
import SQL
|
2009-05-21 05:26:00 +02:00
|
|
|
import Card
|
2008-08-19 00:53:25 +02:00
|
|
|
|
|
|
|
class Database:
|
2009-07-21 23:26:23 +02:00
|
|
|
|
|
|
|
MYSQL_INNODB = 2
|
|
|
|
PGSQL = 3
|
|
|
|
SQLITE = 4
|
|
|
|
|
2009-06-24 00:44:37 +02:00
|
|
|
def __init__(self, c, db_name = None, game = None, sql = None): # db_name and game not used any more
|
2009-06-30 23:00:55 +02:00
|
|
|
print "\ncreating Database instance, sql =", sql
|
2009-06-18 00:03:43 +02:00
|
|
|
self.fdb = fpdb_db.fpdb_db() # sets self.fdb.db self.fdb.cursor and self.fdb.sql
|
|
|
|
self.fdb.do_connect(c)
|
|
|
|
self.connection = self.fdb.db
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2009-06-18 00:03:43 +02:00
|
|
|
db_params = c.get_db_parameters()
|
2009-06-19 23:33:33 +02:00
|
|
|
self.import_options = c.get_import_parameters()
|
2009-06-02 00:27:56 +02:00
|
|
|
self.type = db_params['db-type']
|
2009-06-19 23:33:33 +02:00
|
|
|
self.backend = db_params['db-backend']
|
2009-06-26 00:14:32 +02:00
|
|
|
self.db_server = db_params['db-server']
|
2009-07-22 21:19:41 +02:00
|
|
|
|
|
|
|
if self.backend == self.PGSQL:
|
|
|
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE
|
|
|
|
#ISOLATION_LEVEL_AUTOCOMMIT = 0
|
|
|
|
#ISOLATION_LEVEL_READ_COMMITTED = 1
|
|
|
|
#ISOLATION_LEVEL_SERIALIZABLE = 2
|
|
|
|
|
2009-06-24 00:44:37 +02:00
|
|
|
# where possible avoid creating new SQL instance by using the global one passed in
|
|
|
|
if sql == None:
|
|
|
|
self.sql = SQL.Sql(type = self.type, db_server = db_params['db-server'])
|
|
|
|
else:
|
|
|
|
self.sql = sql
|
2009-06-18 00:03:43 +02:00
|
|
|
|
2009-05-28 00:34:10 +02:00
|
|
|
# To add to config:
|
|
|
|
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
|
2009-07-18 19:29:06 +02:00
|
|
|
self.hud_days = 30 # Max number of days from each player to use for hud stats
|
2009-05-28 00:34:10 +02:00
|
|
|
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)
|
2009-07-18 19:29:06 +02:00
|
|
|
self.cursor = self.fdb.cursor
|
2009-05-28 00:34:10 +02:00
|
|
|
|
2009-07-15 17:32:17 +02:00
|
|
|
if self.fdb.wrongDbVersion == False:
|
2009-07-18 19:29:06 +02:00
|
|
|
# self.hand_1day_ago used to fetch stats for current session (i.e. if hud_style = 'S')
|
2009-07-15 17:32:17 +02:00
|
|
|
self.hand_1day_ago = 0
|
2009-07-18 19:29:06 +02:00
|
|
|
self.cursor.execute(self.sql.query['get_hand_1day_ago'])
|
|
|
|
row = self.cursor.fetchone()
|
2009-07-15 17:32:17 +02:00
|
|
|
if row and row[0]:
|
|
|
|
self.hand_1day_ago = row[0]
|
|
|
|
#print "hand 1day ago =", self.hand_1day_ago
|
2009-05-28 00:34:10 +02:00
|
|
|
|
2009-07-18 19:29:06 +02:00
|
|
|
# self.date_ndays_ago used if hud_style = 'T'
|
2009-07-15 17:32:17 +02:00
|
|
|
d = timedelta(days=self.hud_days)
|
|
|
|
now = datetime.utcnow() - d
|
|
|
|
self.date_ndays_ago = "d%02d%02d%02d" % (now.year-2000, now.month, now.day)
|
2009-05-28 00:34:10 +02:00
|
|
|
|
2009-07-18 19:29:06 +02:00
|
|
|
# self.hand_nhands_ago is used for fetching stats for last n hands (hud_style = 'H')
|
|
|
|
# This option not used yet
|
|
|
|
self.hand_nhands_ago = 0
|
|
|
|
# should use aggregated version of query if appropriate
|
|
|
|
self.cursor.execute(self.sql.query['get_hand_nhands_ago'], (self.hud_hands,self.hud_hands))
|
|
|
|
row = self.cursor.fetchone()
|
|
|
|
if row and row[0]:
|
|
|
|
self.hand_nhands_ago = row[0]
|
|
|
|
print "hand n hands ago =", self.hand_nhands_ago
|
|
|
|
|
|
|
|
#self.cursor.execute(self.sql.query['get_table_name'], (hand_id, ))
|
|
|
|
#row = self.cursor.fetchone()
|
2009-07-15 17:32:17 +02:00
|
|
|
else:
|
|
|
|
print "Bailing on DB query, not sure it exists yet"
|
2009-07-21 23:26:23 +02:00
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
self.saveActions = False if self.import_options['saveActions'] == False else True
|
|
|
|
|
2009-07-21 23:26:23 +02:00
|
|
|
self.connection.rollback() # make sure any locks taken so far are released
|
|
|
|
|
2009-07-18 19:29:06 +02:00
|
|
|
# could be used by hud to change hud style
|
|
|
|
def set_hud_style(self, style):
|
|
|
|
self.hud_style = style
|
|
|
|
|
2009-06-30 23:00:55 +02:00
|
|
|
def do_connect(self, c):
|
|
|
|
self.fdb.do_connect(c)
|
2009-05-28 00:34:10 +02:00
|
|
|
|
2009-06-19 22:15:52 +02:00
|
|
|
def commit(self):
|
|
|
|
self.fdb.db.commit()
|
2009-05-28 00:34:10 +02:00
|
|
|
|
2009-07-19 19:28:13 +02:00
|
|
|
def rollback(self):
|
|
|
|
self.fdb.db.rollback()
|
|
|
|
|
2009-07-21 23:26:23 +02:00
|
|
|
def get_cursor(self):
|
|
|
|
return self.connection.cursor()
|
|
|
|
|
2008-09-15 22:31:55 +02:00
|
|
|
def close_connection(self):
|
|
|
|
self.connection.close()
|
2009-06-30 23:00:55 +02:00
|
|
|
|
|
|
|
def disconnect(self, due_to_error=False):
|
|
|
|
"""Disconnects the DB (rolls back if param is true, otherwise commits"""
|
|
|
|
self.fdb.disconnect(due_to_error)
|
|
|
|
|
|
|
|
def reconnect(self, due_to_error=False):
|
|
|
|
"""Reconnects the DB"""
|
|
|
|
self.fdb.reconnect(due_to_error=False)
|
|
|
|
|
|
|
|
def get_backend_name(self):
|
|
|
|
"""Reconnects the DB"""
|
|
|
|
return self.fdb.get_backend_name()
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2009-06-30 23:00:55 +02:00
|
|
|
|
2008-08-19 00:53:25 +02:00
|
|
|
def get_table_name(self, hand_id):
|
|
|
|
c = self.connection.cursor()
|
2008-09-15 22:31:55 +02:00
|
|
|
c.execute(self.sql.query['get_table_name'], (hand_id, ))
|
2008-08-19 00:53:25 +02:00
|
|
|
row = c.fetchone()
|
|
|
|
return row
|
|
|
|
|
|
|
|
def get_last_hand(self):
|
|
|
|
c = self.connection.cursor()
|
|
|
|
c.execute(self.sql.query['get_last_hand'])
|
|
|
|
row = c.fetchone()
|
|
|
|
return row[0]
|
|
|
|
|
|
|
|
def get_xml(self, hand_id):
|
|
|
|
c = self.connection.cursor()
|
|
|
|
c.execute(self.sql.query['get_xml'], (hand_id))
|
|
|
|
row = c.fetchone()
|
|
|
|
return row[0]
|
|
|
|
|
|
|
|
def get_recent_hands(self, last_hand):
|
|
|
|
c = self.connection.cursor()
|
|
|
|
c.execute(self.sql.query['get_recent_hands'], {'last_hand': last_hand})
|
|
|
|
return c.fetchall()
|
|
|
|
|
|
|
|
def get_hand_info(self, new_hand_id):
|
|
|
|
c = self.connection.cursor()
|
|
|
|
c.execute(self.sql.query['get_hand_info'], new_hand_id)
|
|
|
|
return c.fetchall()
|
|
|
|
|
2008-10-10 02:50:12 +02:00
|
|
|
def get_actual_seat(self, hand_id, name):
|
|
|
|
c = self.connection.cursor()
|
|
|
|
c.execute(self.sql.query['get_actual_seat'], (hand_id, name))
|
|
|
|
row = c.fetchone()
|
|
|
|
return row[0]
|
|
|
|
|
2008-08-19 00:53:25 +02:00
|
|
|
def get_cards(self, hand):
|
2009-02-26 05:35:15 +01:00
|
|
|
"""Get and return the cards for each player in the hand."""
|
2009-06-17 05:00:46 +02:00
|
|
|
cards = {} # dict of cards, the key is the seat number,
|
|
|
|
# the value is a tuple of the players cards
|
|
|
|
# example: {1: (0, 0, 20, 21, 22, 0 , 0)}
|
2008-08-19 00:53:25 +02:00
|
|
|
c = self.connection.cursor()
|
2009-05-02 01:28:53 +02:00
|
|
|
c.execute(self.sql.query['get_cards'], [hand])
|
2008-08-19 00:53:25 +02:00
|
|
|
for row in c.fetchall():
|
2009-06-17 05:00:46 +02:00
|
|
|
cards[row[0]] = row[1:]
|
2009-02-26 05:35:15 +01:00
|
|
|
return cards
|
|
|
|
|
2009-03-10 16:14:23 +01:00
|
|
|
def get_common_cards(self, hand):
|
|
|
|
"""Get and return the community cards for the specified hand."""
|
2008-08-19 00:53:25 +02:00
|
|
|
cards = {}
|
2009-03-10 16:14:23 +01:00
|
|
|
c = self.connection.cursor()
|
2009-05-02 01:28:53 +02:00
|
|
|
c.execute(self.sql.query['get_common_cards'], [hand])
|
2009-06-19 21:51:56 +02:00
|
|
|
# row = c.fetchone()
|
|
|
|
cards['common'] = c.fetchone()
|
2009-03-10 16:14:23 +01:00
|
|
|
return cards
|
|
|
|
|
2009-02-26 05:35:15 +01:00
|
|
|
def convert_cards(self, d):
|
|
|
|
ranks = ('', '', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A')
|
|
|
|
cards = ""
|
2009-03-09 10:06:36 +01:00
|
|
|
for i in xrange(1, 8):
|
2009-03-12 03:46:28 +01:00
|
|
|
# key = 'card' + str(i) + 'Value'
|
|
|
|
# if not d.has_key(key): continue
|
|
|
|
# if d[key] == None:
|
|
|
|
# break
|
|
|
|
# elif d[key] == 0:
|
|
|
|
# cards += "xx"
|
|
|
|
# else:
|
|
|
|
# cards += ranks[d['card' + str(i) + 'Value']] + d['card' +str(i) + 'Suit']
|
2009-06-01 03:25:36 +02:00
|
|
|
cv = "card%dvalue" % i
|
2009-03-11 11:38:17 +01:00
|
|
|
if cv not in d or d[cv] == None:
|
2009-02-26 05:35:15 +01:00
|
|
|
break
|
2009-03-11 11:38:17 +01:00
|
|
|
elif d[cv] == 0:
|
2009-03-10 16:14:23 +01:00
|
|
|
cards += "xx"
|
2009-02-26 05:35:15 +01:00
|
|
|
else:
|
2009-06-01 03:25:36 +02:00
|
|
|
cs = "card%dsuit" % i
|
2009-03-11 11:31:47 +01:00
|
|
|
cards = "%s%s%s" % (cards, ranks[d[cv]], d[cs])
|
2009-02-26 05:35:15 +01:00
|
|
|
return cards
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2008-11-13 04:45:09 +01:00
|
|
|
def get_action_from_hand(self, hand_no):
|
|
|
|
action = [ [], [], [], [], [] ]
|
|
|
|
c = self.connection.cursor()
|
2009-06-18 00:03:43 +02:00
|
|
|
c.execute(self.sql.query['get_action_from_hand'], (hand_no,))
|
2008-11-13 04:45:09 +01:00
|
|
|
for row in c.fetchall():
|
|
|
|
street = row[0]
|
|
|
|
act = row[1:]
|
|
|
|
action[street].append(act)
|
|
|
|
return action
|
2008-12-08 20:10:45 +01:00
|
|
|
|
|
|
|
def get_winners_from_hand(self, hand):
|
|
|
|
"""Returns a hash of winners:amount won, given a hand number."""
|
|
|
|
winners = {}
|
|
|
|
c = self.connection.cursor()
|
2009-06-18 00:03:43 +02:00
|
|
|
c.execute(self.sql.query['get_winners_from_hand'], (hand,))
|
2008-12-08 20:10:45 +01:00
|
|
|
for row in c.fetchall():
|
|
|
|
winners[row[0]] = row[1]
|
|
|
|
return winners
|
|
|
|
|
2009-05-28 00:34:10 +02:00
|
|
|
def get_stats_from_hand(self, hand, aggregate = False):
|
|
|
|
if self.hud_style == 'S':
|
|
|
|
return( self.get_stats_from_hand_session(hand) )
|
|
|
|
else: # self.hud_style == A
|
|
|
|
if aggregate:
|
|
|
|
query = 'get_stats_from_hand_aggregated'
|
|
|
|
else:
|
|
|
|
query = 'get_stats_from_hand'
|
|
|
|
|
|
|
|
if self.hud_style == 'T':
|
|
|
|
stylekey = self.date_ndays_ago
|
|
|
|
else: # assume A (all-time)
|
|
|
|
stylekey = '0000000' # all stylekey values should be higher than this
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2009-05-28 00:34:10 +02:00
|
|
|
subs = (hand, hand, stylekey)
|
|
|
|
#print "get stats: hud style =", self.hud_style, "subs =", subs
|
|
|
|
c = self.connection.cursor()
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2009-05-28 00:34:10 +02:00
|
|
|
# now get the stats
|
2008-11-11 15:50:20 +01:00
|
|
|
c.execute(self.sql.query[query], subs)
|
2008-08-19 00:53:25 +02:00
|
|
|
colnames = [desc[0] for desc in c.description]
|
|
|
|
stat_dict = {}
|
|
|
|
for row in c.fetchall():
|
|
|
|
t_dict = {}
|
|
|
|
for name, val in zip(colnames, row):
|
2009-04-27 22:29:02 +02:00
|
|
|
t_dict[name.lower()] = val
|
2009-04-29 18:16:39 +02:00
|
|
|
# print t_dict
|
2008-08-19 00:53:25 +02:00
|
|
|
stat_dict[t_dict['player_id']] = t_dict
|
2009-05-28 00:34:10 +02:00
|
|
|
|
|
|
|
return stat_dict
|
|
|
|
|
|
|
|
# uses query on handsplayers instead of hudcache to get stats on just this session
|
|
|
|
def get_stats_from_hand_session(self, hand):
|
|
|
|
|
|
|
|
if self.hud_style == 'S':
|
|
|
|
query = self.sql.query['get_stats_from_hand_session']
|
|
|
|
if self.db_server == 'mysql':
|
|
|
|
query = query.replace("<signed>", 'signed ')
|
|
|
|
else:
|
|
|
|
query = query.replace("<signed>", '')
|
|
|
|
else: # self.hud_style == A
|
|
|
|
return None
|
|
|
|
|
|
|
|
subs = (self.hand_1day_ago, hand)
|
|
|
|
c = self.connection.cursor()
|
|
|
|
|
|
|
|
# now get the stats
|
|
|
|
#print "sess_stats: subs =", subs, "subs[0] =", subs[0]
|
|
|
|
c.execute(query, subs)
|
|
|
|
colnames = [desc[0] for desc in c.description]
|
|
|
|
n,stat_dict = 0,{}
|
|
|
|
row = c.fetchone()
|
|
|
|
while row:
|
|
|
|
if colnames[0].lower() == 'player_id':
|
|
|
|
playerid = row[0]
|
|
|
|
else:
|
|
|
|
print "ERROR: query %s result does not have player_id as first column" % (query,)
|
|
|
|
break
|
|
|
|
|
|
|
|
for name, val in zip(colnames, row):
|
|
|
|
if not playerid in stat_dict:
|
|
|
|
stat_dict[playerid] = {}
|
|
|
|
stat_dict[playerid][name.lower()] = val
|
|
|
|
elif not name.lower() in stat_dict[playerid]:
|
|
|
|
stat_dict[playerid][name.lower()] = val
|
|
|
|
elif name.lower() not in ('hand_id', 'player_id', 'seat', 'screen_name', 'seats'):
|
|
|
|
stat_dict[playerid][name.lower()] += val
|
|
|
|
n += 1
|
|
|
|
if n >= 4000: break # todo: don't think this is needed so set nice and high
|
|
|
|
# for now - comment out or remove?
|
|
|
|
row = c.fetchone()
|
|
|
|
#print " %d rows fetched, len(stat_dict) = %d" % (n, len(stat_dict))
|
|
|
|
|
|
|
|
#print "session stat_dict =", stat_dict
|
2008-08-19 00:53:25 +02:00
|
|
|
return stat_dict
|
|
|
|
|
|
|
|
def get_player_id(self, config, site, player_name):
|
|
|
|
c = self.connection.cursor()
|
|
|
|
c.execute(self.sql.query['get_player_id'], {'player': player_name, 'site': site})
|
|
|
|
row = c.fetchone()
|
2009-05-21 22:27:44 +02:00
|
|
|
if row:
|
|
|
|
return row[0]
|
|
|
|
else:
|
|
|
|
return None
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2009-06-20 00:22:57 +02:00
|
|
|
def get_last_insert_id(self):
|
|
|
|
return self.fdb.getLastInsertId()
|
|
|
|
|
2009-06-19 22:15:52 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
,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)
|
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
hands_id = fpdb_simple.storeHands(self.backend, db, cursor, site_hand_no, gametype_id
|
2009-07-01 19:55:12 +02:00
|
|
|
,hand_start_time, names, tableName, maxSeats, hudImportData
|
|
|
|
,(None, None, None, None, None), (None, None, None, None, None))
|
2009-06-19 22:15:52 +02:00
|
|
|
|
|
|
|
#print "before calling store_hands_players_stud, antes:", antes
|
2009-06-19 23:33:33 +02:00
|
|
|
hands_players_ids = fpdb_simple.store_hands_players_stud(self.backend, db, cursor, hands_id, player_ids
|
2009-06-19 22:15:52 +02:00
|
|
|
,start_cashes, antes, card_values
|
|
|
|
,card_suits, winnings, rakes, seatNos)
|
|
|
|
|
2009-07-21 23:26:23 +02:00
|
|
|
if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop':
|
2009-06-19 23:33:33 +02:00
|
|
|
fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametype_id, hand_start_time, player_ids, hudImportData)
|
2009-06-19 22:15:52 +02:00
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
if self.saveActions:
|
2009-06-19 22:15:52 +02:00
|
|
|
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
|
|
|
|
,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()
|
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
hands_id = fpdb_simple.storeHands(self.backend, db, cursor, site_hand_no, gametype_id
|
2009-06-19 22:15:52 +02:00
|
|
|
,hand_start_time, names, tableName, maxSeats,
|
|
|
|
hudImportData, board_values, board_suits)
|
2009-06-20 00:22:57 +02:00
|
|
|
#TEMPORARY CALL! - Just until all functions are migrated
|
2009-06-19 22:15:52 +02:00
|
|
|
t3 = time()
|
|
|
|
hands_players_ids = fpdb_simple.store_hands_players_holdem_omaha(
|
2009-06-19 23:33:33 +02:00
|
|
|
self.backend, db, cursor, category, hands_id, player_ids, start_cashes
|
2009-06-19 22:15:52 +02:00
|
|
|
, positions, card_values, card_suits, winnings, rakes, seatNos, hudImportData)
|
|
|
|
t4 = time()
|
|
|
|
#print "ring holdem, backend=%d" % backend
|
2009-07-21 23:26:23 +02:00
|
|
|
if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop':
|
2009-06-19 23:37:30 +02:00
|
|
|
fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametype_id, hand_start_time, player_ids, hudImportData)
|
2009-06-19 22:15:52 +02:00
|
|
|
t5 = time()
|
|
|
|
t6 = time()
|
2009-06-19 23:33:33 +02:00
|
|
|
if self.saveActions:
|
2009-06-19 22:15:52 +02:00
|
|
|
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)
|
|
|
|
return hands_id
|
|
|
|
#end def ring_holdem_omaha
|
|
|
|
|
|
|
|
def tourney_holdem_omaha(self, config, settings, db, cursor, 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
|
|
|
|
,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 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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
hands_id = fpdb_simple.storeHands(self.backend, db, cursor, site_hand_no, gametype_id
|
2009-06-19 22:15:52 +02:00
|
|
|
,hand_start_time, names, tableName, maxSeats)
|
|
|
|
|
|
|
|
hands_players_ids = fpdb_simple.store_hands_players_holdem_omaha_tourney(
|
2009-06-19 23:33:33 +02:00
|
|
|
self.backend, db, cursor, category, hands_id, player_ids, start_cashes, positions
|
2009-06-19 22:15:52 +02:00
|
|
|
, card_values, card_suits, winnings, rakes, seatNos, tourneys_players_ids)
|
|
|
|
|
|
|
|
#print "tourney holdem, backend=%d" % backend
|
2009-07-21 23:26:23 +02:00
|
|
|
if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop':
|
2009-06-19 23:37:30 +02:00
|
|
|
fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametype_id, hand_start_time, player_ids, hudImportData)
|
2009-06-19 22:15:52 +02:00
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
if self.saveActions:
|
2009-06-19 22:15:52 +02:00
|
|
|
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
|
|
|
|
,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)
|
|
|
|
|
|
|
|
tourney_id = fpdb_simple.store_tourneys(cursor, tourneyTypeId, siteTourneyNo, entries, prizepool, tourneyStartTime)
|
|
|
|
|
|
|
|
tourneys_players_ids = fpdb_simple.store_tourneys_players(cursor, tourney_id, playerIds, payin_amounts, ranks, winnings)
|
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
hands_id = fpdb_simple.storeHands(self.backend, db, cursor, siteHandNo, gametypeId, handStartTime, names, tableName, maxSeats)
|
2009-06-19 22:15:52 +02:00
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
hands_players_ids = fpdb_simple.store_hands_players_stud_tourney(self.backend, db, cursor, hands_id
|
2009-06-19 22:15:52 +02:00
|
|
|
, playerIds, startCashes, antes, cardValues, cardSuits
|
|
|
|
, winnings, rakes, seatNos, tourneys_players_ids)
|
|
|
|
|
2009-07-21 23:26:23 +02:00
|
|
|
if 'dropHudCache' not in settings or settings['dropHudCache'] != 'drop':
|
2009-06-19 23:33:33 +02:00
|
|
|
fpdb_simple.storeHudCache(self.backend, cursor, base, category, gametypeId, hand_start_time, playerIds, hudImportData)
|
2009-06-19 22:15:52 +02:00
|
|
|
|
2009-06-19 23:33:33 +02:00
|
|
|
if self.saveActions:
|
2009-06-19 22:15:52 +02:00
|
|
|
fpdb_simple.storeActions(cursor, hands_players_ids, actionTypes, allIns, actionAmounts, actionNos)
|
|
|
|
return hands_id
|
|
|
|
#end def tourney_stud
|
|
|
|
|
2009-07-21 23:26:23 +02:00
|
|
|
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,)
|
|
|
|
#end def rebuild_hudcache
|
|
|
|
|
|
|
|
|
|
|
|
def analyzeDB(self):
|
|
|
|
"""Do whatever the DB can offer to update index/table statistics"""
|
|
|
|
stime = time()
|
|
|
|
if self.backend == self.MYSQL_INNODB:
|
|
|
|
try:
|
|
|
|
self.cursor.execute(self.sql.query['analyze'])
|
|
|
|
except:
|
|
|
|
print "Error during analyze"
|
|
|
|
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'])
|
|
|
|
except:
|
|
|
|
print "Error during analyze:", str(sys.exc_value)
|
|
|
|
self.connection.set_isolation_level(1) # go back to normal isolation level
|
|
|
|
self.commit()
|
|
|
|
atime = time() - stime
|
|
|
|
print "Analyze took %.1f seconds" % (atime,)
|
|
|
|
#end def analyzeDB
|
|
|
|
|
2008-08-19 00:53:25 +02:00
|
|
|
if __name__=="__main__":
|
|
|
|
c = Configuration.Config()
|
|
|
|
|
2008-10-04 22:43:50 +02:00
|
|
|
db_connection = Database(c, 'fpdb', 'holdem') # mysql fpdb holdem
|
|
|
|
# db_connection = Database(c, 'fpdb-p', 'test') # mysql fpdb holdem
|
2008-08-19 00:53:25 +02:00
|
|
|
# db_connection = Database(c, 'PTrackSv2', 'razz') # mysql razz
|
|
|
|
# db_connection = Database(c, 'ptracks', 'razz') # postgres
|
|
|
|
print "database connection object = ", db_connection.connection
|
|
|
|
print "database type = ", db_connection.type
|
|
|
|
|
|
|
|
h = db_connection.get_last_hand()
|
|
|
|
print "last hand = ", h
|
|
|
|
|
|
|
|
hero = db_connection.get_player_id(c, 'PokerStars', 'nutOmatic')
|
2009-05-21 22:27:44 +02:00
|
|
|
if hero:
|
|
|
|
print "nutOmatic is id_player = %d" % hero
|
2008-08-19 00:53:25 +02:00
|
|
|
|
2008-09-15 22:31:55 +02:00
|
|
|
stat_dict = db_connection.get_stats_from_hand(h)
|
|
|
|
for p in stat_dict.keys():
|
|
|
|
print p, " ", stat_dict[p]
|
|
|
|
|
2009-06-18 00:03:43 +02:00
|
|
|
#print "nutOmatics stats:"
|
|
|
|
#stat_dict = db_connection.get_stats_from_hand(h, hero)
|
|
|
|
#for p in stat_dict.keys():
|
|
|
|
# print p, " ", stat_dict[p]
|
2008-09-15 22:31:55 +02:00
|
|
|
|
2009-06-01 03:25:36 +02:00
|
|
|
print "cards =", db_connection.get_cards(u'1')
|
2008-09-15 22:31:55 +02:00
|
|
|
db_connection.close_connection
|
|
|
|
|
|
|
|
print "press enter to continue"
|
|
|
|
sys.stdin.readline()
|