flexible.py 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  1. from twisted.internet import reactor
  2. from twisted.internet import task
  3. from twisted.internet import defer
  4. from colorama import Fore, Back, Style
  5. # from twisted.python import log
  6. from twisted.internet.task import coiterate
  7. from twisted.internet.defer import gatherResults
  8. from itertools import cycle
  9. import pendulum
  10. from pprint import pformat
  11. from galaxy import GameData, PORT_CLASSES, CLASSES_PORT
  12. from boxes import Boxes
  13. import logging
  14. log = logging.getLogger(__name__)
  15. class SpinningCursor(object):
  16. """ Spinner class, that handles every so many clicks
  17. s = SpinningCursor(5) # every 5
  18. for x in range(10):
  19. if s.click():
  20. print(s.cycle())
  21. """
  22. def __init__(self, every=10):
  23. self.itercycle = cycle(["/", "-", "\\", "|"])
  24. self.count = 0
  25. self.every = every
  26. def reset(self):
  27. self.itercycle = cycle(["/", "-", "\\", "|"])
  28. self.count = 0
  29. def click(self):
  30. self.count += 1
  31. return self.count % self.every == 0
  32. def cycle(self):
  33. return next(self.itercycle)
  34. def merge(color_string):
  35. """ Given a string of colorama ANSI, merge them if you can. """
  36. return color_string.replace("m\x1b[", ";")
  37. class PlayerInput(object):
  38. """ Player Input
  39. Example:
  40. from flexible import PlayerInput
  41. ask = PlayerInput(self.game)
  42. # abort_blank means, if the input field is blank, abort. Use error_back.
  43. d = ask.prompt("What is your quest?", 40, name="quest", abort_blank=True)
  44. # Display the user's input / but not needed.
  45. d.addCallback(ask.output)
  46. d.addCallback(
  47. lambda ignore: ask.prompt(
  48. "What is your favorite color?", 10, name="color"
  49. )
  50. )
  51. d.addCallback(ask.output)
  52. d.addCallback(
  53. lambda ignore: ask.prompt(
  54. "What is your least favorite number?",
  55. 12,
  56. name="number",
  57. digits=True,
  58. )
  59. )
  60. d.addCallback(ask.output)
  61. def show_values(show):
  62. log.debug(show)
  63. self.queue_game.put(pformat(show).replace("\n", "\n\r") + self.nl)
  64. d.addCallback(lambda ignore: show_values(ask.keep))
  65. d.addCallback(self.welcome_back)
  66. # On error, just return back
  67. d.addErrback(self.welcome_back)
  68. """
  69. def __init__(self, game):
  70. # I think game gives us access to everything we need
  71. self.game = game
  72. self.observer = self.game.observer
  73. self.save = None
  74. self.deferred = None
  75. self.queue_game = game.queue_game
  76. self.keep = {}
  77. # default colors
  78. self.c = merge(Style.BRIGHT + Fore.WHITE + Back.BLUE)
  79. self.cp = merge(Style.BRIGHT + Fore.YELLOW + Back.BLUE)
  80. # useful constants
  81. self.r = Style.RESET_ALL
  82. self.nl = "\n\r"
  83. self.bsb = "\b \b"
  84. self.keepalive = None
  85. def color(self, c):
  86. self.c = c
  87. def colorp(self, cp):
  88. self.cp = cp
  89. def alive(self):
  90. log.debug("PlayerInput.alive()")
  91. self.game.queue_player.put(" ")
  92. def prompt(self, user_prompt, limit, **kw):
  93. """ Generate prompt for user input.
  94. Note: This returns deferred.
  95. prompt = text displayed.
  96. limit = # of characters allowed.
  97. default = (text to default to)
  98. keywords:
  99. abort_blank : Abort if they give us blank text.
  100. name : Stores the input in self.keep dict.
  101. digits : Only allow 0-9 to be entered.
  102. """
  103. log.debug("PlayerInput({0}, {1}, {2}".format(user_prompt, limit, kw))
  104. self.limit = limit
  105. self.input = ""
  106. self.kw = kw
  107. assert self.save is None
  108. assert self.keepalive is None
  109. # Note: This clears out the server "keep alive"
  110. self.save = self.observer.save()
  111. self.observer.connect("player", self.get_input)
  112. self.keepalive = task.LoopingCall(self.alive)
  113. self.keepalive.start(30)
  114. # We need to "hide" the game output.
  115. # Otherwise it WITH mess up the user input display.
  116. self.to_player = self.game.to_player
  117. self.game.to_player = False
  118. # Display prompt
  119. # self.queue_game.put(self.r + self.nl + self.c + user_prompt + " " + self.cp)
  120. self.queue_game.put(self.r + self.c + user_prompt + self.r + " " + self.cp)
  121. # Set "Background of prompt"
  122. self.queue_game.put(" " * limit + "\b" * limit)
  123. assert self.deferred is None
  124. d = defer.Deferred()
  125. self.deferred = d
  126. log.debug("Return deferred ...")
  127. return d
  128. def get_input(self, chunk):
  129. """ Data from player (in bytes) """
  130. chunk = chunk.decode("latin-1", "ignore")
  131. for ch in chunk:
  132. if ch == "\b":
  133. if len(self.input) > 0:
  134. self.queue_game.put(self.bsb)
  135. self.input = self.input[0:-1]
  136. else:
  137. self.queue_game.put("\a")
  138. elif ch == "\r":
  139. self.queue_game.put(self.r + self.nl)
  140. log.debug("Restore observer dispatch {0}".format(self.save))
  141. assert not self.save is None
  142. self.observer.load(self.save)
  143. self.save = None
  144. log.debug("Disable keepalive")
  145. self.keepalive.stop()
  146. self.keepalive = None
  147. line = self.input
  148. self.input = ""
  149. assert not self.deferred is None
  150. self.game.to_player = self.to_player
  151. # If they gave us the keyword name, save the value as that name
  152. if "name" in self.kw:
  153. self.keep[self.kw["name"]] = line
  154. if "abort_blank" in self.kw and self.kw["abort_blank"]:
  155. # Abort on blank input
  156. if line.strip() == "":
  157. # Yes, input is blank, abort.
  158. log.info("errback, abort_blank")
  159. reactor.callLater(
  160. 0, self.deferred.errback, Exception("abort_blank")
  161. )
  162. self.deferred = None
  163. return
  164. # Ok, use deferred.callback, or reactor.callLater?
  165. # self.deferred.callback(line)
  166. reactor.callLater(0, self.deferred.callback, line)
  167. self.deferred = None
  168. return
  169. elif ch.isprintable():
  170. # Printable, but is it acceptable?
  171. if "digits" in self.kw:
  172. if not ch.isdigit():
  173. self.queue_game.put("\a")
  174. continue
  175. if len(self.input) + 1 <= self.limit:
  176. self.input += ch
  177. self.queue_game.put(ch)
  178. else:
  179. self.queue_game.put("\a")
  180. def output(self, line):
  181. """ A default display of what they just input. """
  182. log.debug("PlayerInput.output({0})".format(line))
  183. self.game.queue_game.put(self.r + "[{0}]".format(line) + self.nl)
  184. return line
  185. import re
  186. # The CIMWarpReport -- is only needed if the json file gets damaged in some way.
  187. # or needs to be reset. The warps should automatically update themselves now.
  188. class CIMWarpReport(object):
  189. def __init__(self, game):
  190. self.game = game
  191. self.queue_game = game.queue_game
  192. self.queue_player = game.queue_player
  193. self.observer = game.observer
  194. # Yes, at this point we would activate
  195. self.prompt = game.buffer
  196. self.save = self.observer.save()
  197. # I actually don't want the player input, but I'll grab it anyway.
  198. self.observer.connect("player", self.player)
  199. self.observer.connect("prompt", self.game_prompt)
  200. self.observer.connect("game-line", self.game_line)
  201. # If we want it, it's here.
  202. self.defer = None
  203. self.to_player = self.game.to_player
  204. # Hide what's happening from the player
  205. self.game.to_player = False
  206. self.queue_player.put("^") # Activate CIM
  207. self.state = 1
  208. # self.warpdata = {}
  209. self.queue_game.put("Loading ... ") # cycle eats last char.
  210. self.warpcycle = SpinningCursor()
  211. def game_prompt(self, prompt):
  212. if prompt == ": ":
  213. if self.state == 1:
  214. # Ok, then we're ready to request the port report
  215. self.warpcycle.reset()
  216. self.queue_player.put("I")
  217. self.state = 2
  218. elif self.state == 2:
  219. self.queue_player.put("Q")
  220. self.state = 3
  221. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  222. if self.state == 3:
  223. # Ok, time to exit
  224. # exit from this...
  225. self.game.to_player = self.to_player
  226. self.observer.load(self.save)
  227. self.save = None
  228. # self.game.warpdata = self.warpdata
  229. self.queue_game.put("\b \b\r\n")
  230. if not self.defer is None:
  231. self.defer.callback(self.game.gamedata.warps)
  232. self.defer = None
  233. def game_line(self, line):
  234. if line == "" or line == ": ":
  235. return
  236. if line == ": ENDINTERROG":
  237. return
  238. if line.startswith('Command [TL='):
  239. return
  240. # This should be the CIM Report Data -- parse it
  241. if self.warpcycle:
  242. if self.warpcycle.click():
  243. self.queue_game.put("\b" + self.warpcycle.cycle())
  244. work = line.strip()
  245. parts = re.split(r"(?<=\d)\s", work)
  246. parts = [int(x) for x in parts]
  247. sector = parts.pop(0)
  248. # tuples are nicer on memory, and the warpdata map isn't going to be changing.
  249. # self.warpdata[sector] = tuple(parts)
  250. self.game.gamedata.warp_to(sector, *parts)
  251. def __del__(self):
  252. log.debug("CIMWarpReport {0} RIP".format(self))
  253. def whenDone(self):
  254. self.defer = defer.Deferred()
  255. # Call this to chain something after we exit.
  256. return self.defer
  257. def player(self, chunk):
  258. """ Data from player (in bytes). """
  259. chunk = chunk.decode("latin-1", "ignore")
  260. key = chunk.upper()
  261. log.warn("CIMWarpReport.player({0}) : I AM stopping...".format(key))
  262. # Stop the keepalive if we are activating something else
  263. # or leaving...
  264. # self.keepalive.stop()
  265. self.queue_game.put("\b \b\r\n")
  266. if not self.defer is None:
  267. # We have something, so:
  268. self.game.to_player = self.to_player
  269. self.observer.load(self.save)
  270. self.save = None
  271. self.defer.errback(Exception("User Abort"))
  272. self.defer = None
  273. else:
  274. # Still "exit" out.
  275. self.game.to_player = self.to_player
  276. self.observer.load(self.save)
  277. # the CIMPortReport will still be needed.
  278. # We can't get fresh report data (that changes) any other way.
  279. class CIMPortReport(object):
  280. """ Parse data from CIM Port Report
  281. Example:
  282. from flexible import CIMPortReport
  283. report = CIMPortReport(self.game)
  284. d = report.whenDone()
  285. d.addCallback(self.port_report)
  286. d.addErrback(self.welcome_back)
  287. def port_report(self, portdata):
  288. self.portdata = portdata
  289. self.queue_game.put("Loaded {0} records.".format(len(portdata)) + self.nl)
  290. self.welcome_back()
  291. def welcome_back(self,*_):
  292. ... restore keep alive timers, etc.
  293. """
  294. def __init__(self, game):
  295. self.game = game
  296. self.queue_game = game.queue_game
  297. self.queue_player = game.queue_player
  298. self.observer = game.observer
  299. # Yes, at this point we would activate
  300. self.prompt = game.buffer
  301. self.save = self.observer.save()
  302. # I actually don't want the player input, but I'll grab it anyway.
  303. self.observer.connect("player", self.player)
  304. self.observer.connect("prompt", self.game_prompt)
  305. self.observer.connect("game-line", self.game_line)
  306. # If we want it, it's here.
  307. self.defer = None
  308. self.to_player = self.game.to_player
  309. log.debug("to_player (stored) {0}".format(self.to_player))
  310. # Hide what's happening from the player
  311. self.game.to_player = False
  312. self.queue_player.put("^") # Activate CIM
  313. self.state = 1
  314. # self.portdata = {}
  315. self.queue_game.put("Loading ... ") # cycle eats last char.
  316. self.portcycle = SpinningCursor()
  317. def game_prompt(self, prompt):
  318. if prompt == ": ":
  319. if self.state == 1:
  320. # Ok, then we're ready to request the port report
  321. self.portcycle.reset()
  322. self.queue_player.put("R")
  323. self.state = 2
  324. elif self.state == 2:
  325. self.queue_player.put("Q")
  326. self.state = 3
  327. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  328. if self.state == 3:
  329. # Ok, time to exit
  330. # exit from this...
  331. self.game.to_player = self.to_player
  332. self.observer.load(self.save)
  333. self.save = None
  334. # self.game.portdata = self.portdata
  335. self.queue_game.put("\b \b\r\n")
  336. if not self.defer is None:
  337. self.defer.callback(self.game.gamedata.ports)
  338. self.defer = None
  339. def game_line(self, line: str):
  340. if line == "" or line == ": ":
  341. return
  342. if line == ": ENDINTERROG":
  343. return
  344. # This should be the CIM Report Data -- parse it
  345. if self.portcycle:
  346. if self.portcycle.click():
  347. self.queue_game.put("\b" + self.portcycle.cycle())
  348. work = line.replace("%", "")
  349. parts = re.split(r"(?<=\d)\s", work)
  350. if len(parts) == 8:
  351. port = int(parts[0].strip())
  352. data = dict()
  353. def portBS(info):
  354. if info[0] == "-":
  355. bs = "B"
  356. else:
  357. bs = "S"
  358. return (bs, int(info[1:].strip()))
  359. data["fuel"] = dict()
  360. data["fuel"]["sale"], data["fuel"]["units"] = portBS(parts[1])
  361. data["fuel"]["pct"] = int(parts[2].strip())
  362. data["org"] = dict()
  363. data["org"]["sale"], data["org"]["units"] = portBS(parts[3])
  364. data["org"]["pct"] = int(parts[4].strip())
  365. data["equ"] = dict()
  366. data["equ"]["sale"], data["equ"]["units"] = portBS(parts[5])
  367. data["equ"]["pct"] = int(parts[6].strip())
  368. # Store what this port is buying/selling
  369. data["port"] = (
  370. data["fuel"]["sale"] + data["org"]["sale"] + data["equ"]["sale"]
  371. )
  372. # Convert BBS/SBB to Class number 1-8
  373. data["class"] = CLASSES_PORT[data["port"]]
  374. self.game.gamedata.set_port(port, data)
  375. # self.portdata[port] = data
  376. else:
  377. log.error("CIMPortReport: {0} ???".format(line))
  378. def __del__(self):
  379. log.debug("CIMPortReport {0} RIP".format(self))
  380. def whenDone(self):
  381. self.defer = defer.Deferred()
  382. # Call this to chain something after we exit.
  383. return self.defer
  384. def player(self, chunk):
  385. """ Data from player (in bytes). """
  386. chunk = chunk.decode("latin-1", "ignore")
  387. key = chunk.upper()
  388. log.warn("CIMPortReport.player({0}) : I AM stopping...".format(key))
  389. # Stop the keepalive if we are activating something else
  390. # or leaving...
  391. # self.keepalive.stop()
  392. self.queue_game.put("\b \b\r\n")
  393. if not self.defer is None:
  394. # We have something, so:
  395. self.game.to_player = self.to_player
  396. self.observer.load(self.save)
  397. self.save = None
  398. self.defer.errback(Exception("User Abort"))
  399. self.defer = None
  400. else:
  401. # Still "exit" out.
  402. self.game.to_player = self.to_player
  403. self.observer.load(self.save)
  404. class ScriptPort(object):
  405. """ Performs the Port script.
  406. This is close to the original.
  407. We don't ask for the port to trade with --
  408. because that information is available to us after "D" (display).
  409. We look at the adjacent sectors, and see if we know any ports.
  410. If the ports are burnt (< 20%), we remove them from the list.
  411. We sort the best trades first.
  412. If there's just one, we use it. Otherwise we ask them to choose.
  413. We have options Trade_UseFirst, which uses the first one, if
  414. there is more then one.
  415. Option Trade_Turns, will use this as default turns, without
  416. asking.
  417. """
  418. def __init__(self, game):
  419. self.game = game
  420. self.queue_game = game.queue_game
  421. self.queue_player = game.queue_player
  422. self.observer = game.observer
  423. self.r = Style.RESET_ALL
  424. self.nl = "\n\r"
  425. self.this_sector = None # Starting sector
  426. self.sector1 = None # Current Sector
  427. self.sector2 = None # Next Sector Stop
  428. self.percent = self.game.gamedata.get_config('Trade_Percent', '5')
  429. self.stop = self.game.gamedata.get_config('Trade_Stop', '10')
  430. try:
  431. self.stop = int(self.stop)
  432. except ValueError:
  433. self.stop = 10
  434. # Validate what we got from the config.
  435. # Not an int: make it 5, and update.
  436. # > 50, make it 5 and update.
  437. # < 0, make it 0 and update.
  438. update_config = False
  439. try:
  440. self.percent = int(self.percent)
  441. except ValueError:
  442. self.percent = 5
  443. update_config = True
  444. if self.percent > 50:
  445. self.percent = 5
  446. update_config = True
  447. if self.percent < 0:
  448. self.percent = 0
  449. update_config = True
  450. if update_config:
  451. self.game.gamedata.set_config('Trade_Percent', self.percent)
  452. self.credits = 0
  453. self.last_credits = None
  454. self.times_left = 0
  455. # Activate
  456. self.prompt = game.buffer
  457. self.save = self.observer.save()
  458. self.observer.connect('player', self.player)
  459. self.observer.connect("prompt", self.game_prompt)
  460. self.observer.connect("game-line", self.game_line)
  461. self.defer = None
  462. self.send2player("Script based on: Port Pair Trading v2.00")
  463. self.possible_sectors = None
  464. self.state = 1
  465. self.queue_player.put("D")
  466. # Original, send 'D' to display current sector.
  467. # We could get the sector number from the self.prompt string -- HOWEVER:
  468. # IF! We send 'D', we can also get the sectors around -- we might not even need to
  469. # prompt for sector to trade with (we could possibly figure it out ourselves).
  470. # [Command [TL=00:00:00]:[967] (?=Help)? : D]
  471. # [<Re-Display>]
  472. # []
  473. # [Sector : 967 in uncharted space.]
  474. # [Planets : (M) Into the Darkness]
  475. # [Warps to Sector(s) : 397 - (562) - (639)]
  476. # []
  477. def whenDone(self):
  478. self.defer = defer.Deferred()
  479. # Call this to chain something after we exit.
  480. return self.defer
  481. def deactivate(self, andExit=True):
  482. self.state = 0
  483. log.debug("ScriptPort.deactivate ({0})".format(self.times_left))
  484. self.queue_game.put(self.nl + Boxes.alert("Trading Script deactivating..."))
  485. assert(not self.save is None)
  486. self.observer.load(self.save)
  487. self.save = None
  488. if self.defer:
  489. if andExit:
  490. self.defer.callback({'exit':True})
  491. else:
  492. self.defer.callback('done')
  493. self.defer = None
  494. def player(self, chunk: bytes):
  495. # If we receive anything -- ABORT!
  496. self.deactivate()
  497. def send2game(self, txt):
  498. log.debug("ScriptPort.send2game({0})".format(txt))
  499. self.queue_player.put(txt)
  500. def send2player(self, txt):
  501. log.debug("ScriptPort.send2player({0})".format(txt))
  502. self.queue_game.put(txt)
  503. def game_prompt(self, prompt: str):
  504. log.debug("{0} : {1}".format(self.state, prompt))
  505. if self.state == 3:
  506. # log.("game_prompt: ", prompt)
  507. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  508. self.state = 4
  509. log.debug("Ok, state 4")
  510. use_first = self.game.gamedata.get_config('Trade_UseFirst', 'N').upper()[0] == 'Y'
  511. if self.sector2 is None and use_first:
  512. # Use the first one by default
  513. self.sector2 = self.possible[0]
  514. log.info("default to {0}".format(self.sector2))
  515. if self.sector2 is None:
  516. # Ok, we need to prompt for this.
  517. self.queue_game.put(self.r + self.nl +
  518. "Which sector to trade with? {0}".format(GameData.port_show_part(self.this_sector, self.game.gamedata.ports[self.this_sector])) +
  519. self.nl)
  520. for i, p in enumerate(self.possible):
  521. self.queue_game.put(" " + Fore.CYAN + str(i + 1) + " : " + GameData.port_show_part(p, self.game.gamedata.ports[p]) + self.nl)
  522. pi = PlayerInput(self.game)
  523. def got_need1(*_):
  524. log.debug("Ok, I have: {0}".format(pi.keep))
  525. if pi.keep['count'].strip() == '':
  526. self.deactivate()
  527. return
  528. self.times_left = int(pi.keep['count'])
  529. if pi.keep['choice'].strip() == '':
  530. self.deactivate()
  531. return
  532. c = int(pi.keep['choice']) -1
  533. if c < 0 or c >= len(self.possible):
  534. self.deactivate()
  535. return
  536. self.sector2 = self.possible[int(pi.keep['choice']) -1]
  537. # self.queue_game.put(pformat(pi.keep).replace("\n", "\n\r"))
  538. self.state = 5
  539. self.trade()
  540. d = pi.prompt("Choose -=>", 5, name='choice', digits=True)
  541. d.addCallback(lambda ignore: pi.prompt("Times to execute script:", 5, name='count', digits=True))
  542. d.addCallback(got_need1)
  543. else:
  544. # We already have our target port, so...
  545. self.queue_game.put(self.r + self.nl + "Trading from {0} ({1}) to default {2} ({3}).".format(
  546. self.this_sector,
  547. self.game.gamedata.ports[self.this_sector]['port'],
  548. self.sector2, self.game.gamedata.ports[self.sector2]['port']) + self.nl
  549. )
  550. self.queue_game.put(self.r + self.nl + "Trading {0}".format(
  551. self.game.gamedata.port_trade_show(self.this_sector,
  552. self.sector2, 0)
  553. ))
  554. pi = PlayerInput(self.game)
  555. def got_need2(*_):
  556. if pi.keep['count'].strip() == '':
  557. self.deactivate()
  558. return
  559. self.times_left = int(pi.keep['count'])
  560. log.debug("Ok, I have: {0}".format(pi.keep))
  561. # self.queue_game.put(pformat(pi.keep).replace("\n", "\n\r"))
  562. self.state = 5
  563. self.trade()
  564. self.queue_game.put(self.r + self.nl)
  565. default_turns = self.game.gamedata.get_config('Trade_Turns', '0')
  566. if default_turns == '0':
  567. # No default given, ask.
  568. d = pi.prompt("Times to execute script", 5, name='count')
  569. d.addCallback(got_need2)
  570. else:
  571. try:
  572. self.times_left = int(default_turns)
  573. except ValueError:
  574. self.times_left = 30
  575. self.state = 5
  576. self.trade()
  577. elif self.state == 6:
  578. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  579. if self.end_trans:
  580. self.deactivate()
  581. return
  582. if self.fixable:
  583. # self.queue_game.put("Ok! Let's fix this by going to the other sector..." + self.nl)
  584. self.queue_game.put(self.nl + Boxes.alert("Ok, FINE. We'll trade with the other port.", base="green", style=0))
  585. log.debug("Fixing...")
  586. # Swap this and other sector
  587. self.this_sector, self.other_sector = (self.other_sector, self.this_sector)
  588. self.queue_player.put("{0}\r".format(self.sector2))
  589. self.state = 5
  590. self.trade()
  591. elif self.state == 7:
  592. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  593. # Done
  594. if self.end_trans:
  595. self.deactivate()
  596. return
  597. # Swap this and other sector
  598. self.this_sector, self.other_sector = (self.other_sector, self.this_sector)
  599. if self.this_sector == self.sector2:
  600. self.times_left -= 1
  601. if self.times_left <= 0:
  602. # Ok, exit out
  603. self.deactivate()
  604. return
  605. if self.last_credits is None:
  606. self.last_credits = self.credits
  607. else:
  608. if self.credits <= self.last_credits:
  609. log.warn("We don't seem to be making any money here...")
  610. self.queue_game.put(self.r + self.nl + "We don't seem to be making any money here. I'm stopping!" + self.nl)
  611. self.deactivate()
  612. return
  613. self.queue_player.put("{0}\r".format(self.this_sector))
  614. self.state = 10
  615. elif re.match(r'Your offer \[\d+\] \?', prompt):
  616. log.info("Your offer? [{0}]".format(self.fix_offer))
  617. if self.fix_offer:
  618. # Make real offer / WHAT?@?!
  619. work = prompt.replace(',', '')
  620. parts = re.split(r"\s+", work)
  621. amount = parts[2]
  622. # Ok, we have the amount, now to figure pct...
  623. if self.sell_percent > 100:
  624. self.sell_percent -= 1
  625. else:
  626. self.sell_percent += 1
  627. price = amount * self.sell_percent // 100
  628. log.debug("start: {0} % {1} price {2}".format(amount, self.sell_percent, price))
  629. if self.sell_percent > 100:
  630. self.sell_percent -= 1
  631. else:
  632. self.sell_percent += 1
  633. self.queue_player.put("{0}\r".format(price))
  634. # elif re.match(r"How many holds of .+ do you want to sell \[\d+\]\?", prompt):
  635. # log.info("Sell everything we can...")
  636. # this seems to screw up the sync of everything.
  637. # self.queue_player.put("\r")
  638. elif self.state == 8:
  639. # What are we trading
  640. # How many holds of Equipment do you want to buy [75]?
  641. if re.match(r"How many holds of .+ do you want to buy \[\d+\]\?", prompt):
  642. parts = prompt.split()
  643. trade_type = parts[4]
  644. log.info("Buy {0} [{1} ~ {2}]".format(trade_type, self.tpc, self.opc))
  645. if trade_type == 'Fuel':
  646. if (self.tpc in (5,7)) and (self.opc in (2,3,4,8)):
  647. # Can buy equipment - fuel ore is worthless.
  648. self.queue_player.put("0\r")
  649. return
  650. if (self.tpc in(4,7)) and (self.opc in (1,3,5,8)):
  651. # Can buy organics - fuel ore is worthless.
  652. self.queue_player.put("0\r")
  653. return
  654. if (self.tpc in (4,7,3,5)) and (self.opc in (3,4,5,7)):
  655. # No point in buying fuel ore if it can't be sold.
  656. self.queue_player.put("0\r")
  657. return
  658. elif trade_type == 'Organics':
  659. if (self.tpc in (6,7)) and (self.opc in (2,3,4,8)):
  660. # Can buy equipment - organics is worthless.
  661. self.queue_player.put("0\r")
  662. return
  663. if (self.tpc in (2,4,6,7)) and (self.opc in (2,4,6,7)):
  664. # No point in buying organics if it can't be sold.
  665. self.queue_player.put("0\r")
  666. return
  667. elif trade_type == 'Equipment':
  668. if (self.opc in (1,5,6,7)):
  669. # No point in buying equipment if it can't be sold.
  670. self.queue_player.put("0\r")
  671. return
  672. self.queue_player.put("\r")
  673. elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  674. # Done
  675. if self.end_trans:
  676. self.deactivate()
  677. return
  678. # Swap this and other sector
  679. self.this_sector, self.other_sector = (self.other_sector, self.this_sector)
  680. if self.this_sector == self.sector2:
  681. self.times_left -= 1
  682. if self.times_left <= 0:
  683. # Ok, exit out
  684. self.deactivate()
  685. return
  686. if self.last_credits is None:
  687. self.last_credits = self.credits
  688. else:
  689. if self.credits <= self.last_credits:
  690. log.warn("We don't seem to be making any money here...")
  691. self.queue_game.put(self.r + self.nl + "We don't seem to be making any money here. I'm stopping!" + self.nl)
  692. self.deactivate()
  693. return
  694. self.queue_player.put("{0}\r".format(self.this_sector))
  695. self.state = 10
  696. elif self.state == 99:
  697. # This is a good place to deactivate at.
  698. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  699. if hasattr(self, 'message'):
  700. if self.message is not None:
  701. self.queue_game.put(self.message)
  702. self.message = None
  703. self.deactivate()
  704. def trade(self, *_):
  705. # state 5
  706. log.debug("trade!")
  707. self.queue_player.put("pt") # Port Trade
  708. self.end_trans = False
  709. self.fixable = False
  710. self.this_port = self.game.gamedata.ports[self.this_sector]
  711. # I think other_sector will alway be correct, but leaving this
  712. # for now. FUTURE: TODO: REMOVE
  713. if self.this_sector == self.sector1:
  714. self.other_sector = self.sector2
  715. else:
  716. self.other_sector = self.sector1
  717. self.other_port = self.game.gamedata.ports[self.other_sector]
  718. # Ok, perform some calculations
  719. self.tpc = self.this_port['class']
  720. self.opc = self.other_port['class']
  721. self.fixable = 0
  722. self.fix_offer = 0
  723. # [ Items Status Trading % of max OnBoard]
  724. # [ ----- ------ ------- -------- -------]
  725. # [Fuel Ore Selling 2573 93% 0]
  726. # [Organics Buying 2960 100% 0]
  727. # [Equipment Buying 1958 86% 0]
  728. # []
  729. # []
  730. # [You have 1,000 credits and 20 empty cargo holds.]
  731. # []
  732. # [We are selling up to 2573. You have 0 in your holds.]
  733. # [How many holds of Fuel Ore do you want to buy [20]? 0]
  734. def game_line(self, line: str):
  735. if line.startswith("You have ") and 'credits and' in line:
  736. parts = line.replace(',', '').split()
  737. credits = int(parts[2])
  738. log.debug("Credits: {0}".format(credits))
  739. self.credits = credits
  740. if self.state == 1:
  741. # First exploration
  742. if line.startswith("Sector :"):
  743. # We have starting sector information
  744. parts = re.split(r"\s+", line)
  745. self.this_sector = int(parts[2])
  746. # These will be the ones swapped around as we trade back and forth.
  747. self.sector1 = self.this_sector
  748. elif line.startswith("Warps to Sector(s) : "):
  749. # Warps to Sector(s) : 397 - (562) - (639)
  750. _, _, warps = line.partition(':')
  751. warps = warps.replace('-', '').replace('(', '').replace(')', '').strip()
  752. log.debug("Warps: [{0}]".format(warps))
  753. self.warps = [ int(x) for x in re.split(r"\s+", warps)]
  754. log.debug("Warps: [{0}]".format(self.warps))
  755. self.state = 2
  756. elif self.state == 2:
  757. if line == "":
  758. # Ok, we're done
  759. self.state = 3
  760. # Check to see if we have information on any possible ports
  761. # if hasattr(self.game, 'portdata'):
  762. if True:
  763. if not self.this_sector in self.game.gamedata.ports:
  764. self.state = 0
  765. log.debug("Current sector {0} not in portdata.".format(self.this_sector))
  766. self.queue_game.put(self.r + self.nl + "I can't find the current sector in the portdata." + self.nl)
  767. self.deactivate()
  768. return
  769. else:
  770. # Ok, we are in the portdata
  771. pd = self.game.gamedata.ports[self.this_sector]
  772. if GameData.port_burnt(pd):
  773. log.debug("Current sector {0} port is burnt (<= 20%).".format(self.this_sector))
  774. self.queue_game.put(self.r + self.nl + "Current sector port is burnt out. <= 20%." + self.nl)
  775. self.deactivate()
  776. return
  777. possible = [ x for x in self.warps if x in self.game.gamedata.ports ]
  778. log.debug("Possible: {0}".format(possible))
  779. # BUG: Sometimes links to another sector, don't link back!
  780. # This causes the game to plot a course / autopilot.
  781. # if hasattr(self.game, 'warpdata'):
  782. if True:
  783. # Great! verify that those warps link back to us!
  784. possible = [ x for x in possible if x in self.game.gamedata.warps and self.this_sector in self.game.gamedata.warps[x]]
  785. if len(possible) == 0:
  786. self.state = 0
  787. self.queue_game.put(self.r + self.nl + "I don't see any ports in [{0}].".format(self.warps) + self.nl)
  788. self.deactivate()
  789. return
  790. possible = [ x for x in possible if not GameData.port_burnt(self.game.gamedata.ports[x]) ]
  791. log.debug("Possible: {0}".format(possible))
  792. if len(possible) == 0:
  793. self.state = 0
  794. self.queue_game.put(self.r + self.nl + "I don't see any unburnt ports in [{0}].".format(self.warps) + self.nl)
  795. self.deactivate()
  796. return
  797. possible = [ x for x in possible if GameData.port_trading(self.game.gamedata.ports[self.this_sector]['port'], self.game.gamedata.ports[x]['port'])]
  798. # sort by best, then by %
  799. start_port = self.game.gamedata.ports[self.this_sector]
  800. if 'port' in start_port:
  801. start_port_port = start_port['port']
  802. start_with = start_port_port[1:]
  803. if start_with in ('BS', 'SB'):
  804. # Ok, good trades may be possible
  805. best = GameData.flip(start_with)
  806. log.info("Sorting the best ({0}) to the top.".format(best))
  807. log.info("{0}".format(possible))
  808. dec = [ [self.game.gamedata.ports[p].get('port', '---')[1:] == best, p] for p in possible]
  809. dec = sorted(dec, reverse=True)
  810. possible = [x[1] for x in dec]
  811. log.info("{0}".format(possible))
  812. self.possible = possible
  813. if len(possible) == 0:
  814. self.state = 0
  815. self.queue_game.put(self.r + self.nl + "I don't see any possible port trades in [{0}].".format(self.warps) + self.nl)
  816. self.deactivate()
  817. return
  818. elif len(possible) == 1:
  819. # Ok! there's only one!
  820. self.sector2 = possible[0]
  821. # Display possible ports:
  822. # spos = [ str(x) for x in possible]
  823. # self.queue_game.put(self.r + self.nl + self.nl.join(spos) + self.nl)
  824. # At state 3, we only get a prompt.
  825. return
  826. else:
  827. self.state = 0
  828. log.warn("We don't have any portdata!")
  829. self.queue_game.put(self.r + self.nl + "I have no portdata. Please run CIM Port Report." + self.nl)
  830. self.deactivate()
  831. return
  832. elif self.state == 5:
  833. if "-----" in line:
  834. self.state = 6
  835. elif self.state == 6:
  836. if "We are buying up to" in line:
  837. log.info("buying up to -- so sell all")
  838. # Sell
  839. self.state = 7
  840. self.queue_player.put("\r")
  841. self.sell_percent = 100 + self.percent
  842. if "We are selling up to" in line:
  843. log.info("selling up to -- state 8 / set percent")
  844. # Buy
  845. self.state = 8
  846. self.sell_percent = 100 - self.percent
  847. if line.startswith('Fuel Ore') or line.startswith('Organics') or line.startswith('Equipment'):
  848. work = line.replace('Fuel Ore', 'Fuel').replace('%', '')
  849. parts = re.split(r"\s+", work)
  850. # log.debug(parts)
  851. # Equipment, Selling xxx x% xxx
  852. if parts[-1] != '0' and parts[2] != '0' and parts[1] != 'Buying':
  853. log.warn("We have a problem -- they aren't buying what we have in stock!")
  854. stuff = line[0] # F O or E.
  855. if self.game.gamedata.port_buying(self.other_sector, stuff):
  856. log.info("fixable")
  857. self.fixable = True
  858. if int(parts[3]) < self.stop:
  859. log.info("Port is burnt! % < {0} (end_trans)".format(self.stop))
  860. self.end_trans = True
  861. if "You don't have anything they want" in line:
  862. log.warn("Don't have anything they want.")
  863. # Neither! DRAT!
  864. if not self.fixable:
  865. self.state = 99
  866. return
  867. if "We're not interested." in line:
  868. log.warn("Try, try again. :(")
  869. self.state = 5
  870. self.trade()
  871. elif self.state == 7:
  872. # Haggle Sell
  873. if "We'll buy them for" in line or "Our final offer" in line:
  874. if "Our final offer" in line:
  875. self.sell_percent -= 1
  876. parts = line.replace(',', '').split()
  877. start_price = int(parts[4])
  878. price = start_price * self.sell_percent // 100
  879. log.debug("start: {0} % {1} price {2}".format(start_price, self.sell_percent, price))
  880. self.sell_percent -= 1
  881. self.queue_player.put("{0}\r".format(price))
  882. if "We are selling up to" in line:
  883. log.info("selling up to / state 8 / set percent")
  884. # Buy
  885. self.state = 8
  886. self.sell_percent = 100 - self.percent
  887. if "We are buying up to" in line:
  888. log.info("buying up to -- so sell all")
  889. # Sell
  890. self.state = 7
  891. self.queue_player.put("\r")
  892. self.sell_percent = 100 + self.percent
  893. if "We're not interested." in line:
  894. log.info("Not interested. Try, try again. :(")
  895. self.state = 5
  896. self.trade()
  897. if "WHAT?!@!? you must be crazy!" in line:
  898. log.warn("fix offer")
  899. self.fix_offer = 1
  900. if "So, you think I'm as stupid as you look?" in line:
  901. log.warn("fix offer")
  902. self.fix_offer = 1
  903. if "Quit playing around, you're wasting my time!" in line:
  904. log.warn("fix offer")
  905. self.fix_offer = 1
  906. elif self.state == 8:
  907. # Haggle Buy
  908. if "We'll sell them for" in line or "Our final offer" in line:
  909. if "Our final offer" in line:
  910. self.sell_percent += 1
  911. parts = line.replace(',', '').split()
  912. start_price = int(parts[4])
  913. price = start_price * self.sell_percent // 100
  914. log.debug("start: {0} % {1} price {2}".format(start_price, self.sell_percent, price))
  915. self.sell_percent += 1
  916. self.queue_player.put("{0}\r".format(price))
  917. if "We're not interested." in line:
  918. log.info("Not interested. Try, try again. :(")
  919. self.state = 5
  920. self.trade()
  921. elif self.state == 10:
  922. if "Sector : " in line:
  923. # Trade
  924. self.state = 5
  925. reactor.callLater(0, self.trade, 0)
  926. # self.trade()
  927. # elif self.state == 3:
  928. # log.debug("At state 3 [{0}]".format(line))
  929. # self.queue_game.put("At state 3.")
  930. # self.deactivate()
  931. # return
  932. class ScriptExplore(object):
  933. """ Exploration Script
  934. WARNINGS:
  935. We assume the player has lots o turns, or unlimited turns!
  936. """
  937. def __init__(self, game):
  938. self.game = game
  939. self.queue_game = game.queue_game
  940. self.queue_player = game.queue_player
  941. self.observer = game.observer
  942. self.r = Style.RESET_ALL
  943. self.c = merge(Style.BRIGHT + Fore.YELLOW)
  944. self.nl = "\n\r"
  945. # Our Stuff, Not our pants!
  946. self.dense = [] # We did a density, store that info.
  947. self.clear = [] # Warps that we know are clear.
  948. self.highsector = 0 # Selected Sector to move to next!
  949. self.highwarp = 0 # Selected Sector's Warp Count!
  950. self.stacksector = [] # Set of sectors that we have not picked but are unexplored... even though we did a holo!
  951. self.oneMoveSector = False
  952. self.times = 0
  953. self.maxtimes = 0
  954. # Activate
  955. self.prompt = game.buffer
  956. self.save = self.observer.save()
  957. self.observer.connect('player', self.player)
  958. self.observer.connect("prompt", self.game_prompt)
  959. self.observer.connect("game-line", self.game_line)
  960. self.defer = None
  961. self.send2player(Boxes.alert("Explorer", base="green"))
  962. # How many times we going to go today?
  963. ask = PlayerInput(self.game)
  964. def settimes(*_):
  965. times = ask.keep['times'].strip()
  966. log.debug("settimes got '{0}'".format(times))
  967. if times == '':
  968. self.deactivate()
  969. else:
  970. times = int(times)
  971. self.times = times
  972. self.maxtimes = times
  973. self.send2game("D")
  974. log.debug("times: {0} maxtimes: {0}".format(self.times))
  975. self.state = 1
  976. d = ask.prompt("How many sectors would you like to explorer?", 5, name="times", digits=True)
  977. #d.addCallback(ask.output)
  978. #d.addCallback(lambda ignore: self.settimes(ask.keep))
  979. d.addCallback(settimes)
  980. def whenDone(self):
  981. self.defer = defer.Deferred()
  982. # Call this to chain something after we exit.
  983. return self.defer
  984. def deactivate(self, andExit=False):
  985. self.state = 0
  986. log.debug("ScriptExplore.deactivate()")
  987. assert(not self.save is None)
  988. self.observer.load(self.save)
  989. self.save = None
  990. if self.defer:
  991. if andExit:
  992. self.defer.callback({'exit':True})
  993. else:
  994. self.defer.callback('done')
  995. self.defer = None
  996. def player(self, chunk: bytes):
  997. # If we receive anything -- ABORT!
  998. self.deactivate(True)
  999. def send2game(self, txt):
  1000. log.debug("ScriptExplore.send2game({0})".format(txt))
  1001. self.queue_player.put(txt)
  1002. def send2player(self, txt):
  1003. log.debug("ScriptExplore.send2player({0})".format(txt))
  1004. self.queue_game.put(txt)
  1005. def resetStuff(self):
  1006. self.dense = []
  1007. self.clear = []
  1008. self.highwarp = 0
  1009. self.highsector = 0
  1010. log.debug("ScriptExplore.resetStuff()")
  1011. def dead_end(self):
  1012. """ We've reached a dead end.
  1013. Either pop a new location to travel to, or give it up.
  1014. """
  1015. self.send2player(self.nl + Boxes.alert("** DEAD END **", base="blue"))
  1016. if self.stacksector:
  1017. # Ok, there's somewhere to go ...
  1018. self.highsector = self.stacksector.pop()
  1019. # travel state
  1020. self.state = 10
  1021. self.send2game("{0}\r".format(self.highsector))
  1022. else:
  1023. self.send2player(self.nl + Boxes.alert("I've run out of places to look ({0}/{1}).".format(self.maxtimes - self.times, self.maxtimes)))
  1024. self.deactivate(True)
  1025. def game_over(self, msg):
  1026. self.send2player(self.nl + Boxes.alert("STOP: {0} ({1}/{2}).".format(msg, self.maxtimes - self.times, self.maxtimes)))
  1027. self.deactivate()
  1028. def game_prompt(self, prompt: str):
  1029. log.debug("{0} : {1}".format(self.state, prompt))
  1030. if self.state == 2:
  1031. if "Select (H)olo Scan or (D)ensity Scan or (Q)uit" in prompt:
  1032. self.send2game("D")
  1033. # self.state += 1
  1034. elif self.state == 5:
  1035. log.debug("dense is {0} sectors big".format(len(self.dense)))
  1036. self.state += 1
  1037. self.send2game("SH")
  1038. elif self.state == 10:
  1039. if prompt.startswith('Do you want to engage the TransWarp drive? '):
  1040. self.send2game("N")
  1041. elif self.state == 12:
  1042. # Looking for "Engage the Autopilot?"
  1043. if prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
  1044. self.send2game("S")
  1045. self.travel_path.pop(0)
  1046. if prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N]'):
  1047. self.send2game("SD")
  1048. self.state += 1
  1049. # Arriving sector :1691 Autopilot disengaging.
  1050. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  1051. log.info("We made it to where we wanted to go!")
  1052. # can't init state 1, because we're at a prompt, so...
  1053. self.send2game("S")
  1054. self.state = 2
  1055. return
  1056. elif self.state == 15:
  1057. if prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N]'):
  1058. self.send2game("N")
  1059. self.travel_path.pop(0)
  1060. self.state = 12
  1061. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  1062. log.info("We made it to where we wanted to go!")
  1063. # can't init state 1, because we're at a prompt, so...
  1064. self.send2game("S")
  1065. self.state = 2
  1066. return
  1067. elif self.state == 20:
  1068. if prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N]'):
  1069. # Stop in this sector / Yes!
  1070. self.send2game("Y")
  1071. self.state = 1
  1072. # this should re-trigger a scan
  1073. def game_line(self, line: str):
  1074. log.debug("{0} | {1}".format(self.state, line))
  1075. #if "Mine Control" in line: # If we don't have a Holo-Scanner and we attempted to do a Holo-scan, abort
  1076. # self.deactivate()
  1077. if self.state == 1:
  1078. self.send2game("S")
  1079. self.state += 1
  1080. elif self.state == 2:
  1081. if "Relative Density Scan" in line:
  1082. self.state = 3
  1083. elif "You don't have a long range scanner." in line:
  1084. log.warn("FATAL: No Long Range Scanner Installed!")
  1085. self.send2player(Boxes.alert("You need a Long Range Scanner!"))
  1086. self.deactivate(True)
  1087. return
  1088. # elif "Long Range Scan" in line:
  1089. # self.state += 1
  1090. elif self.state == 3:
  1091. # Get the Density Data!
  1092. if line.startswith("Sector"):
  1093. new_sector = '(' in line
  1094. work = line.replace(':', '').replace(')', '').replace('(', '').replace('%', '').replace('==>', '')
  1095. work = re.split(r"\s+", work)
  1096. log.debug(work)
  1097. # 'Sector', '8192', '0', 'Warps', '4', 'NavHaz', '0', 'Anom', 'No'
  1098. # 'Sector', '(', '8192)', '0', 'Warps', '4', 'NavHaz', '0', 'Anom', 'No'
  1099. # New Sector?
  1100. if new_sector:
  1101. # Switch Anom into bool state
  1102. # if(work[8] == 'No'):
  1103. # temp = False
  1104. # else:
  1105. # temp = True
  1106. #self.dense.append( {'sector': int(work[1]), 'density': int(work[2]), 'warps': int(work[4]), 'navhaz': int(work[6]), 'anom': temp} )
  1107. self.dense.append( {'sector': int(work[1]), 'density': int(work[2]), 'warps': int(work[4]), 'navhaz': int(work[6]), 'anom': work[8] == 'Yes'} )
  1108. log.debug(self.dense)
  1109. # {'sector': 8192, 'density': 0, 'warps': 4, 'navhaz': 0, 'anom': False}
  1110. elif line == "":
  1111. self.state += 1
  1112. # yeah, this would be better in the above line...
  1113. # leaving it for now.
  1114. # Which is why I broke the elif chain. ...
  1115. if self.state == 4:
  1116. # Begin Processing our data we got from density scan and find highest warp count in what sectors
  1117. # Remove sectors with one warp
  1118. log.debug("state 4: {0}".format(self.dense))
  1119. # Do we have a new place to go? (That is also worth going to)
  1120. if not self.dense: # Dense contains no new sectors, abort
  1121. log.info("No New Sectors Found!")
  1122. self.dead_end()
  1123. return
  1124. # self.send2player(Boxes.alert("Find a new area for me to search in!"))
  1125. # Attempt to resolve no new sectors!
  1126. # if self.stacksector: # Do we have anything on the stack? (If so we set highsector with one of the randomly selected sectors)
  1127. # self.highsector = self.stacksector.pop()
  1128. # self.deactivate()
  1129. # elif self.dense: # Dense does contain at least 1 new sector, continue on
  1130. # ugh, actually, we don't care about making a list
  1131. # we only want to know if there's any that have warps > 1. :(
  1132. # see any:
  1133. # list comprehension
  1134. # t = [d for d in self.dense if d['warps'] > 1]
  1135. # t = [] # Pre-Test to check if there are just a bunch of 1 warp sectors
  1136. # for d in self.dense:
  1137. # if d['warps'] > 1:
  1138. # t.append(d['sector'])
  1139. # I don't care if there's only one warp. if it's un-explorered then
  1140. # explore it!
  1141. # if not any( w['warps'] > 1 for w in self.dense):
  1142. # # If there are no sectors with more that 1 warp, abort
  1143. # log.debug("No Sectors Found except one move sector!")
  1144. # self.dead_end()
  1145. # return
  1146. # self.send2player(Boxes.alert("Find a new area for me to look at!"))
  1147. # Attempt to resolve no new sectors with more than 1 warp!
  1148. # if self.stacksector: # Do we have anything on the stack? (If so we set highsector with one of the randomly selected sectors)
  1149. # self.highsector = self.stacksector.pop()
  1150. # self.deactivate()
  1151. # Is the sector safe to go into?
  1152. # self.clear = [ x['sector'] for x in self.dense if not x['anom'] and not x['navhaz'] and x['density'] in (0,1,100,101) and x['warps'] > 1 ]
  1153. self.clear = [ x for x in self.dense if not x['anom'] and not x['navhaz'] and x['density'] in (0,1,100,101) ]
  1154. # for d in self.dense:
  1155. # if not d['anom']:
  1156. # # Sector does not contain a Anomoly
  1157. # if not d['navhaz']:
  1158. # # Sector does not contain Hazards
  1159. # if d['density'] in (0, 1, 100, 101):
  1160. # # Sector does contain empty space / a beacon / a port / or a beacon and port
  1161. # if d['warps'] > 1:
  1162. # # If Sector is worth checking out?
  1163. # self.clear.append(d['sector'])
  1164. if self.clear: # We have sector(s) we can move to!
  1165. log.debug("Clear Sectors: {0}".format(len(self.clear)))
  1166. # This was state 5 but why can't we reduce number of states? ( Yeah let's kick California and New York out of the US, oh wrong states :P )
  1167. # Sort to find greatest warp count
  1168. self.highwarp, self.highsector = max( (x['warps'], x['sector']) for x in self.clear)
  1169. # for c in self.clear:
  1170. # for d in self.dense:
  1171. # if d['sector'] == c:
  1172. # if d['warps'] > self.highwarp:
  1173. # self.highwarp = d['warps']
  1174. # self.highsector = d['sector']
  1175. # elif d['warps'] == self.highwarp:
  1176. # if d['sector'] > self.highsector:
  1177. # self.highsector = d['sector']
  1178. # if self.highwarp and self.highsector:
  1179. log.info("Sector: {0:5d} Warps: {1}".format(self.highsector, self.highwarp))
  1180. self.state += 1
  1181. else:
  1182. log.warn("No (safe) sectors to move to!")
  1183. # Let's try this?!
  1184. # self.dead_end() # NO!
  1185. self.game_over("No SAFE moves.")
  1186. # Another NOP state. This also could be merged into above.
  1187. # break the elif chain.
  1188. if self.state == 5:
  1189. # Add the dense scan of unknown sectors onto the stack of sectors, only save the ones we think are clear... for now.
  1190. for c in self.clear:
  1191. sector = c['sector']
  1192. if sector != self.highsector:
  1193. if sector not in self.stacksector:
  1194. self.stacksector.append(sector)
  1195. # Or simply not add it in the first place ...
  1196. # Remove the sector we are just about to go to, we use discard so if the sector does not exist we don't throw a error!
  1197. # self.stacksector.discard(self.highsector)
  1198. # Ok, we need to decide to stop exploring -- before we
  1199. # issue the sector move! :P
  1200. #
  1201. # Warning! Yes we can and will eat all the turns! :P
  1202. if self.times == 0:
  1203. self.send2player(Boxes.alert("Completed {0}".format(self.maxtimes), base="green"))
  1204. log.info("Completed {0}".format(self.maxtimes))
  1205. self.deactivate()
  1206. return
  1207. self.times -= 1
  1208. # Ok we know the sector we want to go to now let's move it!
  1209. self.send2game("m{0}\r".format(self.highsector))
  1210. if self.highsector in self.stacksector:
  1211. log.info("Removing {0} from stacksector list.".format(self.highsector))
  1212. self.stacksector.remove(self.highsector)
  1213. # Reset Variables for fresh data
  1214. self.resetStuff()
  1215. self.state = 1
  1216. elif self.state == 10:
  1217. if line.startswith("You are already in that sector!"):
  1218. log.info("Already here. (Whoops!)")
  1219. self.state = 1
  1220. return
  1221. if line.startswith("Sector : {0}".format(self.highsector)):
  1222. log.info("We're here!")
  1223. # Ok, we're already there! no autopilot needed!
  1224. self.state = 1
  1225. return
  1226. # Warping
  1227. self.go_on = True
  1228. if line.startswith('The shortest path ('):
  1229. # Ok, we've got a path.
  1230. self.state += 1
  1231. self.travel_path = []
  1232. elif self.state == 11:
  1233. if line == '':
  1234. # The end of the (possibly) multiline warp.
  1235. self.state += 1
  1236. self.travel_path.pop(0) # First sector is one we're in.
  1237. self.stophere = False
  1238. self.go_on = True
  1239. else:
  1240. self.travel_path.extend(line.replace('(', '').replace(')', '').split(' > ') )
  1241. log.debug("Travel path: {0}".format(self.travel_path))
  1242. elif self.state == 12:
  1243. # Arriving sector :1691 Autopilot disengaging.
  1244. if 'Autopilot disengaging.' in line:
  1245. log.info("We made it to where we wanted to go!")
  1246. self.state = 1
  1247. return
  1248. elif self.state == 13:
  1249. if 'Relative Density Scan' in line:
  1250. self.state += 1
  1251. elif self.state == 14:
  1252. if line == "":
  1253. log.debug("PATH: {0}".format(self.travel_path))
  1254. # end of the scan, decision time
  1255. if self.stophere:
  1256. log.info("STOPHERE")
  1257. # Ok, let's stop here!
  1258. # Re-save the sector we were trying to get to. (we didn't make it there)
  1259. if self.highsector not in self.stacksector:
  1260. self.stacksector.append(self.highsector)
  1261. self.state = 20
  1262. else:
  1263. if self.go_on:
  1264. log.info("GO ON")
  1265. # Ok, carry on!
  1266. self.state = 15
  1267. else:
  1268. log.warn("Our way is blocked...")
  1269. if self.highsector not in self.stacksector:
  1270. self.stacksector.append(self.highsector)
  1271. self.state = 20
  1272. else:
  1273. if line.strip('-') != '':
  1274. work = line.replace(' :', '').replace('%', '').replace(')', '').replace('==>', '')
  1275. # Does this contain something new? unseen?
  1276. stophere = '(' in work
  1277. work = work.replace('(','')
  1278. #Sector XXXX DENS Warps N NavHaz P Anom YN
  1279. parts = re.split(r'\s+', work)
  1280. # Don't bother stopping if there's only one warp
  1281. # YES! Stop, even if there is just one warp!
  1282. # if stophere and parts[4] == '1':
  1283. # stophere = False
  1284. if stophere:
  1285. self.stophere = True
  1286. next_stop = self.travel_path[0]
  1287. log.debug("next_stop {0} from {1}".format(next_stop, self.travel_path))
  1288. log.debug("parts: {0}".format(parts))
  1289. if parts[1] == next_stop:
  1290. log.info("next_stop {0} found...".format(next_stop))
  1291. # Ok, this is our next stop. Is it safe to travel to?
  1292. if parts[2] not in ('100', '0', '1', '101'):
  1293. # Ok, it's not safe to go on.
  1294. self.go_on = False
  1295. # Check the rest navhav and anom ...
  1296. class ScriptSpace(object):
  1297. """ Space Exploration script.
  1298. Send "SD", verify paths are clear.
  1299. Find nearest unknown. Sector + CR.
  1300. Save "shortest path from to" information.
  1301. At 'Engage the Autopilot', Send "S" (Single Step)
  1302. At '[Stop in this sector', Send "SD", verify path is clear.
  1303. Send "SH" (glean sector/port information along the way.)
  1304. Send "N" (Next)!
  1305. Send "SD" / Verify clear.
  1306. Send "SH"
  1307. Repeat for Next closest.
  1308. """
  1309. def __init__(self, game):
  1310. self.game = game
  1311. self.queue_game = game.queue_game
  1312. self.queue_player = game.queue_player
  1313. self.observer = game.observer
  1314. self.r = Style.RESET_ALL
  1315. self.nl = "\n\r"
  1316. self.this_sector = None # Starting sector
  1317. self.target_sector = None # Sector going to
  1318. self.path = []
  1319. self.times_left = 0 # How many times to look for target
  1320. self.density = dict() # Results of density scan. (Just the numbers)
  1321. # Activate
  1322. self.prompt = game.buffer
  1323. self.save = self.observer.save()
  1324. self.observer.connect('player', self.player)
  1325. self.observer.connect("prompt", self.game_prompt)
  1326. self.observer.connect("game-line", self.game_line)
  1327. self.defer = None
  1328. self.queue_game.put(
  1329. self.nl + "Bugz (like space), is big." + self.r + self.nl
  1330. )
  1331. self.state = 1
  1332. self.queue_player.put("SD")
  1333. # Get current density scan + also get the current sector.
  1334. # [Command [TL=00:00:00]:[XXXX] (?=Help)? : D]
  1335. def whenDone(self):
  1336. self.defer = defer.Deferred()
  1337. # Call this to chain something after we exit.
  1338. return self.defer
  1339. def deactivate(self, andExit=False):
  1340. self.state = 0
  1341. log.debug("ScriptPort.deactivate ({0})".format(self.times_left))
  1342. assert(not self.save is None)
  1343. self.observer.load(self.save)
  1344. self.save = None
  1345. if self.defer:
  1346. if andExit:
  1347. self.defer.callback({'exit':True})
  1348. else:
  1349. self.defer.callback('done')
  1350. self.defer = None
  1351. def player(self, chunk: bytes):
  1352. # If we receive anything -- ABORT!
  1353. self.deactivate(True)
  1354. def unknown_search(self, starting_sector):
  1355. seen = set()
  1356. possible = set()
  1357. possible.add(int(starting_sector))
  1358. done = False
  1359. while not done:
  1360. next_possible = set()
  1361. for s in possible:
  1362. p = self.game.gamedata.get_warps(s)
  1363. if p is not None:
  1364. for pos in p:
  1365. if pos not in seen:
  1366. next_possible.add(pos)
  1367. else:
  1368. log.debug("unknown found: {0}".format(s))
  1369. self.unknown = s
  1370. done = True
  1371. break
  1372. seen.add(s)
  1373. if self.unknown is None:
  1374. log.debug("possible: {0}".format(next_possible))
  1375. possible = next_possible
  1376. yield
  1377. def find_unknown(self, starting_sector):
  1378. log.debug("find_unknown( {0})".format(starting_sector))
  1379. d = defer.Deferred()
  1380. # Process things
  1381. self.unknown = None
  1382. c = coiterate(self.unknown_search(starting_sector))
  1383. c.addCallback(lambda unknown: d.callback(self.unknown))
  1384. return d
  1385. def show_unknown(self, sector):
  1386. if sector is None:
  1387. self.deactivate()
  1388. return
  1389. log.debug("Travel to {0}...".format(sector))
  1390. self.queue_player.put("{0}\r".format(sector))
  1391. def game_prompt(self, prompt: str):
  1392. log.debug("{0} : {1}".format(self.state, prompt))
  1393. if self.state == 3:
  1394. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  1395. # this_sector code isn't working -- so! Get sector from prompt
  1396. self.state = 4
  1397. _, _, sector = prompt.partition(']:[')
  1398. sector, _, _ = sector.partition(']')
  1399. self.this_sector = int(sector)
  1400. # Ok, we're done with Density Scan, and we're back at the command prompt
  1401. log.debug("Go find the nearest unknown...")
  1402. d = self.find_unknown(sector)
  1403. d.addCallback(self.show_unknown)
  1404. elif self.state == 6:
  1405. # Engage the autopilot?
  1406. if prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
  1407. self.state = 7
  1408. sector = self.path.pop(0)
  1409. if sector in self.density:
  1410. if self.density[sector] in (0, 100):
  1411. # Ok, looks safe!
  1412. self.queue_player.put("S")
  1413. self.this_sector = sector
  1414. else:
  1415. log.warn("FATAL: Density for {0} is {1}".format(sector, self.density[sector]))
  1416. self.deactivate(True)
  1417. return
  1418. else:
  1419. log.error("{0} not in density scan? (how's that possible?)".format(sector))
  1420. self.deactivate(True)
  1421. return
  1422. elif self.state == 7:
  1423. # Ok, we're in a new sector (single stepping through space)
  1424. # update density scan
  1425. if prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
  1426. self.queue_player.put("SD")
  1427. self.state = 8
  1428. elif self.state == 10:
  1429. # Because we're here
  1430. if prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
  1431. self.queue_player.put("SH")
  1432. self.state = 11
  1433. elif self.state == 11:
  1434. if prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
  1435. # Ok, is the density scan clear?
  1436. sector = self.path.pop(0)
  1437. if sector in self.density:
  1438. if self.density[sector] in (0, 100):
  1439. # Ok, looks safe
  1440. self.queue_player.put("N")
  1441. self.state = 7
  1442. self.this_sector = sector
  1443. else:
  1444. log.warn("FATAL: Density for {0} is {1}".format(sector, self.density[sector]))
  1445. self.deactivate()
  1446. return
  1447. else:
  1448. log.error("{0} not in density scane? (how's that possible...)".format(sector))
  1449. self.deactivate()
  1450. return
  1451. def next_unknown(self, sector):
  1452. log.info("Unknown is : {0}".format(sector))
  1453. self.deactivate()
  1454. def game_line(self, line: str):
  1455. log.debug("line {0} : {1}".format(self.state, line))
  1456. if line.startswith('Sector : '):
  1457. work = line.strip()
  1458. parts = re.split(r"\s+", work)
  1459. self.this_sector = int(parts[2])
  1460. log.debug("game_line sector {0}".format(self.this_sector))
  1461. elif line.startswith("Command [TL=]"):
  1462. # Ok, get the current sector from this
  1463. _, _, sector = line.partition("]:[")
  1464. sector, _, _ = sector.partition("]")
  1465. self.this_sector = int(sector)
  1466. log.debug("current sector: {0}".format(self.this_sector))
  1467. elif line.startswith('Warps to Sector(s) :'):
  1468. # Warps to Sector(s) : 5468
  1469. _, _, work = line.partition(':')
  1470. work = work.strip().replace('(', '').replace(')', '').replace(' - ', ' ')
  1471. parts = [ int(x) for x in work.split(' ')]
  1472. self.path = list(parts)
  1473. if self.state in (1, 8):
  1474. if 'Relative Density Scan' in line:
  1475. # Start Density Scan
  1476. self.state += 1
  1477. self.density = {}
  1478. elif self.state in (2, 9):
  1479. if line == '':
  1480. # End of Density Scan
  1481. self.state += 1
  1482. log.debug("Density: {0}".format(self.density))
  1483. # self.deactivate()
  1484. elif line.startswith('Sector'):
  1485. # Parse Density Scan values
  1486. work = line.replace('(', '').replace(')', '').replace(':', '').replace('%', '').replace(',', '')
  1487. parts = re.split(r'\s+', work)
  1488. log.debug("Sector {0}".format(parts))
  1489. sector = int(parts[1])
  1490. self.density[sector] = int(parts[3])
  1491. if parts[7] != '0':
  1492. log.warn("NavHaz {0} : {1}".format(parts[7], work))
  1493. self.density[sector] += 99
  1494. if parts[9] != 'No':
  1495. log.warn("Anom {0} : {1}".format(parts[9], work))
  1496. self.density[sector] += 990
  1497. elif self.state == 4:
  1498. # Looking for shortest path message / warp info
  1499. # Or possibly, "We're here!"
  1500. if line.startswith('Sector :') and str(self.unknown) in line:
  1501. # Ok, I'd guess that we're already there!
  1502. # Try it again!
  1503. self.queue_player.put("SD")
  1504. self.state = 1
  1505. if line.startswith('The shortest path'):
  1506. self.state = 5
  1507. elif self.state == 5:
  1508. # This is the warps line
  1509. # Can this be multiple lines?
  1510. if line == "":
  1511. self.state = 6
  1512. else:
  1513. work = line.replace("(", "").replace(")", "").replace(">", "").strip()
  1514. self.path = [int(x) for x in work.split()]
  1515. log.debug("Path: {0}".format(self.path))
  1516. # Verify
  1517. current = self.path.pop(0)
  1518. if current != self.this_sector:
  1519. log.warn("Failed: {0} != {1}".format(current, self.this_sector))
  1520. self.deactivate()
  1521. return
  1522. elif self.state == 7:
  1523. if self.unknown == self.this_sector:
  1524. # We have arrived!
  1525. log.info("We're here!")
  1526. self.deactivate()
  1527. class ScriptTerror(object):
  1528. """ Terror script. """
  1529. def __init__(self, game, proxy, count):
  1530. self.game = game
  1531. self.queue_game = game.queue_game
  1532. self.queue_player = game.queue_player
  1533. self.proxy = proxy
  1534. self.count = count
  1535. self.observer = game.observer
  1536. self.r = Style.RESET_ALL
  1537. self.nl = "\n\r"
  1538. self.target_sector = None
  1539. # Activate
  1540. self.prompt = game.buffer
  1541. self.save = self.observer.save()
  1542. self.observer.connect('player', self.player)
  1543. self.observer.connect("prompt", self.game_prompt)
  1544. self.observer.connect("game-line", self.game_line)
  1545. self.state = 0
  1546. self.defer = None
  1547. c = coiterate(self.find_next_good_trade_pair())
  1548. c.addCallback(lambda unknown: self.scary())
  1549. def scary(self):
  1550. if self.target_sector is None:
  1551. self.queue_game.put(Boxes.alert("Sorry! I don't see any ports to trade with.", base="red"))
  1552. self.deactivate()
  1553. else:
  1554. self.state = 1
  1555. self.queue_player.put("{0}\r".format(self.target_sector))
  1556. def find_next_good_trade_pair(self):
  1557. """ Find the next GOOD trade pair sector. """
  1558. show_limit = 90
  1559. # Look for "GOOD" trades
  1560. for sector in sorted(self.game.gamedata.ports.keys()):
  1561. pd = self.game.gamedata.ports[sector]
  1562. if not GameData.port_burnt(pd):
  1563. pc = pd['class']
  1564. # Ok, let's look into it.
  1565. if not sector in self.game.gamedata.warps:
  1566. continue
  1567. warps = self.game.gamedata.warps[sector]
  1568. for w in warps:
  1569. # We can get there, and get back.
  1570. if w in self.game.gamedata.warps and sector in self.game.gamedata.warps[w]:
  1571. # Ok, we can get there -- and get back!
  1572. if w > sector and w in self.game.gamedata.ports and not GameData.port_burnt(self.game.gamedata.ports[w]):
  1573. wd = self.game.gamedata.ports[w]
  1574. wc = wd['class']
  1575. if pc in (1,5) and wc in (2,4):
  1576. data = self.game.gamedata.port_trade_show(sector, w, show_limit)
  1577. if data:
  1578. self.target_sector = sector
  1579. return sector
  1580. elif pc in (2,4) and wc in (1,5):
  1581. data = self.game.gamedata.port_trade_show(sector, w, show_limit)
  1582. if data:
  1583. self.target_sector = sector
  1584. return sector
  1585. yield
  1586. # Look for OK trades
  1587. for sector in sorted(self.game.gamedata.ports.keys()):
  1588. pd = self.game.gamedata.ports[sector]
  1589. if not GameData.port_burnt(pd):
  1590. pc = pd['class']
  1591. # Ok, let's look into it.
  1592. if not sector in self.game.gamedata.warps:
  1593. continue
  1594. warps = self.game.gamedata.warps[sector]
  1595. for w in warps:
  1596. # We can get there, and get back.
  1597. if w in self.game.gamedata.warps and sector in self.game.gamedata.warps[w]:
  1598. # Ok, we can get there -- and get back!
  1599. if w > sector and w in self.game.gamedata.ports and not GameData.port_burnt(self.game.gamedata.ports[w]):
  1600. wd = self.game.gamedata.ports[w]
  1601. wc = wd['class']
  1602. if GameData.port_trading(pd['port'], self.game.gamedata.ports[w]['port']):
  1603. data = self.game.gamedata.port_trade_show(sector, w, show_limit)
  1604. if data:
  1605. self.target_sector = sector
  1606. return sector
  1607. yield
  1608. self.target_sector = None
  1609. def whenDone(self):
  1610. self.defer = defer.Deferred()
  1611. # Call this to chain something after we exit.
  1612. return self.defer
  1613. def deactivate(self, andExit=False):
  1614. self.state = 0
  1615. log.debug("ScriptTerror.deactivate")
  1616. assert(not self.save is None)
  1617. self.observer.load(self.save)
  1618. self.save = None
  1619. if self.defer:
  1620. if andExit:
  1621. self.defer.callback({'exit':True})
  1622. else:
  1623. self.defer.callback('done')
  1624. self.defer = None
  1625. def player(self, chunk: bytes):
  1626. # If we receive anything -- ABORT!
  1627. self.deactivate(True)
  1628. def journey_on(self, *_):
  1629. log.info("journey_on( {0})".format(self.count))
  1630. if self.count > 0:
  1631. self.count -= 1
  1632. c = coiterate(self.find_next_good_trade_pair())
  1633. c.addCallback(lambda unknown: self.scary())
  1634. # self.target_sector = self.proxy.find_next_good_trade_pair() # Sector going to
  1635. # self.state = 1
  1636. # self.queue_player.put("{0}\r".format(self.target_sector))
  1637. else:
  1638. self.deactivate()
  1639. def game_prompt(self, prompt: str):
  1640. log.debug("{0} : {1}".format(self.state, prompt))
  1641. if self.state == 1:
  1642. if prompt.startswith('Do you want to engage the TransWarp drive? '):
  1643. self.queue_player.put("N")
  1644. elif self.state == 2:
  1645. if prompt.startswith('Engage the Autopilot? (Y/N/Single step/Express) [Y]'):
  1646. self.queue_player.put("E")
  1647. elif prompt.startswith('Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ?'):
  1648. self.queue_player.put("N")
  1649. elif re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  1650. # We should be where we wanted to.
  1651. ports = ScriptPort(self.game)
  1652. d = ports.whenDone()
  1653. d.addCallback(self.journey_on)
  1654. d.addErrback(self.journey_on)
  1655. def game_line(self, line: str):
  1656. log.debug("line {0} : {1}".format(self.state, line))
  1657. if self.state == 1:
  1658. if line.startswith('The shortest path ('):
  1659. self.state = 2
  1660. elif line.startswith("You are already in that sector!"):
  1661. # Whoops.
  1662. ports = ScriptPort(self.game)
  1663. d = ports.whenDone()
  1664. d.addCallback(self.journey_on)
  1665. d.addErrback(self.journey_on)
  1666. class ProxyMenu(object):
  1667. """ Display ProxyMenu
  1668. Example:
  1669. from flexible import ProxyMenu
  1670. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", prompt):
  1671. menu = ProxyMenu(self.game)
  1672. """
  1673. def __init__(self, game):
  1674. self.nl = "\n\r"
  1675. self.c = merge(Style.BRIGHT + Fore.YELLOW + Back.BLUE)
  1676. self.r = Style.RESET_ALL
  1677. self.c1 = merge(Style.BRIGHT + Fore.WHITE + Back.BLUE)
  1678. self.c2 = merge(Style.NORMAL + Fore.CYAN + Back.BLUE)
  1679. # self.portdata = None
  1680. self.game = game
  1681. self.queue_game = game.queue_game
  1682. self.observer = game.observer
  1683. # Am I using self or game? (I think I want game, not self.)
  1684. # if hasattr(self.game, "portdata"):
  1685. # self.portdata = self.game.portdata
  1686. # else:
  1687. # self.portdata = {}
  1688. # if hasattr(self.game, 'warpdata'):
  1689. # self.warpdata = self.game.warpdata
  1690. # else:
  1691. # self.warpdata = {}
  1692. if hasattr(self.game, 'trade_report'):
  1693. self.trade_report = self.game.trade_report
  1694. else:
  1695. self.trade_report = []
  1696. # Yes, at this point we would activate
  1697. self.prompt = game.buffer
  1698. self.save = self.observer.save()
  1699. self.observer.connect("player", self.player)
  1700. # If we want it, it's here.
  1701. self.defer = None
  1702. self.keepalive = task.LoopingCall(self.awake)
  1703. self.keepalive.start(30)
  1704. self.menu()
  1705. def __del__(self):
  1706. log.debug("ProxyMenu {0} RIP".format(self))
  1707. def whenDone(self):
  1708. self.defer = defer.Deferred()
  1709. # Call this to chain something after we exit.
  1710. return self.defer
  1711. def menu(self):
  1712. box = Boxes(30, color=self.c)
  1713. self.queue_game.put(box.top())
  1714. self.queue_game.put(
  1715. box.row(self.c + "{0:^30}".format("TradeWars Proxy Active")))
  1716. self.queue_game.put(box.middle())
  1717. def menu_item(ch: str, desc: str):
  1718. row = self.c1 + " {0} {1}- {2}{3:25}".format(ch, self.c2, self.c1, desc)
  1719. self.queue_game.put(box.row(row))
  1720. # self.queue_game.put(
  1721. # " " + self.c1 + ch + self.c2 + " - " + self.c1 + desc + self.nl
  1722. # )
  1723. menu_item("C", "Configuration ({0})".format(len(self.game.gamedata.config)))
  1724. menu_item("D", "Display Report again")
  1725. # menu_item("Q", "Quest")
  1726. menu_item("P", "Port CIM Report ({0})".format(len(self.game.gamedata.ports)))
  1727. menu_item("W", "Warp CIM Report ({0})".format(len(self.game.gamedata.warps)))
  1728. menu_item("T", "Trading Report")
  1729. menu_item("S", "Scripts")
  1730. menu_item("X", "eXit")
  1731. self.queue_game.put(box.bottom())
  1732. self.queue_game.put(" " + self.c + "-=>" + self.r + " ")
  1733. def awake(self):
  1734. log.info("ProxyMenu.awake()")
  1735. self.game.queue_player.put(" ")
  1736. def port_report(self, portdata: dict):
  1737. # self.portdata = portdata
  1738. # self.game.portdata = portdata
  1739. self.queue_game.put("Loaded {0} ports.".format(len(portdata)) + self.nl)
  1740. self.welcome_back()
  1741. def warp_report(self, warpdata: dict):
  1742. # self.warpdata = warpdata
  1743. # self.game.warpdata = warpdata
  1744. self.queue_game.put("Loaded {0} sectors.".format(len(warpdata)) + self.nl)
  1745. self.welcome_back()
  1746. def make_trade_report(self):
  1747. log.debug("make_trade_report()")
  1748. ok_trades = []
  1749. best_trades = []
  1750. show_best = self.game.gamedata.get_config('Display_Best', 'Y').upper()[0] == 'Y'
  1751. show_ok = self.game.gamedata.get_config('Display_Ok', 'N').upper()[0] == 'Y'
  1752. show_limit = self.game.gamedata.get_config('Display_Percent', '90')
  1753. update_config = False
  1754. try:
  1755. show_limit = int(show_limit)
  1756. except ValueError:
  1757. show_limit = 90
  1758. update_config = True
  1759. if show_limit < 0:
  1760. show_limit = 0
  1761. update_config = True
  1762. elif show_limit > 100:
  1763. show_limit = 100
  1764. update_config = True
  1765. if update_config:
  1766. self.game.gamedata.set_config('Display_Percent', show_limit)
  1767. # for sector, pd in self.game.gamedata.ports.items():
  1768. for sector in sorted(self.game.gamedata.ports.keys()):
  1769. pd = self.game.gamedata.ports[sector]
  1770. if not GameData.port_burnt(pd):
  1771. pc = pd['class']
  1772. # Ok, let's look into it.
  1773. if not sector in self.game.gamedata.warps:
  1774. continue
  1775. warps = self.game.gamedata.warps[sector]
  1776. for w in warps:
  1777. # Verify that we have that warp's info, and that the sector is in it.
  1778. # (We can get back from it)
  1779. if w in self.game.gamedata.warps and sector in self.game.gamedata.warps[w]:
  1780. # Ok, we can get there -- and get back!
  1781. if w > sector and w in self.game.gamedata.ports and not GameData.port_burnt(self.game.gamedata.ports[w]):
  1782. # it is > and has a port.
  1783. wd = self.game.gamedata.ports[w]
  1784. wc = wd['class']
  1785. # 1: "BBS",
  1786. # 2: "BSB",
  1787. # 3: "SBB",
  1788. # 4: "SSB",
  1789. # 5: "SBS",
  1790. # 6: "BSS",
  1791. # 7: "SSS",
  1792. # 8: "BBB",
  1793. if pc in (1,5) and wc in (2,4):
  1794. data = self.game.gamedata.port_trade_show(sector, w, show_limit)
  1795. if data:
  1796. best_trades.append(data)
  1797. # best_trades.append( "{0:5} -=- {1:5}".format(sector, w))
  1798. elif pc in (2,4) and wc in (1,5):
  1799. data = self.game.gamedata.port_trade_show(sector, w, show_limit)
  1800. if data:
  1801. best_trades.append(data)
  1802. # best_trades.append( "{0:5} -=- {1:5}".format(sector, w))
  1803. elif GameData.port_trading(pd['port'], self.game.gamedata.ports[w]['port']):
  1804. # ok_trades.append( "{0:5} -=- {1:5}".format(sector,w))
  1805. data = self.game.gamedata.port_trade_show(sector, w, show_limit)
  1806. if data:
  1807. ok_trades.append(data)
  1808. yield
  1809. if show_best:
  1810. self.trade_report.append("Best Trades: (org/equ)")
  1811. self.trade_report.extend(best_trades)
  1812. if show_ok:
  1813. self.trade_report.append("Ok Trades:")
  1814. self.trade_report.extend(ok_trades)
  1815. if not show_best and not show_ok:
  1816. self.queue_game.put(Boxes.alert("You probably want to choose something to display in configuration!", base="red"))
  1817. # self.queue_game.put("BEST TRADES:" + self.nl + self.nl.join(best_trades) + self.nl)
  1818. # self.queue_game.put("OK TRADES:" + self.nl + self.nl.join(ok_trades) + self.nl)
  1819. def get_display_maxlines(self):
  1820. show_maxlines = self.game.gamedata.get_config('Display_Maxlines', '0')
  1821. try:
  1822. show_maxlines = int(show_maxlines)
  1823. except ValueError:
  1824. show_maxlines = 0
  1825. if show_maxlines <= 0:
  1826. show_maxlines = None
  1827. return show_maxlines
  1828. def show_trade_report(self, *_):
  1829. show_maxlines = self.get_display_maxlines()
  1830. self.game.trade_report = self.trade_report
  1831. for t in self.trade_report[:show_maxlines]:
  1832. self.queue_game.put(t + self.nl)
  1833. self.queue_game.put(self.nl + Boxes.alert("Proxy done.", base="green"))
  1834. self.observer.load(self.save)
  1835. self.save = None
  1836. self.keepalive = None
  1837. self.prompt = None
  1838. # self.welcome_back()
  1839. def player(self, chunk: bytes):
  1840. """ Data from player (in bytes). """
  1841. chunk = chunk.decode("latin-1", "ignore")
  1842. key = chunk.upper()
  1843. log.debug("ProxyMenu.player({0})".format(key))
  1844. # Stop the keepalive if we are activating something else
  1845. # or leaving...
  1846. self.keepalive.stop()
  1847. if key == "T":
  1848. self.queue_game.put(self.c + key + self.r + self.nl)
  1849. # Trade Report
  1850. # do we have enough information to do this?
  1851. # if not hasattr(self.game, 'portdata') and not hasattr(self.game, 'warpdata'):
  1852. # self.queue_game.put("Missing portdata and warpdata." + self.nl)
  1853. # elif not hasattr(self.game, 'portdata'):
  1854. # self.queue_game.put("Missing portdata." + self.nl)
  1855. # elif not hasattr(self.game, 'warpdata'):
  1856. # self.queue_game.put("Missing warpdata." + self.nl)
  1857. # else:
  1858. if True:
  1859. # Yes, so let's start!
  1860. self.trade_report = []
  1861. d = coiterate(self.make_trade_report())
  1862. d.addCallback(self.show_trade_report)
  1863. return
  1864. elif key == "P":
  1865. self.queue_game.put(self.c + key + self.r + self.nl)
  1866. self.game.gamedata.reset_ports()
  1867. # Activate CIM Port Report
  1868. report = CIMPortReport(self.game)
  1869. d = report.whenDone()
  1870. d.addCallback(self.port_report)
  1871. d.addErrback(self.welcome_back)
  1872. return
  1873. elif key == "W":
  1874. self.queue_game.put(self.c + key + self.r + self.nl)
  1875. self.game.gamedata.reset_warps()
  1876. # Activate CIM Warp Report
  1877. report = CIMWarpReport(self.game)
  1878. d = report.whenDone()
  1879. d.addCallback(self.warp_report)
  1880. d.addErrback(self.welcome_back)
  1881. return
  1882. elif key == "S":
  1883. self.queue_game.put(self.c + key + self.r + self.nl)
  1884. # Scripts
  1885. self.activate_scripts_menu()
  1886. return
  1887. elif key == "D":
  1888. self.queue_game.put(self.c + key + self.r + self.nl)
  1889. # (Re) Display Trade Report
  1890. show_maxlines = self.get_display_maxlines()
  1891. if self.trade_report:
  1892. for t in self.trade_report[:show_maxlines]:
  1893. self.queue_game.put(t + self.nl)
  1894. self.queue_game.put(self.nl + Boxes.alert("Proxy done.", base="green"))
  1895. self.observer.load(self.save)
  1896. self.save = None
  1897. self.keepalive = None
  1898. self.prompt = None
  1899. return
  1900. else:
  1901. self.queue_game.put("Missing trade_report." + self.nl)
  1902. elif key == "C":
  1903. self.queue_game.put(self.c + key + self.r + self.nl)
  1904. self.activate_config_menu()
  1905. return
  1906. # self.queue_game.put(pformat(self.portdata).replace("\n", "\n\r") + self.nl)
  1907. # self.queue_game.put(pformat(self.warpdata).replace("\n", "\n\r") + self.nl)
  1908. elif key == "Q":
  1909. self.queue_game.put(self.c + key + self.r + self.nl)
  1910. # This is an example of chaining PlayerInput prompt calls.
  1911. ask = PlayerInput(self.game)
  1912. d = ask.prompt("What is your quest?", 40, name="quest", abort_blank=True)
  1913. # Display the user's input
  1914. d.addCallback(ask.output)
  1915. d.addCallback(
  1916. lambda ignore: ask.prompt(
  1917. "What is your favorite color?", 10, name="color"
  1918. )
  1919. )
  1920. d.addCallback(ask.output)
  1921. d.addCallback(
  1922. lambda ignore: ask.prompt(
  1923. "What is the meaning of the squirrel?",
  1924. 12,
  1925. name="squirrel",
  1926. digits=True,
  1927. )
  1928. )
  1929. d.addCallback(ask.output)
  1930. def show_values(show):
  1931. log.debug(show)
  1932. self.queue_game.put(pformat(show).replace("\n", "\n\r") + self.nl)
  1933. d.addCallback(lambda ignore: show_values(ask.keep))
  1934. d.addCallback(self.welcome_back)
  1935. # On error, just return back
  1936. # This doesn't seem to be getting called.
  1937. # d.addErrback(lambda ignore: self.welcome_back)
  1938. d.addErrback(self.welcome_back)
  1939. return
  1940. elif key == "X":
  1941. self.queue_game.put(self.c + key + self.r + self.nl)
  1942. self.queue_game.put(Boxes.alert("Proxy done.", base="green"))
  1943. self.observer.load(self.save)
  1944. self.save = None
  1945. # It isn't running (NOW), so don't try to stop it.
  1946. # self.keepalive.stop()
  1947. self.keepalive = None
  1948. # Ok, this is a HORRIBLE idea, because the prompt might be
  1949. # outdated.
  1950. # self.queue_game.put(self.prompt)
  1951. self.prompt = None
  1952. # Send '\r' to re-display the prompt
  1953. # instead of displaying the original one.
  1954. self.game.queue_player.put("d")
  1955. # Were we asked to do something when we were done here?
  1956. if self.defer:
  1957. reactor.CallLater(0, self.defer.callback)
  1958. # self.defer.callback()
  1959. self.defer = None
  1960. return
  1961. self.keepalive.start(30, True)
  1962. self.menu()
  1963. def activate_config_menu(self):
  1964. self.observer.disconnect("player", self.player)
  1965. self.observer.connect("player", self.config_player)
  1966. self.config_menu()
  1967. def deactivate_config_menu(self, *data):
  1968. log.warn("deactivate_config_menu ({0})".format(data))
  1969. self.observer.disconnect("player", self.config_player)
  1970. self.observer.connect("player", self.player)
  1971. self.welcome_back()
  1972. def activate_scripts_menu(self):
  1973. self.observer.disconnect("player", self.player)
  1974. self.observer.connect("player", self.scripts_player)
  1975. self.scripts_menu()
  1976. def option_entry(self, entry):
  1977. if len(entry) > 0:
  1978. # Ok, they gave us something
  1979. self.game.gamedata.set_config(self.option_select, entry.strip())
  1980. else:
  1981. self.queue_game.put("Edit aborted." + self.nl)
  1982. self.config_menu()
  1983. def option_input(self, option):
  1984. if len(option) > 0:
  1985. option = int(option)
  1986. if option in self.config_opt:
  1987. # Ok, it's a valid option!
  1988. self.option_select = self.config_opt[option]
  1989. ask = PlayerInput(self.game)
  1990. d = ask.prompt("Change {0} to?".format(self.option_select), 18)
  1991. d.addCallback(self.option_entry)
  1992. # d.addErrback(self.config_menu)
  1993. else:
  1994. self.queue_game.put("Unknown option, sorry." + self.nl)
  1995. self.config_menu()
  1996. else:
  1997. # Aborted
  1998. self.config_menu()
  1999. def config_player(self, chunk: bytes):
  2000. """ Data from player (in bytes). """
  2001. chunk = chunk.decode("latin-1", "ignore")
  2002. key = chunk.upper()
  2003. if key == 'C':
  2004. self.queue_game.put(self.c + key + self.r + self.nl)
  2005. self.game.gamedata.config = {}
  2006. elif key == 'E':
  2007. self.queue_game.put(self.c + key + self.r + self.nl)
  2008. ask = PlayerInput(self.game)
  2009. d = ask.prompt("Which to edit?", 4, name='option', abort_blank=True, digits=True)
  2010. d.addCallback(self.option_input)
  2011. d.addErrback(self.config_menu)
  2012. return
  2013. elif key == 'X':
  2014. self.queue_game.put(self.c + key + self.r + self.nl)
  2015. self.deactivate_config_menu()
  2016. return
  2017. else:
  2018. self.queue_game.put(self.c + "?" + self.r + self.nl)
  2019. self.config_menu()
  2020. def config_menu(self, *_):
  2021. titlecolor = merge(Style.BRIGHT + Fore.CYAN + Back.BLUE)
  2022. tc = merge(Style.BRIGHT + Fore.YELLOW + Back.BLUE)
  2023. c1 = merge(Style.BRIGHT + Fore.WHITE + Back.BLUE)
  2024. c2 = merge(Style.BRIGHT + Fore.CYAN + Back.BLUE)
  2025. #box = Boxes(44, color=titlecolor)
  2026. box = Boxes(44, color=tc)
  2027. self.queue_game.put(box.top())
  2028. #self.queue_game.put(box.row(titlecolor + "{0:^44}".format("Configuration")))
  2029. self.queue_game.put(box.row(tc + "{0:^44}".format("Configuration")))
  2030. self.queue_game.put(box.middle())
  2031. def config_option(index, key, value):
  2032. row = "{0}{1:2} {2:19}{3}{4:<20}".format(c1, index, key, c2, value)
  2033. self.queue_game.put(box.row(row))
  2034. def menu_item(ch, desc):
  2035. row = "{0} {1} {2}-{3} {4:39}".format(c1, ch, c2, c1, desc)
  2036. # self.queue_game.put(
  2037. # " " + c1 + ch + c2 + " - " + c1 + desc + self.nl
  2038. # )
  2039. self.queue_game.put(box.row(row))
  2040. index = 1
  2041. self.config_opt = {}
  2042. for k in sorted(self.game.gamedata.config.keys()):
  2043. # for k, v in self.game.gamedata.config.items():
  2044. v = self.game.gamedata.config[k]
  2045. self.config_opt[index] = k
  2046. config_option(index, k, v)
  2047. index += 1
  2048. self.queue_game.put(box.middle())
  2049. menu_item("C", "Clear Config")
  2050. menu_item("E", "Edit Item")
  2051. menu_item("X", "eXit")
  2052. self.queue_game.put(box.bottom())
  2053. self.queue_game.put(" " + tc + "-=>" + self.r + " ")
  2054. def deactivate_scripts_menu(self, *data):
  2055. log.warn("deactivate_scripts_menu ({0})".format(data))
  2056. self.observer.disconnect("player", self.scripts_player)
  2057. self.observer.connect("player", self.player)
  2058. # Did they request exit?
  2059. if len(data) > 0 and type(data[0]) == dict:
  2060. info = data[0]
  2061. if 'exit' in info and info['exit']:
  2062. log.warn("exit proxy...")
  2063. # Exit Proxy Code
  2064. self.queue_game.put(self.nl + Boxes.alert("Proxy done.", base="green",style=3))
  2065. self.observer.load(self.save)
  2066. self.save = None
  2067. # It isn't running (NOW), so don't try to stop it.
  2068. # self.keepalive.stop()
  2069. self.keepalive = None
  2070. # Ok, this is a HORRIBLE idea, because the prompt might be
  2071. # outdated.
  2072. # self.queue_game.put(self.prompt)
  2073. self.prompt = None
  2074. # I'm not sure where we are, we might not be at a prompt.
  2075. # let's check!
  2076. if re.match(r"Command \[TL=.* \(\?=Help\)\? :", self.game.getPrompt()):
  2077. # Send '\r' to re-display the prompt
  2078. # instead of displaying the original one.
  2079. self.game.queue_player.put("d")
  2080. # Were we asked to do something when we were done here?
  2081. if self.defer:
  2082. reactor.CallLater(0, self.defer.callback)
  2083. # self.defer.callback()
  2084. self.defer = None
  2085. return
  2086. log.warn("calling welcome_back")
  2087. self.welcome_back()
  2088. def scripts_menu(self, *_):
  2089. c1 = merge(Style.BRIGHT + Fore.CYAN)
  2090. c2 = merge(Style.NORMAL + Fore.CYAN)
  2091. box = Boxes(40, color=c1)
  2092. self.queue_game.put(box.top())
  2093. self.queue_game.put(box.row(c1 + "{0:^40}".format("Scripts")))
  2094. self.queue_game.put(box.middle())
  2095. def menu_item(ch, desc):
  2096. row = " {0}{1} {2}-{3} {4:35}".format(c1, ch, c2, c1, desc)
  2097. # self.queue_game.put(
  2098. # " " + c1 + ch + c2 + " - " + c1 + desc + self.nl
  2099. # )
  2100. self.queue_game.put(box.row(row))
  2101. menu_item("1", "Ports (Trades between two sectors)")
  2102. menu_item("2", "Explore (Strange new sectors)")
  2103. menu_item("3", "Space... the final frontier...")
  2104. menu_item("X", "eXit")
  2105. self.queue_game.put(box.bottom())
  2106. self.queue_game.put(" " + c1 + "-=>" + self.r + " ")
  2107. def terror(self, *_):
  2108. log.debug("terror {0}".format(_))
  2109. loops = _[0]
  2110. if loops.strip() == '':
  2111. self.deactivate_scripts_menu()
  2112. else:
  2113. # Ok, we have something here, I think...
  2114. terror = ScriptTerror(self.game, self, int(loops))
  2115. d = terror.whenDone()
  2116. d.addCallback(self.deactivate_scripts_menu)
  2117. d.addErrback(self.deactivate_scripts_menu)
  2118. def scripts_player(self, chunk: bytes):
  2119. """ Data from player (in bytes). """
  2120. chunk = chunk.decode("latin-1", "ignore")
  2121. key = chunk.upper()
  2122. if key == '1':
  2123. self.queue_game.put(self.c + key + self.r + self.nl)
  2124. # Activate this magical event here
  2125. ports = ScriptPort(self.game)
  2126. d = ports.whenDone()
  2127. # d.addCallback(self.scripts_menu)
  2128. # d.addErrback(self.scripts_menu)
  2129. d.addCallback(self.deactivate_scripts_menu)
  2130. d.addErrback(self.deactivate_scripts_menu)
  2131. return
  2132. elif key == '!':
  2133. self.queue_game.put(self.c + key + self.r + self.nl)
  2134. ask = PlayerInput(self.game)
  2135. d = ask.prompt("How many loops of terror?", 4, name="loops", digits=True, abort_blank=True)
  2136. d.addCallback(self.terror, ask)
  2137. return
  2138. elif key == '2':
  2139. self.queue_game.put(self.c + key + self.r + self.nl)
  2140. explore = ScriptExplore(self.game)
  2141. d = explore.whenDone()
  2142. d.addCallback(self.deactivate_scripts_menu)
  2143. d.addErrback(self.deactivate_scripts_menu)
  2144. return
  2145. elif key == '3':
  2146. self.queue_game.put(self.c + key + self.r + self.nl)
  2147. space = ScriptSpace(self.game)
  2148. d = space.whenDone()
  2149. d.addCallback(self.deactivate_scripts_menu)
  2150. d.addErrback(self.deactivate_scripts_menu)
  2151. return
  2152. elif key == 'X':
  2153. self.queue_game.put(self.c + key + self.r + self.nl)
  2154. self.deactivate_scripts_menu()
  2155. return
  2156. else:
  2157. self.queue_game.put(self.c + "?" + self.r + self.nl)
  2158. self.scripts_menu()
  2159. def welcome_back(self, *_):
  2160. log.debug("welcome_back")
  2161. self.keepalive.start(30, True)
  2162. self.menu()