flaskapp.py 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from flask import Flask, abort, render_template, request, session
  2. from game import Game, randomword
  3. app = Flask(__name__)
  4. app.secret_key = "somebullshit"
  5. app.games = []
  6. @app.route("/")
  7. def home():
  8. return render_template("home.html")
  9. @app.route("/newgame")
  10. def new_game(name):
  11. try:
  12. name = request.args["playerName"]
  13. hand_size = request.args["handSize"]
  14. match_count = request.args["matchCount"]
  15. turn_count = request.args["turnCount"]
  16. except KeyError as e:
  17. return abort(400)
  18. game_id = None
  19. while game_id := randomword(4):
  20. for game in app.games:
  21. if game_id != game.id:
  22. break
  23. game = Game(name, hand_size, match_count, turn_count, game_id)
  24. app.games.append(game)
  25. session["game_id"] = game.id
  26. session["player_id"] = game.player1.id
  27. @app.route("/game/<game_id>")
  28. def game(game_id):
  29. return render_template("game.html", game_id=game_id)
  30. if __name__ == "__main__":
  31. app.run(debug=True)