[hamster-applet] focused, insomnia, jumper, layered and wait a minute



commit fa6d81c9ff4cdee2608f641ec46a05a1959c45c2
Author: Toms Bauģis <toms baugis gmail com>
Date:   Wed Aug 18 23:41:55 2010 +0100

    focused, insomnia, jumper, layered and wait a minute

 src/hamster/trophies.py |   66 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 57 insertions(+), 9 deletions(-)
---
diff --git a/src/hamster/trophies.py b/src/hamster/trophies.py
index d05878a..c6cc921 100644
--- a/src/hamster/trophies.py
+++ b/src/hamster/trophies.py
@@ -30,6 +30,7 @@ except:
     trophies_client = None
 
 import stuff
+import datetime as dt
 
 class Checker(object):
     def __init__(self):
@@ -37,20 +38,33 @@ class Checker(object):
         if trophies_client:
             self.trophies = trophies_client.Storage()
 
-        # some state flags
-        self.last_update_id = None
+        # use runtime flags where practical
+        self.flags = {}
+
+
+    def check_today(self, facts):
+        for fact in facts[-2:]: # consider just the last two current ongoing and the previous one
+            # focused - spent 6 hours on single activity
+            if fact['end_time'] and fact['end_time'] - fact['start_time'] > dt.timedelta(hours = 6):
+                self.trophies.unlock_achievement("hamster-applet", "focused")
+
+            # 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()):
+                self.trophies.unlock_achievement("hamster-applet", "insomnia")
+
 
 
     def check_update_based(self, prev_id, new_id, activity_name, tags, start_time, end_time, category_name, description):
-        if not self.last_update_id or prev_id != self.last_update_id:
-            self.same_updates_in_row = 0
-        elif self.last_update_id == prev_id:
-            self.same_updates_in_row +=1
-        self.last_update_id = new_id
+        if not self.flags.get('last_update_id') or prev_id != self.flags['last_update_id']:
+            self.flags['same_updates_in_row'] = 0
+        elif self.flags['last_update_id'] == prev_id:
+            self.flags['same_updates_in_row'] +=1
+        self.flags['last_update_id'] = new_id
 
 
         # all wrong â?? edited same activity 5 times in a row
-        if self.same_updates_in_row == 5:
+        if self.flags['same_updates_in_row'] == 5:
             self.trophies.unlock_achievement("hamster-applet", "all_wrong")
 
 
@@ -71,9 +85,43 @@ class Checker(object):
         fact.tags = [tag.strip() for tag in tags.split(",") if tag.strip()] or fact.tags
         fact.category_name = category_name or fact.category_name
         fact.description = description or fact.description
-        fact.start_time = start_time or fact.start_time
+        fact.start_time = start_time or fact.start_time or dt.datetime.now()
         fact.end_time = end_time or fact.end_time
 
+
+        # Jumper - hidden - made 10 switches within an hour (radical)
+        if not fact.end_time: #end time normally denotes switch
+            last_ten = self.flags.setdefault('last_ten_ongoing', [])
+            last_ten.append(fact)
+            last_ten = last_ten[-10:]
+
+            if len(last_ten) == 10 and (last_ten[-1].start_time - last_ten[0].start_time) <= dt.timedelta(hours=1):
+                self.trophies.unlock_achievement("hamster-applet", "jumper")
+
+
+        # layering - entered 4 activities in a row in one of previous days, each one overlapping the previous one
+        #            avoiding today as in that case the layering
+        last_four = self.flags.setdefault('last_four', [])
+        last_four.append(fact)
+        last_four = last_ten[-4:]
+        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 \
+                   next.start_time < prev.start_time or \
+                   (prev.end_time and prev.end_time < next.start_time):
+                    layered = False
+
+            if layered:
+                self.trophies.unlock_achievement("hamster-applet", "layered")
+
+        # wait a minute! - Switch to a new activity within 60 seconds
+        if len(last_four) >= 2:
+            prev, next = last_four[-2:]
+            if prev.end_time is None and next.end_time is None and (next.start_time - prev.start_time) < dt.timedelta(minutes = 1):
+                self.trophies.unlock_achievement("hamster-applet", "wait_a_minute")
+
+
         # alpha bravo charlie â?? used delta times to enter at least 50 activities
         if fact.start_time and activity_name.startswith("-"):
             counter = self.trophies.increment_counter("hamster-applet", "alpha_bravo_charlie")



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