From b7aebadffe409d00c3dbeaf417b7487a59680c59 Mon Sep 17 00:00:00 2001 From: Leonardo Segala Date: Sat, 2 Aug 2025 09:05:30 +0200 Subject: [PATCH] Fix gps coords --- backend/src/app.py | 14 ++++++++------ backend/src/gps.py | 2 ++ backend/src/room.py | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/backend/src/app.py b/backend/src/app.py index d70afd4..1d304e9 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -85,7 +85,9 @@ def join(): if room.pin is not None and room.pin != code: return error("Invalid code") - if room.distance > room.range_size: + distance = distance_between_coords(room.coord, Coordinates(latitude=int(request.args["lat"]), longitude=int(request.args["lot"]))) + + if distance > room.range_size: return error("You are not within the room range") return {"success": True, "ws": f"/ws/{room_id}"} @@ -145,7 +147,7 @@ def room_new(): room = Room( id=max(state.rooms or [0]) + 1, # - coord=(float(lat), float(lon)), + coord=Coordinates(int(lat), int(lon)), range_size=int(room_range), name=room_name, pin=room_pin, @@ -165,12 +167,12 @@ def room_new(): def room(): lat = request.args.get("lat") lon = request.args.get("lon") + if lat and lon: - user_coords = Coordinates(latitude=float(lat), longitude=float(lon)) + user_coords = Coordinates(latitude=int(lat), longitude=int(lon)) else: return error("Missing user coordinates") - distance = distance_between_coords(user_coords, room.coord) return [ { "id": room.id, @@ -178,10 +180,10 @@ def room(): "private": room.pin is not None, "coords": room.coord, "range": room.range_size, - "distance": distance, + "distance": d, } for room in state.rooms.values() - if distance <= room.range_size + if (d := distance_between_coords(user_coords, room.coord)) <= room.range_size ] diff --git a/backend/src/gps.py b/backend/src/gps.py index 546531b..4232267 100644 --- a/backend/src/gps.py +++ b/backend/src/gps.py @@ -1,6 +1,8 @@ import math +from dataclasses import dataclass +@dataclass class Coordinates: latitude: int longitude: int diff --git a/backend/src/room.py b/backend/src/room.py index cad4020..cb775b6 100644 --- a/backend/src/room.py +++ b/backend/src/room.py @@ -1,6 +1,7 @@ import random from dataclasses import dataclass +from gps import Coordinates from song import Song USER_SCORE_WEIGHT = 0.7 @@ -30,12 +31,11 @@ class Rank: @dataclass class Room: id: int - coord: tuple[float, float] + coord: Coordinates name: str pin: int | None tags: set[str] range_size: int # in meters ?? - distance: float songs: dict[str, UserScoredSong] # all songs + user score (the playlist) history: list[Song] # all songs previously played