Added cookies

master
Tibor Bizjak 2019-09-10 22:29:59 +02:00
parent fdbd4d2f09
commit 661e87715a
1 changed files with 16 additions and 9 deletions

21
main.py
View File

@ -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():
if request.get_cookie("gameid", secret=KEY) not in games.keys():
game = Game() game = Game()
id = max(list(games.keys())+[0]) + 1 id = str(max(list(map(int, games.keys()))+[0]) + 1)
games[id] = game games[id] = game
redirect("/game/{}/".format(id)) 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("/")