Skip to content

Commit

Permalink
Instead of system links, station links are much more useful
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Smith committed Nov 16, 2014
1 parent a9a933d commit b3ff475
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
38 changes: 18 additions & 20 deletions buildcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,21 +557,18 @@ def processPricesFile(tdenv, db, pricesPath, stationID=None, defaultZero=False):
sells.append([ stnID, itemID, cr, units, level, modified ])

if items:
print("itemBinds")
db.executemany("""
INSERT INTO StationItem
(station_id, item_id, ui_order, modified)
VALUES (?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", items)
if sells:
print("sellingBinds")
db.executemany("""
INSERT INTO StationSelling
(station_id, item_id, price, units, level, modified)
VALUES (?, ?, ?, ?, ?, IFNULL(?, CURRENT_TIMESTAMP))
""", sells)
if buys:
print("buyingBinds")
db.executemany("""
INSERT INTO StationBuying
(station_id, item_id, price, units, level, modified)
Expand Down Expand Up @@ -710,24 +707,25 @@ def processImportFile(tdenv, db, importPath, tableName):
table=tableName)


def generateSystemLinks(tdenv, db, maxLinkDist):
tdenv.DEBUG0("Generating SystemLink table")
def generateStationLink(tdenv, db):
tdenv.DEBUG0("Generating StationLink table")
db.create_function("sqrt", 1, math.sqrt)
db.execute("""
INSERT INTO SystemLink (lhs_id, rhs_id, dist)
SELECT lhs.system_id AS lhs_id,
rhs.system_id AS rhs_id,
((lhs.pos_x - rhs.pos_x) * (lhs.pos_x - rhs.pos_x)) +
((lhs.pos_y - rhs.pos_y) * (lhs.pos_y - rhs.pos_y)) +
((lhs.pos_z - rhs.pos_z) * (lhs.pos_z - rhs.pos_z)) dist
FROM System AS lhs, System AS rhs
WHERE lhs.system_id != rhs.system_id
""")
db.execute("UPDATE SystemLink SET dist = sqrt(dist)")
db.execute("""
CREATE INDEX
idx_system_links_systems
ON SystemLink(lhs_id, dist, rhs_id)
INSERT INTO StationLink
SELECT lhs.system_id AS lhs_system_id,
lhs.station_id AS lhs_station_id,
rhs.system_id AS rhs_system_id,
rhs.station_id AS rhs_station_id,
((lSys.pos_x - rSys.pos_x) * (lSys.pos_x - rSys.pos_x)) +
((lSys.pos_y - rSys.pos_y) * (lSys.pos_y - rSys.pos_y)) +
((lSys.pos_z - rSys.pos_z) * (lSys.pos_z - rSys.pos_z)) dist
FROM System AS lSys
INNER JOIN Station lhs
ON (lSys.system_id = lhs.system_id),
System AS rSys
INNER JOIN Station rhs
ON (rSys.system_id = rhs.system_id)
WHERE lhs.system_id != rhs.system_id
""")
db.commit()

Expand Down Expand Up @@ -774,14 +772,14 @@ def buildCache(tdenv, dbPath, sqlPath, pricesPath, importTables, defaultZero=Fal
if dbPath.exists():
tdenv.DEBUG0("Removing old database file")
dbPath.unlink()
generateStationLink(tdenv, tempDB)

newDB = sqlite3.connect(str(dbPath))
importScript = "".join(tempDB.iterdump())
tdenv.DEBUG3(importScript)
newDB.executescript(importScript)
newDB.commit()

generateSystemLinks(tdenv, newDB, maxLinkDist=1000)

tdenv.DEBUG0("Finished")

Expand Down
29 changes: 14 additions & 15 deletions data/TradeDangerous.sql
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,18 @@ CREATE TABLE StationBuying
/* We're often going to be asking "using (item_id) where buying.price > selling.cost" */
CREATE INDEX idx_buying_price ON StationBuying (item_id, price);

CREATE TABLE SystemLink
CREATE TABLE StationLink
(
lhs_id INTEGER NOT NULL,
rhs_id INTEGER NOT NULL,
dist DOUBLE NOT NULL
lhs_system_id INTEGER NOT NULL,
lhs_station_id INTEGER NOT NULL,
rhs_system_id INTEGER NOT NULL CHECK (lhs_system_id != rhs_system_id),
rhs_station_id INTEGER NOT NULL CHECK (lhs_station_id != rhs_station_id),
dist DOUBLE NOT NULL CHECK (dist > 0.0),
PRIMARY KEY (lhs_station_id, rhs_station_id)
)
;
CREATE INDEX idx_stn_dist ON StationLink (lhs_station_id, rhs_station_id);
CREATE INDEX idx_stn_sys ON StationLink (lhs_system_id, rhs_system_id);

CREATE VIEW vPrice AS
SELECT si.station_id AS station_id,
Expand All @@ -266,24 +271,18 @@ CREATE VIEW vPrice AS
;

CREATE VIEW vProfitableTrades AS
SELECT srcStn.system_id AS src_system_id,
src.station_id AS src_station_id,
SELECT src.station_id AS src_station_id,
src.item_id AS item_id,
dst.station_id AS dst_station_id,
dstStn.system_id AS dst_system_id,
src.price AS cost,
dst.price - src.price AS profit,
link.dist AS dist
FROM StationSelling AS src
INNER JOIN StationBuying AS dst
ON (src.item_id = dst.item_id),
Station AS srcStn
INNER JOIN SystemLink AS link
ON (srcStn.system_id = link.lhs_id)
INNER JOIN Station AS dstStn
ON (link.rhs_id = dstStn.system_id)
WHERE src.station_id = srcStn.station_id
AND dst.station_id = dstStn.station_id
ON (src.item_id = dst.item_id AND dst.price > src.price)
INNER JOIN StationLink AS link
ON (src.station_id = link.lhs_station_id
AND dst.station_id = link.rhs_station_id)
;


Expand Down

0 comments on commit b3ff475

Please sign in to comment.