0ptr/nullptr/models/marketplace.py

118 lines
3.1 KiB
Python
Raw Normal View History

2024-01-13 20:42:49 +00:00
from .base import Base, Reference
from time import time
from nullptr.util import *
2024-01-13 10:27:32 +00:00
from dataclasses import field, dataclass
from nullptr.models import Waypoint
2024-01-13 10:27:32 +00:00
from typing import List, Tuple
2024-01-13 10:27:32 +00:00
SUPPLY = ['SCARCE','LIMITED','MODERATE','HIGH','ABUNDANT']
ACTIVITY =['RESTRICTED','WEAK','GROWING','STRONG']
2024-01-27 19:47:12 +00:00
2024-01-13 10:27:32 +00:00
class MarketEntry:
2024-01-27 19:47:12 +00:00
def __init__(self):
self.buy = 0
self.sell = 0
self.volume = 0
self.supply = 0
self.activity = 0
self.history = []
2024-01-13 10:27:32 +00:00
2024-01-27 19:47:12 +00:00
def upg(self):
if not hasattr(self, 'history'):
self.history = []
def f(self, detail=1):
2024-01-27 19:47:12 +00:00
self.upg()
return f'b: {self.buy} s:{self.sell} hist: {len(self.history)}'
2024-01-13 10:27:32 +00:00
def add(self, buy, sell, volume, supply, activity):
2024-01-27 19:47:12 +00:00
self.upg()
2024-01-13 10:27:32 +00:00
self.buy = buy
self.sell = sell
self.volume = volume
self.supply = supply
self.activity = activity
2024-02-01 17:51:27 +00:00
#self.history.append((int(time()), buy, sell, volume, supply, activity))
2024-01-13 10:27:32 +00:00
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
2024-01-13 20:42:49 +00:00
def get_waypoint(self):
return self.store.get('Waypoint', self.symbol, create=True)
2024-01-06 06:17:53 +00:00
def is_fuel(self):
return self.imports + self.exports + self.exchange == ['FUEL']
2024-01-13 10:27:32 +00:00
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')
2024-01-13 20:42:49 +00:00
volume = mg(g, 'tradeVolume')
2024-01-13 10:27:32 +00:00
supply = SUPPLY.index(mg(g, 'supply'))
activity = ACTIVITY.index(sg(g, 'activity','STRONG'))
e.add(buy, sell, volume, supply, activity)
self.dirty()
2024-01-06 06:17:53 +00:00
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()
2024-01-13 10:27:32 +00:00
self.record_prices(mg(d, 'tradeGoods'))
2024-01-04 20:34:31 +00:00
def buy_price(self, resource):
if resource not in self.prices:
return None
2024-01-13 20:42:49 +00:00
return self.prices[resource].buy
2024-01-06 06:17:53 +00:00
def volume(self, resource):
if resource not in self.prices:
return None
2024-01-13 20:42:49 +00:00
return self.prices[resource].volume
2024-01-04 20:34:31 +00:00
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):
2024-01-02 05:35:26 +00:00
r = super().f(detail)
if detail > 2:
2023-12-30 16:23:37 +00:00
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'
2024-01-15 18:39:08 +00:00
for res, p in self.prices.items():
t = self.rtype(res)
2024-01-20 19:33:50 +00:00
r += f'{t} {res:25s} {p.buy:5d} {p.sell:5d}\n'
return r