Commits

Marcin Kasperski committed 13e1b75

Added example of periodical finger refresh

Comments (0)

Files changed (1)

sample/clock_statistician.py

 """
 
 import getopt, sys, logging, os
-from twisted.internet import defer, reactor
+from twisted.internet import defer, reactor, task
 from twisted.enterprise import adbapi
 
 from mekk.fics import fics_connector
     def on_login(self, my_username):
         print "I am logged as %s, use \"tell %s help\" to start conversation on FICS" % (
             my_username, my_username)
+        # Spawn periodical finger update
+        self._finger_refresh_task = task.LoopingCall(self._refresh_finger)
+        self._finger_refresh_task.start(300, now=True)
+        # Normal post-login processing
         return defer.DeferredList([
                 self.set_finger(FINGER_TEXT % my_username),
                 # Commands below are unnecessary as variables_to_set_after_login above
                 # self.subscribe_channel(90),
                 ])
 
+    def on_logout(self):
+        if hasattr(self, '_finger_refresh_task'):
+            self._finger_refresh_task.stop()
+            del self._finger_refresh_task
+
+    @defer.inlineCallbacks
+    def _refresh_finger(self):
+        best = yield self.clock_statistician.stat_best()
+        for pos, data in enumerate(best[:3]):
+            yield self.set_finger_line(8+pos, "%s %s %s - %s games" % data)
+
     @defer.inlineCallbacks
     def on_game_started(self, info):
         #current_time = datetime.datetime.utcnow()