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

@ -3,6 +3,7 @@ from nullptr.models.jumpgate import Jumpgate
from nullptr.models.system import System
from nullptr.models.waypoint import Waypoint
from dataclasses import dataclass
from nullptr.util import pprint
from copy import copy
class AnalyzerException(Exception):
@ -109,6 +110,7 @@ class Analyzer:
mkts = [m.waypoint for m in self.store.all_members(orig.system, Marketplace)]
cur = orig
if orig == to:
return []
while cur != to:
best = cur
@ -157,8 +159,8 @@ class Analyzer:
prices[r] = []
prices[r].append({
'wp': m.waypoint,
'buy': p['buy'],
'sell': p['sell']
'buy': p.buy,
'sell': p.sell
})
return prices

View File

@ -121,6 +121,7 @@ class Api:
units = min(units, fuel_avail)
data = {'fromCargo': from_cargo, 'units': units }
data = self.request('post', f'my/ships/{ship}/refuel', data)
self.log_transaction(data)
if from_cargo:
boxes = ceil(float(units) / 100)
ship.take_cargo('FUEL', boxes)
@ -236,7 +237,18 @@ class Api:
result = self.store.update_list('Survey', mg(data, 'surveys'))
return result
######## Commerce #########
def log_transaction(self, data):
if not 'transaction' in data: return
typ = mg(data, 'transaction.tradeSymbol')
ppu = mg(data, 'transaction.pricePerUnit')
shipsym = mg(data, 'transaction.shipSymbol')
ship = self.store.get('Ship', shipsym)
units = mg(data, 'transaction.units')
act = mg(data,'transaction.type')
ship.log(f'{act} {units} of {typ} for {ppu} at {ship.location}')
def sell(self, ship, typ,units=None):
if units is None:
units = ship.get_cargo(typ)
@ -245,7 +257,7 @@ class Api:
'units': units
}
data = self.request('post', f'my/ships/{ship}/sell', data)
ship.log(f'sell {units} of {typ}')
self.log_transaction(data)
if 'cargo' in data:
ship.update(data)
if 'agent' in data:
@ -258,7 +270,7 @@ class Api:
'units': amt
}
data = self.request('post', f'my/ships/{ship}/purchase', data)
ship.log(f'buy {amt} of {typ} at {ship.location}')
self.log_transaction(data)
if 'cargo' in data:
ship.update(data)
if 'agent' in data:

View File

@ -184,7 +184,7 @@ class Commander(CommandLine):
for w in r:
wname = w.symbol.split('-')[2]
traits = ", ".join(w.traits())
traits = ", ".join(w.itraits())
typ = w.type[0]
if typ not in ['F','J'] and len(traits) == 0:
continue
@ -542,3 +542,9 @@ class Commander(CommandLine):
pprint(prices[resource.upper()])
else:
pprint(prices)
def do_path(self, waypoint_str):
if not self.has_ship(): return
w = self.resolve('Waypoint', waypoint_str)
p = self.analyzer.find_nav_path(self.ship.location, w, self.ship.fuel_capacity)
pprint(p)

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,8 +2,28 @@
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):
@ -22,22 +42,28 @@ 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')
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
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')
@ -55,3 +55,8 @@ class Waypoint(Base):
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