home · contact · privacy
Refactor player session code.
[plomrogue2] / plomrogue / game.py
index 84bebb98c2d4a45f0a1f68619681790f07ce5d99..23a0d7bbc21cd7763e9c58e7f2e7c49da584c3a9 100755 (executable)
@@ -119,12 +119,18 @@ class Game(GameBase):
     def get_map_geometry_shape(self):
         return self.map_geometry.__class__.__name__[len('MapGeometry'):]
 
+    def get_player(self, connection_id):
+        if not connection_id in self.sessions:
+            return None
+        player = self.get_thing(self.sessions[connection_id]['thing_id'])
+        return player
+
     def send_gamestate(self, connection_id=None):
         """Send out game state data relevant to clients."""
 
         self.io.send('TURN ' + str(self.turn))
         for c_id in self.sessions:
-            player = self.get_thing(self.sessions[c_id])
+            player = self.get_player(c_id)
             visible_terrain = player.fov_stencil_map()
             self.io.send('FOV %s' % quote(player.fov_stencil.terrain), c_id)
             self.io.send('MAP %s %s %s' % (self.get_map_geometry_shape(),
@@ -163,7 +169,7 @@ class Game(GameBase):
                     connection_id_found = True
                     break
             if not connection_id_found:
-                t = self.get_thing(self.sessions[connection_id])
+                t = self.get_player(connection_id)
                 if hasattr(t, 'name'):
                     self.io.send('CHAT ' + quote(t.name + ' left the map.'))
                 self.things.remove(t)
@@ -177,11 +183,11 @@ class Game(GameBase):
                     t.proceed()
                 except GameError as e:
                     for connection_id in [c_id for c_id in self.sessions
-                                          if self.sessions[c_id] == t.id_]:
+                                          if self.sessions[c_id]['thing_id'] == t.id_]:
                         self.io.send('GAME_ERROR ' + quote(str(e)), connection_id)
                 except PlayError as e:
                     for connection_id in [c_id for c_id in self.sessions
-                                          if self.sessions[c_id] == t.id_]:
+                                          if self.sessions[c_id]['thing_id'] == t.id_]:
                         self.io.send('PLAY_ERROR ' + quote(str(e)), connection_id)
         if self.changed:
             self.turn += 1
@@ -198,9 +204,9 @@ class Game(GameBase):
             return p
 
         def cmd_TASK_colon(task_name, game, *args, connection_id):
-            if connection_id not in game.sessions:
+            t = self.get_player(connection_id)
+            if not t:
                 raise GameError('Not registered as player.')
-            t = game.get_thing(game.sessions[connection_id])
             t.set_next_task(task_name, args)
 
         def task_prefixed(command_name, task_prefix, task_command):