Skip to content

Commit

Permalink
Merged kfsone/tradedangerous into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maddavo committed Dec 16, 2014
2 parents 2139508 + cdb4b51 commit 8b37266
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

v6.2.2 [wip]
. (kfsone) "--check --mfd" should now work if you have 64-bit drivers
. (kfsone) Added "--max-days" to run command
+ Stations, ships: kfsone

Expand Down
80 changes: 62 additions & 18 deletions commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ def validateRunArguments(tdb, cmdenv):
raise CommandLineError("Same to/from; more than one hop required.")

viaSet = cmdenv.viaSet = set(cmdenv.viaPlaces)
cmdenv.DEBUG0("Via: {}", viaSet)
viaSystems = set()
for place in viaSet:
if isinstance(place, Station):
Expand All @@ -345,6 +346,18 @@ def validateRunArguments(tdb, cmdenv):
else:
viaSystems.add(place)

avoids = cmdenv.avoidPlaces or []
for via in viaSet:
if isinstance(via, Station):
conflict = (via in avoids or via.system in avoids)
else:
conflict = (via in avoids)
if conflict:
raise CommandLineError(
"Via {} conflicts with avoid list".format(
via
))

# How many of the hops do not have pre-determined stations. For example,
# when the user uses "--from", they pre-determine the starting station.
fixedRoutePoints = 0
Expand Down Expand Up @@ -413,26 +426,55 @@ def validateRunArguments(tdb, cmdenv):
if startStn:
tdb.loadStationTrades([startStn.ID])
if stopStn and cmdenv.hops == 1 and not stopStn in startStn.tradingWith:
raise CommandLineError("No profitable items found between {} and {}".format(
startStn.name(), stopStn.name()))
raise CommandLineError(
"No profitable items found between {} and {}".format(
startStn.name(), stopStn.name()))
if len(startStn.tradingWith) == 0:
raise NoDataError("No data found for potential buyers for items from {}.".format(
startStn.name()))
raise NoDataError(
"No data found for potential buyers for items from {}.".format(
startStn.name()))


######################################################################


def filterByVia(routes, viaSet, viaStartPos):
matchedRoutes = list(routes)
if not routes:
return []

matchedRoutes = []
partialRoutes = {}
maxMet = 0
for route in routes:
met = 0
for hop in route.route[viaStartPos:]:
if hop in viaSet or hop.system in viaSet:
met += 1
if met >= len(viaSet):
matchedRoutes.append(route)
return matchedRoutes
if met > 0:
if met >= len(viaSet):
matchedRoutes.append(route)
else:
if met > maxMet:
partialRoutes[met] = []
if met >= maxMet:
maxMet = met
partialRoutes[met].append(route)

if matchedRoutes:
return matchedRoutes, None

if not maxMet:
raise NoDataError(
"No routes were found which matched your 'via' selections."
)

return partialRoutes[maxMet], (
"No runs visited all of your via destinations. "
"Listing runs that matched at least {}.".format(
maxMet
)
)


######################################################################
# Perform query and populate result set
Expand Down Expand Up @@ -501,36 +543,38 @@ def run(results, cmdenv, tdb):
restrictTo = None
if hopNo == lastHop and stopStations:
restrictTo = stopStations
### TODO:
### if hopsLeft < len(viaSet):
### ... only keep routes that include len(viaSet)-hopsLeft routes
### Thus: If you specify 5 hops via a,b,c and we are on hop 3, only keep
### routes that include a, b or c. On hop 4, only include routes that
### already include 2 of the vias, on hop 5, require all 3.
if viaSet:
routes = filterByVia(routes, viaSet, viaStartPos)
elif len(viaSet) > cmdenv.adhocHops:
restrictTo = viaSet

routes = calc.getBestHops(routes, restrictTo=restrictTo)

if viaSet:
routes = filterByVia(routes, viaSet, viaStartPos)
results.summary = ResultRow()
results.summary.exception = None

if not routes:
raise NoDataError("No profitable trades matched your critera, or price data along the route is missing.")

if viaSet:
routes, caution = filterByVia(routes, viaSet, viaStartPos)
if caution:
results.summary.exception = caution

routes.sort()
results.data = routes

return results


######################################################################
# Transform result set into output

def render(results, cmdenv, tdb):
from formatting import RowFormat, ColumnFormat

exception = results.summary.exception
if exception:
print("\aCAUTION: {}\n".format(exception))

routes = results.data

