0ptr/nullptr/models/marketplace.py

63 lines
1.6 KiB
Python
Raw Normal View History

2023-07-10 17:25:01 +00:00
from .base import Base
from time import time
from nullptr.util import *
from dataclasses import field
from nullptr.models import Waypoint
2023-07-10 17:25:01 +00:00
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()
2023-07-12 20:26:25 +00:00
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
2023-06-18 18:42:30 +00:00
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