from .base import Base, Reference from time import time from nullptr.util import * from dataclasses import field, dataclass from nullptr.models import Waypoint from typing import List, Tuple SUPPLY = ['SCARCE','LIMITED','MODERATE','HIGH','ABUNDANT'] ACTIVITY =['RESTRICTED','WEAK','GROWING','STRONG'] class MarketEntry: def __init__(self): self.buy = 0 self.sell = 0 self.volume = 0 self.supply = 0 self.activity = 0 self.history = [] def upg(self): if not hasattr(self, 'history'): self.history = [] def f(self, detail=1): self.upg() return f'b: {self.buy} s:{self.sell} hist: {len(self.history)}' def add(self, buy, sell, volume, supply, activity): self.upg() self.buy = buy self.sell = sell self.volume = volume self.supply = supply self.activity = activity #self.history.append((int(time()), buy, sell, volume, supply, activity)) class Marketplace(Base): def define(self): self.imports:list = [] self.exports:list = [] self.exchange:list = [] self.prices:dict = {} self.last_prices:int = 0 def get_waypoint(self): return self.store.get('Waypoint', self.symbol, create=True) def is_fuel(self): return self.imports + self.exports + self.exchange == ['FUEL'] def record_prices(self, data): for g in data: symbol= mg(g, 'symbol') if symbol in self.prices: e = self.prices[symbol] else: e = self.prices[symbol] = MarketEntry() buy = mg(g, 'purchasePrice') sell = mg(g, 'sellPrice') volume = mg(g, 'tradeVolume') supply = SUPPLY.index(mg(g, 'supply')) activity = ACTIVITY.index(sg(g, 'activity','STRONG')) e.add(buy, sell, volume, supply, activity) self.dirty() 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() self.record_prices(mg(d, 'tradeGoods')) def buy_price(self, resource): if resource not in self.prices: return None return self.prices[resource].buy def volume(self, resource): if resource not in self.prices: return None return self.prices[resource].volume 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 = super().f(detail) if detail > 2: r += '\n' if len(self.imports) > 0: r += 'I: ' + ', '.join(self.imports) + '\n' if len(self.exports) > 0: r += 'E: ' + ', '.join(self.exports) + '\n' if len(self.exchange) > 0: r += 'X: ' + ', '.join(self.exchange) + '\n' r += '\n' for res, p in self.prices.items(): t = self.rtype(res) r += f'{t} {res:25s} {p.buy:5d} {p.sell:5d}\n' return r