Skip to content

Commit

Permalink
Merged kfsone/tradedangerous into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maddavo committed Feb 22, 2015
2 parents c51a8f9 + bed80eb commit 40e8565
Show file tree
Hide file tree
Showing 10 changed files with 541 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

[wip]
v6.11.0 Feb 21 2015
. (kfsone) Added "market" command for viewing station buy/sell list
e.g. trade.py market ramoncity --buy --sell -vv
. (kfsone) Lots of improvements to edscupdate.py;
- added --random, checks 10 random new systems,
- added --conf N for changing the confidence rating,
Expand Down
45 changes: 45 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,51 @@ RARES sub-command:
ANY NA/Libby Orbital Any Na Coffee 1,790 170.32 11 ? ?


MARKET sub-command:

Lists items bought / sold at a given station; with --detail (-v) also
includes the average price for those items.

trade.py market <station> [--buy] [--sell] [--detail]

station
Name of the station to list, e.g. "paes/ramon" or "ramoncity",

--buy
-B
List items bought by the station (listed as 'SELL' in-game)

--sell
-S
List items sold by the station (listed as 'BUY' in-game)

--detail
-v
Once: includes average prices
Twice: include demand column and category headings

$ trade.py market --buy ramoncity
Item Buying
------------------------------
Hydrogen Fuel 90
Clothing 221
Domestic Appliances 417
Food Cartridges 35
...

$ trade.py market --buy --sell ramoncity -v
Item Buying Avg Age/Days Selling Avg Supply Age/Days
-------------------------------------------------------------------------------------
+CHEMICALS
Hydrogen Fuel 90 100 0.01 94 102 74,034H 0.01
+CONSUMER ITEMS
Clothing 221 361 0.01 237 238 1,706M 0.01
Domestic Appliances 417 582 0.01 437 436 1,022M 0.01
+FOODS
Food Cartridges 35 125 0.01 45 50 32,019H 0.01

...

NAV sub-command:

Provides details of routes without worrying about trade. By default, if
Expand Down
1 change: 1 addition & 0 deletions commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import commands.export_cmd
import commands.import_cmd
import commands.local_cmd
import commands.market_cmd
import commands.nav_cmd
import commands.olddata_cmd
import commands.rares_cmd
Expand Down
183 changes: 183 additions & 0 deletions commands/market_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
from __future__ import absolute_import, with_statement, print_function, division, unicode_literals
from commands.commandenv import ResultRow
from commands.exceptions import *
from commands.parsing import MutuallyExclusiveGroup, ParseArgument
from formatting import RowFormat, ColumnFormat
from tradedb import TradeDB

######################################################################
# Parser config

help='Lists items bought/sold at a given station.'
name='market'
epilog=None
wantsTradeDB=True
arguments = [
ParseArgument(
'origin',
help='Station being queried.',
metavar='STATIONNAME',
type=str,
),
]
switches = [
ParseArgument(
'--buying', '-B',
help='Show items station is buying',
action='store_true',
),
ParseArgument(
'--selling', '-S',
help='Show items station is selling',
action='store_true',
)
]

######################################################################
# Perform query and populate result set


def render_units(units, level):
if level == 0:
return '-'
if units < 0:
return '?'
levelNames = { -1: '?', 1: 'L', 2: 'M', 3: 'H' }
return "{:n}{}".format(units, levelNames[level])


def run(results, cmdenv, tdb):
origin = cmdenv.startStation
if not origin.itemCount:
raise CommandLineError(
"No trade data available for {}".format(origin.name())
)

buying, selling = cmdenv.buying, cmdenv.selling
if not buying and not selling:
raise CommandLineError(
"Please specify one or both of --buying (-B) "
"or --selling (-S)."
)

results.summary = ResultRow()
results.summary.origin = origin
results.summary.buying = cmdenv.buying
results.summary.selling = cmdenv.selling

tdb.getAverageSelling()
tdb.getAverageBuying()
cur = tdb.query("""
SELECT si.item_id,
sb.price,
sb.units,
sb.level,
JULIANDAY('now') - JULIANDAY(sb.modified),
ss.price,
ss.units,
ss.level,
JULIANDAY('now') - JULIANDAY(ss.modified)
FROM StationItem si,
StationBuying AS sb,
StationSelling AS ss
WHERE si.station_id = ? AND
sb.station_id = ? AND
ss.station_id = ? AND
sb.item_id = si.item_id AND
ss.item_id = si.item_id
""", [
origin.ID, origin.ID, origin.ID
])

