session change (add the option name)
authorNiki Roo <niki@nikiroo.be>
Wed, 27 Aug 2025 15:06:07 +0000 (17:06 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 27 Aug 2025 15:06:07 +0000 (17:06 +0200)
gamiki/game.py

index ea442fb6cd049a0bbd33cec8f44ada8d7ee5e082..0d7a21b9744472b17b56cf204e2a8fd91fcda584 100644 (file)
@@ -8,6 +8,19 @@ from random   import choice
 class Game(dict):
     """Game object generated from a directory."""
     
+    
+    # Known tags:
+    # > Name: full name
+    # > Code: short code to find it with gamiki
+    # > Desc: backslash-n encoded
+    # > From: ((Maker;)Editor;)Publisher
+    # > Icon: icon for the game, see below
+    # > Intr: introduction videos (can be multiple, semicolon-separated)
+    # > Opts: optional modes, see below
+    # > Tags: semicolon-separated tags
+    # > Vers: version, if relevant
+    # > Year: year of game
+    
     dir            : PurePath = None
     name           : str      = ""
     code           : str      = None
@@ -17,7 +30,7 @@ class Game(dict):
     desc           : str      = ""
     total_time     : int      = 0 
     total_sessions : int      = 0
-    sessions       : list     = None # list of (YYYY-MM-DD, HH:MM, time in sec)
+    sessions       : list     = None # list of (YYYY-MM-DD, HH:MM, secs, opt)
     icon           : Path     = None # "main" icon
     icons          : dict     = None # all found icons
     intro          : list     = None # all found intro videos
@@ -85,10 +98,17 @@ class Game(dict):
                 ln = data.readline() # number of sessions
                 sessions = int(ln.split(":", maxsplit=1)[1].strip())
                 ln = data.readline() # total time in human formated time
-                ln = data.readline() # total time in seconds
-                total = int(ln.split(":", maxsplit=1)[1].strip())
+                human = ln.split(":", maxsplit=1)[1].strip()
+                tab = human.split(":")
+                total = int((int)(tab[0]) * 3600 
+                          + (int)(tab[1]) * 60 
+                          + (int)(tab[2])
+                )
+                
                 self.total_time = total
                 self.total_sessions = sessions
+                
+                # TODO: read the total per option times
         except FileNotFoundError:
             pass
         except Exception as e:
@@ -136,36 +156,44 @@ class Game(dict):
                         start_d = ""
                         start_h = ""
                         total = 0
+                        so = ""
                         tab = ln.strip().split(" ")
                         if (len(tab) >= 4):
                             start_d = tab[2]
-                            start_h = tab[3]
-                        tab = ln.strip().split(":")
-                        if (len(tab) == 5):
-                            total += int(tab[2]) * 3600 
-                            total += int(tab[3]) * 60 
-                            total += int(tab[4])
+                            start_h = tab[3].split("(")[1].split(")")[0]
+                        if (len(tab) >= 6):
+                            htab = tab[5].split(":")
+                            if (len(htab) == 3):
+                                total += int(htab[0]) * 3600 
+                                total += int(htab[1]) * 60 
+                                total += int(htab[2])
+                        if (len(tab) >= 8):
+                            so = tab[7]
                         if (start_d and start_h):
-                            self.sessions.append((start_d, start_h, total))
+                            self.sessions.append((start_d, start_h, total, so))
             except Exception as e:
                 print(f"Error when querying {file.as_posix()}: {e}",file=stderr)
     
-    def add_session(self, when: datetime, elapsed: int):
+    def add_session(self, when: datetime, elapsed: int, opt: str):
         def human(seconds: int) -> str:
             ss = (seconds % 60)
             mm = (seconds // 60) % 60
             hh = (seconds // 3600)
             return f"{hh:02}:{mm:02}:{ss:02}"
-    
+        
+        O = ""
+        if (opt):
+            O = f" - {opt}"
+        
         start_d = when.strftime('%Y-%m-%d')
         start_h = when.strftime('%H:%M')
-        text = f"Session of {start_d} ({start_h}) lasted: {human(elapsed)}"
+        text = f"Session of {start_d} ({start_h}) lasted: {human(elapsed)}{O}"
         print(f"[{self.name}]: {text}")
         
         if (not self.sessions):
             self._read_sessions()
         
-        self.sessions.append((start_d, start_h, elapsed))
+        self.sessions.append((start_d, start_h, elapsed, opt))
         
         total = 0
         for session in self.sessions:
@@ -190,7 +218,22 @@ class Game(dict):
                 with open(file, "w", encoding="utf-8") as data:
                     data.write(f"Sessions: {len(self.sessions)}\n")
                     data.write(f"Total time: {human(total)}\n")
-                    data.write(f"...in seconds: {total}\n")
+                    
+                    if (len(self.opts) > 1):
+                        data.write("\nTotal per option:\n")
+                        total = 0
+                        for session in self.sessions:
+                            if (session[3] == None or session[3] == ""):
+                                total += session[2]
+                        data.write(f"* {human(total)}\n");
+                        for opt in self.opts:
+                            total = 0
+                            for session in self.sessions:
+                                if (len(session) > 3):
+                                    if (session[3] == opt):
+                                        total += session[2]
+                            data.write(f"* {human(total)} - {opt}\n");
+                            
         except:
             print("Cannot save total time", file=stderr)
 
@@ -212,5 +255,5 @@ class Game(dict):
         
         elapsed = int(time() - begin)
         if (elapsed >= 60): # Ignore very short sessions
-            self.add_session(today, elapsed)
+            self.add_session(today, elapsed, opt)