def run(self, route, cr): tdb, mfd = self.tdb, self.mfd stations, hops, jumps = route.route, route.hops, route.jumps lastHopIdx = len(stations) - 1 gainCr = 0 self.stepNo = 0 heading = "(i) BEGINNING CHECKLIST FOR {} (i)".format(route.str()) print(heading, "\n", '-' * len(heading), "\n\n", sep='') cmdenv = self.cmdenv if cmdenv.detail: print(route.summary()) print() for idx in range(lastHopIdx): hopNo = idx + 1 cur, nxt, hop = stations[idx], stations[idx + 1], hops[idx] sortedTradeOptions = sorted( hop[0], key=lambda tradeOption: \ tradeOption[1] * tradeOption[0].gainCr, reverse=True ) # Tell them what they need to buy. if cmdenv.detail: self.note("HOP {} of {}".format(hopNo, lastHopIdx)) self.note("Buy at {}".format(cur.name())) for (trade, qty) in sortedTradeOptions: self.doStep( 'Buy {:n} x'.format(qty), trade.name(), '@ {}cr / {} old'.format( trade.costCr, describeAge(trade.srcAge), )) if cmdenv.detail: self.doStep('Refuel') print() # If there is a next hop, describe how to get there. self.note( "Fly {}" .format( " -> ".join(jump.name() for jump in jumps[idx]) ) ) if idx < len(hops) and jumps[idx]: for jump in jumps[idx][1:]: self.doStep('Jump to', jump.name()) if cmdenv.detail: self.doStep('Dock at', nxt.str()) print() self.note("Sell at {}".format(nxt.name())) for (trade, qty) in sortedTradeOptions: self.doStep( 'Sell {:n} x'.format(qty), trade.name(), '@ {:n}cr / {} old'.format( trade.costCr + trade.gainCr, describeAge(trade.dstAge), )) print() gainCr += hop[1] if cmdenv.detail and gainCr > 0: self.note("GAINED: {:n}cr, CREDITS: {:n}cr".format( gainCr, cr + gainCr)) if hopNo < lastHopIdx: print("\n--------------------------------------\n") if mfd: mfd.display('FINISHED', "+{:n}cr".format(gainCr), "={:n}cr".format(cr + gainCr)) mfd.attention(3) from time import sleep sleep(1.5)
def detail(self, tdenv): """ Return a string describing this route to a given level of detail. """ detail, goalSystem = tdenv.detail, tdenv.goalSystem credits = self.startCr + (tdenv.insurance or 0) gainCr = 0 route = self.route hops = self.hops # TODO: Write as a comprehension, just can't wrap my head # around it this morning. def genSubValues(): for hop in hops: for (tr, qty) in hop[0]: yield len(tr.name(detail)) longestNameLen = max(genSubValues()) text = self.str() if detail >= 1: text += " (score: {:f})".format(self.score) text += "\n" jumpsFmt = (" Jump {jumps}\n") cruiseFmt = (" Supercruise to {stn}\n") distFmt = None if detail > 1: if detail > 2: text += self.summary() + "\n" if tdenv.maxJumpsPer > 1: distFmt = ( " Direct: {dist:0.2f}ly, Trip: {trav:0.2f}ly\n") hopFmt = " Load from {station}:\n{purchases}" hopStepFmt = (" {qty:>4} x {item:<{longestName}} " "{eacost:>8n}cr vs {easell:>8n}cr, " "{age}") if detail > 2: hopStepFmt += ", total: {ttlcost:>10n}cr" hopStepFmt += "\n" if not tdenv.summary: dockFmt = (" Unload at {station} => Gain {gain:n}cr " "({tongain:n}cr/ton) => {credits:n}cr\n") else: jumpsFmt = re.sub(" ", " ", jumpsFmt, re.M) cruiseFmt = re.sub(" ", " ", cruiseFmt, re.M) if distFmt: distFmt = re.sub(" ", " ", distFmt, re.M) hopFmt = "\n" + hopFmt dockFmt = " Expect to gain {gain:n}cr ({tongain:n}cr/ton)\n" footer = ' ' + '-' * 76 + "\n" endFmt = ("Finish at {station} " "gaining {gain:n}cr ({tongain:n}cr/ton) " "=> est {credits:n}cr total\n") elif detail: hopFmt = " Load from {station}:{purchases}\n" hopStepFmt = " {qty} x {item} (@{eacost}cr)," footer = None dockFmt = " Dock at {station}\n" endFmt = (" Finish {station} " "+ {gain:n}cr ({tongain:n}cr/ton)" "=> {credits:n}cr\n") else: hopFmt = " {station}:{purchases}\n" hopStepFmt = " {qty} x {item}," footer = None dockFmt = None endFmt = " {station} +{gain:n}cr ({tongain:n}/ton)" def jumpList(jumps): text, last = "", None travelled = 0. for jump in jumps: if last: dist = last.distanceTo(jump) if dist: if tdenv.detail: text += ", {:.2f}ly -> ".format(dist) else: text += " -> " else: text += " >>> " travelled += dist text += jump.name() last = jump return travelled, text if detail > 1: def decorateStation(station): details = [] if station.lsFromStar: details.append(station.distFromStar(True)) if station.blackMarket != '?': details.append('BMk:' + station.blackMarket) if station.maxPadSize != '?': details.append('Pad:' + station.maxPadSize) if station.planetary != '?': details.append('Plt:' + station.planetary) if station.shipyard != '?': details.append('Shp:' + station.shipyard) if station.outfitting != '?': details.append('Out:' + station.outfitting) if station.refuel != '?': details.append('Ref:' + station.refuel) details = "{} ({})".format( station.name(), ", ".join(details or ["no details"])) return details else: def decorateStation(station): return station.name() if detail and goalSystem: def goalDistance(station): return " [Distance to {}: {:.2f} ly]\n".format( goalSystem.name(), station.system.distanceTo(goalSystem), ) else: def goalDistance(station): return "" for i, hop in enumerate(hops): hopGainCr, hopTonnes = hop[1], 0 purchases = "" for (trade, qty) in sorted( hop[0], key=lambda tradeOpt: tradeOpt[1] * tradeOpt[0].gainCr, reverse=True): # Are they within 30 minutes of each other? if abs(trade.srcAge - trade.dstAge) <= (30 * 60): age = max(trade.srcAge, trade.dstAge) age = describeAge(age) else: srcAge = describeAge(trade.srcAge) dstAge = describeAge(trade.dstAge) age = "{} vs {}".format(srcAge, dstAge) purchases += hopStepFmt.format( qty=qty, item=trade.name(detail), eacost=trade.costCr, easell=trade.costCr + trade.gainCr, ttlcost=trade.costCr * qty, longestName=longestNameLen, age=age, ) hopTonnes += qty text += goalDistance(route[i]) text += hopFmt.format(station=decorateStation(route[i]), purchases=purchases) if tdenv.showJumps and jumpsFmt and self.jumps[i]: startStn = route[i] endStn = route[i + 1] if startStn.system is not endStn.system: fmt = jumpsFmt travelled, jumps = jumpList(self.jumps[i]) else: fmt = cruiseFmt travelled, jumps = 0., "{start} >>> {stop}".format( start=startStn.name(), stop=endStn.name()) text += fmt.format(jumps=jumps, gain=hopGainCr, tongain=hopGainCr / hopTonnes, credits=credits + gainCr + hopGainCr, stn=route[i + 1].dbname) if travelled and distFmt and len(self.jumps[i]) > 2: text += distFmt.format( dist=startStn.system.distanceTo(endStn.system), trav=travelled, ) if dockFmt: stn = route[i + 1] stnName = stn.name() text += dockFmt.format(station=decorateStation(stn), gain=hopGainCr, tongain=hopGainCr / hopTonnes, credits=credits + gainCr + hopGainCr) gainCr += hopGainCr lastStation = self.lastStation if lastStation.system is not goalSystem: text += goalDistance(lastStation) text += footer or "" text += endFmt.format(station=decorateStation(lastStation), gain=gainCr, credits=credits + gainCr, tongain=self.gpt) return text
def detail(self, tdenv): """ Return a string describing this route to a given level of detail. """ detail, goalSystem = tdenv.detail, tdenv.goalSystem credits = self.startCr + (tdenv.insurance or 0) gainCr = 0 route = self.route hops = self.hops # TODO: Write as a comprehension, just can't wrap my head # around it this morning. def genSubValues(): for hop in hops: for (tr, qty) in hop[0]: yield len(tr.name()) longestNameLen = max(genSubValues()) text = self.str() if detail >= 1: text += " (score: {:f})".format(self.score) text += "\n" jumpsFmt = (" Jump {jumps}\n") cruiseFmt = (" Supercruise to {stn}\n") distFmt = None if detail > 1: if detail > 2: text += self.summary() + "\n" if tdenv.maxJumpsPer > 1: distFmt = ( " Direct: {dist:0.2f}ly, Trip: {trav:0.2f}ly\n" ) hopFmt = " Load from {station}:\n{purchases}" hopStepFmt = ( " {qty:>4} x {item:<{longestName}} " "{eacost:>8n}cr vs {easell:>8n}cr, " "{age}" ) if detail > 2: hopStepFmt += ", total: {ttlcost:>10n}cr" hopStepFmt += "\n" if not tdenv.summary: dockFmt = ( " Unload at {station} => Gain {gain:n}cr " "({tongain:n}cr/ton) => {credits:n}cr\n" ) else: jumpsFmt = re.sub(" ", " ", jumpsFmt, re.M) cruiseFmt = re.sub(" ", " ", cruiseFmt, re.M) if distFmt: distFmt = re.sub(" ", " ", distFmt, re.M) hopFmt = "\n" + hopFmt dockFmt = " Expect to gain {gain:n}cr ({tongain:n}cr/ton)\n" footer = ' ' + '-' * 76 + "\n" endFmt = ( "Finish at {station} " "gaining {gain:n}cr ({tongain:n}cr/ton) " "=> est {credits:n}cr total\n" ) elif detail: hopFmt = " Load from {station}:{purchases}\n" hopStepFmt = " {qty} x {item} (@{eacost}cr)," footer = None dockFmt = " Dock at {station}\n" endFmt = ( " Finish {station} " "+ {gain:n}cr ({tongain:n}cr/ton)" "=> {credits:n}cr\n" ) else: hopFmt = " {station}:{purchases}\n" hopStepFmt = " {qty} x {item}," footer = None dockFmt = None endFmt = " {station} +{gain:n}cr ({tongain:n}/ton)" def jumpList(jumps): text, last = "", None travelled = 0. for jump in jumps: if last: dist = last.distanceTo(jump) if dist: if tdenv.detail: text += ", {:.2f}ly -> ".format(dist) else: text += " -> " else: text += " >>> " travelled += dist text += jump.name() last = jump return travelled, text if detail > 1: def decorateStation(station): details = [] if station.lsFromStar: details.append(station.distFromStar(True)) if station.blackMarket != '?': details.append('BMk:'+station.blackMarket) if station.maxPadSize != '?': details.append('Pad:'+station.maxPadSize) if station.shipyard != '?': details.append('Shp:'+station.shipyard) if station.outfitting != '?': details.append('Out:'+station.outfitting) if station.refuel != '?': details.append('Ref:'+station.refuel) details = "{} ({})".format( station.name(), ", ".join(details or ["no details"]) ) return details else: def decorateStation(station): return station.name() if detail and goalSystem: def goalDistance(station): return " [Distance to {}: {:.2f} ly]\n".format( goalSystem.name(), station.system.distanceTo(goalSystem), ) else: def goalDistance(station): return "" for i, hop in enumerate(hops): hopGainCr, hopTonnes = hop[1], 0 purchases = "" for (trade, qty) in sorted( hop[0], key=lambda tradeOpt: tradeOpt[1] * tradeOpt[0].gainCr, reverse=True ): # Are they within 30 minutes of each other? if abs(trade.srcAge - trade.dstAge) <= (30*60): age = max(trade.srcAge, trade.dstAge) age = describeAge(age) else: srcAge = describeAge(trade.srcAge) dstAge = describeAge(trade.dstAge) age = "{} vs {}".format(srcAge, dstAge) purchases += hopStepFmt.format( qty=qty, item=trade.name(), eacost=trade.costCr, easell=trade.costCr + trade.gainCr, ttlcost=trade.costCr*qty, longestName=longestNameLen, age=age, ) hopTonnes += qty text += goalDistance(route[i]) text += hopFmt.format( station=decorateStation(route[i]), purchases=purchases ) if tdenv.showJumps and jumpsFmt and self.jumps[i]: startStn = route[i] endStn = route[i+1] if startStn.system is not endStn.system: fmt = jumpsFmt travelled, jumps = jumpList(self.jumps[i]) else: fmt = cruiseFmt travelled, jumps = 0., "{start} >>> {stop}".format( start=startStn.name(), stop=endStn.name() ) text += fmt.format( jumps=jumps, gain=hopGainCr, tongain=hopGainCr / hopTonnes, credits=credits + gainCr + hopGainCr, stn=route[i+1].dbname ) if travelled and distFmt and len(self.jumps[i]) > 2: text += distFmt.format( dist=startStn.system.distanceTo(endStn.system), trav=travelled, ) if dockFmt: stn = route[i+1] stnName = stn.name() text += dockFmt.format( station=decorateStation(stn), gain=hopGainCr, tongain=hopGainCr / hopTonnes, credits=credits + gainCr + hopGainCr ) gainCr += hopGainCr lastStation = self.lastStation if lastStation.system is not goalSystem: text += goalDistance(lastStation) text += footer or "" text += endFmt.format( station=decorateStation(lastStation), gain=gainCr, credits=credits + gainCr, tongain=self.gpt ) return text