Created backend

master
Tibor Bizjak 2019-09-10 22:01:20 +02:00
parent 9766116a99
commit fdbd4d2f09
1 changed files with 42 additions and 0 deletions

42
main.py 100644
View File

@ -0,0 +1,42 @@
from bottle import template, get, run, post, request, redirect
from chess import Game
TEMPLATE = "template.html"
games = dict()
def is_submitted(name):
return request.forms.get(name) != None
@get("/")
def index():
game = Game()
id = max(list(games.keys())+[0]) + 1
games[id] = game
redirect("/game/{}/".format(id))
@get("/game/<id:int>/")
def show(id):
return template(TEMPLATE, game=games[id], valid=True)
@post("/game/<id:int>/")
def move(id):
valid = True
if is_submitted("newgame"):
redirect("/")
if is_submitted("prev"):
games[id].prev()
elif is_submitted("first"):
games[id].first()
elif is_submitted("next"):
games[id].next()
elif is_submitted("last"):
games[id].last()
else:
AN = request.forms.get("move")
valid = games[id].AN_move(AN)
return template(TEMPLATE, game=games[id], valid=valid)
run(host="localhost", port=8080)