0ptr/nullptr/models/marketplace.py
2023-06-18 20:42:30 +02:00

59 lines
1.5 KiB
Python

from .system_member import SystemMember
from time import time
from nullptr.util import *
from dataclasses import field
class Marketplace(SystemMember):
def define(self):
self.imports:list = []
self.exports:list = []
self.exchange:list = []
self.prices:dict = {}
self.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
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 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():
t = self.rtype(p['symbol'])
r += f'{t} {p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n'
return r