Added cookies
parent
fdbd4d2f09
commit
661e87715a
25
main.py
25
main.py
|
@ -1,7 +1,8 @@
|
||||||
from bottle import template, get, run, post, request, redirect
|
from bottle import template, get, run, post, request, redirect, response
|
||||||
from chess import Game
|
from chess import Game
|
||||||
|
|
||||||
|
|
||||||
|
KEY = "abcdefgh"
|
||||||
TEMPLATE = "template.html"
|
TEMPLATE = "template.html"
|
||||||
games = dict()
|
games = dict()
|
||||||
|
|
||||||
|
@ -10,17 +11,23 @@ def is_submitted(name):
|
||||||
|
|
||||||
@get("/")
|
@get("/")
|
||||||
def index():
|
def index():
|
||||||
game = Game()
|
if request.get_cookie("gameid", secret=KEY) not in games.keys():
|
||||||
id = max(list(games.keys())+[0]) + 1
|
game = Game()
|
||||||
games[id] = game
|
id = str(max(list(map(int, games.keys()))+[0]) + 1)
|
||||||
redirect("/game/{}/".format(id))
|
games[id] = game
|
||||||
|
response.set_cookie("gameid", id, path='/', secret=KEY)
|
||||||
|
redirect("/game/")
|
||||||
|
|
||||||
@get("/game/<id:int>/")
|
@get("/game/")
|
||||||
def show(id):
|
def show():
|
||||||
|
id = request.get_cookie("gameid", secret=KEY)
|
||||||
|
if id not in games:
|
||||||
|
redirect("/")
|
||||||
return template(TEMPLATE, game=games[id], valid=True)
|
return template(TEMPLATE, game=games[id], valid=True)
|
||||||
|
|
||||||
@post("/game/<id:int>/")
|
@post("/game/")
|
||||||
def move(id):
|
def move():
|
||||||
|
id = request.get_cookie("gameid", secret=KEY)
|
||||||
valid = True
|
valid = True
|
||||||
if is_submitted("newgame"):
|
if is_submitted("newgame"):
|
||||||
redirect("/")
|
redirect("/")
|
||||||
|
|
Loading…
Reference in New Issue