Эх сурвалжийг харах

Some auto-formatting and docs of the back-end

Kirk Trombley 5 жил өмнө
parent
commit
1f6b0c2970

+ 13 - 0
server/app.py

@@ -1,3 +1,10 @@
+"""
+Main entry-point for the TerrAssumptions API server
+
+Running this file directly will launch a development server.
+The WSGI-compatible Flask server is exposed from this module as app.
+"""
+
 import os
 import os
 
 
 from flask import Flask, jsonify
 from flask import Flask, jsonify
@@ -21,11 +28,17 @@ db.create_all(app=app)
 
 
 @app.route("/")
 @app.route("/")
 def version():
 def version():
+    """
+    Return the version of the API and a status message, "healthy"
+    """
     return jsonify({"version": "1", "status": "healthy"})
     return jsonify({"version": "1", "status": "healthy"})
 
 
 
 
 @app.route("/googleApiKey")
 @app.route("/googleApiKey")
 def google_api_key():
 def google_api_key():
+    """
+    Return the google API key configured for the application
+    """
     return jsonify({"googleApiKey": app.config["GOOGLE_API_KEY"]})
     return jsonify({"googleApiKey": app.config["GOOGLE_API_KEY"]})
 
 
 
 

+ 9 - 7
server/db.py

@@ -2,6 +2,7 @@ from flask_sqlalchemy import SQLAlchemy
 
 
 db = SQLAlchemy()
 db = SQLAlchemy()
 
 
+
 class Game(db.Model):
 class Game(db.Model):
     game_id = db.Column(db.String, primary_key=True)
     game_id = db.Column(db.String, primary_key=True)
     timer = db.Column(db.Integer)
     timer = db.Column(db.Integer)
@@ -48,7 +49,7 @@ class CoordSet(db.Model):
             c = self.coord_5
             c = self.coord_5
         else:
         else:
             raise ValueError(f"Invalid round number {round_num}")
             raise ValueError(f"Invalid round number {round_num}")
-        
+
         if c is not None:
         if c is not None:
             return tuple(float(x) for x in c.split(","))
             return tuple(float(x) for x in c.split(","))
         # returns None if the selected coord was None
         # returns None if the selected coord was None
@@ -70,7 +71,8 @@ class CoordSet(db.Model):
 
 
 
 
 class GuessSet(db.Model):
 class GuessSet(db.Model):
-    game_id = db.Column(db.String, db.ForeignKey("game.game_id"), primary_key=True)
+    game_id = db.Column(db.String, db.ForeignKey(
+        "game.game_id"), primary_key=True)
     player_name = db.Column(db.String, primary_key=True)
     player_name = db.Column(db.String, primary_key=True)
     coord_id = db.Column(db.Integer, db.ForeignKey("coord_set.coord_id"))
     coord_id = db.Column(db.Integer, db.ForeignKey("coord_set.coord_id"))
     coord_set = db.relationship("CoordSet")
     coord_set = db.relationship("CoordSet")
@@ -96,11 +98,11 @@ class GuessSet(db.Model):
         # returns None if all rounds completed
         # returns None if all rounds completed
 
 
     def get_total_score(self):
     def get_total_score(self):
-        return ((self.score_1 or 0) + 
-            (self.score_2 or 0) + 
-            (self.score_3 or 0) + 
-            (self.score_4 or 0) + 
-            (self.score_5 or 0))
+        return ((self.score_1 or 0) +
+                (self.score_2 or 0) +
+                (self.score_3 or 0) +
+                (self.score_4 or 0) +
+                (self.score_5 or 0))
 
 
     def set_timed_out(self, round_num):
     def set_timed_out(self, round_num):
         self.set_guess(round_num, -200, -200, 0)
         self.set_guess(round_num, -200, -200, 0)

+ 1 - 5
server/game_api.py

@@ -31,10 +31,6 @@ def require_guess_set(game_id):
     return gs
     return gs
 
 
 
 
-def new_coord_string():
-    return f"{lat},{lng}"
-
-
 @game.route("", methods=["PUT"])
 @game.route("", methods=["PUT"])
 def create_game():
 def create_game():
     name = require_name()
     name = require_name()
@@ -142,7 +138,7 @@ def make_guess(game_id, round_num):
     lng = request.json.get("lng", None)
     lng = request.json.get("lng", None)
     if not isinstance(lat, float) or not isinstance(lng, float):
     if not isinstance(lat, float) or not isinstance(lng, float):
         abort(400)
         abort(400)
-    
+
     target = require_game(game_id).coord_set.get_coord(round_num)
     target = require_game(game_id).coord_set.get_coord(round_num)
     guess_score = score(target, (lat, lng))
     guess_score = score(target, (lat, lng))
     gs.set_guess(round_num, lat, lng, guess_score)
     gs.set_guess(round_num, lat, lng, guess_score)

+ 5 - 4
server/lib.py

@@ -13,7 +13,7 @@ def generate_coord(key):
     Takes in a Google API key.
     Takes in a Google API key.
     Returns (latitude, longitude) of usable coord (where google has data).
     Returns (latitude, longitude) of usable coord (where google has data).
     Returns None if, after trying several random points, no usable coords could be found.
     Returns None if, after trying several random points, no usable coords could be found.
-    
+
     This function calls the streetview metadata endpoint - there is no quota consumed
     This function calls the streetview metadata endpoint - there is no quota consumed
     """
     """
     points_res = requests.get(mapcrunch_url).text
     points_res = requests.get(mapcrunch_url).text
@@ -32,8 +32,9 @@ def generate_coord(key):
 mean_earth_radius_km = (6378 + 6357) / 2
 mean_earth_radius_km = (6378 + 6357) / 2
 # the farthest you can be from another point on Earth
 # the farthest you can be from another point on Earth
 antipode_dist_km = math.pi * mean_earth_radius_km
 antipode_dist_km = math.pi * mean_earth_radius_km
-min_dist_km = 0.15 # if you're within 150m, you get a perfect score
-max_dist_km = antipode_dist_km / 2 # if you're more than 1/4 of the Earth away, you get 0
+min_dist_km = 0.15  # if you're within 150m, you get a perfect score
+# if you're more than 1/4 of the Earth away, you get 0
+max_dist_km = antipode_dist_km / 2
 perfect_score = 5000
 perfect_score = 5000
 
 
 
 
@@ -46,7 +47,7 @@ def score(target, guess):
     dist_km = haversine.haversine(target, guess)
     dist_km = haversine.haversine(target, guess)
     if dist_km <= min_dist_km:
     if dist_km <= min_dist_km:
         return perfect_score
         return perfect_score
-    
+
     if dist_km >= max_dist_km:
     if dist_km >= max_dist_km:
         return 0
         return 0