from .base import Base from time import time from nullptr.util import * from dataclasses import field from nullptr.models import Waypoint class Marketplace(Base): def define(self): self.imports:list = [] self.exports:list = [] self.exchange:list = [] self.prices:dict = {} self.last_prices:int = 0 self.set_waypoint() self.system = self.get_system() def set_waypoint(self): waypoint = self.store.get(Waypoint, self.symbol, create=True) self.waypoint = waypoint def update(self, d): self.setlst('imports', d, 'imports', 'symbol') self.setlst('exports', d, 'exports', 'symbol') self.setlst('exchange', d, 'exchange', 'symbol') if 'tradeGoods' in d: self.last_prices = time() prices = {} for g in mg(d, 'tradeGoods'): price = {} symbol= mg(g, 'symbol') price['symbol'] = symbol price['buy'] = mg(g, 'purchasePrice') price['sell'] = mg(g, 'sellPrice') price['volume'] = mg(g, 'tradeVolume') prices[symbol] = price self.prices = prices def sellable_items(self, resources): return [r for r in resources if r in self.prices] @classmethod def ext(self): return 'mkt' def rtype(self, r): if r in self.imports: return 'I' if r in self.exports: return 'E' if r in self.exchange: return 'X' return '?' def f(self, detail=1): r = self.symbol if detail > 1: r += '\n' for p in self.prices.values(): t = self.rtype(p['symbol']) r += f'{t} {p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n' return r