From b0ef68a7212d51f6b42173859e47a8326fe23997 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 21 Jan 2024 20:21:38 +0100 Subject: [PATCH] rewrote hauling and highscores --- nullptr/api.py | 7 +++++ nullptr/central_command.py | 2 ++ nullptr/commander.py | 12 +++++++++ nullptr/missions/haul.py | 37 +++++--------------------- nullptr/missions/siphon.py | 54 +++++++++++++++++++++++++++++++------- nullptr/models/ship.py | 3 +++ 6 files changed, 76 insertions(+), 39 deletions(-) diff --git a/nullptr/api.py b/nullptr/api.py index eab7237..41f124d 100644 --- a/nullptr/api.py +++ b/nullptr/api.py @@ -73,6 +73,13 @@ class Api: self.agent.update(mg(result, 'agent')) self.agent.token = token + def status(self): + try: + self.request('get', '') + except ApiError: + pass + return self.last_result + def info(self): data = self.request('get', 'my/agent') self.agent.update(data) diff --git a/nullptr/central_command.py b/nullptr/central_command.py index 5503377..285d338 100644 --- a/nullptr/central_command.py +++ b/nullptr/central_command.py @@ -132,9 +132,11 @@ class CentralCommand: def assign_hauler(self, s): w = self.find_gas(s) m = self.analyzer.best_sell_market(s.location.system, 'HYDROCARBON') + resources = list(m.prices.keys()) self.init_mission(s, 'haul') self.smipa(s, 'site', w) self.smipa(s, 'dest', m) + self.smipa(s, 'resources', resources) def assign_siphon(self, s): w = self.find_gas(s) diff --git a/nullptr/commander.py b/nullptr/commander.py index 54f4307..ea38cf5 100644 --- a/nullptr/commander.py +++ b/nullptr/commander.py @@ -517,6 +517,18 @@ class Commander(CommandLine): pprint(self.ship) ######## Analysis ######### + def do_server(self): + data = self.api.status() + pprint(data) + + def do_highscore(self): + data = self.api.status() + leaders = mg(data, 'leaderboards.mostCredits') + for l in leaders: + a = mg(l,'agentSymbol') + c = mg(l, 'credits') + print(f'{a:15s} {c}') + def do_stats(self): total = 0 for t in self.store.data: diff --git a/nullptr/missions/haul.py b/nullptr/missions/haul.py index 995823e..19413fc 100644 --- a/nullptr/missions/haul.py +++ b/nullptr/missions/haul.py @@ -20,48 +20,25 @@ class HaulMission(BaseMission): return False return True - def wait_cargo(self): - dmkt = self.store.get('Marketplace', self.st('dest')) - sellables = dmkt.prices.keys() - for s in self.store.all("Ship"): - if s.location != self.ship.location: continue - if s.mission not in ['mine','siphon']: continue - for r, a in s.cargo.items(): - if r not in sellables: continue - return True - return False - def step_load(self): - cargo_space = self.ship.cargo_capacity - self.ship.cargo_units - dmkt = self.store.get('Marketplace', self.st('dest')) - sellables = dmkt.prices.keys() - for s in self.store.all("Ship"): - if s.location != self.ship.location: continue - if s.mission not in ['mine','siphon']: continue - for r, a in s.cargo.items(): - if r not in sellables: continue - amount = min(cargo_space, a) - - res = self.api.transfer(s, self.ship, r, amount) - - return 'done' if amount == cargo_space else 'more' - return 'more' - + pass + + def cargo_full(self): + return self.ship.cargo_space() == 0 + @classmethod def params(cls): return { 'site': MissionParam(Waypoint, True), 'dest': MissionParam(Waypoint, True), + 'resources': MissionParam(list, True) } def steps(self): return { **self.travel_steps('to', 'site', 'wait-turn'), 'wait-turn': (self.step_turn, 'load', self.wait_turn), - 'load': (self.step_load, { - 'more': 'load', - 'done': 'travel-back' - }, self.wait_cargo), + 'load': (self.step_load, 'travel-back', self.cargo_full), **self.travel_steps('back', 'dest', 'dock-dest'), 'dock-dest': (self.step_dock, 'unload'), 'unload': (self.step_sell, { diff --git a/nullptr/missions/siphon.py b/nullptr/missions/siphon.py index 81a45b2..b587af2 100644 --- a/nullptr/missions/siphon.py +++ b/nullptr/missions/siphon.py @@ -10,16 +10,52 @@ class SiphonMission(BaseMission): return { 'site': MissionParam(Waypoint, True), } - - def steps(self): - return { - **self.travel_steps('to', 'site', 'siphon'), - 'siphon': (self.step_siphon, 'done', self.cargo_full) - } - - def cargo_full(self): - return self.ship.cargo_capacity - self.ship.cargo_units > 5 def step_siphon(self): result = self.api.siphon(self.ship) self.next_step = self.ship.cooldown + if self.ship.cargo_space() > 5: + return 'more' + else: + return 'full' + + def find_hauler(self, r): + for s in self.store.all('Ship'): + if s.mission != 'haul': continue + if s.location != self.ship.location: + continue + if s.mission_status != 'load': + continue + if r not in s.mission_state['resources']: continue + return s + return None + + def step_unload(self): + if len(self.ship.cargo) == 0: + return 'done' + r = list(self.ship.cargo.keys())[0] + amt = self.ship.cargo[r] + h = self.find_hauler(r) + if h is None: + self.api.jettison(self.ship, r) + else: + space = h.cargo_space() + amt = min(space, amt) + self.api.transfer(self.ship, h, r, amt) + return 'more' + + def steps(self): + return { + **self.travel_steps('to', 'site', 'siphon'), + 'siphon': (self.step_siphon, { + 'more': 'siphon', + 'full': 'unload' + }), + 'unload': (self.step_unload, { + 'more': 'unload', + 'done': 'done' + }) + } + + + diff --git a/nullptr/models/ship.py b/nullptr/models/ship.py index 33fbf2d..1fdf2c6 100644 --- a/nullptr/models/ship.py +++ b/nullptr/models/ship.py @@ -123,6 +123,9 @@ class Ship(Base): garbage = [c for c in cargo if c not in deliveries] return garbage + def cargo_space(self): + return self.cargo_capacity - self.cargo_units + def update_timers(self): if self.status == 'IN_TRANSIT' and self.arrival < time(): self.status = 'IN_ORBIT'