Skip to content

Commit

Permalink
added support for navigation events & engines to the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Mar 13, 2024
1 parent 14749c4 commit c1f5784
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 13 deletions.
64 changes: 59 additions & 5 deletions PostgresSchema.SQL
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,8 @@ CREATE TABLE IF NOT EXISTS public.ship_frame_links
CONSTRAINT ship_frame_links_pkey PRIMARY KEY (ship_symbol, frame_symbol)
)

TABLESPACE pg_default;;


ALTER TABLE IF EXISTS public.ship_frame_links
OWNER to spacetraders;
Expand Down Expand Up @@ -2811,10 +2813,6 @@ ALTER TABLE ONLY public.ship_cooldowns
-- Name: ship_frame_links ship_frame_links_pkey; Type: CONSTRAINT; Schema: public; Owner: spacetraders
--

ALTER TABLE ONLY public.ship_frame_links
ADD CONSTRAINT ship_frame_links_pkey PRIMARY KEY (ship_symbol, frame_symbol);


--
-- TOC entry 3632 (class 2606 OID 16878)
-- Name: ship_frames ship_frames_pkey; Type: CONSTRAINT; Schema: public; Owner: spacetraders
Expand Down Expand Up @@ -3016,7 +3014,43 @@ CREATE OR REPLACE VIEW public.survey_average_values AS
ORDER BY (sum((mp.import_price * (sd.count)::numeric)) / (sum(sd.count))::numeric) DESC, s.expiration;


--

