| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- from flask import (
- Flask,
- abort,
- flash,
- redirect,
- render_template,
- request,
- session,
- url_for,
- )
- import logging
- from game import Game, randomword
- app = Flask(__name__)
- app.secret_key = "somebullshit"
- app.games = []
- logging.basicConfig(
- level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
- )
- @app.route("/")
- def home():
- return render_template("home.html")
- @app.route("/newgame")
- def new_game():
- try:
- app.logger.info("Creating game...")
- name = request.args["playerName"]
- hand_size = int(request.args["handSize"])
- match_count = int(request.args["matchCount"])
- turn_count = int(request.args["turnCount"])
- if not name or not hand_size or not match_count or not turn_count:
- raise KeyError
- except KeyError as e:
- app.logger.error(e)
- app.logger.error("Failed, missing args")
- flash("Failed to create game, bad arguments")
- return redirect(url_for("home"))
- except TypeError as e:
- app.logger.error(e)
- app.logger.error("Failed, bad args")
- return abort(400)
- game_id = None
- while game_id := randomword(4):
- if game_id not in [game.id for game in app.games]:
- break
- app.logger.info(
- f"Game created: {game_id} {name} {hand_size} {match_count} {turn_count}"
- )
- game = Game(name, hand_size, match_count, turn_count, game_id)
- app.games.append(game)
- session["game_id"] = game.id
- session["player_id"] = game.player1.id
- return redirect(url_for("game", game_id=game.id))
- @app.route("/game/<game_id>")
- @app.route("/game")
- def game(game_id=None):
- try:
- name = request.args["playerName"]
- if not name or not game_id:
- raise KeyError
- if game_id not in [game.id for game in app.games]:
- flash("Game not found")
- return redirect(url_for("home"))
- except KeyError as e:
- app.logger.error(e)
- app.logger.error("Failed, missing args")
- flash("Failed to join game, bad game code or player name")
- return redirect(url_for("home"))
- except Exception as e:
- app.logger.error(e)
- app.logger.error("Failed, bad args")
- return abort(400)
- return render_template("game.html", game_id=game_id)
- @app.route("/test")
- def test():
- return render_template("game.html")
- if __name__ == "__main__":
- app.run(debug=True)
|