for row in cur:
it = iter(row)
item = tdb.itemByID[next(it)]
row = ResultRow()
row.item = item
row.buyCr = int(next(it) or 0)
row.avgBuy = tdb.avgBuying[item.ID]
units, level = int(next(it) or 0), int(next(it) or 0)
row.buyUnits = units
row.buyLevel = level
row.demand = render_units(units, level)
row.buyAge = float(next(it) or 0)
if buying:
hasBuy = (row.buyCr or units or level)
else:
hasBuy = False
row.sellCr = int(next(it) or 0)
row.avgSell = tdb.avgSelling[item.ID]
units, level = int(next(it) or 0), int(next(it) or 0)
row.sellUnits = units
row.sellLevel = level
row.supply = render_units(units, level)
row.sellAge = float(next(it) or 0)
if selling:
hasSell = (row.sellCr or units or level)
else:
hasSell = False
if hasBuy or hasSell:
results.rows.append(row)

if not results.rows:
raise CommandLineError("No items found")

results.rows.sort(key=lambda row: row.item.dbname)
results.rows.sort(key=lambda row: row.item.category.dbname)

return results

#######################################################################
## Transform result set into output


def render(results, cmdenv, tdb):
longest = max(results.rows, key=lambda row: len(row.item.name()))
longestLen = len(longest.item.name())
longestDmd = max(results.rows, key=lambda row: len(row.demand)).demand
longestSup = max(results.rows, key=lambda row: len(row.supply)).supply
dmdLen = max(len(longestDmd), len("Demand"))
supLen = max(len(longestSup), len("Supply"))

showCategories = (cmdenv.detail > 0)

rowFmt = RowFormat()
if showCategories:
rowFmt.prefix = ' '

rowFmt.addColumn('Item', '<', longestLen,
key=lambda row: row.item.name())
if cmdenv.buying:
rowFmt.addColumn('Buying', '>', 7, 'n',
key=lambda row: row.buyCr)
if cmdenv.detail:
rowFmt.addColumn('Avg', '>', 7, 'n',
key=lambda row: row.avgBuy)
if cmdenv.detail > 1:
rowFmt.addColumn('Demand', '>', dmdLen,
key=lambda row: row.demand)
if cmdenv.detail:
rowFmt.addColumn('Age/Days', '>', 7, '.2f',
key=lambda row: row.buyAge)
if cmdenv.selling:
rowFmt.addColumn('Selling', '>', 7, 'n',
key=lambda row: row.sellCr)
if cmdenv.detail:
rowFmt.addColumn('Avg', '>', 7, 'n',
key=lambda row: row.avgSell)
rowFmt.addColumn('Supply', '>', supLen,
key=lambda row: row.supply)
if cmdenv.detail:
rowFmt.addColumn('Age/Days', '>', 7, '.2f',
key=lambda row: row.buyAge)

if not cmdenv.quiet:
heading, underline = rowFmt.heading()
print(heading, underline, sep='\n')

lastCat = None
for row in results.rows:
if showCategories and row.item.category is not lastCat:
print("+{}".format(row.item.category.name()))
lastCat = row.item.category
print(rowFmt.format(row))
1 change: 1 addition & 0 deletions corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"TOXANDJI/TSU NENAGA ORBITAL": DELETED,
"AMAHU/ALEDNDRIA GATEWAY": DELETED,
"LTT 7548/ALEDNDRIA RING": DELETED,
"DJINAJERI/MESSERSCHMID LANDING": DELETED,
}

categories = {
Expand Down
5 changes: 5 additions & 0 deletions data/ShipVendor.csv
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ unq:[email protected]_id,unq:[email protected]_id,unq:[email protected]_id
'APTET','Torricelli Port','Type 6'
'APTET','Torricelli Port','Type 7'
'APTET','Torricelli Port','Type 9'
'ATTACATES','Buckell Ring','Cobra'
'ATTACATES','Buckell Ring','Eagle'
'ATTACATES','Buckell Ring','Sidewinder'
'ATTACATES','Buckell Ring','Type 6'
'ATTACATES','Buckell Ring','Type 7'
'BAST','Hart Station','Cobra'
'BAST','Hart Station','Eagle'
'BHRITZAMENO','Feynman Terminal','Adder'
Expand Down
Loading

0 comments on commit 40e8565

Please sign in to comment.