slotUI/SlotCli.py

changeset 95
85c4a4da2ffd
parent 94
c7ff8cde6b1e
child 96
bcba10c35f09
--- a/slotUI/SlotCli.py	Sat Dec 17 15:38:17 2011 +0100
+++ b/slotUI/SlotCli.py	Sat Dec 17 16:47:04 2011 +0100
@@ -12,7 +12,7 @@
 from copy import copy
 import curses
 
-VERSION = "1.5"
+VERSION = "1.6"
 MAXSLOTS = 6
 TERM = {
     "caption": "\033[1;37m\033[1;44m",
@@ -38,6 +38,7 @@
         "panic":           os.path.abspath(SOUNDPREFIX + "sound/panic.mp3"),
         "panic_shortcut":  os.path.abspath(SOUNDPREFIX + "sound/panic_shortcut.mp3"),
         "resume":          os.path.abspath(SOUNDPREFIX + "sound/resume.mp3"),
+        "win":             os.path.abspath(SOUNDPREFIX + "sound/win.mp3"),
 
     }
 
@@ -55,6 +56,7 @@
         self.slot_dummy = {
             "name": "Unnamed",
             "laps": 0,
+            "laps_last": 0,
             "last": 0.00,
             "best": 0.00,
             "fuel": 0,
@@ -64,6 +66,7 @@
             "status": "Idle",
             "clk": 0,
             "car": 0,
+            "limit": 15,
             }
 
         self.slot = [
@@ -82,6 +85,7 @@
         idx = 0
         for slt in self.slot:
             slt["laps"] = 0
+            slt["laps_last"] = 0
             slt["last"] = 0.00
             slt["best"] = 0.00
             slt["fuel"] = 100
@@ -90,6 +94,7 @@
             slt["car"] = idx # used for sort order calculation
             slt["status"] = self.slot_dummy["status"]
             slt["clk"] = 0
+            slt["limit"] = 15
             idx += 1
         self.bestlap = 99999.00
 
@@ -134,18 +139,19 @@
                 ),
                 curses.color_pair(11 + idx) )
             self.scr.addstr((4 + (self.slot[idx]["position"] * 2)), 4,
-                "  %4i | %7.2fs | %7.2fs | %3i%% | %10s %19s" % (
+                "  %4i | %7.2fs | %7.2fs | %3i%% | %10s | %2i% 15s" % (
                 self.slot[idx]["laps"],
                 self.slot[idx]["best"],
                 self.slot[idx]["last"],
                 self.slot[idx]["fuel"],
                 self.slot[idx]["status"],
+                self.slot[idx]["limit"],
                 ""
                 ),
                 curses.color_pair(11 + idx) )
 
     def cleartop(self):
-        self.scr.addstr(0,0, "%80s" % "Live monitor running, press keys to control or (q)uit")
+        self.scr.addstr(0,0, "%60s" % "Race Limits: %i Laps / %i Minutes" % (self.laplimit, self.timelimit))
         self.scr.addstr(1,0, "%80s" % " ")
 
     def readName(self, slot):
@@ -164,19 +170,30 @@
         curses.noecho()
         self.scr.nodelay(1) # disable delay on readkey
 
-    def readInt(self, msg, default):
+    def readLimit(self, slot):
+        limit = self.readInt("SPEEDLIMIT for %s (%i)" % (
+            self.slot[slot]["name"],
+            slot + 1),
+            self.slot[slot]["limit"], 15)
+        if limit:
+            self.slot[slot]["limit"] = limit
+            self.cleartop()
+            self.box.speedlimit(slot, limit)
+
+
+    def readInt(self, msg, default, maximum = 999999):
         self.scr.nodelay(0) # enable delay on readkey
         curses.echo()
         self.scr.addstr(0,0, "%s [%i]:" % (
             msg,
-            default,
+            default),
             curses.color_pair(1))
-            )
         self.scr.refresh()
         inp = self.scr.getstr(1,0, 4)
         if inp != "":
             try:
                 inp = int(inp)
+                if inp > maximum: inp = maximum
             except Exception:
                 inp = None
         else:
@@ -226,7 +243,7 @@
 
         while 1:
             key = self.scr.getch()
-            if key == ord('q'): break
+            if key == ord('c'): break
             elif key == ord(' '): self.box.query("+") # panic / resume
             elif key == 10: self.box.query("#") # remote start button press
             elif key == ord('1'): self.readName(0)
@@ -235,6 +252,12 @@
             elif key == ord('4'): self.readName(3)
             elif key == ord('5'): self.readName(4)
             elif key == ord('6'): self.readName(5)
+            elif key == ord('q'): self.readLimit(0)
+            elif key == ord('w'): self.readLimit(1)
+            elif key == ord('e'): self.readLimit(2)
+            elif key == ord('r'): self.readLimit(3)
+            elif key == ord('t'): self.readLimit(4)
+            elif key == ord('z'): self.readLimit(5)
             elif key == ord('a'):
                 if self.slot[4]["drive"] > 0: self.slot[4]["drive"] -= 1
                 cli.box.speedminimum(4, self.slot[4]["drive"])
@@ -270,6 +293,7 @@
                         slot = int(data[3]) - 1
                         t = int(data[4], 16) / 2000.00
                         self.sysclk = int(data[5], 16) / 2000.00
+                        self.slot[slot]["laps_last"] = self.slot[slot]["laps"]
                         self.slot[slot]["laps"] = l
                         self.slot[slot]["last"] = t
                         self.slot[slot]["clk"] = self.sysclk
@@ -280,6 +304,12 @@
                             self.bestlap = t
 
                         self.slot[slot]["status"] = "IN-RACE"
+                        if (self.slot[slot]["laps_last"] != l) and (l == self.laplimit):
+                            # we have lap limit reached!
+                            trigger_sound("win")
+                            self.slot[slot]["status"] = "WINNER!"
+                            self.box.query("+") # stop race
+
                         self.render_slots()
 
                     if rx[:2] == "F:":
@@ -369,6 +399,16 @@
                         trigger_sound("resume")
 
 
+                    if (self.sysclk_last != self.sysclk) and ((self.sysclk / 60) >= self.timelimit):
+                            # we have time limit reached!
+                            trigger_sound("win")
+                            # get the one with position 1
+                            for slot in self.slots:
+                                if slot["position"] == 1:
+                                    slot["status"] = "WINNER!"
+                            self.box.query("+") # stop race
+
+
 
                     self.scr.addstr(17,31,
                         "Race Timer: %7.3f min" % (self.sysclk / 60),

mercurial