Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowgar authored and Shadowgar committed Oct 16, 2014
2 parents 4ba4311 + 388ece7 commit 3f6c1d9
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 2,744 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ tmp

data/TradeDangerous.db
data/TradeDangerous.db-journal
data/TradeDangerous.prices
market/
*.orig
108 changes: 108 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ factors that into the shopping for each subsequent hop.
== CHANGE LOG
==============================================================================

v4.2.0 Oct 15/2014
Merged Smacker65's "local" sub-command (lists systems within jump range of you)

v4.1.0 Oct 15/2014
Merged Bernd Gollesch's (gazelle) changes to eliminate the .prices file from the
repository and to add "--all" to the "update" command.
Also fixed problems caused when the .prices file is missing and there is
limited or no data available - user should get better feedback now.

v4.0.4 Oct 15/2014
Issue #20 Improved fuzzy-matching of system/star names
Fixed "Lacaille Prospect"
"trade.py" is now executable by default on Linux/MacOS

v4.0.3 Oct 12/2014
Issue #17 "--avoid gold" conflicted with "Goldstein Mines"
Issue #13 "Nelson Port" was listed as "Nelson Point"
Issue #12 "-w" was failing because Rigel has no links
Issue #11 Partial name matches weren't generating an ambiguity (e.g. 'ra' or 'ross')
Issue #19 Beryllium and Gallium were incorrectly identified as Minerals

v4.0.2 Oct 06/2014
More systems/stations from ShadowGar!

Expand Down Expand Up @@ -465,6 +486,93 @@ NAV sub-command:
Via | BD+47 2112 | 11.81 | 26.28
Arrive | LHS 2887 | 11.73 | 38.01

LOCAL sub-command:

Provides details of local stations without worrying about trade. By default, if
given a ship, it uses the max dry range of the ship. Use --full if you
want to restrict to systems with a full cargo hold.

trade.py [-q | -v] local [--ship name [--full]] [--ly N.NN] [--pill | --percent] system

--ship name
Uses the values for an empty ship to constrain jump ranges,
--ship=ana
--ship type6
--ship 6

--full
Used with --ship, uses the max range of the ship with a full load,
--ship cobra --full

--ly N.NN
Constrains local systems to a maximum ly distance
--ly 20.0

--pill
Show estimated length along the Pill in ly

--percent
Like --pill but shows percentage instead

-v
Show stations

system
Name of the system or a station in the system,

Examples:
> trade.py local --ly 11.0 dahan
Local systems to DAHAN within 11.0 ly.
--------------------------------------
4.66 Asellus Primus
5.12 Morgor
6.41 Eranin
8.26 Meliae
8.58 LHS 2884
8.60 LP 98-132
9.20 Aulis
9.75 GD 319
10.08 BD+47 2112
10.33 i Bootis

> trade.py -v local --ly 11.0 sur
Local systems to SURYA within 11.0 ly.
--------------------------------------
9.22 [ 2.2] 14 Herculis
9.23 [ 1.0] Vaccimici
9.35 [ 10.0] CM Draco
10.59 [ 10.3] V1090 Herculis
10.69 [ -1.6] Chi Herculis

> trade.py -vv local --ly 10.0 3006
Local systems to LHS 3006 within 10.0 ly.
-----------------------------------------
5.64 [ 0.4] Acihaut
<Cuffey Plant>
<Mastracchio Base>
6.00 [ 5.1] G 239-25
<Bresnik Mine>
6.47 [ 1.1] Nang Ta-khian
<Hay Point>
<Hadwell Orbital>
7.51 [ -0.0] Eranin
<Azeban City>
<Azeban Orbital>
<Eranin 4 Survey>
7.74 [ -4.9] Aulin
<Aulin Enterprise>
<Harbaugh Station>
<Onufrienko Station>
8.12 [ -2.8] i Bootis
<Chango Dock>
<Maher Stellar Research>
8.52 [ -6.4] BD+47 2112
<Olivas Settlement>
8.78 [ 7.9] Lalande 29917
9.40 [ 5.7] DN Draconis
9.72 [ 4.9] LP 98-132
<Freeport>
<Prospect Five>

==============================================================================
== How can I add or update the data?
Expand Down
2,668 changes: 0 additions & 2,668 deletions data/TradeDangerous.prices

This file was deleted.

