Skip to content

Commit

Permalink
change jumpgates to 2.1 models
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Oct 28, 2023
1 parent f93ed39 commit 997f19e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion straders_sdk/client_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def system_jumpgate(self, wp: Waypoint) -> JumpGate or SpaceTradersResponse:
url = _url(f"systems/{wp.system_symbol}/waypoints/{wp.symbol}/jump-gate")
resp = get_and_validate(url, headers=self._headers(), session=self.session)
if resp:
gate = JumpGate.from_json(resp.data)
gate = JumpGate.from_json(wp.symbol, resp.data)
gate.waypoint_symbol = wp.symbol
return gate
return resp
Expand Down
14 changes: 4 additions & 10 deletions straders_sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,16 @@ class JumpGate:
def __init__(
self,
waypoint_symbol: str,
jump_range: int,
connected_waypoints: list["JumpGateConnection"],
faction_symbol: str = "",
connected_waypoints: list["str"],
) -> None:
self.waypoint_symbol = waypoint_symbol
self.jump_range = jump_range
self.faction_symbol = faction_symbol
self.connected_waypoints = connected_waypoints

@classmethod
def from_json(cls, json_data: dict):
def from_json(cls, waypoint_symbol, json_data: dict):
return cls(
"",
json_data["jumpRange"],
[JumpGateConnection.from_json(wp) for wp in json_data["connectedSystems"]],
json_data.get("factionSymbol", ""),
waypoint_symbol,
[wp for wp in json_data["connections"]],
)


Expand Down
52 changes: 16 additions & 36 deletions straders_sdk/pg_pieces/jump_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,56 @@

def _upsert_jump_gate(connect, jump_gate: JumpGate):
sql = """INSERT INTO public.jump_gates(
waypoint_symbol, faction_symbol, jump_range)
VALUES (%s, %s, %s) on conflict do nothing;"""
waypoint_symbol)
VALUES (%s) on conflict do nothing;"""
resp = try_execute_upsert(
connect,
sql,
(jump_gate.waypoint_symbol, jump_gate.faction_symbol, jump_gate.jump_range),
(jump_gate.waypoint_symbol,),
)
if not resp:
return resp

connection_sql = """INSERT INTO public.jumpgate_connections(
s_waypoint_symbol, s_system_symbol, d_system_symbol, distance)
VALUES (%s, %s, %s, %s) on conflict do nothing;"""
s_waypoint_symbol, s_system_symbol, d_system_symbol)
VALUES (%s, %s, %s) on conflict do nothing;"""

for connection in jump_gate.connected_waypoints:
for dest_system in jump_gate.connected_waypoints:
dest_system: str
resp = try_execute_upsert(
connect,
connection_sql,
(
jump_gate.waypoint_symbol,
waypoint_slicer(jump_gate.waypoint_symbol),
connection.symbol,
connection.distance,
dest_system,
),
)
if not resp:
return resp

resp = try_execute_upsert(
connect,
connection_sql,
(
None,
connection.symbol,
waypoint_slicer(jump_gate.waypoint_symbol),
connection.distance,
),
)
if not resp:
return resp

return LocalSpaceTradersRespose(None, 0, 0, url=f"{__name__}._upsert_jump_gate")


def select_jump_gate_one(connection, waypoint: Waypoint):
sql = """SELECT waypoint_symbol, jump_range, faction_symbol FROM public.jump_gates WHERE waypoint_symbol = %s"""
sql = (
"""SELECT waypoint_symbol FROM public.jump_gates WHERE waypoint_symbol = %s"""
)
resp = try_execute_select(connection, sql, (waypoint.symbol,))
if not resp:
return resp

connection_sql = """
select jc.d_system_symbol , s.sector_symbol, s.type, s.x, s.y, jc.distance
from jumpgate_connections jc
left join systems s on jc.d_system_symbol = s.system_symbol
where s_waypoint_symbol = %s"""
select jc.d_system_symbol
from jumpgate_connections jc
where s_waypoint_symbol = %s"""
conn_resp = try_execute_select(connection, connection_sql, (waypoint.symbol,))
if not conn_resp:
return conn_resp
for one_row in resp:
resp_obj = JumpGate(one_row[0], one_row[1], [], one_row[2])
for link_row in conn_resp:
lr = JumpGateConnection(
link_row[0],
link_row[1],
link_row[2],
link_row[3],
link_row[4],
link_row[5],
"",
)
resp_obj.connected_waypoints.append(lr)
resp_obj = JumpGate(one_row[0], [l[0] for l in conn_resp])

return resp_obj
8 changes: 8 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ def test_ship_unmount():

resp = client.ship_remove_mount(ship, "MINING_LASER_I")
assert resp


def test_ship_jumpgate():
client = SpaceTradersApiClient("token", BASE_URL, VERSION)

wp = client.waypoints_view_one("OE-PM", "OE-PM-TR")
resp = client.system_jumpgate(wp)
assert resp
18 changes: 18 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from straders_sdk.client_postgres import SpaceTradersPostgresClient
from straders_sdk.utils import try_execute_select
from straders_sdk.models import Waypoint
from straders_sdk.models import JumpGate, JumpGateConnection

ST_HOST = os.getenv("ST_DB_HOST")
ST_NAME = os.getenv("ST_DB_NAME")
Expand Down Expand Up @@ -38,3 +40,19 @@ def test_find_waypoints_by_type():
assert resp
for wayp in resp:
assert wayp.type == "ASTEROID_FIELD"


def test_get_jumpgates():
client = SpaceTradersPostgresClient(
ST_HOST, ST_NAME, ST_USER, ST_PASS, TEST_AGENT_NAME, db_port=ST_PORT
)

test_jumpgate = JumpGate(
"SECT-SYS-TESTJUMP", ["test_destination1", "test_destination2"]
)
client.update(test_jumpgate)

wayp = Waypoint("SECT-SYS", "SECT-SYS-TESTJUMP", "JUMP_GATE", 5, 5, [], [], {}, {})
jump = client.system_jumpgate(wayp)
assert jump.waypoint_symbol == "SECT-SYS-TESTJUMP"
assert jump.connected_waypoints == ["test_destination1", "test_destination2"]

0 comments on commit 997f19e

Please sign in to comment.