Skip to content

Commit

Permalink
Finally added orbital support to the DB, API, and JSON Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Mar 6, 2024
1 parent ba2d1de commit 2bfd7a3
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions PostgresSchema.SQL
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions straders_sdk/client_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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(
Expand All @@ -254,10 +263,11 @@ def waypoints_view_one(
row[1],
row[3],
row[4],
[],
row[8],
orbitals,
traits,
{},
{},
chart,
None,
row[6],
row[7],
)
Expand Down
16 changes: 10 additions & 6 deletions straders_sdk/models_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class Waypoint:
type: str
x: int
y: int
oribtals: list
orbitals: list
traits: list[WaypointTrait]
chart: dict
faction: dict
Expand All @@ -364,6 +364,7 @@ def __init__(
type,
x,
y,
orbits=None,
orbitals=None,
traits=None,
chart=None,
Expand All @@ -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.
Expand All @@ -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"],
Expand All @@ -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})"

Expand Down
8 changes: 5 additions & 3 deletions straders_sdk/pg_pieces/upsert_waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ 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,
waypoint.type,
waypoint.system_symbol,
waypoint.x,
waypoint.y,
waypoint.orbits,
[w["symbol"] for w in waypoint.orbitals],
waypoint.modifiers,
waypoint.under_construction,
checked,
Expand Down
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_waypoint():
wp = client.waypoints_view_one(
"OE-PM",
)
assert len(wp.orbital_symbols) > 0

assert wp

Expand Down
3 changes: 3 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2bfd7a3

Please sign in to comment.