Skip to content

Commit

Permalink
add "to_json" for various ship components, add SUPPLY and ACTIVITY fi…
Browse files Browse the repository at this point in the history
…elds to ShipyardShip model
  • Loading branch information
Ctri-The-Third committed Mar 7, 2024
1 parent 2bfd7a3 commit 4b6853e
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 79 deletions.
2 changes: 2 additions & 0 deletions PostgresSchema.SQL
Original file line number Diff line number Diff line change
Expand Up @@ -1569,6 +1569,8 @@ CREATE TABLE public.shipyard_types (
shipyard_symbol text NOT NULL,
ship_type text NOT NULL,
ship_cost integer,
supply text,
activity text,
last_updated timestamp without time zone
);

Expand Down
88 changes: 17 additions & 71 deletions straders_sdk/client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def __init__(

def agents_view_one(self, agent_symbol: str) -> "Agent" or SpaceTradersResponse:
url = _url(f"/agents/{agent_symbol}")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
return Agent.from_json(resp.data)
return resp
Expand All @@ -73,9 +71,7 @@ def set_current_agent(self, agent_symbol: str, token: str = None):

def view_my_self(self) -> "Agent" or SpaceTradersResponse:
url = _url("my/agent")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
self.current_agent = Agent.from_json(resp.data)
self.current_agent_symbol = self.current_agent.symbol
Expand All @@ -90,7 +86,6 @@ def view_my_contracts(self) -> list["Contract"] or SpaceTradersResponse:
20,
50,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand All @@ -104,9 +99,7 @@ def waypoints_view_one(
if waypoint_symbol == "":
raise ValueError("waypoint_symbol cannot be empty")
url = _url(f"systems/{system_symbol}/waypoints/{waypoint_symbol}")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if not resp:
print(resp.error)
return resp
Expand All @@ -133,7 +126,6 @@ def waypoints_view(
20,
50,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand Down Expand Up @@ -161,9 +153,7 @@ def register(self, callsign, faction="COSMIC", email=None) -> SpaceTradersRespon
data = {"symbol": callsign, "faction": faction}
if email is not None:
data["email"] = email
resp = post_and_validate(
url, data, session=self.session, priority=self.priority
)
resp = post_and_validate(url, data, priority=self.priority)
if resp:
self.token = resp.data.get("token")
return resp
Expand All @@ -173,9 +163,7 @@ def ship_orbit(self, ship: Ship):
url = _url(f"my/ships/{ship.name}/orbit")
if ship.nav.status == "IN_ORBIT":
return LocalSpaceTradersRespose(None, 0, None, url=url)
resp = post_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = post_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
self.update(resp.data)
return resp
Expand All @@ -188,7 +176,6 @@ def ship_patch_nav(self, ship: Ship, flight_mode: str):
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand All @@ -207,7 +194,6 @@ def ship_move(self, ship: Ship, dest_waypoint_symbol: str):
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority + MOVEMENT_OFFSET,
)
if resp:
Expand All @@ -217,9 +203,7 @@ def ship_move(self, ship: Ship, dest_waypoint_symbol: str):
def ship_create_chart(self, ship: "Ship"):
"""my/ships/:shipSymbol/chart"""
url = _url(f"my/ships/{ship.name}/chart")
resp = post_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = post_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
self.update(resp.data)
return resp
Expand All @@ -231,7 +215,6 @@ def ship_scan_waypoints(self, ship: Ship) -> SpaceTradersResponse:
resp = post_and_validate(
url,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)
if resp:
Expand All @@ -251,7 +234,6 @@ def ship_scan_ships(self, ship: Ship) -> SpaceTradersResponse:
resp = post_and_validate(
url,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)
if resp:
Expand All @@ -271,7 +253,6 @@ def ship_warp(self, ship: Ship, dest_waypoint_symbol: str):
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority + MOVEMENT_OFFSET,
)
if resp:
Expand All @@ -286,7 +267,6 @@ def ship_jump(self, ship: Ship, dest_waypoint_symbol: str):
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)
if resp:
Expand All @@ -296,9 +276,7 @@ def ship_jump(self, ship: Ship, dest_waypoint_symbol: str):
def ship_negotiate(self, ship: "Ship") -> "Contract" or SpaceTradersResponse:
"/my/ships/{shipSymbol}/negotiate/contract"
url = _url(f"my/ships/{ship.name}/negotiate/contract")
resp = post_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = post_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
resp = Contract.from_json(resp.data.get("contract"))
return resp
Expand Down Expand Up @@ -339,7 +317,6 @@ def ship_extract(self, ship: Ship, survey: Survey = None) -> SpaceTradersRespons
url,
json=data,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)
if resp:
Expand Down Expand Up @@ -375,9 +352,7 @@ def ship_dock(self, ship: Ship):

if ship.nav.status == "DOCKED":
return LocalSpaceTradersRespose(None, 200, None, url=url)
resp = post_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = post_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
self.update(resp.data)
ship.update(resp.data)
Expand All @@ -395,7 +370,6 @@ def ship_refuel(self, ship: Ship, from_cargo: bool = False):
resp = post_and_validate(
url,
headers=self._headers(),
session=self.session,
priority=self.priority,
json=body,
)
Expand All @@ -418,7 +392,6 @@ def ship_sell(self, ship: Ship, symbol: str, quantity: int) -> SpaceTradersRespo
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand Down Expand Up @@ -454,7 +427,6 @@ def ship_survey(self, ship: Ship) -> list[Survey] or SpaceTradersResponse:
resp = post_and_validate(
url,
headers=self._headers(),
session=self.session,
priority=self.priority + COOLDOWN_OFFSET,
)

