1 """ World and player state. """
3 from .scenes.base import WorldEvent
34 """ World and player state. """
37 self._state = self._build_initial_state()
41 return self._state["level"]
45 return self._state["roaches"]
47 def _build_initial_state(self):
50 #self._build_roach("roachel", smart=True),
51 #self._build_roach("roeginald", strong=True),
52 #self._build_roach("roichard", fast=True),
53 self._build_roach("roupert"),
56 "smart", "strong", "fast",
62 "roachel", None, "roeginald",
66 "robot": {"seating": []},
67 "roomba": {"seating": []},
68 "quadcopter": {"seating": []},
75 def _build_roach(self, name, health=5, **kw):
83 def _apply_set(self, action, updates):
84 for name, value in updates.items():
85 parts = name.split(".")
88 if isinstance(obj, dict):
90 elif isinstance(obj, list):
93 raise KeyError("%r not found in world" % (name,))
94 obj[parts[-1]] = value
96 def _apply_reset(self, action):
97 self._state = self._build_initial_state()
99 def _apply_unknown(self, action, *args, **kw):
100 raise ValueError("Unknown world event action: %r" % (action,))
103 return WorldDictProxy(self._state)
105 def add_new_roach(self):
106 roach_names = [x['name'] for x in self.roaches]
108 if cand not in roach_names:
109 roach = self._build_roach(cand)
110 self._state['roaches'].append(roach)
113 def apply_event(self, action, *args, **kw):
114 handler = getattr(self, "_apply_%s" % (action,))
115 return handler(action, *args, **kw)
118 def _maybe_subproxy(proxy, name, value):
119 """ Return a sub world proxy if appropriate. """
120 if isinstance(value, dict):
121 prefix = "%s%s." % (proxy._prefix, name)
122 return WorldDictProxy(value, _prefix=prefix, _top=proxy._top)
123 elif isinstance(value, list):
124 prefix = "%s%s." % (proxy._prefix, name)
125 return WorldListProxy(value, _prefix=prefix, _top=proxy._top)
129 class WorldBaseProxy:
130 """ Base for world proxies. """
132 def __init__(self, state, _prefix='', _top=None):
138 self.__dict__.update({
145 def _record_change(self, fullname, value):
146 self._events.append(WorldEvent("set", {
150 def pop_events(self):
151 events, self._events = self._events, []
155 class WorldDictProxy(WorldBaseProxy):
156 """ World dictionary proxy that records changes and produces events. """
160 (k, _maybe_subproxy(self, k, v)) for k, v in self._state.items())
162 def __setattr__(self, name, value):
163 self._top._record_change("%s%s" % (self._prefix, name), value)
165 def __getattr__(self, name):
166 # return None for attributes that don't exist
167 value = self._state.get(name)
168 return _maybe_subproxy(self, name, value)
170 def __setitem__(self, name, value):
171 return self.__setattr__(name, value)
173 def __getitem__(self, name):
174 return self.__getattr__(name)
177 class WorldListProxy(WorldBaseProxy):
178 """ World list proxy that records changes and produces events. """
180 def __setitem__(self, index, value):
181 self._top._record_change("%s%s" % (self._prefix, index), value)
183 def __getitem__(self, index):
184 return _maybe_subproxy(self, index, self._state[index])
187 return len(self._state)
190 return bool(self._state)