0ptr/nullptr/models/marketplace.py
2023-06-16 23:05:47 +02:00

45 lines
1.3 KiB
Python

from .system_member import SystemMember
from time import time
from nullptr.util import *
from dataclasses import field
class Marketplace(SystemMember):
imports:list = field(default_factory=list)
exports:list = field(default_factory=list)
exchange:list = field(default_factory=list)
prices:dict = field(default_factory=dict)
last_prices:int = 0
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')
prices[symbol] = price
self.prices = prices
@classmethod
def ext(self):
return 'mkt'
def path(self):
sector, system, _ = self.symbol.split('-')
return f'atlas/{sector}/{system[0:1]}/{system}/{self.symbol}.{self.ext()}'
def f(self, detail=1):
r = self.symbol
if detail > 1:
r += '\n'
for p in self.prices.values():
r += f'{p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n'
return r