12 changes: 5 additions & 7 deletions data/TradeDangerous.sql
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ INSERT INTO "Station" VALUES(20,'Stone Enterprise',47,0.0);
INSERT INTO "Station" VALUES(21,'Derrickson''s Escape',22,0.0);
INSERT INTO "Station" VALUES(22,'Xiaoguan Hub',39,0.0);
INSERT INTO "Station" VALUES(23,'Gernhardt Camp',32,0.0);
INSERT INTO "Station" VALUES(24,'Louis De Lacaille Prospect',31,759.0);
INSERT INTO "Station" VALUES(24,'Lacaille Prospect',31,759.0);
INSERT INTO "Station" VALUES(25,'Maunder''s Hope',53,0.0);
INSERT INTO "Station" VALUES(26,'Novitski Oasis',43,0.0);
INSERT INTO "Station" VALUES(27,'Brooks Estate',46,0.0);
Expand Down Expand Up @@ -586,7 +586,7 @@ INSERT INTO "Station" VALUES(180,'Darwin Terminal',189,0.0);
INSERT INTO "Station" VALUES(181,'Kurland Dock',190,0.0);
INSERT INTO "Station" VALUES(182,'Bush High',191,0.0);
INSERT INTO "Station" VALUES(183,'Noblehome',192,0.0);
INSERT INTO "Station" VALUES(184,'Andreas Horizons',194,0.0);
INSERT INTO "Station" VALUES(184,'Andreas Horizons',194,3335.0);
INSERT INTO "Station" VALUES(185,'Clement Plant',195,0.0);
INSERT INTO "Station" VALUES(186,'Hobaugh Beacon',197,0.0);
INSERT INTO "Station" VALUES(187,'Horowitz Reserve',198,0.0);
Expand Down Expand Up @@ -627,7 +627,7 @@ INSERT INTO "Station" VALUES(221,'Guidoni Landing',240,0.0);
INSERT INTO "Station" VALUES(222,'Gannt Enterprise',241,0.0);
INSERT INTO "Station" VALUES(223,'Solovyov Colony',242,0.0);
INSERT INTO "Station" VALUES(224,'Comino Dock',243,0.0);
INSERT INTO "Station" VALUES(225,'Nelson Point',244,0.0);
INSERT INTO "Station" VALUES(225,'Nelson Port',244,0.0);
INSERT INTO "Station" VALUES(226,'Mcmonagle Hub',246,0.0);
INSERT INTO "Station" VALUES(227,'Volterra Refinery',247,0.0);
INSERT INTO "Station" VALUES(228,'Al-Khalili Refinery',248,0.0);
Expand Down Expand Up @@ -691,8 +691,6 @@ INSERT INTO "Station" VALUES(285,'Kippax Depot',312,0.0);





