Ver código fonte

Fix unique game id creation

Pjotir 10 meses atrás
pai
commit
7bd4a63a00
1 arquivos alterados com 15 adições e 6 exclusões
  1. 15 6
      flaskapp.py

+ 15 - 6
flaskapp.py

@@ -1,4 +1,12 @@
-from flask import Flask, abort, render_template, request, session
+from flask import (
+    Flask,
+    abort,
+    redirect,
+    render_template,
+    request,
+    session,
+    url_for,
+)
 
 from game import Game, randomword
 
@@ -13,26 +21,27 @@ def home():
 
 
 @app.route("/newgame")
-def new_game(name):
+def new_game():
     try:
         name = request.args["playerName"]
         hand_size = request.args["handSize"]
         match_count = request.args["matchCount"]
         turn_count = request.args["turnCount"]
-    except KeyError as e:
+    except (KeyError, TypeError) as e:
         return abort(400)
 
     game_id = None
     while game_id := randomword(4):
-        for game in app.games:
-            if game_id != game.id:
-                break
+        if game_id not in [game.id for game in app.games]:
+            break
 
     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>")
 def game(game_id):