CREATE TABLE IF NOT EXISTS public.ship_engine_links
(
ship_symbol text COLLATE pg_catalog."default" NOT NULL,
engine_symbol text COLLATE pg_catalog."default",
condition numeric,
integrity numeric,
CONSTRAINT ship_symbol_links_pkey PRIMARY KEY (ship_symbol)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.ship_engine_links
OWNER to spacetraders;--


-- Table: public.ship_engines

-- DROP TABLE IF EXISTS public.ship_engines;

CREATE TABLE IF NOT EXISTS public.ship_engines
(
engine_symbol text COLLATE pg_catalog."default" NOT NULL,
name text COLLATE pg_catalog."default",
description text COLLATE pg_catalog."default",
speed integer,
required_power integer,
required_crew integer,
required_slots integer,
CONSTRAINT ship_engines_pkey PRIMARY KEY (engine_symbol)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.ship_engines
OWNER to spacetraders;

-- TOC entry 3854 (class 0 OID 0)
-- Dependencies: 5
-- Name: SCHEMA public; Type: ACL; Schema: -; Owner: spacetraders
Expand All @@ -3033,3 +3067,23 @@ GRANT ALL ON SCHEMA public TO PUBLIC;
--


-- Table: public.ship_damage_events

-- DROP TABLE IF EXISTS public.ship_damage_events;
-- Table: public.ship_damage_events

CREATE TABLE IF NOT EXISTS public.ship_damage_events
(
event_timestamp timestamp without time zone NOT NULL,
ship_symbol text COLLATE pg_catalog."default" NOT NULL,
event_symbol text COLLATE pg_catalog."default" NOT NULL,
component text COLLATE pg_catalog."default" NOT NULL,
name text COLLATE pg_catalog."default",
description text COLLATE pg_catalog."default",
CONSTRAINT ship_damage_events_pkey PRIMARY KEY (event_timestamp, ship_symbol)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.ship_damage_events
OWNER to spacetraders;
1 change: 1 addition & 0 deletions straders_sdk/client_mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ def ship_move(self, ship: "Ship", dest_waypoint_symbol: str):
if resp:
ship.update(resp.data)
self.db_client.update(ship)
self.db_client.update(resp.data)
self.ships[ship.name] = ship
return resp

Expand Down
4 changes: 3 additions & 1 deletion straders_sdk/models_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ class ShipEngine(SymbolClass):
symbol: str
name: str
description: str
condition: int
condition: float
integrity: float
speed: int
requirements: ShipRequirements

Expand All @@ -234,6 +235,7 @@ def from_json(cls, json_data: dict):
json_data.get("name", ""),
json_data.get("description", ""),
json_data.get("condition", 0),
json_data.get("integrity", 0),
json_data.get("speed", ""),
ShipRequirements.from_json(json_data.get("requirements", {})),
)
Expand Down
12 changes: 11 additions & 1 deletion straders_sdk/models_ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def __init__(self) -> None:
self.nav_dirty = False
self.fuel_dirty = False
self.mounts_dirty = False
self.frame_dirty = False
self.engine_dirty = False
self.cooldown_dirty = False
self.logger = logging.getLogger("ship-logger")

Expand All @@ -96,7 +98,7 @@ def __init__(self) -> None:
self.nav = ShipNav("", "", "", None, None, None, "", "")
self.frame = ShipFrame("", "", "", 0, 0, 0, 0, 0, ShipRequirements(0, 0, 0))
self.reactor = ShipReactor("", "", "", 0, 0, ShipRequirements(0, 0, 0))
self.engine = ShipEngine("", "", "", 0, 0, ShipRequirements(0, 0, 0))
self.engine = ShipEngine("", "", "", 0, 0, 0, ShipRequirements(0, 0, 0))
self.crew_capacity: int = 0
self.crew_current: int = 0
self.crew_required: int = 0
Expand Down Expand Up @@ -266,6 +268,8 @@ def mark_clean(self):
self.fuel_dirty = False
self.mounts_dirty = False
self.cooldown_dirty = False
self.engine_dirty = False
self.frame_dirty = False

def receive_cargo(self, trade_symbol, units):
for inventory_item in self.cargo_inventory:
Expand Down Expand Up @@ -318,6 +322,12 @@ def update(self, json_data: dict):
self.mounts: list[ShipMount] = [
ShipMount(d) for d in json_data["mounts"]
]
if "engine" in json_data:
self.engine_dirty = True
self.engine = ShipEngine.from_json(json_data["engine"])
if "frame" in json_data:
self.frame_dirty = True
self.frame = ShipFrame.from_json(json_data["frame"])
# pass the updated ship to the client to be logged appropriately

@property
Expand Down
48 changes: 44 additions & 4 deletions straders_sdk/pg_pieces/select_ship.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..resp_local_resp import LocalSpaceTradersRespose
from ..models_ship import Ship, ShipFrame, ShipNav, ShipInventory
from ..models_ship import Ship, ShipFrame, ShipNav, ShipInventory, ShipEngine
from ..models_misc import RouteNode
from ..client_interface import SpaceTradersClient
from ..models_misc import ShipRequirements
Expand All @@ -16,16 +16,21 @@ def _select_ships(agent_name, db_client: SpaceTradersClient):
, n.o_waypoint_symbol, o.type, o.system_symbol, o.x, o.y --32
, n.d_waypoint_symbol, d.type, n.system_symbol, d.x, d.y --38
, s.mount_symbols, s.module_symbols --40
, sel.engine_symbol, sel.condition, sel.integrity --43
, se.name, se.description, se.speed --46
, se.required_power, se.required_crew, se.required_slots --49
from ships s join ship_nav n on s.ship_symbol = n.ship_symbol
left join ship_frame_links sfl on s.ship_symbol = sfl.ship_symbol
left join ship_frames sf on sf.frame_symbol = sfl.frame_symbol
left join ship_engine_links sel on s.ship_symbol = sel.ship_symbol
left join ship_engines se on sel.engine_symbol = se.engine_symbol
left join ship_cooldown sc on s.ship_symbol = sc.ship_symbol
left join waypoints o on n.o_waypoint_symbol = o.waypoint_symbol
left join waypoints d on n.d_waypoint_symbol = d.waypoint_symbol
where s.agent_name = %s
order by s.ship_symbol
"""
Expand All @@ -45,15 +50,22 @@ def _select_ship_one(ship_symbol: str, db_client: SpaceTradersClient):
, sc.expiration, sc.total_seconds --27
, n.o_waypoint_symbol, o.type, o.system_symbol, o.x, o.y --32
, n.d_waypoint_symbol, d.type, n.system_symbol, d.x, d.y --38
, s.mount_symbols, s.module_symbols --40
, s.mount_symbols, s.module_symbols --40
, sel.engine_symbol, sel.condition, sel.integrity --43
, se.name, se.description, se.speed --46
, se.required_power, se.required_crew, se.required_slots --49
from ships s join ship_nav n on s.ship_symbol = n.ship_symbol
left join ship_frame_links sfl on s.ship_symbol = sfl.ship_symbol
left join ship_frames sf on sf.frame_symbol = sfl.frame_symbol
left join ship_engine_links sel on s.ship_symbol = sel.ship_symbol
left join ship_engines se on sel.engine_symbol = se.engine_symbol
left join ship_cooldown sc on s.ship_symbol = sc.ship_symbol
left join waypoints o on n.o_waypoint_symbol = o.waypoint_symbol
left join waypoints d on n.d_waypoint_symbol = d.waypoint_symbol
where s.ship_symbol = %s
"""
ships = _select_some_ships(db_client, sql, (ship_symbol,))
Expand Down Expand Up @@ -113,7 +125,7 @@ def _select_some_ships(db_client: SpaceTradersClient, sql, params):
ship.cargo_units_used = row[5]
ship.cargo_inventory = []
# , 6: n.waypoint_symbol, n.departure_time, n.arrival_time, n.origin_waypoint, n.destination_waypoint, n.flight_status, n.flight_mode

ship.engine = _engine_from_row(row)
ship.nav = _nav_from_row(row)
ship.frame = _frame_from_row(row)
ship.fuel_capacity = row[24]
Expand All @@ -135,6 +147,34 @@ def _select_some_ships(db_client: SpaceTradersClient, sql, params):
)


def _engine_from_row(row) -> ShipEngine:
"""expected:
s.ship_symbol, s.agent_name, s.faction_symbol, s.ship_role, s.cargo_capacity, s.cargo_in_use
, n.waypoint_symbol, n.departure_time, n.arrival_time, n.o_waypoint_symbol, n.d_waypoint_symbol, n.flight_status, n.flight_mode
, sfl.condition, sfl.integrity --13
, sf.frame_symbol, sf.name, sf.description, sf.module_slots, sf.mount_points, sf.fuel_capacity, sf.required_power, sf.required_crew, sf.required_slots
, s.fuel_capacity, s.fuel_current --25
, sc.expiration, sc.total_seconds --27
, n.o_waypoint_symbol, o.type, o.system_symbol, o.x, o.y --32
, n.d_waypoint_symbol, d.type, n.system_symbol, d.x, d.y --38
, s.mount_symbols, s.module_symbols --39
, sel.engine_symbol, sel.condition, sel.integrity --42
, se.name, se.description, se.speed --45
, se.required_power, se.required_crew, se.required_slots --48
"""
requirements = ShipRequirements(row[47], row[46], row[48])
return_obj = ShipEngine(
symbol=row[40],
name=row[43],
description=row[44],
condition=float(row[41]),
integrity=float(row[42]),
speed=row[45],
requirements=requirements,
)
return return_obj


def _nav_from_row(row) -> ShipNav:
"""
expected:
Expand Down
39 changes: 38 additions & 1 deletion straders_sdk/pg_pieces/upsert_ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ def _upsert_ship(ship: Ship, connection, owner: Agent = None):
if not resp:
logging.warning("Failed to upsert ship nav because %s", resp.error)
return resp
if ship.dirty:
if ship.dirty or ship.frame_dirty:
resp = _upsert_ship_frame(ship, connection)
if not resp:
logging.warning("Failed to upsert ship frame because %s", resp.error)
return resp
if ship.engine_dirty or ship.dirty:
resp = _upsert_ship_engine(ship, connection)
if not resp:
logging.warning("Failed to upsert ship engine because %s", resp.error)
return resp
if ship.cooldown_dirty:
resp = _upsert_ship_cooldown(ship, connection)
if not resp:
Expand Down Expand Up @@ -161,6 +166,38 @@ def _upsert_ship_frame(ship: Ship, connection):
return resp


def _upsert_ship_engine(ship: Ship, connection):

sql = """
INSERT INTO ship_engines (engine_symbol, name, description, speed, required_power, required_crew, required_slots)
VALUES (%s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (engine_symbol) DO NOTHING;
"""
values = (
ship.engine.symbol,
ship.engine.name,
ship.engine.description,
ship.engine.speed,
ship.engine.requirements.power,
ship.engine.requirements.crew,
ship.engine.requirements.module_slots,
)
resp = try_execute_upsert(sql, values, connection)

sql = """INSERT INTO ship_engine_links (ship_symbol, engine_symbol, condition, integrity)
VALUES (%s, %s, %s, %s)
ON CONFLICT (ship_symbol) DO UPDATE
SET condition = EXCLUDED.condition, integrity = EXCLUDED.integrity;"""
values = (
ship.name,
ship.engine.symbol,
ship.engine.condition,
ship.engine.integrity,
)
resp = try_execute_upsert(sql, values, connection)
return resp


def _upsert_ship_cooldown(ship: Ship, connection):
sql = """insert into ship_cooldowns (ship_symbol, total_seconds, expiration)
values (%s, %s, %s) ON CONFLICT (ship_symbol, expiration) DO NOTHING;"""
Expand Down
3 changes: 3 additions & 0 deletions straders_sdk/pg_pieces/upsert_waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def _upsert_waypoint(waypoint: Waypoint, connection):
( waypoint_symbol, submitted_by, submitted_on)
VALUES (%s, %s, %s)
ON CONFLICT do nothing"""
submitted_on = waypoint.chart["submittedOn"]
if not submitted_on:
submitted_on = None
try_execute_upsert(
sql,
(
Expand Down
3 changes: 2 additions & 1 deletion straders_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def try_execute_upsert(sql="", params=(), connection=None) -> LocalSpaceTradersR
except Exception as err:
logging.error("Couldn't execute upsert: %s", err)
logging.debug("SQL: %s", sql)
connection.rollback()
if not connection.closed > 0:
connection.rollback()
return LocalSpaceTradersRespose(
error=err, status_code=0, error_code=0, url=f"{__name__}.try_execute_upsert"
)
Expand Down

0 comments on commit c1f5784

Please sign in to comment.