2005-11-23 18:34:20 +01:00
|
|
|
import helper,login,database
|
2005-11-24 16:39:22 +01:00
|
|
|
from cherrypy import cpg
|
|
|
|
from cherrypy.lib import httptools
|
2005-09-11 19:11:09 +02:00
|
|
|
|
2005-09-20 09:05:05 +02:00
|
|
|
|
2005-09-11 19:11:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_user_dropdown_list(listname,selected = ""):
|
|
|
|
"""
|
|
|
|
gets a name for the generated list and, optionally, the preselected value.
|
|
|
|
returns a <select> form as string.
|
|
|
|
"""
|
2005-11-23 18:34:20 +01:00
|
|
|
userlist = database.get_users_with_free_game_slots()
|
2005-09-11 19:11:09 +02:00
|
|
|
data = '<select name="%s">' % listname
|
|
|
|
for item in userlist:
|
|
|
|
#check whether current item = item for preselection
|
|
|
|
if item == selected:
|
|
|
|
tmp = 'selected'
|
|
|
|
else:
|
|
|
|
tmp = ''
|
|
|
|
data += '<option %s> %s </option>' % (tmp,item)
|
|
|
|
data += '</select>'
|
|
|
|
return data
|
|
|
|
|
|
|
|
def create_goban_size_dropdown_list(listname):
|
|
|
|
"""
|
|
|
|
gets a name, returns a string with a html form for selecting the goban size.
|
|
|
|
"""
|
|
|
|
data = """
|
|
|
|
<select name="%s">
|
|
|
|
<option> 9 </option>
|
|
|
|
<option> 13 </option>
|
|
|
|
<option> 19 </option>
|
|
|
|
</select>
|
|
|
|
""" % listname
|
|
|
|
return data
|
|
|
|
|
2005-11-24 16:39:22 +01:00
|
|
|
class GameCreation:
|
2005-09-11 19:11:09 +02:00
|
|
|
"""
|
2005-11-24 16:39:22 +01:00
|
|
|
manages the creation of games.
|
2005-09-11 19:11:09 +02:00
|
|
|
"""
|
2005-11-24 16:39:22 +01:00
|
|
|
def index(self,player1 = None, player2 = None, description = None,gobansize = None):
|
|
|
|
username = cpg.request.sessionMap["username"]
|
|
|
|
myuser = database.Users.byUsername(username)
|
|
|
|
sessionid = cpg.request.sessionMap["_sessionId"]
|
|
|
|
if myuser.sessionid == sessionid:
|
|
|
|
if player1 != None and player2 != None and gobansize != None:
|
|
|
|
return self.process_creation_form(player1,player2,gobansize,description)
|
|
|
|
else:
|
|
|
|
return self.display_game_creation_form()
|
|
|
|
|
|
|
|
else:
|
|
|
|
httptools.redirect("/login")
|
|
|
|
|
|
|
|
def display_game_creation_form(self):
|
|
|
|
"""
|
|
|
|
prints a html form with multiple drop-down lists for choosing players,
|
|
|
|
goban size and so on.
|
2005-09-14 16:29:58 +02:00
|
|
|
|
2005-11-24 16:39:22 +01:00
|
|
|
"""
|
|
|
|
username = cpg.request.sessionMap["username"]
|
|
|
|
myuser = database.Users.byUsername(username)
|
|
|
|
sessionid = cpg.request.sessionMap["_sessionId"]
|
|
|
|
|
|
|
|
|
|
|
|
data = helper.header()
|
|
|
|
p1data = create_user_dropdown_list("player1",username)
|
|
|
|
p2data = create_user_dropdown_list("player2")
|
|
|
|
gobansize = create_goban_size_dropdown_list("gobansize")
|
|
|
|
#start form
|
|
|
|
#choose player one (black),choose player two (white),choose goban size
|
|
|
|
data += """
|
|
|
|
<form method="post">
|
|
|
|
<p>Player One (Black): %s </p>
|
|
|
|
<p>Player Two (White): %s </p>
|
|
|
|
<p>Goban Size: %s fields </p>
|
|
|
|
<p>Optional name for game: <input type="text" name="description" size="20" class="text"></p>
|
|
|
|
<input type="submit" class="submit" value="create game">
|
|
|
|
</form>
|
|
|
|
""" % (p1data,p2data,gobansize)
|
|
|
|
data += helper.footer()
|
|
|
|
return data
|
|
|
|
|
|
|
|
def process_creation_form(self,player1,player2,gobansize,description=None):
|
|
|
|
"""
|
|
|
|
validates and processes the game creation form.
|
|
|
|
If everything was ok, a game will be created.
|
|
|
|
TODO: change this function, so that the original database state canbe
|
|
|
|
restored, if for example one of the users does not have a free game slot.
|
|
|
|
"""
|
|
|
|
username = cpg.request.sessionMap["username"]
|
|
|
|
myuser = database.Users.byUsername(username)
|
|
|
|
sessionid = cpg.request.sessionMap["_sessionId"]
|
|
|
|
|
|
|
|
data = helper.header()
|
|
|
|
#check if at least one of the players is the current user
|
|
|
|
if (player1 == username) or (player2 == username):
|
|
|
|
#create game
|
|
|
|
mygame = database.GobanTable(player1=player1,player2=player2,size=gobansize,description = description)
|
|
|
|
gamename = mygame.id
|
|
|
|
#update entries for player one and player two
|
|
|
|
for player in [player1,player2]:
|
|
|
|
myplayer = database.Users.byUsername(player)
|
|
|
|
if myplayer.free_slot_left:
|
|
|
|
myplayer.add_game(gamename)
|
|
|
|
else:#should not happen: no free game slot.
|
|
|
|
#print error msg
|
|
|
|
data += "Error: No free game slots for player %s!<br>" % player
|
|
|
|
#display form again
|
|
|
|
return self.display_game_creation_form()
|
|
|
|
|
|
|
|
httptools.redirect("/overview")
|
|
|
|
else:
|
|
|
|
#give error message
|
|
|
|
data += "Sorry, you must be one of the players!<br>"
|
|
|
|
#display form again
|
|
|
|
return self.display_game_creation_form()
|
|
|
|
|
|
|
|
data+=helper.footer(req,form)
|
|
|
|
return data
|
|
|
|
index.exposed = True
|