chiark / gitweb /
WIP ship aid. Currently does not work at all
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 13:37:29 +0000 (14:37 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 16 May 2009 13:37:29 +0000 (14:37 +0100)
yoweb-scrape

index a8a1a1169b0ee5f4ddb4b5548575d53b3c9ae348..ca174e27647efdba100a326e8559e9f6fd62d5c4 100755 (executable)
@@ -216,7 +216,7 @@ u'\\s*\\S*/([-A-Za-z]+)\\s*$|\\s*\\S*/\\S*\\s*\\(ocean\\-wide(?:\\s|\\xa0)+([-A-
                                skl.msg('puzzle "%s" multiple standings %s' %
                                                (puzzle, `sl`))
                                continue
-                       if not len(sl):
+                       if not sl:
                                skl.msg('puzzle "%s" no standing found' % puzzle)
                                continue
                        standing = sl[0]
@@ -386,6 +386,143 @@ def do_standings_crew_of(args, bu):
                        tab.pirate(pi)
        print tab.results()
 
+class PirateAboard:
+       # pa.v
+       # pa.last_time
+       # pa.last_event
+       # pa.gunner
+       def __init__(pa, v, time, event):
+               pa.v = v
+               pa.last_time = time
+               pa.last_event = event
+               pa.gunner = False
+
+class ShipCrewTracker:
+       def __init__(self, myself_pi):
+               self._pl = {}   # self._pl['Pirate'] =
+               self._vl = {}   #   self._vl['Vessel']['Pirate'] = PirateAboard
+                               # self._vl['Vessel']['#lastaboard']
+               self._v = None          # self._v =
+               self._vessel = None     #       self._vl[self._vessel]
+               self._date = None
+               self._myself = myself_pi
+               self._need_redisplay = False
+
+       def _refresh(self):
+               self._need_redisplay = True
+
+       def _onboard_event(self,timestamp,pirate,event):
+               try: pa = self._pl[pirate]
+               except KeyError: pa = None
+               if pa is not None and pa.v is self._v:
+                       pa.last_time = timestamp
+                       pa.last_event = event
+               else:
+                       if pa is not None: del pa.v[pirate]
+                       pa = PirateAboard(self._v, timestamp, event)
+                       self._pl[pirate] = pa
+                       self._v[pirate] = pa
+               self._v['#lastaboard'] = timestamp
+               self._refresh()
+               return pa
+
+       def clear_vessel(self, timestamp):
+               if self._v is not None:
+                       for p in self._v:
+                               if p.startswith('#'): continue
+                               del self._pl[p]
+               self._v = {'#lastaboard': timestamp}
+
+       def _warn(self, m):
+               pass
+
+       def chatline(self,l):
+               rm = lambda re: regexp.match(re,l)
+               m = rm('=+ (\\d+)/(\\d+)/(\\d+) =+$')
+               if m:
+                       self._date = m.groups()
+                       return
+               if self._date is None:
+                       return
+               m = rm('\\[(\d\d):(\d\d):(\d\d)\\] ')
+               if not m:
+                       self._warn('undated? '+l)
+                       return
+
+               time_tuple = [int(x) for x in self._date + m.groups()]
+               time_tuple += (-1,-1,-1)
+               print `time_tuple`
+               timestamp = time.mktime(time_tuple)
+               l = l[l.find(' ')+1:]
+
+               ob = lambda who, event: self._onboard_event(
+                       timestamp, who, event)
+               oba = lambda m, did: ob(
+                       m.group(1), '%s %s' % (did, m.group(2)))
+
+               m = rm('Going aboard the (\\S.*\\S)\\.\\.\\.$')
+               if m:
+                       self._vessel = m.group(1)
+                       try:             self._v = self._vl[self._vessel]
+                       except KeyError: self._v = None
+                       if self._v is not None:  la = self._v['#lastaboard']
+                       else:                    la = 0
+                       if timestamp - la > 3600:
+                               self.clear_vessel(timestamp)
+                       ob(self._myself.name, 'we boarded')
+
+               if self._v is None: return
+
+               m = rm('You have ordered (\\w+) to do some (\\S.*\\S)\\.$')
+               if m:
+                       pa = oba(m, 'ordered')
+                       if m.group(2) == 'Gunning':
+                               pa.gunner = True
+                       return
+
+               m = rm('(\\w+) abandoned a (\\S.*\\S) station\\.$')
+               if m: oba(m,'abandoned'); return
+
+               m = rm('(\\w+) says, "')
+               if m: ob(m.group(1), 'talked'); return
+
+               m = rm('(\\w+) has left the vessel\.')
+               if m:
+                       who = m.group(1)
+                       ob(who, 'disembarked')
+                       del self._v[who]
+                       del self._pl[who]
+                       return
+
+               return
+
+def do_ship_aid(args, bu):
+       if len(args) != 1: bu('ship-aid takes only chat log filename')
+       logfn = args[0]
+       logfn_re = '(?:.*/)?([A-Z][a-z]+)_([a-z]+)_chat-log-\\w+$'
+       match = regexp.match(logfn_re, logfn)
+       if not match: bu('ship-aid chat log filename is not in default format')
+       (pirate, fetcher.ocean) = match.groups()
+       myself_pi = PirateInfo(pirate,3600)
+       track = ShipCrewTracker(myself_pi)
+       f = file(logfn)
+       l = ''
+       while True:
+               print "1>"+l+"<"
+               l += f.readline()
+               if l.endswith('\n'):
+                       l.rstrip()
+                       print "2>"+l+"<"
+                       track.chatline(l)
+#                      print `track.__dict__`
+                       l = ''
+                       continue
+               if l:
+                       continue
+               print "3>EOF<"
+               print `track.__dict__`
+               os.sleep(1)
+
 def main():
        global opts, fetcher