Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arihant2math committed May 25, 2023
1 parent 93d555a commit cc3dd09
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 59 deletions.
4 changes: 2 additions & 2 deletions example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ actions: # actions are per ship
mine: # Definition of the action "mine"
steps:
- orbit
- extract
- extract:
fail-fast: false
- dock
- navigate:
destination:
Expand All @@ -27,7 +28,6 @@ triggers:
- navigate:
destination:
type: "ASTEROID_FIELD"
- orbit
- action: # Execute the action named "mine"
name: mine
repeat:
Expand Down
7 changes: 3 additions & 4 deletions spacelang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from threading import Thread

import yaml
from autotraders import session as s
from autotraders.agent import Agent
from autotraders.ship import Ship

Expand Down Expand Up @@ -58,7 +57,7 @@ def __init__(self, data):
self.triggers = [Trigger(d, data["triggers"][d]) for d in data["triggers"]]

def run(self, session):
logging.info("Initializing state ...") # TODO: Tech Debt on threads (use logging instead)
logging.info("Initializing state ...")
agent = Agent(session)
logging.info("Agent: " + agent.symbol)
ships = {}
Expand All @@ -72,9 +71,9 @@ def run(self, session):
thread = Thread(target=on_start.run, name="on_start", args=(ships, self.events, session))
thread.start()
while True:
logging.info("Checking triggers ...")
logging.debug("Checking triggers ...")
# agent.update()
time.sleep(5) # TODO: Actually check triggers
time.sleep(10) # TODO: Actually check triggers


def load_text(stream):
Expand Down
63 changes: 10 additions & 53 deletions spacelang/step/__init__.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,19 @@
import logging
import math
import time
from datetime import datetime, timezone

from autotraders.map.waypoint import Waypoint
from autotraders.ship import Ship

from spacelang.step.navigate import Navigate
from spacelang.step.ship_command import ShipCommand


class Step:
def __init__(self, command, data):
self.command = command
self.args = data

def navigate(self, ship, session):
if type(self.args["destination"]) is str:
ship.navigate(self.command)
else:
choices, _ = Waypoint.all(ship.nav.location.system, session)
ship_current_waypoint = [choice for choice in choices if choice.symbol == ship.nav.location][0]
tmp = []
for choice in choices:
if "trait" in self.args["destination"]:
if len([trait for trait in choice.traits if
trait.symbol == self.args["destination"]["trait"]]) != 0:
tmp.append(choice)
if "type" in self.args["destination"]:
if choice.waypoint_type == self.args["destination"]["type"]:
tmp.append(choice)
choices = tmp
if len(choices) == 0:
raise Exception("0 possible waypoints")
if "selection" not in self.args["destination"] or self.args["destination"][
"selection"] == "NEAREST":
accepted = choices[0]
best = 1000000
for choice in choices:
if choice.symbol == ship.nav.location:
accepted = choice
best = -1
distance = math.sqrt(
((choice.x - ship_current_waypoint.x) ** 2) + ((choice.y - ship_current_waypoint.y) ** 2))
if distance < best:
best = distance
accepted = choice
else:
accepted = choices[0]
if str(accepted.symbol) != str(ship.nav.location):
ship.navigate(str(accepted.symbol))
time.sleep(1)
ship.update()
if ship.nav.status == "IN_TRANSIT":
time.sleep((ship.nav.route.arrival - datetime.now(timezone.utc)).seconds)
if self.command == "navigate":
self.inner = Navigate(self.args)
elif self.command in ["dock", "orbit", "refuel", "extract"]:
self.inner = ShipCommand(self.command, self.args)

def action(self, ship, events, session):
repeat_interval = -1
Expand All @@ -75,17 +38,11 @@ def action(self, ship, events, session):
def execute(self, ship: Ship, events, session):
logging.debug(self.command)
if self.command == "navigate":
self.navigate(ship, session)
self.inner.execute(ship, session)
elif self.command in ["dock", "orbit", "refuel", "extract"]:
self.inner.execute(ship)
elif self.command == "action":
self.action(ship, events, session)
elif self.command == "orbit":
ship.orbit()
elif self.command == "dock":
ship.dock()
elif self.command == "refuel":
ship.refuel()
elif self.command == "extract":
ship.extract()
elif self.command == "refine":
ship.refine(self.args)
elif self.command == "sell":
Expand Down
59 changes: 59 additions & 0 deletions spacelang/step/navigate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import math
import time
from datetime import datetime, timezone

from autotraders.map.waypoint import Waypoint

class WaypointQuery:
def __init__(self, args):
self.data = args

def query(self, ship, session):
if type(self.data["destination"]) is str:
return self.data["destination"]
else:
choices, _ = Waypoint.all(ship.nav.location.system, session)
ship_current_waypoint = [choice for choice in choices if choice.symbol == ship.nav.location][0]
tmp = []
for choice in choices:
if "trait" in self.data["destination"]:
if len([trait for trait in choice.traits if
trait.symbol == self.data["destination"]["trait"]]) != 0:
tmp.append(choice)
if "type" in self.data["destination"]:
if choice.waypoint_type == self.data["destination"]["type"]:
tmp.append(choice)
choices = tmp
if len(choices) == 0:
raise Exception("0 possible waypoints")
if "selection" not in self.data["destination"] or self.data["destination"][
"selection"] == "NEAREST":
accepted = choices[0]
best = 1000000
for choice in choices:
if choice.symbol == ship.nav.location:
accepted = choice
best = -1
distance = math.sqrt(
((choice.x - ship_current_waypoint.x) ** 2) + ((choice.y - ship_current_waypoint.y) ** 2))
if distance < best:
best = distance
accepted = choice
else:
accepted = choices[0]
return accepted


class Navigate:
def __init__(self, args):
self.data = args
self.query = WaypointQuery(args)

def execute(self, ship, session):
accepted = self.query.query(ship, session)
if str(accepted.symbol) != str(ship.nav.location):
ship.navigate(str(accepted.symbol))
time.sleep(1)
ship.update()
if ship.nav.status == "IN_TRANSIT":
time.sleep((ship.nav.route.arrival - datetime.now(timezone.utc)).seconds)
33 changes: 33 additions & 0 deletions spacelang/step/ship_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import logging

from autotraders.ship import Ship


class ShipCommand:
def __init__(self, command, args):
if type(args) is not dict:
args = {}
self.command = command
self.fail_fast = args.get("fail-fast", True)

def execute(self, ship: Ship):
if self.command == "dock":
ship.dock()
elif self.command == "orbit":
ship.orbit()
elif self.command == "refuel":
try:
ship.refuel()
except IOError as e:
if self.fail_fast:
raise e
else:
logging.warning(self.command + " failed to execute")
elif self.command == "extract":
try:
ship.refuel()
except IOError as e:
if self.fail_fast:
raise e
else:
logging.warning(self.command + " failed to execute")

0 comments on commit cc3dd09

Please sign in to comment.