Skip to content

Commit

Permalink
Merge branch 'master' of bitbucket.org:kfsone/tradedangerous
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Aug 18, 2014
2 parents e1ed24c + 30cd5e1 commit fad8efb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
6 changes: 2 additions & 4 deletions distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
# Import data from http://forums.frontier.co.uk/showthread.php?t=34824
# and ensure existing data is correct.

from tradedb import *

from collections import namedtuple
import math

from tradedb import *

class Star(object):
def __init__(self, name, x, y, z, links):
self.name, self.x, self.y, self.z, self.links = name, x, y, z, links
Expand Down Expand Up @@ -146,4 +145,3 @@ def __repr__(self):
if tdDist != dist:
print("%s -> %s is wrong: %.2f vs %.2f" % (star.name, dest.name, tdDist, dist))
tdb.query("UPDATE Links SET `distLy` = %.2f WHERE `from` = %d AND `to` = %d" % (dist, srcID, dstID)).commit()

10 changes: 4 additions & 6 deletions import.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
# would match the "dom. appliances" item (but only if you have selected
# the category at the moment)

import re # regex functionality
from tradedb import *

# Assume that we're going to allow unknown stars for pre-declarations
Expand Down Expand Up @@ -82,13 +81,12 @@ def addLinks(station, links):
def changeStation(line):
global tdb

station = None
matches = re.match(r'\s*(.*?)\s*/\s*(.*?)(:\s*(.*?))?\s*$', line)
if matches:
# Long format: system/station:links ...
sysName, stnName, links = matches.group(1), matches.group(2), matches.group(4)
if stnName == '*':
stnName = sysName.upper() + '*'
stnName = sysName.upper().join('*')
try:
station = tdb.getStation(stnName)
except LookupError:
Expand All @@ -99,7 +97,7 @@ def changeStation(line):
if links:
addLinks(station, links)
else:
# Short formnat: system/station name.
# Short format: system/station name.
station = tdb.getStation(line)

print("Station: ", station)
Expand Down Expand Up @@ -134,7 +132,7 @@ def parseItem(station, cat, line, uiOrder):


def main():
with open('import.txt', 'r') as f:
with open('import.txt') as f:
curStation = None
curCat = None
uiOrder = 0
Expand All @@ -157,7 +155,7 @@ def main():
curCat = changeCategory(line[1:])
uiOrder = 0
else:
if curStation == None or curCat == None:
if curStation is None or curCat is None:
raise ValueError("Expecting station and category before items: " + line)
uiOrder += 1
parseItem(curStation, curCat, line, uiOrder)
Expand Down
16 changes: 7 additions & 9 deletions trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
######################################################################
# Database and calculator modules.

from tradedb import TradeDB, Trade, Station
from tradedb import TradeDB
from tradecalc import Route, TradeCalc, localedNo

tdb = TradeDB('.\\TradeDangerous.accdb')
Expand Down Expand Up @@ -188,8 +188,8 @@ def parse_command_line():
if args.unique and args.hops >= len(tdb.stations):
raise ValueError("Requested unique trip with more hops than there are stations...")
if args.unique and ( \
(originStation and originStation == finalStation) or \
(originStation and originStation == viaStation) or \
(originStation and originStation == finalStation) or
(originStation and originStation == viaStation) or
(viaStation and viaStation == finalStation)):
raise ValueError("from/to/via repeat conflicts with --unique")

Expand Down Expand Up @@ -258,7 +258,7 @@ def doChecklist(route, credits):
print()

# If there is a next hop, describe how to get there.
note('Fly', "[%s]" % " -> ".join([ jump.str() for jump in jumps[idx] ]))
note('Fly' + "[%s]" % " -> ".join([ jump.str() for jump in jumps[idx] ]))
if idx < len(hops) and jumps[idx]:
for jump in jumps[idx][1:]:
stepNo = doStep(stepNo, 'Jump to', '%s' % (jump.str()))
Expand Down Expand Up @@ -293,10 +293,9 @@ def main():
for src in origins
if not (src in avoidStations or src.system in avoidSystems)
]
numHops = args.hops
numHops = args.hops
lastHop = numHops - 1
viaStartPos = 1 if originStation else 0
viaEndPos = -1 if finalStation else -2

