flaskapp.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from flask import (
  2. Flask,
  3. abort,
  4. redirect,
  5. render_template,
  6. request,
  7. session,
  8. url_for,
  9. )
  10. import logging
  11. from game import Game, randomword
  12. app = Flask(__name__)
  13. app.secret_key = "somebullshit"
  14. app.games = []
  15. logging.basicConfig(
  16. level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
  17. )
  18. @app.route("/")
  19. def home():
  20. return render_template("home.html")
  21. @app.route("/newgame")
  22. def new_game():
  23. try:
  24. app.logger.info("Creating game...")
  25. name = request.args["playerName"]
  26. hand_size = request.args["handSize"]
  27. match_count = request.args["matchCount"]
  28. turn_count = request.args["turnCount"]
  29. except (KeyError, TypeError) as e:
  30. app.logger.error(e)
  31. app.logger.error("Failed, bad args")
  32. return abort(400)
  33. game_id = None
  34. while game_id := randomword(4):
  35. if game_id not in [game.id for game in app.games]:
  36. break
  37. app.logger.info(
  38. f"Game created: {game_id} {name} {hand_size} {match_count} {turn_count}"
  39. )
  40. game = Game(name, hand_size, match_count, turn_count, game_id)
  41. app.games.append(game)
  42. session["game_id"] = game.id
  43. session["player_id"] = game.player1.id
  44. return redirect(url_for("game", game_id=game.id))
  45. @app.route("/game/<game_id>")
  46. def game(game_id):
  47. return render_template("game.html", game_id=game_id)
  48. if __name__ == "__main__":
  49. app.run(debug=True)