flaskapp.py 1.1 KB

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