webgo/database.py

184 lines
5.2 KiB
Python
Raw Normal View History

#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"""
return (self.player1,self.player2)
def set_time(self):
"""updates self.lastmove to current time"""
self.lastmove = int(time.time)
#create table which has been defined above:
# if ifNotExists=True, then an existing table does not get overridden.
GobanTable.createTable(ifNotExists=True)
# add a game to goban table
gob = GobanTable(player1="gast",player2="gast",size=9)
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 removegame(self,gamename):
"""removes a game whose name is given."""
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.game1 = gamename
elif self.game3 == None: self.game1 = gamename
elif self.game4 == None: self.game1 = gamename
elif self.game5 == None: self.game1 = gamename
elif self.game6 == None: self.game1 = gamename
elif self.game7 == None: self.game1 = gamename
elif self.game8 == None: self.game1 = gamename
elif self.game9 == None: self.game1 = gamename
elif self.game10 == None: self.game1 = 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 = Users.select(Users.q.free_slot_left == True)
retlist = []
for entry in idlist:
retlist.append(Users.get(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)
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()