Pārlūkot izejas kodu

Completed creation of new games

Kirk Trombley 5 gadi atpakaļ
vecāks
revīzija
a285b81b95
1 mainītis faili ar 20 papildinājumiem un 7 dzēšanām
  1. 20 7
      server/game_api.py

+ 20 - 7
server/game_api.py

@@ -3,7 +3,7 @@ import uuid
 
 from flask import Blueprint, session, abort, request, current_app, jsonify
 
-from db import db, Game
+from db import db, Game, CoordSet, GuessSet
 from lib import generate_coord
 
 game = Blueprint("game", __name__)
@@ -17,12 +17,17 @@ def require_name():
 
 
 def require_game(game_id):
-    g = Game.get(game_id)
+    g = Game.query.get(game_id)
     if g is None:
         abort(404)
     return g
 
 
+def new_coord_string():
+    lat, lng = generate_coord(current_app.config["GOOGLE_API_KEY"])
+    return f"{lat},{lng}"
+
+
 @game.route("", methods=["PUT"])
 def create_game():
     name = require_name()
@@ -31,17 +36,25 @@ def create_game():
         abort(400)
 
     game_id = str(uuid.uuid4())
-    while Game.get(game_id) is not None:
+    while Game.query.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
+    cs = CoordSet(
+        coord_1=new_coord_string(),
+        coord_2=new_coord_string(),
+        coord_3=new_coord_string(),
+        coord_4=new_coord_string(),
+        coord_5=new_coord_string()
+    )
+    db.session.add(cs)
+    db.session.commit()
 
     new_game = Game(
         game_id=game_id,
         timer=timer,
-        creator=name
+        creator=name,
+        coord_set=cs
     )
     db.session.add(new_game)
     db.session.commit()
@@ -59,7 +72,7 @@ def game_settings(game_id):
         "gameId": g.game_id,
         "creator": g.creator,
         "timer": g.timer,
-        "coords": {}, # TODO
+        "coords": g.coord_set.to_dict(),
         "guesses": [], # TODO
     })