historical marketprices

api.py
This commit is contained in:
Richard
2024-01-13 11:27:32 +01:00
parent 08ab3f0999
commit 188ef320cc
6 changed files with 72 additions and 18 deletions

View File

@@ -36,6 +36,9 @@ class Base:
def __getstate__(self):
return {k:v for k,v in self.__dict__.items() if not k.startswith('_')}
def dirty(self):
self.store.dirty(self)
@classmethod
def ext(cls):
raise NotImplementedError('no ext')

View File

@@ -2,9 +2,29 @@
from .base import Base
from time import time
from nullptr.util import *
from dataclasses import field
from dataclasses import field, dataclass
from nullptr.models import Waypoint
from typing import List, Tuple
SUPPLY = ['SCARCE','LIMITED','MODERATE','HIGH','ABUNDANT']
ACTIVITY =['RESTRICTED','WEAK','GROWING','STRONG']
class MarketEntry:
buy: int
sell: int
volume: int
supply: int
activity: int
history: List[Tuple[int, int, int, int, int, int]] = []
def add(self, buy, sell, volume, supply, activity):
self.buy = buy
self.sell = sell
self.volume = volume
self.supply = supply
self.activity = activity
self.history.append((int(time()), buy, sell, volume, supply, activity))
class Marketplace(Base):
def define(self):
self.imports:list = []
@@ -21,6 +41,21 @@ class Marketplace(Base):
def is_fuel(self):
return self.imports + self.exports + self.exchange == ['FUEL']
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')
volume = mg(g, 'tradeVolumes')
supply = SUPPLY.index(mg(g, 'supply'))
activity = ACTIVITY.index(sg(g, 'activity','STRONG'))
e.add(buy, sell, volume, supply, activity)
self.dirty()
def update(self, d):
self.setlst('imports', d, 'imports', 'symbol')
@@ -28,16 +63,7 @@ class Marketplace(Base):
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
self.record_prices(mg(d, 'tradeGoods'))
def buy_price(self, resource):
if resource not in self.prices:

View File

@@ -32,7 +32,7 @@ class Waypoint(Base):
def ext(self):
return 'way'
def traits(self):
def itraits(self):
traits = []
if self.type == 'JUMP_GATE':
traits.append('JUMP')
@@ -54,4 +54,9 @@ class Waypoint(Base):
if 'STRIPPED' in self.traits:
traits.append('STRIPPED')
return traits
def f(self, detail=1):
r = self.symbol
if detail > 3:
r += f'\n{self.x} {self.y}'
return r