2005-09-20 09:05:05 +02:00
|
|
|
import goban,helper,psql,login
|
2005-09-14 18:48:07 +02:00
|
|
|
import string
|
|
|
|
|
2005-09-20 09:05:05 +02:00
|
|
|
DEBUG = 1
|
|
|
|
|
2005-09-14 18:48:07 +02:00
|
|
|
def is_my_turn(req,form,gobandict):
|
|
|
|
"""
|
|
|
|
gets request and util.FiedStorage form.
|
|
|
|
check wether or not the current this is the players turn.
|
|
|
|
return true or false.
|
|
|
|
"""
|
|
|
|
#INFO: player1 is black,player2 is white. black starts the game.
|
|
|
|
me = form["username"]
|
|
|
|
player1 = gobandict["player1"]
|
|
|
|
player2 = gobandict["player2"]
|
|
|
|
#get turn_number.
|
|
|
|
turn_number = gobandict["turn_number"]
|
2005-09-23 16:37:07 +02:00
|
|
|
#if player 2 can play: are we player two?
|
2005-09-14 18:48:07 +02:00
|
|
|
# yes:return True, else return False
|
|
|
|
if turn_number % 2 == 0:
|
|
|
|
if me == player2:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
#else:are we player1?
|
|
|
|
# yes:return True, else return False
|
|
|
|
else:
|
|
|
|
if me == player1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-09-12 19:30:02 +02:00
|
|
|
|
|
|
|
def main(req,form):
|
|
|
|
"""
|
|
|
|
display selected goban and manage user input.
|
|
|
|
"""
|
2005-09-23 16:37:07 +02:00
|
|
|
|
2005-09-14 16:29:58 +02:00
|
|
|
try:
|
|
|
|
gamename = form["game"]
|
|
|
|
except:
|
|
|
|
gamename = ""
|
|
|
|
if gamename != "":
|
2005-09-23 16:37:07 +02:00
|
|
|
data = ""
|
|
|
|
req.write(helper.header())
|
|
|
|
#helper.debug(req,form,str(form.keys()))
|
2005-09-14 16:29:58 +02:00
|
|
|
#read goban table from database
|
|
|
|
tmplist = psql.read_table(gamename)
|
|
|
|
#make a dictionary out of the list
|
|
|
|
gobandict = psql.fetchall_list_to_goban_dict(tmplist)
|
2005-09-14 18:48:07 +02:00
|
|
|
#check if user has already clicked onto a field:
|
2005-09-23 16:37:07 +02:00
|
|
|
click_on_field = False
|
2005-09-14 18:48:07 +02:00
|
|
|
for item in form.keys():
|
|
|
|
if string.find(item,").x") > 0:
|
2005-09-23 16:37:07 +02:00
|
|
|
click_on_field = True
|
|
|
|
if click_on_field:
|
|
|
|
#if yes: is this the user's turn? then process form.
|
2005-09-14 18:48:07 +02:00
|
|
|
if is_my_turn(req,form,gobandict):
|
2005-09-23 16:37:07 +02:00
|
|
|
helper.debug(req,form,"its my turn , i am going to process the form: --")
|
|
|
|
retstring = goban.process_form(req,form,gobandict)
|
|
|
|
if retstring != "":
|
|
|
|
helper.debug(req,form,"playgame.main says: "+str(retstring))
|
|
|
|
else:
|
|
|
|
#reload gobandict, it has been changed.
|
|
|
|
gobandict = psql.fetchall_list_to_goban_dict(psql.read_table(gamename))
|
|
|
|
else:
|
|
|
|
helper.debug(req,form,"its not my turn.")
|
|
|
|
data += """<br>Turn number: %s. %s plays.\n
|
|
|
|
""" % (str(gobandict["turn_number"]), ["White","Black"][int(gobandict["turn_number"] % 2)])
|
2005-09-14 16:29:58 +02:00
|
|
|
|
|
|
|
#check whether its our turn
|
|
|
|
#if yes: print 'your move' and display goban and process move
|
|
|
|
#if not: print '...s move' and display goban.
|
2005-09-14 18:48:07 +02:00
|
|
|
if is_my_turn(req,form,gobandict):
|
2005-09-23 16:37:07 +02:00
|
|
|
data += "<br>Its your turn.<br>\n"
|
2005-09-14 18:48:07 +02:00
|
|
|
else:
|
2005-09-23 16:37:07 +02:00
|
|
|
data += "<br>This is not your turn. You have to wait for the move of the other player.<br>\n"
|
2005-09-14 18:48:07 +02:00
|
|
|
#print goban
|
2005-09-14 16:29:58 +02:00
|
|
|
data += goban.display_goban(gobandict,req,form)
|
2005-09-20 11:51:12 +02:00
|
|
|
data += login.navigation_bar(req,form)
|
2005-09-14 16:29:58 +02:00
|
|
|
data += helper.footer()
|
|
|
|
req.write(data)
|
2005-09-12 19:30:02 +02:00
|
|
|
|
2005-09-14 16:29:58 +02:00
|
|
|
else:
|
2005-09-14 18:48:07 +02:00
|
|
|
req.write('Error: You have to select a game to play it!')
|