|
@@ -1,6 +1,10 @@
|
|
|
from functools import wraps
|
|
|
+import uuid
|
|
|
|
|
|
-from flask import Blueprint, session, abort
|
|
|
+from flask import Blueprint, session, abort, request, current_app, jsonify
|
|
|
+
|
|
|
+from db import db, Game
|
|
|
+from lib import generate_coord
|
|
|
|
|
|
game = Blueprint("game", __name__)
|
|
|
|
|
@@ -12,21 +16,63 @@ def require_name():
|
|
|
return name
|
|
|
|
|
|
|
|
|
-@game.route("/", methods=["PUT"])
|
|
|
+def require_game(game_id):
|
|
|
+ g = Game.get(game_id)
|
|
|
+ if g is None:
|
|
|
+ abort(404)
|
|
|
+ return g
|
|
|
+
|
|
|
+
|
|
|
+@game.route("", methods=["PUT"])
|
|
|
def create_game():
|
|
|
- pass
|
|
|
+ name = require_name()
|
|
|
+ timer = request.json.get("timer", None)
|
|
|
+ if not isinstance(timer, int) or timer <= 0:
|
|
|
+ abort(400)
|
|
|
+
|
|
|
+ game_id = str(uuid.uuid4())
|
|
|
+ while Game.get(game_id) is not None:
|
|
|
+ # basically impossible collision, but let's be safe
|
|
|
+ game_id = str(uuid.uuid4())
|
|
|
+
|
|
|
+ coords = [generate_coord(current_app.config["GOOGLE_API_KEY"]) for _ in range(5)]
|
|
|
+ print(coords) # TODO
|
|
|
+
|
|
|
+ new_game = Game(
|
|
|
+ game_id=game_id,
|
|
|
+ timer=timer,
|
|
|
+ creator=name
|
|
|
+ )
|
|
|
+ db.session.add(new_game)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ return jsonify({
|
|
|
+ "gameId": game_id
|
|
|
+ })
|
|
|
|
|
|
|
|
|
@game.route("/<game_id>")
|
|
|
def game_settings(game_id):
|
|
|
- pass
|
|
|
+ g = require_game(game_id)
|
|
|
+
|
|
|
+ return jsonify({
|
|
|
+ "gameId": g.game_id,
|
|
|
+ "creator": g.creator,
|
|
|
+ "timer": g.timer,
|
|
|
+ "coords": {}, # TODO
|
|
|
+ "guesses": [], # TODO
|
|
|
+ })
|
|
|
|
|
|
|
|
|
-@game.route("/<game_id>/guesses/<name>")
|
|
|
-def guesses(game_id, name):
|
|
|
- pass
|
|
|
+@game.route("/<game_id>/guesses")
|
|
|
+def guesses(game_id):
|
|
|
+ name = require_name()
|
|
|
+ g = require_game(game_id)
|
|
|
+ return "Unimplemented", 500 # TODO
|
|
|
|
|
|
|
|
|
-@game.route("/<game_id>/guesses/<name>/<round>", methods=["POST"])
|
|
|
-def make_guess(game_id, name, round):
|
|
|
- pass
|
|
|
+@game.route("/<game_id>/guesses/<round>", methods=["POST"])
|
|
|
+def make_guess(game_id, round):
|
|
|
+ name = require_name()
|
|
|
+ g = require_game(game_id)
|
|
|
+ return "Unimplemented", 500 # TODO
|