2023-06-13 07:27:13 +00:00
|
|
|
from nullptr.models.marketplace import Marketplace
|
2023-06-13 14:33:22 +00:00
|
|
|
from nullptr.models.jumpgate import Jumpgate
|
|
|
|
from nullptr.models.system import System
|
2023-06-21 07:32:31 +00:00
|
|
|
from nullptr.models.waypoint import Waypoint
|
2023-06-14 13:37:48 +00:00
|
|
|
from dataclasses import dataclass
|
2023-06-13 07:27:13 +00:00
|
|
|
|
2023-06-14 13:37:48 +00:00
|
|
|
@dataclass
|
|
|
|
class SearchNode:
|
|
|
|
system: System
|
|
|
|
parent: 'SearchNode'
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.system.symbol)
|
|
|
|
|
|
|
|
def path(self):
|
|
|
|
result = []
|
|
|
|
n = self
|
|
|
|
while n is not None:
|
|
|
|
result.append(n.system)
|
|
|
|
n = n.parent
|
|
|
|
result.reverse()
|
|
|
|
return result
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.system.symbol
|
|
|
|
|
2023-06-13 07:27:13 +00:00
|
|
|
class Analyzer:
|
|
|
|
def __init__(self, store):
|
|
|
|
self.store = store
|
|
|
|
|
2023-06-14 14:55:45 +00:00
|
|
|
def find_markets(self, resource, sellbuy):
|
2023-06-13 07:27:13 +00:00
|
|
|
for m in self.store.all(Marketplace):
|
2023-06-23 11:32:08 +00:00
|
|
|
if 'sell' in sellbuy and resource in m.imports:
|
|
|
|
yield ('sell', m)
|
|
|
|
|
|
|
|
elif 'buy' in sellbuy and resource in m.exports:
|
|
|
|
yield ('buy', m)
|
|
|
|
|
|
|
|
elif 'exchange' in sellbuy and resource in m.exchange:
|
|
|
|
yield ('exchange', m)
|
|
|
|
|
|
|
|
def find_closest_markets(self, resource, sellbuy, location):
|
2023-06-20 20:18:20 +00:00
|
|
|
if type(location) == str:
|
2023-06-21 07:32:31 +00:00
|
|
|
location = self.store.get(Waypoint, location)
|
|
|
|
mkts = self.find_markets(resource, sellbuy)
|
2023-06-23 11:32:08 +00:00
|
|
|
candidates = []
|
2023-06-23 11:49:09 +00:00
|
|
|
origin = self.store.get(System, location.system())
|
2023-06-23 11:32:08 +00:00
|
|
|
for typ, m in mkts:
|
2023-06-20 19:46:05 +00:00
|
|
|
system = self.store.get(System, m.system())
|
2023-06-23 11:49:09 +00:00
|
|
|
d = origin.distance(system)
|
|
|
|
candidates.append((typ, m, d))
|
|
|
|
possibles = sorted(candidates, key=lambda m: m[2])
|
|
|
|
possibles = possibles[:10]
|
|
|
|
results = []
|
|
|
|
for typ,m,d in possibles:
|
|
|
|
system = self.store.get(System, m.system())
|
2023-06-21 07:32:31 +00:00
|
|
|
p = self.find_path(origin, system)
|
2023-06-20 19:46:05 +00:00
|
|
|
if p is None: continue
|
2023-06-23 11:49:09 +00:00
|
|
|
results.append((typ,m,d,len(p)))
|
|
|
|
return results
|
|
|
|
|
2023-06-25 15:35:06 +00:00
|
|
|
def solve_tsp(self, waypoints):
|
|
|
|
# todo actually try to solve it
|
|
|
|
return waypoints
|
2023-06-13 20:29:04 +00:00
|
|
|
|
|
|
|
def get_jumpgate(self, system):
|
|
|
|
gates = self.store.all_members(system, Jumpgate)
|
|
|
|
return next(gates, None)
|
2023-06-14 13:37:48 +00:00
|
|
|
|
2023-06-14 19:26:46 +00:00
|
|
|
def find_path(self, orig, to, depth=100, seen=None):
|
2023-06-14 13:37:48 +00:00
|
|
|
if depth < 1: return None
|
2023-06-14 19:26:46 +00:00
|
|
|
if seen is None:
|
|
|
|
seen = set()
|
2023-06-14 13:37:48 +00:00
|
|
|
if type(orig) == System:
|
|
|
|
orig = set([SearchNode(orig,None)])
|
|
|
|
result = [n for n in orig if n.system==to]
|
|
|
|
if len(result) > 0:
|
|
|
|
return result[0].path()
|
|
|
|
dest = set()
|
|
|
|
for o in orig:
|
|
|
|
jg = self.get_jumpgate(o.system)
|
|
|
|
if jg is None: continue
|
|
|
|
for s in jg.systems:
|
2023-06-14 19:26:46 +00:00
|
|
|
if s in seen: continue
|
2023-06-14 13:54:32 +00:00
|
|
|
seen.add(s)
|
2023-06-14 13:37:48 +00:00
|
|
|
system = self.store.get(System, s)
|
|
|
|
if system is None: continue
|
|
|
|
dest.add(SearchNode(system, o))
|
2023-06-14 19:20:47 +00:00
|
|
|
if len(dest) == 0:
|
|
|
|
return None
|
2023-06-14 19:26:46 +00:00
|
|
|
return self.find_path(dest, to, depth-1, seen)
|