[hamster-applet] trying to hand out the achievements when they are happening. plus added the welcome trophy (have to



commit 27a887fac236d38ef3684437585847322ba28b05
Author: Toms Bauģis <toms baugis gmail com>
Date:   Sat Aug 21 13:40:14 2010 +0100

    trying to hand out the achievements when they are happening. plus added the welcome trophy (have to test if it's not too eager)

 src/hamster-time-tracker |   14 ++++++++----
 src/hamster/applet.py    |   17 ++++++++++-----
 src/hamster/client.py    |    4 +--
 src/hamster/trophies.py  |   48 +++++++++++++++++++++++++++------------------
 4 files changed, 50 insertions(+), 33 deletions(-)
---
diff --git a/src/hamster-time-tracker b/src/hamster-time-tracker
index 8e2a503..9b6581d 100755
--- a/src/hamster-time-tracker
+++ b/src/hamster-time-tracker
@@ -79,10 +79,7 @@ class ProjectHamster(object):
 
         # Load today's data, activities and set label
         self.last_activity = None
-        self.load_day()
-
-        # refresh hamster every 60 seconds to update duration
-        gobject.timeout_add_seconds(60, self.refresh_hamster)
+        self.todays_facts = None
 
         runtime.storage.connect('activities-changed',self.after_activity_update)
         runtime.storage.connect('facts-changed',self.after_fact_update)
@@ -128,6 +125,12 @@ class ProjectHamster(object):
 
         self.window.show_all()
 
+        self.load_day()
+        # refresh hamster every 60 seconds to update duration
+        gobject.timeout_add_seconds(60, self.refresh_hamster)
+        self.refresh_hamster()
+
+
     def init_workspace_tracking(self):
         if not wnck: # can't track if we don't have the trackable
             return
@@ -141,6 +144,7 @@ class ProjectHamster(object):
         """refresh hamster every x secs - load today, check last activity etc."""
         try:
             self.check_user()
+            trophies.check_ongoing(self.todays_facts)
         finally:  # we want to go on no matter what, so in case of any error we find out about it sooner
             return True
 
@@ -176,7 +180,7 @@ class ProjectHamster(object):
         """sets up today's tree and fills it with records
            returns information about last activity"""
 
-        facts = runtime.storage.get_todays_facts()
+        facts = self.todays_facts = runtime.storage.get_todays_facts()
 
         self.treeview.detach_model()
 
diff --git a/src/hamster/applet.py b/src/hamster/applet.py
index d78c964..9ab5b5d 100755
--- a/src/hamster/applet.py
+++ b/src/hamster/applet.py
@@ -245,12 +245,9 @@ class HamsterApplet(object):
 
         # Load today's data, activities and set label
         self.last_activity = None
-        self.load_day()
-        self.update_label()
+        self.todays_facts = None
 
 
-        # refresh hamster every 60 seconds to update duration
-        gobject.timeout_add_seconds(60, self.refresh_hamster)
         runtime.storage.connect('activities-changed', self.after_activity_update)
         runtime.storage.connect('facts-changed', self.after_fact_update)
         runtime.storage.connect('toggle-called', self.on_toggle_called)
@@ -266,9 +263,15 @@ class HamsterApplet(object):
             self.notification.set_urgency(pynotify.URGENCY_LOW) # lower than grass
 
         self._gui.connect_signals(self)
-
         self.prev_size = None
 
+        self.load_day()
+        gobject.timeout_add_seconds(60, self.refresh_hamster) # refresh hamster every 60 seconds to update duration
+        self.refresh_hamster()
+
+
+
+
     def init_workspace_tracking(self):
         if not wnck: # can't track if we don't have the trackable
             return
@@ -287,9 +290,11 @@ class HamsterApplet(object):
 
             self.update_label()
             self.check_user()
+            trophies.check_ongoing(self.todays_facts)
         finally:  # we want to go on no matter what, so in case of any error we find out about it sooner
             return True
 
+
     def update_label(self):
         if self.last_activity and self.last_activity['end_time'] is None:
             delta = dt.datetime.now() - self.last_activity['start_time']
@@ -334,7 +339,7 @@ class HamsterApplet(object):
         """sets up today's tree and fills it with records
            returns information about last activity"""
 
-        facts = runtime.storage.get_todays_facts()
+        facts = self.todays_facts = runtime.storage.get_todays_facts()
 
         if facts and facts[-1]["end_time"] == None:
             self.last_activity = facts[-1]
diff --git a/src/hamster/client.py b/src/hamster/client.py
index cbb42c5..0f558a1 100644
--- a/src/hamster/client.py
+++ b/src/hamster/client.py
@@ -111,9 +111,7 @@ class Storage(gobject.GObject):
         """returns facts of the current date, respecting hamster midnight
            hamster midnight is stored in gconf, and presented in minutes
         """
-        today = [from_dbus_fact(fact) for fact in self.conn.GetTodaysFacts()]
-        checker.check_today(today)
-        return today
+        return [from_dbus_fact(fact) for fact in self.conn.GetTodaysFacts()]
 
     def get_facts(self, date, end_date = None, search_terms = ""):
         """Returns facts for the time span matching the optional filter criteria.
diff --git a/src/hamster/trophies.py b/src/hamster/trophies.py
index b10a438..70666a4 100644
--- a/src/hamster/trophies.py
+++ b/src/hamster/trophies.py
@@ -45,29 +45,39 @@ def increment(counter_id, context = ""):
     if not storage: return 0
     return storage.increment_counter("hamster-applet", counter_id, context)
 
-class Checker(object):
-    def __init__(self):
-        # use runtime flags where practical
-        self.flags = {}
 
 
-    def check_today(self, facts):
-        if not storage: return
+def check_ongoing(todays_facts):
+    if not storage or not todays_facts: return
+
+    last_activity = None
+    if todays_facts[-1]['end_time'] is None:
+        last_activity = todays_facts[-1]
+        last_activity['delta'] = dt.datetime.now() - last_activity['start_time']
+
+    # overwhelmed: tracking for more than 16 hours during one day
+    total = stuff.duration_minutes([fact['delta'] for fact in todays_facts])
+    if total > 16 * 60:
+        unlock("overwhelmed")
 
-        for fact in facts[-2:]: # consider just the last two current ongoing and the previous one
-            # in_the_zone - spent 6 hours on single activity
-            if fact['end_time'] and fact['end_time'] - fact['start_time'] > dt.timedelta(hours = 6):
-                unlock("in_the_zone")
+    if last_activity:
+        # Welcome! â?? track an activity for 10 minutes
+        if last_activity['delta'] >= dt.timedelta(minutes = 10):
+            unlock("welcome")
 
-            # insomnia - finish activity in a new day
-            if (fact['end_time'] and fact['start_time'].date() != fact['end_time'].date()) or \
-               (fact['end_time'] is None and fact['start_time'].date() != dt.date.today()):
-                unlock("insomnia")
+        # in_the_zone - spend 6 hours non-stop on an activity
+        if last_activity['delta'] > dt.timedelta(hours = 6):
+            unlock("in_the_zone")
 
-        # overwhelmed: tracking for more than 16 hours during one da
-        total = stuff.duration_minutes([fact['delta'] for fact in facts])
-        if total > 16 * 60:
-            unlock("overwhelmed")
+        # insomnia - meet the new day while tracking an activity
+        if last_activity['start_time'].date() != dt.date.today():
+            unlock("insomnia")
+
+
+class Checker(object):
+    def __init__(self):
+        # use runtime flags where practical
+        self.flags = {}
 
 
     def check_update_based(self, prev_id, new_id, activity_name, tags, start_time, end_time, category_name, description):
@@ -132,7 +142,7 @@ class Checker(object):
         if len(last_four) == 4:
             layered = True
             for prev, next in zip(last_four, last_four[1:]):
-                if next.start_time.date == dt.date.today() or \
+                if next.start_time.date() == dt.date.today() or \
                    next.start_time < prev.start_time or \
                    (prev.end_time and prev.end_time < next.start_time):
                     layered = False



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]