示例#1
0
def main():
    form = cgi.FieldStorage()

    otherplayer = ""
    try:
        user_data = data.read_users()
        user_info = user.get_session_from_cookie(user_data)
        game_data = data.read_games()
        selfplayer = user_info["username"]
        otherplayer = get_form_otherplayer(form)
        # lookup will verify that otherplayer exists
        otherplayer_info = user.find_info(user_data, otherplayer)

        if (selfplayer == otherplayer):
            raise GameVsSelf("""<p>
			Choose a different player</p>""")

        if game.exists(game_data, selfplayer, otherplayer):
            raise GameAlreadyExists(
                """<p><a href="/cgi-bin/play.py?p0=%s&p1=%s">
			%s vs %s already exists</a></p>""" % (cgi.escape(selfplayer, quote=True),
                                         cgi.escape(otherplayer, quote=True),
                                         cgi.escape(selfplayer, quote=True),
                                         cgi.escape(otherplayer, quote=True)))

        game.create_new(game_data, selfplayer, otherplayer)

        data.write_games(game_data)

        #print "<pre>"
        #print game_data
        #print "</pre>"
        print "Status: 302 Found"
        print "Location: /cgi-bin/play.py?p0=%s&p1=%s\n\n" % (cgi.escape(
            selfplayer, quote=True), cgi.escape(otherplayer, quote=True))

    except user.NotLoggedIn:
        print "Content-Type: text/html\n\n"
        print "<html><body>"

        print """<p>Not logged in.
		<a href="/cgi-bin/login.py">Log In</a>
		</p>"""

        print "</body></html>"
        # session not found, redirect back to login
        #print "Location: /cgi-bin/login.py\n\n"

    except user.UnknownUser:
        print_header()
        print "<p>Unknown player</p>"
        print_form({"otherplayer": otherplayer})
        print_footer()

    except Exception as ex:
        print_header()
        print ex
        print_form({"otherplayer": otherplayer})
        print_footer()
示例#2
0
def main():
	form = cgi.FieldStorage()

	username = ""
	try:
		user_data = data.read_users()

		username = get_form_username(form)
		password = get_form_password(form)
		user_info = user.find_info_check_password(user_data, username, password)

		session_id = user.register_user_session(user_data, username)
		expiration = datetime.datetime.now() + datetime.timedelta(days=1)
		cookie = Cookie.SimpleCookie()
		cookie["session"] = session_id

		data.write_users(user_data)

		print cookie.output()
		print "Status: 302 Found"
		print "Location: /cgi-bin/lobby.py\n\n"

	except (Exception, user.UnknownUser) as ex:
		print "Content-Type: text/html\n\n"
		print "<html><body>"
		print "<h2>Log In</h2>"

		#print "<pre>\n"
		#print user_data
		#print "</pre>"

		if isinstance(ex, user.UnknownUser):
			print "<p>Unknown username</p>"
		else:
			print ex

		print """
		<form method="POST" action="/cgi-bin/login.py">
		"""

		print """
		<p>username: <input type="text" name="username" value="%s"/></p>
		""" % cgi.escape(username, quote=True)

		print """
		<p>password: <input type="password" name="password"/></p>
		<input type="submit" value="Log In"/>
		</form>
		"""

		print """
		<p><a href="/cgi-bin/signup.py">Sign Up</a></p>
		"""

		print "</body></html>"
示例#3
0
def main():
    user_data = data.read_users()

    user_info = user.get_session_from_cookie(user_data)

    if user_info:
        user.delete_user_session(user_data, user_info["username"])
        data.write_users(user_data)

    print "Status: 302 Found"
    print "Location: /cgi-bin/login.py\n\n"