Expand All @@ -478,7 +450,6 @@ def ship_transfer_cargo(
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
self.update(resp.data)
Expand All @@ -494,7 +465,6 @@ def ship_install_mount(self, ship: Ship, mount_symbol: str) -> SpaceTradersRespo
url,
json=data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand All @@ -513,7 +483,6 @@ def ship_remove_mount(self, ship: Ship, mount_symbol: str) -> SpaceTradersRespon
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand Down Expand Up @@ -575,7 +544,6 @@ def systems_view_twenty(
url,
page_number,
headers=self._headers(),
session=self.session,
priority=self.priority,
)

Expand All @@ -590,7 +558,6 @@ def systems_view_all(self) -> list[System] or SpaceTradersResponse:
per_page=20,
page_limit=999,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand All @@ -599,9 +566,7 @@ def systems_view_all(self) -> list[System] or SpaceTradersResponse:

def systems_view_one(self, system_symbol: str) -> System or SpaceTradersResponse:
url = _url(f"systems/{system_symbol}")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
return System.from_json(resp.data)
return resp
Expand All @@ -610,19 +575,15 @@ def system_construction(
self, wp: Waypoint
) -> ConstructionSite or SpaceTradersResponse:
url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/construction")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
return ConstructionSite.from_json(resp.data)
return resp

def system_market(self, wp: Waypoint) -> Market:
# /systems/{systemSymbol}/waypoints/{waypointSymbol}/market
url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/market")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
resp = Market.from_json(resp.data)
return resp
Expand All @@ -638,9 +599,7 @@ def system_shipyard(self, wp: Waypoint) -> Shipyard or SpaceTradersResponse:
"""

url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/shipyard")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)

if resp:
return Shipyard.from_json(resp.data)
Expand All @@ -650,9 +609,7 @@ def system_shipyard(self, wp: Waypoint) -> Shipyard or SpaceTradersResponse:
def system_jumpgate(self, wp: Waypoint) -> JumpGate or SpaceTradersResponse:
"""/systems/{systemSymbol}/waypoints/{waypointSymbol}/jump-gate"""
url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/jump-gate")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
gate = JumpGate.from_json(wp.symbol, resp.data)
gate.waypoint_symbol = wp.symbol
Expand All @@ -663,9 +620,7 @@ def ship_cooldown(self, ship: "Ship") -> SpaceTradersResponse:
"""/my/ships/{shipSymbol}/cooldown"""
# /my/ships/{shipSymbol}/cooldown
url = _url(f"my/ships/{ship.name}/cooldown")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp and "expiration" in resp.data:
ship.update({"cooldown": resp.data})
else:
Expand All @@ -681,7 +636,6 @@ def ships_view(self) -> dict[str:"Ship"] or SpaceTradersResponse:
20,
10,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if resp:
Expand All @@ -692,9 +646,7 @@ def ships_view(self) -> dict[str:"Ship"] or SpaceTradersResponse:
def ships_view_one(self, symbol: str) -> "Ship" or SpaceTradersResponse:
"/my/ships/{shipSymbol}"
url = _url(f"my/ships/{symbol}")
resp = get_and_validate(
url, headers=self._headers(), session=self.session, priority=self.priority
)
resp = get_and_validate(url, headers=self._headers(), priority=self.priority)
if resp:
return Ship.from_json(resp.data)
return resp
Expand All @@ -708,7 +660,6 @@ def ships_purchase(
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
if not resp:
Expand All @@ -728,7 +679,6 @@ def construction_supply(
url,
data,
headers=self._headers(),
session=self.session,
priority=self.priority,
)
return resp
Expand All @@ -739,9 +689,7 @@ def contracts_deliver(
url = _url(f"/my/contracts/{contract.id}/deliver")
data = {"shipSymbol": ship.name, "tradeSymbol": trade_symbol, "units": units}
headers = self._headers()
resp = post_and_validate(
url, data, headers=headers, session=self.session, priority=self.priority
)
resp = post_and_validate(url, data, headers=headers, priority=self.priority)
if not resp:
print(f"failed to deliver to contract {resp.status_code}, {resp.error}")
return resp
Expand All @@ -750,9 +698,7 @@ def contracts_deliver(
def contracts_fulfill(self, contract: Contract):
url = _url(f"/my/contracts/{contract.id}/fulfill")
headers = self._headers()
resp = post_and_validate(
url, headers=headers, session=self.session, priority=self.priority
)
resp = post_and_validate(url, headers=headers, priority=self.priority)
if not resp:
print(f"failed to fulfill contract {resp.status_code}, {resp.error}")
return resp
Expand Down
14 changes: 12 additions & 2 deletions straders_sdk/client_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,15 +668,25 @@ def system_shipyard(self, wp: Waypoint) -> Shipyard or SpaceTradersResponse:
Returns:
Either a list of ship types (symbols for purchase) or a SpaceTradersResponse object on failure.
"""
sql = """SELECT ship_type, ship_cost FROM shipyard_types where shipyard_symbol = %s"""
sql = """SELECT ship_type, ship_cost, supply, activity FROM shipyard_types where shipyard_symbol = %s"""
rows = try_execute_select(sql, (wp.symbol,), self.connection)
if not rows:
return rows
if len(rows) >= 1:
types = [row[0] for row in rows]
ships = {
row[0]: ShipyardShip(
None, None, None, row[0], None, row[0], row[1], [], []
None,
None,
None,
row[0],
None,
row[0],
row[1],
[],
[],
row[2],
row[3],
)
for row in rows
}
Expand Down
Loading

0 comments on commit 4b6853e

Please sign in to comment.