CREATE TABLE Ship
(
ship_id INTEGER PRIMARY KEY AUTOINCREMENT,
Expand Down Expand Up @@ -875,8 +873,8 @@ INSERT INTO "Item" VALUES(64,'Palladium',6);
INSERT INTO "Item" VALUES(65,'Cobalt',6);
INSERT INTO "Item" VALUES(66,'Indium',6);
INSERT INTO "Item" VALUES(67,'Uranium',6);
INSERT INTO "Item" VALUES(68,'Gallium',7);
INSERT INTO "Item" VALUES(69,'Beryllium',7);
INSERT INTO "Item" VALUES(68,'Gallium',6);
INSERT INTO "Item" VALUES(69,'Beryllium',6);
INSERT INTO "Item" VALUES(70,'Semiconductors',4);
INSERT INTO "Item" VALUES(71,'Polymers',4);
INSERT INTO "Item" VALUES(72,'Natural Fabrics',13);
Expand Down
39 changes: 24 additions & 15 deletions data/buildcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ def priceLineNegotiator(priceFile, db, debug=0):
systemByName = { name: ID for (ID, name) in cur }

categoriesByName = { name: ID for (ID, name) in cur.execute("SELECT category_id, name FROM category") }
itemsByName = { "{}:{}".format(catID, name): itemID for (catID, itemID, name) in cur.execute("SELECT category_id, item_id, name FROM item") }

# Are there any item names which appear in two categories?
qualityItemWithCategory = cur.execute("SELECT COUNT(*) FROM (SELECT name FROM Item GROUP BY 1 HAVING COUNT(*) > 1)").fetchone()[0]
if qualityItemWithCategory:
itemsByName = { "{}.{}".format(catID, name): itemID for (catID, itemID, name) in cur.execute("SELECT category_id, item_id, name FROM item") }
else:
itemsByName = { name: itemID for (itemID, name) in cur.execute("SELECT item_id, name FROM item") }

for line in priceFile:
try:
Expand Down Expand Up @@ -90,7 +96,7 @@ def priceLineNegotiator(priceFile, db, debug=0):
sys.exit(1)
itemName, stationPaying, stationAsking, modified = matches.group(1), int(matches.group(2)), int(matches.group(3)), matches.group(4)
demand, demandLevel, stock, stockLevel = int(matches.group(5) or -1), int(matches.group(6) or -1), int(matches.group(7) or -1), int(matches.group(8) or -1)
itemID = itemsByName["{}:{}".format(categoryID, itemName)]
itemID = itemsByName["{}:{}".format(categoryID, itemName)] if qualityItemWithCategory else itemsByName[itemName]
uiOrder += 1
yield PriceEntry(stationID, itemID, stationPaying, stationAsking, uiOrder, modified, demand, demandLevel, stock, stockLevel)
except (AttributeError, IndexError):
Expand All @@ -106,18 +112,21 @@ def processPricesFile(db, pricesPath, stationID=None, debug=0):
if debug: print("* Deleting stale entries for {}".format(stationID))
db.execute("DELETE FROM Price WHERE station_id = {}".format(stationID))

with pricesPath.open() as pricesFile:
bindValues = []
for price in priceLineNegotiator(pricesFile, db, debug):
if debug > 2: print(price)
bindValues += [ price ]
stmt = """
INSERT OR REPLACE INTO Price (station_id, item_id, sell_to, buy_from, ui_order, modified, demand, demand_level, stock, stock_level)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP), ?, ?, ?, ?)
"""
db.executemany(stmt, bindValues)
db.commit()

try:
with pricesPath.open() as pricesFile:
bindValues = []
for price in priceLineNegotiator(pricesFile, db, debug):
if debug > 2: print(price)
bindValues += [ price ]
stmt = """
INSERT OR REPLACE INTO Price (station_id, item_id, sell_to, buy_from, ui_order, modified, demand, demand_level, stock, stock_level)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP), ?, ?, ?, ?)
"""
db.executemany(stmt, bindValues)
db.commit()
except FileNotFoundError:
if debug:
print("WARNING: processPricesFile found no {} file".format(pricesPath))

def buildCache(dbPath, sqlPath, pricesPath, debug=0):
"""
Expand Down Expand Up @@ -145,7 +154,7 @@ def buildCache(dbPath, sqlPath, pricesPath, debug=0):
tempDB.executescript(sqlScript)

# Parse the prices file
processPricesFile(tempDB, pricesPath)
processPricesFile(tempDB, pricesPath, debug=debug)

# Database is ready; copy it to a persistent store.
if debug: print("* Populating SQLite database file '%s'" % dbPath)
Expand Down
62 changes: 44 additions & 18 deletions data/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,51 @@ def dumpPrices(dbFilename, withModified=False, stationID=None, file=None, debug=
longestName = max(items.values(), key=lambda name: len(name))
longestNameLen = len(longestName)

if stationID:
# check if there are prices for the station
cur.execute("SELECT COUNT(*) FROM Price WHERE Price.station_id = {}".format(stationID))
priceCount = cur.fetchone()[0]
# generate new timestamp in the select
modifiedStamp = "CURRENT_TIMESTAMP"
else:
# no station, no check
priceCount = 1
# use old timestamp for the export
modifiedStamp = "Price.modified"

stationClause = "1" if not stationID else "Station.station_id = {}".format(stationID)
cur.execute("""
SELECT Station.system_id
, Price.station_id
, Item.category_id
, Price.item_id
, Price.sell_to
, Price.buy_from
, Price.modified
, IFNULL(Price.demand, -1)
, IFNULL(Price.demand_level, -1)
, IFNULL(Price.stock, -1)
, IFNULL(Price.stock_level, -1)
FROM Station, Item, Category, Price
WHERE {} -- station clause
AND Station.station_id = Price.station_id
AND (Item.category_id = Category.category_id) AND Item.item_id = Price.item_id
ORDER BY Station.system_id, Station.station_id, Category.name, Price.ui_order, Price.item_id
""".format(stationClause))
defaultDemandVal = -1
if priceCount == 0:
# no prices, generate an emtpy one with all items
cur.execute("""
SELECT Station.system_id, Station.station_id, Item.category_id, Item.item_id,
0, 0, CURRENT_TIMESTAMP,
{defDemand}, {defDemand}, {defDemand}, {defDemand}
FROM Item LEFT OUTER JOIN Station, Category
WHERE {stationClause}
AND Item.category_id = Category.category_id
ORDER BY Station.system_id, Station.station_id, Category.name, Item.item_id
""".format(stationClause=stationClause, defDemand=defaultDemandVal))
else:
cur.execute("""
SELECT Station.system_id
, Price.station_id
, Item.category_id
, Price.item_id
, Price.sell_to
, Price.buy_from
, {modStamp} -- real or current timestamp
, IFNULL(Price.demand, {defDemand})
, IFNULL(Price.demand_level, {defDemand})
, IFNULL(Price.stock, {defDemand})
, IFNULL(Price.stock_level, {defDemand})
FROM Station, Item, Category, Price
WHERE {stationClause} -- station clause
AND Station.station_id = Price.station_id
AND (Item.category_id = Category.category_id) AND Item.item_id = Price.item_id
ORDER BY Station.system_id, Station.station_id, Category.name, Price.ui_order, Price.item_id
""".format(modStamp=modifiedStamp, stationClause=stationClause, defDemand=defaultDemandVal))

lastSys, lastStn, lastCat = None, None, None

if not file: file = sys.stdout
Expand Down
10 changes: 5 additions & 5 deletions data/ships.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Ship(namedtuple('Ship', [ 'name', 'capacity', 'mass', 'driveRating', 'maxJ

ships = [
# Name Cap Mass Rating MaxLy FullLy Speed Boost
Ship('Eagle', 6, 52, 348, 6.59, 6.00, 240, 350, [ 'Abnett', 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Hay Point', 'Louis De Lac' ]),
Ship('Sidewinder', 4, 47, 348, 8.13, 7.25, 220, 293, [ 'Abnett', 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Hay Point', 'Louis De Lac', 'Vonarburg Co-op' ]),
Ship('Hauler', 16, 39, 348, 8.74, 6.10, 200, 246, [ 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Hay Point', 'Louis De Lac', 'Vonarburg Co-op' ]),
Ship('Viper', 8, 40, 348, 13.49, 9.16, 320, 500, [ 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Chango Dock', 'Hay Point', 'Louis De Lac' ]),
Ship('Eagle', 6, 52, 348, 6.59, 6.00, 240, 350, [ 'Abnett', 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Hay Point', 'Lacaille Prosp' ]),
Ship('Sidewinder', 4, 47, 348, 8.13, 7.25, 220, 293, [ 'Abnett', 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Hay Point', 'Lacaille Prosp', 'Vonarburg Co-op' ]),
Ship('Hauler', 16, 39, 348, 8.74, 6.10, 200, 246, [ 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Hay Point', 'Lacaille Prosp', 'Vonarburg Co-op' ]),
Ship('Viper', 8, 40, 348, 13.49, 9.16, 320, 500, [ 'Aulin Enterprise', 'Beagle2', 'Bradfield', 'Chango Dock', 'Hay Point', 'Lacaille Prosp' ]),
Ship('Cobra', 36, 114, 1155, 9.94, 7.30, 280, 400, [ 'Aulin Enterprise', 'Chango Dock' ]),
Ship('Lakon Type 6', 100, 113, 3455, 29.36, 15.64, 220, 329, [ 'Aulin Enterprise', 'Chango Dock', 'Vonarburg Co-op' ]),
Ship('Lakon Type 9', 440, 1275, 23720, 18.22, 13.34, 130, 200, [ 'Chango Dock' ]),
Ship('Anaconda', 228, 2600, 52345, 19.70, 17.60, 180, 235, [ 'Louis De Lac' ]),
Ship('Anaconda', 228, 2600, 52345, 19.70, 17.60, 180, 235, [ 'Lacaille Prosp' ]),
]
Loading

0 comments on commit 3f6c1d9

Please sign in to comment.