From c656e7f3a6492d33605312747d3a6f2624583891 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Sat, 13 Dec 2014 01:32:53 -0800 Subject: [PATCH] "buy" and "sell" --near now works in-system too --- CHANGES.txt | 1 + commands/buy_cmd.py | 32 +++++++++++++++++--------------- commands/sell_cmd.py | 32 +++++++++++++++++--------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index cd3fa52f..ec88e813 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -3,6 +3,7 @@ TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014 ============================================================================== v6.2.1 [wip] +. (kfsone) "buy" and "sell" --near now checks the station/system too . (kfsone) "buy" now shows average cost if you specify --detail (-v) . (kfsone) "sell" now shows average value if you specify --detail (-v) . (kfsone) Fixed item name matching (--avoid) diff --git a/commands/buy_cmd.py b/commands/buy_cmd.py index c8471fa5..66d8d354 100644 --- a/commands/buy_cmd.py +++ b/commands/buy_cmd.py @@ -98,26 +98,28 @@ def run(results, cmdenv, tdb): columns.append('0') nearSystem = cmdenv.nearSystem - distances = dict() if nearSystem: maxLy = cmdenv.maxLyPer or tdb.maxSystemLinkLy results.summary.near = nearSystem results.summary.ly = maxLy cmdenv.DEBUG0("Searching within {}ly of {}", maxLy, nearSystem.name()) + systemRanges = { + system: dist + for system, dist in tdb.genSystemsInRange( + nearSystem, + maxLy, + includeSelf=True, + ) + } tables += ( - " INNER JOIN StationLink AS sl" - " ON (sl.rhs_station_id = ss.station_id)" - ) - columns.append('sl.dist') - constraints.append("(lhs_system_id = {})".format( - nearSystem.ID - )) - constraints.append("(dist <= {})".format( - maxLy - )) - else: - columns.append('0') + " INNER JOIN Station AS stn" + " ON (stn.station_id = ss.station_id)" + ) + constraints.append("(stn.system_id IN ({}))".format( + ",".join(['?'] * len(systemRanges)) + )) + bindValues += list(system.ID for system in systemRanges.keys()) whereClause = ' AND '.join(constraints) stmt = """ @@ -133,12 +135,12 @@ def run(results, cmdenv, tdb): cur = tdb.query(stmt, bindValues) stationByID = tdb.stationByID - for (stationID, priceCr, stock, age, dist) in cur: + for (stationID, priceCr, stock, age) in cur: row = ResultRow() row.station = stationByID[stationID] cmdenv.DEBUG2("{} {}cr {} units", row.station.name(), priceCr, stock) if nearSystem: - row.dist = dist + row.dist = systemRanges[row.station.system] row.price = priceCr row.stock = stock row.age = age diff --git a/commands/sell_cmd.py b/commands/sell_cmd.py index 3f413579..99fc2093 100644 --- a/commands/sell_cmd.py +++ b/commands/sell_cmd.py @@ -69,26 +69,28 @@ def run(results, cmdenv, tdb): bindValues.append(cmdenv.quantity) nearSystem = cmdenv.nearSystem - distances = dict() if nearSystem: maxLy = cmdenv.maxLyPer or tdb.maxSystemLinkLy results.summary.near = nearSystem results.summary.ly = maxLy cmdenv.DEBUG0("Searching within {}ly of {}", maxLy, nearSystem.name()) + systemRanges = { + system: dist + for system, dist in tdb.genSystemsInRange( + nearSystem, + maxLy, + includeSelf=True, + ) + } tables += ( - " INNER JOIN StationLink AS sl" - " ON (sl.rhs_station_id = sb.station_id)" - ) - columns.append('sl.dist') - constraints.append("(lhs_system_id = {})".format( - nearSystem.ID - )) - constraints.append("(dist <= {})".format( - maxLy - )) - else: - columns += [ '0' ] + " INNER JOIN Station AS stn" + " ON (stn.station_id = sb.station_id)" + ) + constraints.append("(stn.system_id IN ({}))".format( + ",".join(['?'] * len(systemRanges)) + )) + bindValues += list(system.ID for system in systemRanges.keys()) whereClause = ' AND '.join(constraints) stmt = """ @@ -104,12 +106,12 @@ def run(results, cmdenv, tdb): cur = tdb.query(stmt, bindValues) stationByID = tdb.stationByID - for (stationID, priceCr, demand, dist) in cur: + for (stationID, priceCr, demand) in cur: row = ResultRow() row.station = stationByID[stationID] cmdenv.DEBUG2("{} {}cr {} units", row.station.name(), priceCr, demand) if nearSystem: - row.dist = dist + row.dist = systemRanges[row.station.system] row.price = priceCr row.demand = demand results.rows.append(row)