示例#4
0
文件: play.py 项目: ivanwick/cgi-game
def main():
    form = cgi.FieldStorage()

    try:
        user_data = data.read_users()
        user_info = user.get_session_from_cookie(user_data)
        selfplayer = user_info["username"]
        game_data = data.read_games()
        (p0, p1) = get_form_players(form)
        game_info = game.get_game_info(game_data, p0, p1)

        print_header()

        move = get_form_move(form)
        if move:
            try:
                game_data = game.move(game_data, game_info, selfplayer, move)
                data.write_games(game_data)
            except game.InvalidMove:
                print "<p>Invalid Move</p>"

        #print "<pre>"
        #print game_data
        #print "</pre>"

        game_over = game.is_game_over(game_info)

        if game_over[0]:
            print_game_over(game_info, game_over[1])
        else:
            print_game(game_info, selfplayer)

        print_footer()

    except user.NotLoggedIn:
        print "Content-Type: text/html\n\n"
        print "<html><body>"

        print """<p>Not logged in.
		<a href="/cgi-bin/login.py">Log In</a>
		</p>"""

        print "</body></html>"
        # session not found, redirect back to login
        #print "Location: /cgi-bin/login.py\n\n"

    except (game.UnknownGame, MissingPlayers) as ex:
        print_header()
        print ex
        print """<a href="/cgi-bin/newgame.py">New Game</a></p>"""
        print_footer()
示例#5
0
def main():
    form = cgi.FieldStorage()

    username = ""

    try:
        user_data = data.read_users()

        username = check_username(form, user_data)
        newpass = check_password(form)

        user.register_new_user(user_data, username, newpass)

        data.write_users(user_data)

        print "Status: 302 Found"
        print "Location: /cgi-bin/login.py\n\n"

    except Exception as e:
        print "Content-Type: text/html\n\n"
        print "<html><body>"
        print "<h2>Sign Up</h2>"

        #print "<pre>\n"
        #print user_data
        #print "</pre>"

        print e

        print """
		<form method="POST" action="/cgi-bin/signup.py">
		"""

        print """
		<p>username: <input type="text" name="username" value="%s"/></p>
		""" % cgi.escape(username, quote=True)

        print """
		<p>password: <input type="password" name="password1"/></p>
		<p>password again: <input type="password" name="password2"/></p>
		<input type="submit"/>
		</form>
		"""

        print "</body></html>"
示例#6
0
def main():

    try:
        user_data = data.read_users()
        user_info = user.get_session_from_cookie(user_data)
        game_data = data.read_games()

        print "Content-Type: text/html\n\n"
        print "<html><body>"
        print "<h2>Lobby</h2>"

        username = user_info["username"]

        print """
		<p>Welcome back %s</p>
		""" % username

        join_games = game.find_games(game_data, username)
        for g in join_games:
            p0 = g["players"][0]
            p1 = g["players"][1]
            print """<p>
			<a href="/cgi-bin/play.py?p0=%s&p1=%s">%s vs %s</a>
			</p>""" % (p0, p1, p0, p1)

        print """
		<p><a href="/cgi-bin/newgame.py">New Game</a></p>
		<p><a href="/cgi-bin/logout.py">Log Out</a></p>
		"""

        print "</body></html>"

    except user.NotLoggedIn:
        print "Content-Type: text/html\n\n"
        print "<html><body>"

        print """<p>Not logged in.
		<a href="/cgi-bin/login.py">Log In</a>
		</p>"""

        print "</body></html>"
示例#7
0
from interface import request_chosing_type
from interface import request_login
from interface import request_password_change, request_username_change, request_email_change, request_user_removal
from process import get_users, upload_users_list
from data import read_users, write_users
#to run the program
if __name__ == '__main__':
    # retrieve a list of users (data module)
    list = read_users()
    # upload a list of users to be processed (process module)
    upload_users_list(list)
    # request for a registration with a new users' details (interface module)
    # register the user (process module)
    request_chosing_type()
    # update the users list (data module)
    write_users(get_users())

    data = request_login()
    empty = {}
    if data != empty:
        request_username_change(data)
        request_password_change(data)
        request_email_change(data)
        request_user_removal(data)
示例#8
0
def users():
    users_list = read_users()
    return render_template("users.html", result=users_list)