diff --git a/autotraders/faction/__init__.py b/autotraders/faction/__init__.py index 47c0b48..6051e38 100644 --- a/autotraders/faction/__init__.py +++ b/autotraders/faction/__init__.py @@ -1,5 +1,3 @@ -from typing import Optional - from autotraders.paginated_list import PaginatedList from autotraders.space_traders_entity import SpaceTradersEntity from autotraders.session import AutoTradersSession @@ -8,12 +6,14 @@ class Faction(SpaceTradersEntity): + is_recruiting: bool + traits: list[Trait] + headquarters: MapSymbol + description: str + name: str + symbol: str + def __init__(self, symbol, session: AutoTradersSession, data=None): - self.is_recruiting: Optional[bool] = None - self.traits: Optional[list[Trait]] = None - self.headquarters: Optional[MapSymbol] = None - self.description: Optional[str] = None - self.name: Optional[str] = None self.symbol: str = symbol super().__init__(session, "factions/" + self.symbol, data) diff --git a/autotraders/faction/contract.py b/autotraders/faction/contract.py index 0b95413..19943f5 100644 --- a/autotraders/faction/contract.py +++ b/autotraders/faction/contract.py @@ -5,7 +5,7 @@ from autotraders.space_traders_entity import SpaceTradersEntity from autotraders.session import AutoTradersSession from autotraders.shared_models.map_symbol import MapSymbol -from autotraders.util import parse_time +from autotraders.time import parse_time class Deliver: @@ -17,15 +17,17 @@ def __init__(self, data): class Contract(SpaceTradersEntity): + accepted: bool + fulfilled: bool + deadline: datetime + accept_deadline: datetime + on_fulfilled: str + on_accepted: str + contract_id: str + def __init__(self, contract_id: str, session: AutoTradersSession, data=None): self.contract_data = None - self.accepted: Optional[bool] = None - self.fulfilled: Optional[bool] = None - self.deadline: Optional[datetime] = None - self.accept_deadline: Optional[datetime] = None self.contract_type = None - self.on_fulfilled: Optional[str] = None - self.on_accepted: Optional[str] = None self.contract_id: str = contract_id super().__init__(session, "my/contracts/" + self.contract_id, data) diff --git a/autotraders/map/system.py b/autotraders/map/system.py index 3512f06..ba78aac 100644 --- a/autotraders/map/system.py +++ b/autotraders/map/system.py @@ -41,14 +41,14 @@ def paginated_func(p, num_per_page): + "&page=" + str(p) ) - j = r.json()["data"] + j = r.json() if "error" in j: raise IOError(j["error"]["message"]) systems = [] - for system in j: + for system in j["data"]: s = System(system["symbol"], session, system) systems.append(s) - return systems, r.json()["meta"]["total"] + return systems, j["meta"]["total"] return PaginatedList(paginated_func, page) diff --git a/autotraders/shared_models/transaction.py b/autotraders/shared_models/transaction.py index 80270d3..f62e087 100644 --- a/autotraders/shared_models/transaction.py +++ b/autotraders/shared_models/transaction.py @@ -1,6 +1,6 @@ from autotraders.shared_models.item import Item from autotraders.shared_models.map_symbol import MapSymbol -from autotraders.util import parse_time +from autotraders.time import parse_time class ShipyardTransaction: diff --git a/autotraders/ship/__init__.py b/autotraders/ship/__init__.py index 10be4cc..32b26c9 100644 --- a/autotraders/ship/__init__.py +++ b/autotraders/ship/__init__.py @@ -13,7 +13,7 @@ from autotraders.shared_models.map_symbol import MapSymbol from autotraders.ship.ship_components import Frame, Reactor, Engine, Module, Mount from autotraders.ship.survey import Survey -from autotraders.util import parse_time +from autotraders.time import parse_time from autotraders.map.waypoint import Waypoint @@ -42,19 +42,32 @@ def __init__(self, data): self.role = data["role"] +class Capabilities: + def __init__(self, modules, mounts): + warp_drives = [module for module in modules if "warp" in module.symbol.lower()] + jump_drives = [module for module in modules if "jump" in module.symbol.lower()] + mine = [mount for mount in mounts if "mine" in mount.symbol.lower()] + self.warp = len(warp_drives) > 0 + self.jump = len(jump_drives) > 0 + self.mine = len(mine) > 0 + + class Ship(SpaceTradersEntity): + cargo: Cargo + fuel: Fuel + nav: Nav + symbol: str + frame: Frame + reactor: Reactor + engine: Engine + modules: list[Module] + mounts: list[Mount] + crew: Crew + registration: Registration + capabilities: Optional[Capabilities] + def __init__(self, symbol, session: AutoTradersSession, data=None): - self.cargo: Optional[Cargo] = None - self.fuel: Optional[Fuel] = None - self.nav: Optional[Nav] = None - self.symbol: str = symbol - self.frame: Optional[Frame] = None - self.reactor: Optional[Reactor] = None - self.engine: Optional[Engine] = None - self.modules: Optional[list[Module]] = None - self.mounts: Optional[list[Mount]] = None - self.crew: Optional[Crew] = None - self.registration: Optional[Registration] = None + self.symbol = symbol super().__init__(session, "my/ships/" + self.symbol, data) def update(self, data: dict = None) -> None: @@ -81,6 +94,10 @@ def update(self, data: dict = None) -> None: self.cargo = Cargo(self.symbol, self.session, data["cargo"]) if "registration" in data: self.registration = Registration(data["registration"]) + try: + self.capabilities = Capabilities(self.modules, self.mounts) + except: + pass async def navigate_async(self, waypoint: Union[str, MapSymbol], interval=1): """Attempts to move ship to the provided waypoint. diff --git a/autotraders/ship/nav.py b/autotraders/ship/nav.py index 130c6ff..10079b4 100644 --- a/autotraders/ship/nav.py +++ b/autotraders/ship/nav.py @@ -4,7 +4,7 @@ from autotraders import AutoTradersSession from autotraders.shared_models.map_symbol import MapSymbol from autotraders.space_traders_entity import SpaceTradersEntity -from autotraders.util import parse_time +from autotraders.time import parse_time class Route: diff --git a/autotraders/ship/states.py b/autotraders/ship/states.py new file mode 100644 index 0000000..768fbe5 --- /dev/null +++ b/autotraders/ship/states.py @@ -0,0 +1,14 @@ +from enum import Enum + + +class FlightMode(str, Enum): + CRUISE = "CRUISE" + DRIFT = "DRIFT" + BURN = "BURN" + STEALTH = "STEALTH" + + +class NavState(str, Enum): + DOCK = "DOCK" + ORBIT = "ORBIT" + IN_TRANSIT = "IN_TRANSIT" diff --git a/autotraders/ship/survey.py b/autotraders/ship/survey.py index 8d76726..45bc4a6 100644 --- a/autotraders/ship/survey.py +++ b/autotraders/ship/survey.py @@ -1,4 +1,4 @@ -from autotraders.util import parse_time +from autotraders.time import parse_time class Survey: diff --git a/autotraders/status.py b/autotraders/status.py index f97b2da..dbca399 100644 --- a/autotraders/status.py +++ b/autotraders/status.py @@ -2,7 +2,7 @@ import requests -from autotraders.util import parse_time +from autotraders.time import parse_time class LeaderboardPlayer: diff --git a/autotraders/time.py b/autotraders/time.py new file mode 100644 index 0000000..57cafb3 --- /dev/null +++ b/autotraders/time.py @@ -0,0 +1,17 @@ +import sys +from datetime import datetime, timezone + + +def parse_time(time: str) -> datetime: + if sys.version_info.minor >= 11: + return datetime.fromisoformat(time) + else: + try: + d = datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ") + except ValueError: + try: + d = datetime.strptime(time, "%Y-%m-%dT%H:%M:%SZ") + except ValueError: + d = datetime.fromisoformat(time) + d = d.replace(tzinfo=timezone.utc) + return d diff --git a/autotraders/util.py b/autotraders/util.py index ce7d4cf..2918a48 100644 --- a/autotraders/util.py +++ b/autotraders/util.py @@ -1,39 +1,23 @@ -import sys -from datetime import datetime, timezone - - -def parse_time(time: str) -> datetime: - if sys.version_info.minor >= 11: - return datetime.fromisoformat(time) - else: - try: - d = datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ") - except ValueError: - try: - d = datetime.strptime(time, "%Y-%m-%dT%H:%M:%SZ") - except ValueError: - d = datetime.fromisoformat(time) - d = d.replace(tzinfo=timezone.utc) - return d +from autotraders.ship.states import FlightMode def travel_fuel(distance, mode): - if mode == "CRUISE": + if mode == FlightMode.CRUISE: return distance - elif mode == "DRIFT": + elif mode == FlightMode.DRIFT: return 1 - elif mode == "BURN": + elif mode == FlightMode.BURN: return 2 * distance - elif mode == "STEALTH": + elif mode == FlightMode.STEALTH: return distance def travel_time(distance, ship_speed, mode): - if mode == "CRUISE": + if mode == FlightMode.CRUISE: return 15 + 10 * distance / ship_speed - elif mode == "DRIFT": + elif mode == FlightMode.DRIFT: return 15 + 100 * distance / ship_speed - elif mode == "BURN": + elif mode == FlightMode.BURN: return 15 + 5 * distance / ship_speed - elif mode == "STEALTH": + elif mode == FlightMode.STEALTH: return 15 + 20 * distance / ship_speed diff --git a/pyproject.toml b/pyproject.toml index 994a36b..9d6ed78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "autotraders" -version = "1.4.3" +version = "1.5.0" authors = [ { name="Ashwin Naren", email="arihant2math@gmail.com" }, ] diff --git a/source/conf.py b/source/conf.py index 1c18e10..4db19a1 100644 --- a/source/conf.py +++ b/source/conf.py @@ -12,7 +12,7 @@ project = "Autotraders" copyright = "2023, cosmictraders" author = "cosmictraders" -release = "1.4.2" +release = "1.5.0" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration diff --git a/tests/test_util.py b/tests/test_util.py index c43d943..e0873dd 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -1,5 +1,5 @@ from autotraders.shared_models.map_symbol import MapSymbol -from autotraders.util import parse_time +from autotraders.time import parse_time def test_time_parser():