webgo/database.py
phear a000c9dd9a fixed 3 bugs:
- check for timeout in gamecreation.py
- game deletion did not work correctly
- game creation always wrote to the first game slot.
2005-12-14 14:23:14 +00:00

184 lines
5.1 KiB
Python

#apt-get install python-sqlobject python-psycopg
from sqlobject import *
import sys, os, time
import gnugo, helper
#account and database name
dbname = "/home/mtsrc/daten/projekte/webgo/webgo.sqlite"
#dbname = "/tmp/webgo.sqlite"
dbdebug = "debug=t&debugOutput=t" #don't want SQL debug messages? just change to an empty string: "debug="
#dbdebug = "debug="
#build connection string and connect
connection_string='sqlite:%s?%s' %(dbname,dbdebug)
connection=connectionForURI(connection_string)
__connection__ = connection
class GobanTable(SQLObject):
"""
Abstract:
A GobanTable instance contains all data of a current goban: players, board etc.
Details: The table looks like this:
player1
player2
turn_number
size
name
description
sgf
created
lastmove
"""
player1 = StringCol()
player2 = StringCol()
size = IntCol()
#name = StringCol(default=helper.generate_game_name(), unique=True,notNone=True,alternateID=True)
turn_number = 1
description = StringCol(default=None)
sgf = gnugo.create_sgf_file(size)
created = int(time.time())
lastmove = int(time.time())
def players(self):
"""returns both player names as a tuple"""
print self.player1,self.player2
return (self.player1,self.player2)
def set_time(self):
"""updates self.lastmove to current time"""
self.lastmove = int(time.time())
class Users(SQLObject):
"""
creates a table named users with following columns:
name - name of user
password - passsword of user
game1 - the 10 game slots of this user contain names
... of goban tables
game10
sessionid - id of current session
timeout - when does session time out?
"""
username = StringCol(unique=True,notNone=True,alternateID=True)
password = StringCol()
game1 = StringCol(default=None)
game2 = StringCol(default=None)
game3 = StringCol(default=None)
game4 = StringCol(default=None)
game5 = StringCol(default=None)
game6 = StringCol(default=None)
game7 = StringCol(default=None)
game8 = StringCol(default=None)
game9 = StringCol(default=None)
game10 = StringCol(default=None)
sessionid = StringCol(default=None)
timeout = IntCol(default=None)
def gamelist(self):
"""
return the list of all games of the given user.
"""
#TODO: how to generate this list automatically?
ret = []
ret.append(self.game1)
ret.append(self.game2)
ret.append(self.game3)
ret.append(self.game4)
ret.append(self.game5)
ret.append(self.game6)
ret.append(self.game7)
ret.append(self.game8)
ret.append(self.game9)
ret.append(self.game10)
return ret
def remove_game(self,gamename):
"""removes a game whose name is given."""
gamename = int(gamename) #sqlobject does a int(value) for self.game[1..10]
if self.game1 == gamename: self.game1=None
if self.game2 == gamename: self.game2=None
if self.game3 == gamename: self.game3=None
if self.game4 == gamename: self.game4=None
if self.game5 == gamename: self.game5=None
if self.game6 == gamename: self.game6=None
if self.game7 == gamename: self.game7=None
if self.game8 == gamename: self.game8=None
if self.game9 == gamename: self.game9=None
if self.game10 == gamename: self.game10=None
def free_slot_left(self):
"""Returns True, if there is an empty game slot left, else returns False."""
if self.game1 == None: return True
elif self.game2 == None: return True
elif self.game3 == None: return True
elif self.game4 == None: return True
elif self.game5 == None: return True
elif self.game6 == None: return True
elif self.game7 == None: return True
elif self.game8 == None: return True
elif self.game9 == None: return True
elif self.game10 == None: return True
else: return False
def add_game(self,gamename):
"""saves the game name into an empty slot"""
if self.game1 == None: self.game1 = gamename
elif self.game2 == None: self.game2 = gamename
elif self.game3 == None: self.game3 = gamename
elif self.game4 == None: self.game4 = gamename
elif self.game5 == None: self.game5 = gamename
elif self.game6 == None: self.game6 = gamename
elif self.game7 == None: self.game7 = gamename
elif self.game8 == None: self.game8 = gamename
elif self.game9 == None: self.game9 = gamename
elif self.game10 == None: self.game10 = gamename
else: return False
def set_timeout(self):
self.timeout = int(time.time()) + 900 #current time in seconds + seconds for session
def get_users_with_free_game_slots():
"""
returns a list of users who have at least one empty game slot """
idlist = list(Users.select())
retlist = []
for entry in idlist:
if entry.free_slot_left() == True:
retlist.append(entry.username)
return retlist
#create table which has been defined above:
# if ifNotExists=True, then an existing table does not get overridden.
Users.createTable(ifNotExists=True)
GobanTable.createTable(ifNotExists=True)
if __name__ == "__main__":
connection_string='sqlite:%s?%s' %(os.path.abspath("webgo.sqlite"),dbdebug)
connection=connectionForURI(connection_string)
__connection__ = connection
# add a game to goban table
user = Users(username="gast",password="gast")
#print user.gamelist()
print user.sessionid
user.sessionid=1
print user.sessionid
#print Users.byUsername("gast")
#deleting a row:
#f = MyObject(1)
#f.destroySelf()