for i in range(0, min(len(routes), cmdenv.routes)):
Expand Down
2 changes: 1 addition & 1 deletion commands/update_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def validate(self, widget):
# It seems that stations usually pay within 25% of the
# asking price as a buy-back price. If the user gives
# us something out of those bounds, check with them.
if paying < asking * 0.75 or \
if paying < int(asking * 0.75) or \
paying < asking - 200:
widget.bell()
ok = mbox.askokcancel(
Expand Down
5 changes: 5 additions & 0 deletions data/ShipVendor.csv
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ unq:[email protected]_id,unq:[email protected]_id,unq:[email protected]_id,cos
'HIP 87369','Carey Port','Lakon Type 7',17472252
'HIP 87369','Carey Port','Sidewinder',32000
'HIP 87369','Carey Port','Viper',142931
'Hu Jona','Russell Station','Eagle',44800
'Hu Jona','Russell Station','Hauler',52720
'Hu Jona','Russell Station','Lakon Type 6',1045945
'Hu Jona','Russell Station','Lakon Type 7',17472252
'Hu Jona','Russell Station','Sidewinder',32000
'LFT 1448','Dirac Enterprise','Anaconda',146969451
'LFT 1448','Dirac Enterprise','Asp Explorer',6661153
'LFT 1448','Dirac Enterprise','Cobra',379718
Expand Down
11 changes: 11 additions & 0 deletions data/Station.csv
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,15 @@ unq:[email protected]_id,unq:name,ls_from_star
'HR 8170','Fiennes Vision',0.0
'Hreintania','Kozeyev Gateway',0.0
'Hsien Baji','Novitski Mines',0.0
'Hu Jona','Crichton Station',16340.0
'Hu Jona','Crown Dock',1998.0
'Hu Jona','Hyecho Terminal',173.0
'Hu Jona','Kovalevskaya Dock',567.0
'Hu Jona','Pratchett Station',2000.0
'Hu Jona','Roddenberry Orbital',791.0
'Hu Jona','Rowley Terminal',160.0
'Hu Jona','Russell Station',90.8
'Hu Jona','Zoline Station',254.0
'Huokang','Solovyev City',0.0
'Hyperion','Ivens Ring',0.0
'i Bootis','Chango Dock',0.0
Expand Down Expand Up @@ -1197,6 +1206,8 @@ unq:[email protected]_id,unq:name,ls_from_star
'V2689 Orionis','Capek Ring',0.0
'V2689 Orionis','Hodgson Dock',0.0
'V2689 Orionis','Wyndham Ring',0.0
'V401 Hydrae','Hinz Terminal',5828.0
'V401 Hydrae','Swanwick Terminal',3837.0
'V417 Hydrae','Clervoy Vista',0.0
'V538 Aurigae','Card Park',0.0
'V749 Herculis','Pauling Gateway',0.0
Expand Down
19 changes: 15 additions & 4 deletions mfd/saitek/directoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@

import ctypes
import ctypes.wintypes

import logging
import os
import platform

S_OK = 0x00000000
E_HANDLE = 0x80070006
Expand Down Expand Up @@ -311,23 +312,33 @@ def __repr__(self):
direct_output = None
debug_level = 0

def __init__(self, dll_path="C:\\Program Files (x86)\\Saitek\\DirectOutput\\DirectOutput.dll", debug_level=0, name=None):
def __init__(self, debug_level=0, name=None):
"""
Initialises device, creates internal state (device_handle) and registers callbacks.
"""

logging.info("DirectOutputDevice.__init__")

prog_dir = os.environ["ProgramFiles"]
if platform.machine().endswith('86'):
# 32-bit machine, nothing to worry about
pass
elif platform.machine().endswith('64'):
# 64-bit machine, are we a 32-bit python?
if platform.architecture()[0] == '32bit':
prog_dir = os.environ["ProgramFiles(x86)"]
dll_path = os.path.join(prog_dir, "Saitek\\DirectOutput\\DirectOutput.dll")

self.application_name = name or DirectOutputDevice.application_name
self.debug_level = debug_level

try:
logging.debug("DirectOutputDevice -> DirectOutput")
logging.debug("DirectOutputDevice -> DirectOutput: {}".format(dll_path))
self.direct_output = DirectOutput(dll_path)
logging.debug("direct_output = {}".format(self.direct_output))
except WindowsError as e:
logging.warning("DLLError {}".format(e.winerror))
logging.warning("DLLError: {}: {}".format(dll_path, e.winerror))
raise DLLError(e.winerror) from None

result = self.direct_output.Initialize(self.application_name)
Expand Down
2 changes: 1 addition & 1 deletion scripts/tdbuyfrom
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ then
exit 1
fi

cmd="\"${TRADEPY}\" buy --near $near $prod $*"
cmd="\"${TRADEPY}\" buy --age -vv --near $near $prod $*"
echo \$ $cmd
eval "$cmd"

0 comments on commit 8b37266

Please sign in to comment.