From 1d3411457a0ac41e86b44c635b6896ba7f3ab0f1 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 12 Dec 2018 01:40:17 +0100
Subject: [PATCH] Fix faulty assumption about position of player in
 world.things.

---
 game_common.py  |  6 +++---
 server.py       |  7 ++-----
 server_/game.py | 10 +++++++---
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/game_common.py b/game_common.py
index 6a77d9c..c6fb220 100644
--- a/game_common.py
+++ b/game_common.py
@@ -27,11 +27,11 @@ class World:
         self.terrain_map = self.terrain_map[:y * width_map] + line + \
                            self.terrain_map[(y + 1) * width_map:]
 
-    def get_thing(self, i):
+    def get_thing(self, id_):
         for thing in self.things:
-            if i == thing.id_:
+            if id_ == thing.id_:
                 return thing
-        t = self.Thing(self, i)
+        t = self.Thing(self, id_)
         self.things += [t]
         return t
 
diff --git a/server.py b/server.py
index 3e1ae6f..912a692 100755
--- a/server.py
+++ b/server.py
@@ -170,21 +170,18 @@ class CommandHandler:
         self.world.proceed_to_next_player_turn()
         self.send_all_gamestate()
 
-    def get_player(self):
-        return self.world.get_thing(self.world.player_id)
-
     def cmd_MOVE(self, direction, connection_id):
         """Set player task to 'move' with direction arg, finish player turn."""
         if direction not in {'UP', 'DOWN', 'RIGHT', 'LEFT'}:
             raise ArgError('Move argument must be one of: '
                            'UP, DOWN, RIGHT, LEFT')
-        self.get_player().set_task('move', direction=direction)
+        self.world.get_player().set_task('move', direction=direction)
         self.proceed()
     cmd_MOVE.argtypes = 'string'
 
     def cmd_WAIT(self, connection_id):
         """Set player task to 'wait', finish player turn."""
-        self.get_player().set_task('wait')
+        self.world.get_player().set_task('wait')
         self.proceed()
 
     def cmd_MAP_SIZE(self, yx, connection_id):
diff --git a/server_/game.py b/server_/game.py
index da2f611..148c5c7 100644
--- a/server_/game.py
+++ b/server_/game.py
@@ -38,16 +38,20 @@ class World(game_common.World):
         the player's task is finished, the loop breaks.
         """
         while True:
-            for thing in self.things[self.player_id+1:]:
+            player = self.get_player()
+            player_i = self.things.index(player)
+            for thing in self.things[player_i+1:]:
                 thing.proceed()
             self.turn += 1
-            for thing in self.things[:self.player_id]:
+            for thing in self.things[:player_i]:
                 thing.proceed()
-            player = self.get_thing(self.player_id)
             player.proceed(is_AI=False)
             if player.task is None:
                 break
 
+    def get_player(self):
+        return self.get_thing(self.player_id)
+
 
 class Task:
 
-- 
2.30.2