if args.debug:
print("From %s via %s to %s with %d credits for %d hops" % (originName, viaName, destName, args.credits, numHops))
Expand All @@ -316,8 +315,8 @@ def main():
# Cull to routes that include the viaStation, might save us some calculations
routes = [ route for route in routes if viaStation in route.route[viaStartPos:] ]
routes = calc.getBestHops(routes, startCr,
restrictTo=restrictTo, avoidItems=avoidItems, avoidPlaces=avoidPlaces,
maxJumps=args.maxJumps, maxJumpsPer=args.maxJumpsPer, maxLyPer=args.maxLyPer)
restrictTo=restrictTo, avoidItems=avoidItems, avoidPlaces=avoidPlaces,
maxJumps=args.maxJumps, maxJumpsPer=args.maxJumpsPer, maxLyPer=args.maxLyPer)

# if viaStation:
# If the user doesn't specify start or end stations, expand the
Expand All @@ -344,4 +343,3 @@ def main():
main()
if mfd:
mfd.finish()

17 changes: 9 additions & 8 deletions tradecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _fit_combos(offset, cr, cap):
if combWeight == bestLoad.units:
if combCost >= bestLoad.costCr:
continue
bestLoad = TradeLoad(load.items+subLoad.items, load.gainCr+subLoad.gainCr, load.costCr+subLoad.costCr, load.units+subLoad.units)
bestLoad = TradeLoad(load.items + subLoad.items, load.gainCr + subLoad.gainCr, load.costCr + subLoad.costCr, load.units + subLoad.units)
return bestLoad

bestLoad = _fit_combos(0, credits, capacity)
Expand Down Expand Up @@ -152,8 +152,10 @@ def _fit_combos(offset, cr, cap):

return bestLoad

def getBestTrade(self, src, dst, credits, capacity=None, avoidItems=[], focusItems=[], fitFunction=None):
def getBestTrade(self, src, dst, credits, capacity=None, avoidItems=None, focusItems=None, fitFunction=None):
""" Find the most profitable trade between stations src and dst. """
if not focusItems: focusItems = []
if not avoidItems: avoidItems = []
if self.debug: print("# %s -> %s with %dcr" % (src, dst, credits))

if not dst in src.stations:
Expand Down Expand Up @@ -202,16 +204,15 @@ def getBestHopFrom(self, src, credits, capacity=None, maxJumps=None, maxLy=None,
hop = TradeHop(destSys=destSys, destStn=destStn, load=load.items, gainCr=load.gainCr, jumps=jumps, ly=ly)
return hop

def getBestHops(self,
routes, credits,
restrictTo=None, avoidItems=[], avoidPlaces=[],
maxJumps=None, maxLy=None, maxJumpsPer=None, maxLyPer=None
):
def getBestHops(self, routes, credits, restrictTo=None, avoidItems=None, avoidPlaces=None, maxJumps=None,
maxLy=None, maxJumpsPer=None, maxLyPer=None):
""" Given a list of routes, try all available next hops from each
route. Store the results by destination so that we pick the
best route-to-point for each destination at each step. If we
have two routes: A->B->D, A->C->D and A->B->D produces more
profit, then there is no point in continuing the A->C->D path. """
if not avoidPlaces: avoidPlaces = []
if not avoidItems: avoidItems = []

bestToDest = {}
safetyMargin = 1.0 - self.margin
Expand All @@ -228,7 +229,7 @@ def getBestHops(self,
if jumpLimit <= 0:
if self.debug: print("Jump Limit")
continue

for (destSys, destStn, jumps, ly) in src.getDestinations(maxJumps=jumpLimit, maxLy=maxLy, maxLyPer=maxLyPer, avoiding=avoidPlaces):
if self.debug:
print("#destSys = %s, destStn = %s, jumps = %s, ly = %s" % (destSys.str(), destStn, "->".join([jump.str() for jump in jumps]), ly))
Expand Down
6 changes: 3 additions & 3 deletions tradedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
######################################################################
# Imports

import sys # For sys.maxint
import re # Because irregular expressions are dull
import pypyodbc # Because its documentation was better
from queue import Queue # Because we're British.
Expand All @@ -31,7 +30,7 @@ def __init__(self, item, itemID, costCr, gainCr):


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


def __repr__(self):
Expand Down Expand Up @@ -117,7 +116,8 @@ def getDestinations(self, maxJumps=None, maxLy=None, maxLyPer=None, avoiding=[])

openList, closedList, destStations = Queue(), [sys for sys in avoiding if isinstance(sys, System)] + [self], []
openList.put([self.system, [], 0])
maxJumpDist = float(maxLyPer or sys.maxint)
# Sys is always available, so we don't need to import it. maxint was deprecated in favour of maxsize.
maxJumpDist = float(maxLyPer or sys.maxsize)
while not openList.empty():
(sys, jumps, dist) = openList.get()
if maxJumps and len(jumps) > maxJumps:
Expand Down

0 comments on commit fad8efb

Please sign in to comment.