+#----- ship management aid -----
+
+class Display_dumb(ProgressPrintPercentage):
+ def __init__(self):
+ ProgressPrintPercentage.__init__(self)
+ def show(self, s):
+ print '\n\n', s;
+ def realstart(self):
+ pass
+
+class Display_overwrite(ProgressPrintPercentage):
+ def __init__(self):
+ ProgressPrintPercentage.__init__(self)
+
+ null = file('/dev/null','w')
+ curses.setupterm(fd=null.fileno())
+
+ self._clear = curses.tigetstr('clear')
+ if not self._clear:
+ self._debug('missing clear!')
+ self.show = Display_dumb.show
+ return
+
+ self._t = {'el':'', 'ed':''}
+ if not self._init_sophisticated():
+ for k in self._t.keys(): self._t[k] = ''
+ self._t['ho'] = self._clear
+
+ def _debug(self,m): debug('display overwrite: '+m)
+
+ def _init_sophisticated(self):
+ for k in self._t.keys():
+ s = curses.tigetstr(k)
+ self._t[k] = s
+ self._t['ho'] = curses.tigetstr('ho')
+ if not self._t['ho']:
+ cup = curses.tigetstr('cup')
+ self._t['ho'] = curses.tparm(cup,0,0)
+ missing = [k for k in self._t.keys() if not self._t[k]]
+ if missing:
+ self.debug('missing '+(' '.join(missing)))
+ return 0
+ return 1
+
+ def show(self, s):
+ w = sys.stdout.write
+ def wti(k): w(self._t[k])
+
+ wti('ho')
+ nl = ''
+ for l in s.rstrip().split('\n'):
+ w(nl)
+ w(l)
+ wti('el')
+ nl = '\r\n'
+ wti('ed')
+ w(' ')
+ sys.stdout.flush()
+
+ def realstart(self):
+ sys.stdout.write(self._clear)
+ sys.stdout.flush()
+
+
+def do_ship_aid(args, bu):
+ if opts.ship_duty is None: opts.ship_duty = True
+
+ displayer = globals()['Display_'+opts.display]()
+
+ (myself, track) = prep_chat_log(args, bu, progress=displayer)
+
+ displayer.realstart()
+
+ if os.isatty(0): kr_create = KeystrokeReader
+ else: kr_create = DummyKeystrokeReader
+
+ try:
+ kreader = kr_create(0, 10)
+ ship_aid_core(myself, track, displayer, kreader)
+ finally:
+ kreader.stop()
+ print '\n'
+
+class KeyBasedSorter:
+ def compar_key_pa(self, pa):
+ pi = pa.pirate_info()
+ if pi is None: return None
+ return self.compar_key(pi)
+ def lsort_pa(self, l):
+ l.sort(key = self.compar_key_pa)
+
+class NameSorter(KeyBasedSorter):
+ def compar_key(self, pi): return pi.name
+ def desc(self): return 'name'
+
+class SkillSorter(NameSorter):
+ def __init__(self, relevant):
+ self._want = frozenset(relevant.split('/'))
+ self._avoid = set()
+ for p in core_duty_puzzles:
+ if isinstance(p,basestring): self._avoid.add(p)
+ else: self._avoid |= set(p)
+ self._avoid -= self._want
+ self._desc = '%s' % relevant
+
+ def desc(self): return self._desc
+
+ def compar_key(self, pi):
+ best_want = max([
+ pi.standings.get(puz,-1)
+ for puz in self._want
+ ])
+ best_avoid = [
+ -pi.standings.get(puz,standing_limit)
+ for puz in self._avoid
+ ]
+ best_avoid.sort()
+ def negate(x): return -x
+ debug('compar_key %s bw=%s ba=%s' % (pi.name, `best_want`,
+ `best_avoid`))
+ return (-best_want, map(negate, best_avoid), pi.name)
+
+def ship_aid_core(myself, track, displayer, kreader):
+
+ def find_vessel():
+ vn = track.vesselname()
+ if vn: return (vn, " on board the %s" % vn)
+ vn = track.lastvesselname()
+ if vn: return (vn, " ashore from the %s" % vn)
+ return (None, " not on a vessel")
+
+ def timeevent(t,e):
+ if t is None: return ' ' * 22
+ return " %-4s %-16s" % (format_time_interval(now - t),e)
+
+ displayer.show(track.myname() + find_vessel()[1] + '...')
+
+ rotate_nya = '/-\\'
+
+ sort = NameSorter()
+
+ while True:
+ track.catchup()
+ now = time.time()
+
+ (vn, s) = find_vessel()
+ s = track.myname() + s
+ s += " at %s" % time.strftime("%Y-%m-%d %H:%M:%S")
+ s += kreader.info()
+ s += '\n'
+
+ aboard = track.aboard(vn)
+ sort.lsort_pa(aboard)
+
+ tbl_s = StringIO()
+ tbl = StandingsTable(tbl_s)
+
+ if track.vesselname(): howmany = ' %d aboard' % len(aboard)
+ else: howmany = ''
+
+ tbl.headings(howmany, ' sorted by '+sort.desc())
+
+ for pa in aboard:
+ pi = pa.pirate_info()