|
@@ -3,7 +3,7 @@ import uuid
|
|
|
|
|
|
from flask import Blueprint, abort, request, jsonify
|
|
|
|
|
|
-from db import db, Game, CoordSet, GuessSet
|
|
|
+from db import db, Game, CoordSet, GuessSet, create_game
|
|
|
from lib import generate_coord, score
|
|
|
|
|
|
game = Blueprint("game", __name__)
|
|
@@ -38,57 +38,15 @@ def create_game():
|
|
|
if not isinstance(timer, int) or timer <= 0:
|
|
|
abort(400)
|
|
|
|
|
|
- game_id = str(uuid.uuid4())
|
|
|
- while Game.query.get(game_id) is not None:
|
|
|
- # basically impossible collision, but let's be safe
|
|
|
- game_id = str(uuid.uuid4())
|
|
|
+ new_game = create_game()
|
|
|
|
|
|
- cs = CoordSet()
|
|
|
- for round_num in "12345":
|
|
|
- coord = generate_coord()
|
|
|
- while coord is None:
|
|
|
- # very unlikely, but it is possible for generate_coord to fail
|
|
|
- coord = generate_coord()
|
|
|
- cs.set_coord(round_num, *coord)
|
|
|
- db.session.add(cs)
|
|
|
- db.session.commit()
|
|
|
-
|
|
|
- new_game = Game(
|
|
|
- game_id=game_id,
|
|
|
- timer=timer,
|
|
|
- creator=name,
|
|
|
- coord_set=cs
|
|
|
- )
|
|
|
- db.session.add(new_game)
|
|
|
- db.session.commit()
|
|
|
-
|
|
|
- gs = GuessSet(game_id=game_id, player_name=name, coord_set=CoordSet())
|
|
|
- db.session.add(gs)
|
|
|
- db.session.commit()
|
|
|
-
|
|
|
- return jsonify({
|
|
|
- "gameId": game_id
|
|
|
- })
|
|
|
+ return jsonify({"gameId": new_game.game_id})
|
|
|
|
|
|
|
|
|
@game.route("/<game_id>")
|
|
|
def game_settings(game_id):
|
|
|
g = require_game(game_id)
|
|
|
-
|
|
|
- return jsonify({
|
|
|
- "gameId": g.game_id,
|
|
|
- "creator": g.creator,
|
|
|
- "timer": g.timer,
|
|
|
- "coords": g.coord_set.to_dict(),
|
|
|
- "players": sorted([
|
|
|
- {
|
|
|
- "name": gs.player_name,
|
|
|
- "currentRound": gs.get_current_round(),
|
|
|
- "totalScore": gs.get_total_score(),
|
|
|
- "guesses": gs.to_dict(),
|
|
|
- } for gs in g.guess_sets
|
|
|
- ], key=lambda x: x["totalScore"] or 0, reverse=True),
|
|
|
- })
|
|
|
+ return jsonify(g.to_dict())
|
|
|
|
|
|
|
|
|
@game.route("/<game_id>/join", methods=["POST"])
|
|
@@ -131,6 +89,7 @@ def make_guess(game_id, round_num):
|
|
|
return jsonify({
|
|
|
"score": 0,
|
|
|
"totalScore": gs.get_total_score(),
|
|
|
+ "distance": None,
|
|
|
}), 201
|
|
|
|
|
|
lat = request.json.get("lat", None)
|
|
@@ -139,10 +98,11 @@ def make_guess(game_id, round_num):
|
|
|
abort(400)
|
|
|
|
|
|
target = require_game(game_id).coord_set.get_coord(round_num)
|
|
|
- guess_score = score(target, (lat, lng))
|
|
|
+ guess_score, distance = score(target, (lat, lng))
|
|
|
gs.set_guess(round_num, lat, lng, guess_score)
|
|
|
db.session.commit()
|
|
|
return jsonify({
|
|
|
"score": guess_score,
|
|
|
"totalScore": gs.get_total_score(),
|
|
|
+ "distance": distance,
|
|
|
}), 201
|