Skip to content

Commit

Permalink
Increment hops improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Aug 11, 2014
1 parent 1569462 commit 81539fd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
37 changes: 34 additions & 3 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def routes(maxHops=2, stn=None, cr=None, cap=None, maxJumps=None, maxLy=None, ma
srcStn = stn if stn else curStation
withCr = cr if cr else curCredits
routes = [ Route([srcStn], [], withCr, 0, 0) ]
lastHop = numHops - 1
lastHop = maxHops - 1

print("From %s via %s to %s with %d credits for %d hops" % (srcStn, "None", "Any", withCr, numHops))
print("From %s via %s to %s with %d credits for %d hops" % (srcStn, "None", "Any", withCr, maxHops))

for hopNo in range(numHops):
for hopNo in range(maxHops):
if calc.debug: print("# Hop %d" % hopNo)
restrictTo = None
# if hopNo == 0 and viaStation:
Expand All @@ -68,3 +68,34 @@ def routes(maxHops=2, stn=None, cr=None, cap=None, maxJumps=None, maxLy=None, ma

for i in range(0, min(len(routes), maxRoutes)):
print(routes[i])

def find(item, stn=None):
srcStn = tdb.getStation(stn if stn else curStation)
qry = """
SELECT p.station_id, p.buy_cr
FROM Items AS i
INNER JOIN Prices AS p
ON i.ID = P.item_id
WHERE i.item LIKE '%%%s%%'
AND p.buy_cr > 0
AND p.ui_order > 0
ORDER BY buy_cr ASC
""" % item
prices = [ row for row in tdb.fetch_all(qry) ]
if not prices:
raise ValueError("No items match '%s'" % item)
dests = { dest[1].ID: dest for dest in srcStn.getDestinations() }
if not prices[0][0] in dests:
raise ValueError("No connecting stations found")
cheapest = dests[prices[0][0]]
print("Cheapest: %s: %dcr, %djumps, %dly" % (cheapest[1], prices[0][1], cheapest[2], cheapest[3]))
best, bestCr, bestJumps, bestLy = None, 0, 0, 0
for price in prices:
stnID = price[0]
if not stnID in dests:
next
dest = dests[stnID]
if not best or (price[1] < bestCr or (price[1] >= bestCr - 16 and dest[2] < bestJumps)):
best, bestCr, bestJumps, bestLy = dest[1], price[1], dest[2], dest[3]
if best:
print("Closest: %s: %dcr, %djumps, %dly" % (best, bestCr, bestJumps, bestLy))
15 changes: 15 additions & 0 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def load(self, avoiding=[], ignoreLinks=False):
station.organizeTrades()

def getStation(self, name):
if isinstance(name, Station):
return name
upperName = name.upper()
if upperName in self.systemIDs:
return self.stations[self.systemIDs[upperName]]
Expand All @@ -178,3 +180,16 @@ def query(self, sql):
def fetch_all(self, sql):
for row in self.query(sql):
yield row

def list_search(listType, lookup, values):
match = None
needle = lookup.casefold()
for val in values:
if val.casefold().find(needle) > -1:
if match:
raise ValueError("Ambiguity: %s '%s' could match %s or %s" % (
listType, lookup, match, val))
match = val
if not match:
raise ValueError("Error: '%s' doesn't match any %s" % (lookup, listType))
return match

0 comments on commit 81539fd

Please sign in to comment.