Browse Source

Just America

Kirk Trombley 5 years ago
parent
commit
2f435cb190

+ 2 - 1
README.md

@@ -33,7 +33,8 @@ POST /score
 PUT /game
     Accepts {
         "timer": number,
-        "rounds": number
+        "rounds": number,
+        "onlyAmerica": boolean (default: false)
     }
     Returns 200 and {
         "gameId": string

+ 11 - 1
client/src/components/util/GameCreationForm.jsx

@@ -31,6 +31,7 @@ export default ({ afterCreate }) => {
   const [loading, setLoading] = useState(false);
   const [timer, setTimer] = useState(300);
   const [rounds, setRounds] = useState(5);
+  const [onlyAmerica, setOnlyAmerica] = useState(false);
 
   if (loading) {
     return <Loading/>
@@ -38,7 +39,7 @@ export default ({ afterCreate }) => {
 
   const onCreateGame = async () => {
     setLoading(true);
-    const gameId = await createGame(timer, rounds);
+    const gameId = await createGame(timer, rounds, onlyAmerica);
     if (afterCreate) {
       afterCreate(gameId);
     }
@@ -68,6 +69,15 @@ export default ({ afterCreate }) => {
         >
           Rounds: {rounds}
         </Dropdown>
+        <Dropdown
+          options={{
+            "All Countries": false,
+            "Just America": true,
+          }}
+          onChange={setOnlyAmerica}
+        >
+          {onlyAmerica ? "Just America" : "All Countries" }
+        </Dropdown>
       </DropdownContainer>
       <StartButton onClick={onCreateGame}>
         Create New Game

+ 2 - 2
client/src/domain/apiMethods.js

@@ -12,13 +12,13 @@ export const getStatus = async () => {
     }
 }
 
-export const createGame = async (timer, rounds) => {
+export const createGame = async (timer, rounds, onlyAmerica) => {
     const res = await fetch(`${API_BASE}/game`, {
         method: "PUT",
         headers: {
             "Content-Type": "application/json",
         },
-        body: JSON.stringify({ timer, rounds }),
+        body: JSON.stringify({ timer, rounds, onlyAmerica }),
     });
     if (!res.ok) {
         throw Error(res.statusText);

+ 2 - 2
server/db.py

@@ -17,7 +17,7 @@ class Game(db.Model):
     players = db.relationship("Player", lazy=True, backref="game")
 
     @staticmethod
-    def create(timer, rounds):
+    def create(timer, rounds, only_america=False):
         game_id = str(uuid.uuid4())
         while Game.query.get(game_id) is not None:
             # basically impossible collision, but let's be safe
@@ -31,7 +31,7 @@ class Game(db.Model):
         db.session.add(new_game)
 
         for round_num in range(rounds):
-            (lat, lng) = generate_coord()
+            (lat, lng) = generate_coord(only_america=only_america)
             coord = Coordinate(
                 game_id=game_id,
                 round_number=round_num+1,

+ 5 - 1
server/game_api.py

@@ -37,7 +37,11 @@ def create_game():
     if not isinstance(rounds, int) or rounds <= 0:
         abort(400)
 
-    new_game = db.Game.create(timer, rounds)
+    only_america = request.json.get("onlyAmerica", False)
+    if not isinstance(only_america, bool):
+        abort(400)
+
+    new_game = db.Game.create(timer, rounds, only_america=only_america)
 
     return jsonify({"gameId": new_game.game_id})
 

+ 4 - 2
server/lib.py

@@ -10,7 +10,7 @@ metadata_url = "https://maps.googleapis.com/maps/api/streetview/metadata"
 mapcrunch_url = "http://www.mapcrunch.com/_r/"
 
 
-def generate_coord(max_retries=100000):
+def generate_coord(max_retries=100000, only_america=False):
     """
     Returns (latitude, longitude) of usable coord (where google has data).
     This function will attempt at most max_retries calls to map crunch to fetch
@@ -18,8 +18,10 @@ def generate_coord(max_retries=100000):
 
     This function calls the streetview metadata endpoint - there is no quota consumed
     """
+    mc_url = mapcrunch_url + ("?c=21" if only_america else "")
+    print(mc_url)
     for _ in range(max_retries):
-        points_res = requests.get(mapcrunch_url).text
+        points_res = requests.get(mc_url).text
         points_js = json.loads(points_res.strip("while(1); "))
         for lat, lng in points_js["points"]:
             params = {