Skip to content

Commit

Permalink
Use station references directly
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Jul 12, 2014
1 parent 263a878 commit 6070fce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
38 changes: 18 additions & 20 deletions trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ def __eq__(self, rhs):
return self.gainCr == rhs.gainCr

def __repr__(self):
src = tdb.stations[self.route[0]]
src = self.route[0]
credits = args.credits
gainCr = 0
route = self.route

str = "%s -> %s:\n" % (src, tdb.stations[route[-1]])
str = "%s -> %s:\n" % (src, route[-1])
for i in range(len(route) - 1):
hop = self.hops[i]
str += " @ %-20s Buy" % tdb.stations[route[i]]
str += " @ %-20s Buy" % route[i]
for item in hop[0]:
str += " %d*%s," % (item[1], item[0])
str += "\n"
gainCr += hop[1]

str += " $ %s %dcr + %dcr => %dcr total" % (tdb.stations[route[-1]], credits, gainCr, credits + gainCr)
str += " $ %s %dcr + %dcr => %dcr total" % (route[-1], credits, gainCr, credits + gainCr)

return str

Expand Down Expand Up @@ -93,9 +93,9 @@ def parse_command_line():
if args.origin:
originName = args.origin
originID = tdb.get_station_id(originName)
origins = [ originID ]
origins = [ tdb.stations[originID] ]
else:
origins = list(tdb.stations.keys())
origins = [ station for station in tdb.stations.values() ]

if args.dest:
destName = args.dest
Expand Down Expand Up @@ -149,33 +149,31 @@ def try_combinations(capacity, credits, tradeList):
return [ best, bestCr ]


def get_profits(srcID, dstID, startCr):
src = tdb.stations[srcID]

if args.debug: print("%s -> %s with %dcr" % (src, tdb.stations[dstID], startCr))
def get_profits(src, dst, startCr):
if args.debug: print("%s -> %s with %dcr" % (src, dst, startCr))

# Get a list of what we can buy
trades = src.links[dstID]
trades = src.links[dst.ID]
return try_combinations(args.capacity, startCr, trades)


def generate_routes():
q = deque([[origID] for origID in origins])
q = deque([[origin] for origin in origins])
hops = args.hops
while q:
# get the most recent partial route
route = q.pop()
# furthest station on the route
lastStation = tdb.stations[route[-1]]
lastStation = route[-1]
if len(route) >= hops: # destination
# upsize the array so we can reuse the slot.
route.append(0)
for dstID in lastStation.links:
route[-1] = dstID
for dest in lastStation.stations:
route[-1] = dest
yield route
else:
for dstID in lastStation.links:
q.append(route + [dstID])
for dest in lastStation.stations:
q.append(route + [dest])

def main():
parse_command_line()
Expand All @@ -195,9 +193,9 @@ def main():
gainCr = 0
hops = []
for i in range(0, len(route) - 1):
srcID, dstID = route[i], route[i + 1]
if args.debug: print("hop %d: %d -> %d" % (i, srcID, dstID))
bestTrade = get_profits(srcID, dstID, credits + gainCr)
src, dst = route[i], route[i + 1]
if args.debug: print("hop %d: %s -> %s" % (i, src, dst))
bestTrade = get_profits(src, dst, credits + gainCr)
if not bestTrade:
break
hops.append(bestTrade)
Expand Down
28 changes: 21 additions & 7 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
class Trade(object):
""" Describes what it would cost and how much you would gain
when selling an item between two specific stations. """
def __init__(self, item, costCr, gainCr):
def __init__(self, item, itemID, costCr, gainCr):
self.item = item
self.itemID = itemID
self.costCr = costCr
self.gainCr = gainCr

def describe(self):
print(self.item, self.costCr, self.gainCr, self.value)
print(self.item, self.itemID, self.costCr, self.gainCr, self.value)

def __repr__(self):
return "%s@%d+%d" % (self.item, self.costCr, self.gainCr)
return "%s (%dcr)" % (self.item, self.costCr)


class Station(object):
Expand All @@ -34,12 +35,25 @@ class Station(object):
def __init__(self, ID, system, station):
self.ID, self.system, self.station = ID, system.replace(' ', ''), station.replace(' ', '')
self.links = {}
self.items = {}
self.stations = []

def addTrade(self, dstID, item, costCr, gainCr):
def addTrade(self, dest, item, itemID, costCr, gainCr):
""" Add a Trade entry from this to a destination station """
dstID = dest.ID
if not dstID in self.links:
self.links[dstID] = []
self.links[dstID].append(Trade(item, costCr, gainCr))
self.stations.append(dest)
trade = Trade(item, itemID, costCr, gainCr)
self.links[dstID].append(trade)
self.items[((dstID) << 16) + itemID] = trade

def getTrade(self, dstID, itemID):
key = ((dstID) << 16) + itemID
try:
return self.items[key]
except:
return None

def organizeTrades(self):
for station in self.links:
Expand Down Expand Up @@ -78,9 +92,9 @@ def load(self):
' FROM Prices AS src INNER JOIN Prices AS dst ON src.item_id = dst.item_id'
' WHERE src.station_id <> dst.station_id AND src.buy_cr > 0 AND dst.sell_cr > src.buy_cr'
' AND src.ui_order > 0 AND dst.ui_order > 0'
' ORDER BY (dst.sell_cr - src.buy_cr) / src.buy_cr DESC')
' ORDER BY (dst.sell_cr - src.buy_cr) DESC')
for row in cur:
self.stations[row[0]].addTrade(row[1], self.items[row[2]], row[3], row[4])
self.stations[row[0]].addTrade(self.stations[row[1]], self.items[row[2]], row[2], row[3], row[4])

for station in self.stations.values():
station.organizeTrades()
Expand Down

0 comments on commit 6070fce

Please sign in to comment.