Skip to content

Commit

Permalink
TradeCalc.getProfits renamed getBestTrade. Added optional capacity pa…
Browse files Browse the repository at this point in the history
…rameter to getBestHopFrom
  • Loading branch information
kfsone committed Aug 6, 2014
1 parent 8654bbc commit ced8c73
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions tradecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ def __init__(self, tdb, debug=False, capacity=4, maxUnits=0, margin=0.02, unique
self.unique = unique
self.maxUnits = maxUnits if maxUnits > 0 else capacity

def tryCombinations(self, startCr, tradeList):
startCapacity, maxUnits = self.capacity, self.maxUnits
def tryCombinations(self, startCr, tradeList, capacity=None):
startCapacity = capacity if capacity else self.capacity
maxUnits = self.maxUnits

firstTrade = tradeList[0]
if maxUnits >= startCapacity and firstTrade.costCr * startCapacity <= startCr:
return [ [ [ firstTrade, startCapacity ] ], firstTrade.gainCr * startCapacity ]
Expand Down Expand Up @@ -87,19 +89,19 @@ def tryCombinations(self, startCr, tradeList):
return [ best, bestGainCr ]


def getProfits(self, src, dst, startCr):
def getBestTrade(self, src, dst, startCr, capacity=None):
if self.debug: print("# %s -> %s with %dcr" % (src, dst, startCr))

# Get a list of what we can buy
return self.tryCombinations(startCr, src.links[dst.ID])
return self.tryCombinations(startCr, src.links[dst.ID], capacity=capacity)

def getBestHopFrom(self, src, credits):
""" Determine the best trade run from a given station. """
if isinstance(src, str):
src = self.tdb.getStation(src)
bestDst, bestLoad, bestGainCr = None, None, 0
for dst in src.stations:
trade = self.getProfits(src, dst, credits)
trade = self.getBestTrade(src, dst, credits)
if trade and trade[1] > bestGainCr:
bestDst, bestLoad, bestGainCr = dst, trade[0], trade[1]
return bestDst, bestLoad, bestGainCr
Expand All @@ -122,7 +124,7 @@ def getBestHops(self, routes, credits, restrictTo=None):
continue
if unique and dst in route.route:
continue
trade = self.getProfits(src, dst, startCr)
trade = self.getBestTrade(src, dst, startCr)
if not trade:
continue
dstID = dst.ID
Expand Down

0 comments on commit ced8c73

Please sign in to comment.