Update the menu.
[koperkapel.git] / koperkapel / world.py
index f06d3bcbca1f4063e91dd9f300191c3f1f1639e1..7115194c87e57d6cc299eda89850714172615126 100644 (file)
@@ -3,6 +3,33 @@
 from .scenes.base import WorldEvent
 
 
+NAMES =[
+        "roupert",
+        "roachel",
+        "roeginald",
+        "roichard",
+        "rory",
+        "roalph",
+        "roabia",
+        "roafi",
+        "roaman",
+        "roemus",
+        "roadley",
+        "roanaell",
+        "roashwan",
+        "roashid",
+        "roaphael",
+        "roenfield",
+        "roani",
+        "roaya",
+        "roaza",
+        "robekka",
+        "rogan",
+        "roiana",
+        "roberta",
+       ]
+
+
 class World:
     """ World and player state. """
 
@@ -20,33 +47,40 @@ class World:
     def _build_initial_state(self):
         state = {}
         state["roaches"] = [
-            self._build_roach("roachel", intelligence=3),
-            self._build_roach("roeginald", strength=3),
-            self._build_roach("roichard", quickness=3),
+            #self._build_roach("roachel", smart=True),
+            #self._build_roach("roeginald", strong=True),
+            #self._build_roach("roichard", fast=True),
+            self._build_roach("roupert"),
+        ]
+        state["serums"] = [
+            "smart", "strong", "fast",
         ]
         state["vehicles"] = {
             "current": "walking",
-            "available": ["walking"],
+            "walking": {
+                "seating": [
+                    "roachel", None, "roeginald",
+                    None, None, None,
+                ]
+            },
+            "robot": {"seating": []},
+            "roomba": {"seating": []},
+            "quadcopter": {"seating": []},
         }
         state["level"] = {
             "name": "level1",
         }
         return state
 
-    def _build_roach(self, name, **kw):
-        attributes = {
-            "intelligence": 1,
-            "strength": 1,
-            "quickness": 1,
-            "health": 5,
-        }
-        attributes.update(kw)
-        return {
+    def _build_roach(self, name, health=5, **kw):
+        roach = {
             "name": name,
-            "attributes": attributes,
+            "health": health,
         }
+        roach.update(kw)
+        return roach
 
-    def _apply_set(self, updates):
+    def _apply_set(self, action, updates):
         for name, value in updates.items():
             parts = name.split(".")
             obj = self._state
@@ -59,13 +93,26 @@ class World:
                     raise KeyError("%r not found in world" % (name,))
             obj[parts[-1]] = value
 
+    def _apply_reset(self, action):
+        self._state = self._build_initial_state()
+
+    def _apply_unknown(self, action, *args, **kw):
+        raise ValueError("Unknown world event action: %r" % (action,))
+
     def proxy(self):
         return WorldDictProxy(self._state)
 
+    def add_new_roach(self):
+        roach_names = [x['name'] for x in self.roaches]
+        for cand in NAMES:
+            if cand not in roach_names:
+                roach = self._build_roach(cand)
+                self._state['roaches'].append(roach)
+                break
+
     def apply_event(self, action, *args, **kw):
-        if action == "set":
-            return self._apply_set(*args, **kw)
-        raise ValueError("Unknown world event action: %r" % (action,))
+        handler = getattr(self, "_apply_%s" % (action,))
+        return handler(action, *args, **kw)
 
 
 def _maybe_subproxy(proxy, name, value):
@@ -116,7 +163,15 @@ class WorldDictProxy(WorldBaseProxy):
         self._top._record_change("%s%s" % (self._prefix, name), value)
 
     def __getattr__(self, name):
-        return _maybe_subproxy(self, name, self._state[name])
+        # return None for attributes that don't exist
+        value = self._state.get(name)
+        return _maybe_subproxy(self, name, value)
+
+    def __setitem__(self, name, value):
+        return self.__setattr__(name, value)
+
+    def __getitem__(self, name):
+        return self.__getattr__(name)
 
 
 class WorldListProxy(WorldBaseProxy):
@@ -127,3 +182,9 @@ class WorldListProxy(WorldBaseProxy):
 
     def __getitem__(self, index):
         return _maybe_subproxy(self, index, self._state[index])
+
+    def __len__(self):
+        return len(self._state)
+
+    def __bool__(self):
+        return bool(self._state)