2023-06-11 18:51:03 +00:00
|
|
|
|
2023-07-10 17:25:01 +00:00
|
|
|
from .base import Base
|
2023-06-16 12:41:11 +00:00
|
|
|
from time import time
|
|
|
|
from nullptr.util import *
|
|
|
|
from dataclasses import field
|
2023-07-11 15:48:51 +00:00
|
|
|
from nullptr.models import Waypoint
|
2023-06-11 18:51:03 +00:00
|
|
|
|
2023-07-10 17:25:01 +00:00
|
|
|
class Marketplace(Base):
|
2023-06-18 17:15:51 +00:00
|
|
|
def define(self):
|
|
|
|
self.imports:list = []
|
|
|
|
self.exports:list = []
|
|
|
|
self.exchange:list = []
|
|
|
|
self.prices:dict = {}
|
|
|
|
self.last_prices:int = 0
|
2023-07-11 15:48:51 +00:00
|
|
|
self.set_waypoint()
|
2023-07-12 20:26:25 +00:00
|
|
|
self.system = self.get_system()
|
2023-07-11 15:48:51 +00:00
|
|
|
|
|
|
|
def set_waypoint(self):
|
|
|
|
waypoint = self.store.get(Waypoint, self.symbol, create=True)
|
|
|
|
self.waypoint = waypoint
|
2023-06-11 18:51:03 +00:00
|
|
|
|
|
|
|
def update(self, d):
|
|
|
|
self.setlst('imports', d, 'imports', 'symbol')
|
|
|
|
self.setlst('exports', d, 'exports', 'symbol')
|
|
|
|
self.setlst('exchange', d, 'exchange', 'symbol')
|
2023-06-16 12:41:11 +00:00
|
|
|
if 'tradeGoods' in d:
|
|
|
|
self.last_prices = time()
|
2023-06-16 21:05:47 +00:00
|
|
|
prices = {}
|
2023-06-16 12:41:11 +00:00
|
|
|
for g in mg(d, 'tradeGoods'):
|
2023-06-16 21:05:47 +00:00
|
|
|
price = {}
|
|
|
|
symbol= mg(g, 'symbol')
|
|
|
|
price['symbol'] = symbol
|
|
|
|
price['buy'] = mg(g, 'purchasePrice')
|
|
|
|
price['sell'] = mg(g, 'sellPrice')
|
2023-07-03 17:12:26 +00:00
|
|
|
price['volume'] = mg(g, 'tradeVolume')
|
2023-06-16 21:05:47 +00:00
|
|
|
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]
|
|
|
|
|
2023-06-11 18:51:03 +00:00
|
|
|
@classmethod
|
|
|
|
def ext(self):
|
|
|
|
return 'mkt'
|
|
|
|
|
2023-06-17 18:23:24 +00:00
|
|
|
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 '?'
|
2023-06-16 21:05:47 +00:00
|
|
|
|
|
|
|
def f(self, detail=1):
|
|
|
|
r = self.symbol
|
|
|
|
if detail > 1:
|
|
|
|
r += '\n'
|
|
|
|
for p in self.prices.values():
|
2023-06-17 18:23:24 +00:00
|
|
|
t = self.rtype(p['symbol'])
|
|
|
|
r += f'{t} {p["symbol"]:25s} {p["sell"]:5d} {p["buy"]:5d}\n'
|
2023-06-16 21:05:47 +00:00
|
|
|
return r
|