From 2bfd7a3572f91cae7234ce00be523fe35591853d Mon Sep 17 00:00:00 2001 From: C'tri Goudie Date: Wed, 6 Mar 2024 22:01:27 +0000 Subject: [PATCH] Finally added orbital support to the DB, API, and JSON Cache --- PostgresSchema.SQL | 2 ++ straders_sdk/client_postgres.py | 18 ++++++++++++++---- straders_sdk/models_misc.py | 16 ++++++++++------ straders_sdk/pg_pieces/upsert_waypoint.py | 8 +++++--- tests/test_api.py | 1 + tests/test_cache.py | 3 +++ tests/test_db.py | 2 ++ 7 files changed, 37 insertions(+), 13 deletions(-) diff --git a/PostgresSchema.SQL b/PostgresSchema.SQL index 62314ec..03fee22 100644 --- a/PostgresSchema.SQL +++ b/PostgresSchema.SQL @@ -438,6 +438,8 @@ CREATE TABLE public.waypoints ( system_symbol text NOT NULL, x smallint NOT NULL, y smallint NOT NULL, + parent_symbol text COLLATE pg_catalog."default", + orbital_symbols text[] COLLATE pg_catalog."default", checked boolean DEFAULT false NOT NULL, modifiers text[], under_construction boolean DEFAULT false NOT NULL diff --git a/straders_sdk/client_postgres.py b/straders_sdk/client_postgres.py index 4fad77e..e8a292c 100644 --- a/straders_sdk/client_postgres.py +++ b/straders_sdk/client_postgres.py @@ -234,7 +234,9 @@ def waypoints_view_one( Returns: Either a Waypoint object or a SpaceTradersResponse object on failure.""" - sql = """SELECT * FROM waypoints WHERE waypoint_symbol = %s LIMIT 1;""" + sql = """SELECT waypoint_symbol, type, system_symbol, x, y --0-4 + , checked, modifiers, under_construction, parent_symbol, orbital_symbols --5-9 +FROM waypoints WHERE waypoint_symbol = %s LIMIT 1;""" rows = try_execute_select(sql, (waypoint_symbol,), self.connection) waypoints = [] @@ -246,6 +248,13 @@ def waypoints_view_one( new_sql, (waypoint_symbol,), self.connection ) traits = [] + chart_sql = """select waypoint_symbol, submitted_by, submitted_on from waypoint_charts +where waypoint_symbol = %s""" + chart_rows = try_execute_select( + chart_sql, (waypoint_symbol,), self.connection + ) + orbitals = [{"symbol": o} for o in row[9]] + chart = {"submittedBy": chart_rows[0][1], "submittedOn": chart_rows[0][2]} for trait_row in trait_rows: traits.append(WaypointTrait(trait_row[1], trait_row[2], trait_row[3])) waypoint = Waypoint( @@ -254,10 +263,11 @@ def waypoints_view_one( row[1], row[3], row[4], - [], + row[8], + orbitals, traits, - {}, - {}, + chart, + None, row[6], row[7], ) diff --git a/straders_sdk/models_misc.py b/straders_sdk/models_misc.py index 20f611f..d2b5a6e 100644 --- a/straders_sdk/models_misc.py +++ b/straders_sdk/models_misc.py @@ -350,7 +350,7 @@ class Waypoint: type: str x: int y: int - oribtals: list + orbitals: list traits: list[WaypointTrait] chart: dict faction: dict @@ -364,6 +364,7 @@ def __init__( type, x, y, + orbits=None, orbitals=None, traits=None, chart=None, @@ -376,13 +377,18 @@ def __init__( self.type = type self.x = x self.y = y - self.oribtals = orbitals or [] + self.orbits = orbits or "" + self.orbitals = orbitals or [] self.traits = traits or [] self.chart = chart or {} self.faction = faction or {} self.modifiers = modifiers or [] self.under_construction = under_construction or False + @property + def orbital_symbols(self): + return [o["symbol"] for o in self.orbitals] + @classmethod def from_json(cls, json_data: dict): # a waypoint can be a fully scanned thing, or a stub from scanning the system. @@ -403,6 +409,7 @@ def from_json(cls, json_data: dict): json_data["type"], json_data["x"], json_data["y"], + json_data.get("orbits", ""), json_data["orbitals"], json_data["traits"], json_data["chart"], @@ -424,15 +431,12 @@ def to_json(self) -> dict: "type": self.type, "x": self.x, "y": self.y, - "orbitals": [], + "orbitals": [{"symbol": o["symbol"]} for o in self.orbitals], "traits": [t.to_json() for t in self.traits], "chart": {}, "faction": {}, } - def __dict__(self): - return self.to_json() - def __repr__(self) -> str: return f"Waypoint({self.symbol}, X={self.x}, Y={self.y})" diff --git a/straders_sdk/pg_pieces/upsert_waypoint.py b/straders_sdk/pg_pieces/upsert_waypoint.py index f496927..3fce060 100644 --- a/straders_sdk/pg_pieces/upsert_waypoint.py +++ b/straders_sdk/pg_pieces/upsert_waypoint.py @@ -11,13 +11,13 @@ def _upsert_waypoint(waypoint: Waypoint, connection): ) # a system waypoint will not return any traits. Even if it's uncharted, we've checked it. # a waypoint with exactly zero traits that has been checked is a jump gate, and will have a chart # if it'snot been charted, then it's UNCHARTED and the first condition gets it. - sql = """INSERT INTO waypoints (waypoint_symbol, type, system_symbol, x, y, modifiers, under_construction, checked) - VALUES (%s, %s, %s, %s, %s, %s, %s, %s) + sql = """INSERT INTO waypoints (waypoint_symbol, type, system_symbol, x, y, parent_symbol, orbital_symbols, modifiers, under_construction, checked) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT (waypoint_symbol) DO UPDATE SET checked = EXCLUDED.checked , modifiers = EXCLUDED.modifiers , under_construction = EXCLUDED.under_construction""" - try_execute_upsert( + resp = try_execute_upsert( sql, ( waypoint.symbol, @@ -25,6 +25,8 @@ def _upsert_waypoint(waypoint: Waypoint, connection): waypoint.system_symbol, waypoint.x, waypoint.y, + waypoint.orbits, + [w["symbol"] for w in waypoint.orbitals], waypoint.modifiers, waypoint.under_construction, checked, diff --git a/tests/test_api.py b/tests/test_api.py index 8f1301b..2d056ce 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -75,6 +75,7 @@ def test_waypoint(): wp = client.waypoints_view_one( "OE-PM", ) + assert len(wp.orbital_symbols) > 0 assert wp diff --git a/tests/test_cache.py b/tests/test_cache.py index 51e2ebf..219562d 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -25,11 +25,14 @@ def test_save_waypoint(waypoint_response_data): def test_waypoints_view_one(waypoint_response_data): client = SpaceTradersJSONClient(FOLDER) check_wp = Waypoint.from_json(waypoint_response_data) + client.update(check_wp) wp = client.waypoints_view_one("DOESNOTEXIST") assert not wp wp = client.waypoints_view_one(check_wp.symbol) + assert wp.symbol == check_wp.symbol + assert len(wp.orbital_symbols) > 0 def test_save_system(system_response_data): diff --git a/tests/test_db.py b/tests/test_db.py index a3f48f0..3b51f99 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -154,6 +154,8 @@ def test_waypoint(waypoint_response_data): assert wp assert "STRIPPED" in wp.modifiers assert wp.under_construction + for orbital in wp.orbitals: + assert orbital["symbol"] in wp.orbital_symbols def test_waypoints(waypoint_response_data):