home · contact · privacy
Refactor.
[plomrogue2-experiments] / server_ / game.py
index e0e63b97aeb4d3067b01e4af3ff20fccd69763ea..9c264ed019584fdc6fdb009ab5434623bd0cdbdd 100644 (file)
@@ -1,3 +1,8 @@
+import sys
+sys.path.append('../')
+import game_common
+
+
 class GameError(Exception):
     pass
 
@@ -13,6 +18,41 @@ def move_pos(direction, pos_yx):
         pos_yx[1] -= 1
 
 
+class World(game_common.World):
+
+    def __init__(self):
+        super().__init__()
+        self.Thing = Thing  # use local Thing class instead of game_common's
+        self.player_id = 0
+
+    def proceed_to_next_player_turn(self):
+        """Run game world turns until player can decide their next step.
+
+        Iterates through all non-player things, on each step
+        furthering them in their tasks (and letting them decide new
+        ones if they finish). The iteration order is: first all things
+        that come after the player in the world things list, then
+        (after incrementing the world turn) all that come before the
+        player; then the player's .proceed() is run, and if it does
+        not finish his task, the loop starts at the beginning. Once
+        the player's task is finished, the loop breaks.
+        """
+        while True:
+            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[:player_i]:
+                thing.proceed()
+            player.proceed(is_AI=False)
+            if player.task is None:
+                break
+
+    def get_player(self):
+        return self.get_thing(self.player_id)
+
+
 class Task:
 
     def __init__(self, thing, name, args=(), kwargs={}):
@@ -35,17 +75,15 @@ class Task:
                test_pos[1] >= self.thing.world.map_size[1]:
                 raise GameError('would move outside map bounds')
             pos_i = test_pos[0] * self.thing.world.map_size[1] + test_pos[1]
-            map_tile = self.thing.world.map_[pos_i]
+            map_tile = self.thing.world.terrain_map[pos_i]
             if map_tile != '.':
                 raise GameError('would move into illegal terrain')
 
 
-class Thing:
+class Thing(game_common.Thing):
 
-    def __init__(self, world, type_, position):
-        self.world = world
-        self.type_ = type_
-        self.position = position
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
         self.task = Task(self, 'wait')
 
     def task_wait(self):
@@ -62,8 +100,8 @@ class Thing:
         else:
             self.set_task('wait')
 
-    def set_task(self, task, *args, **kwargs):
-        self.task = Task(self, task, args, kwargs)
+    def set_task(self, task_name, *args, **kwargs):
+        self.task = Task(self, task_name, args, kwargs)
         self.task.check()
 
     def proceed(self, is_AI=True):
@@ -82,19 +120,32 @@ class Thing:
             self.decide_task()
 
 
-class World:
+class Commander():
 
-    def __init__(self):
-        self.turn = 0
-        self.map_size = (5, 5)
-        self.map_ = 'xxxxx' +\
-                    'x...x' +\
-                    'x.X.x' +\
-                    'x...x' +\
-                    'xxxxx'
-        self.things = [
-            Thing(self, 'human', [3, 3]),
-            Thing(self, 'monster', [1, 1])
-        ]
-        self.player_i = 0
-        self.player = self.things[self.player_i]
+    def cmd_MOVE(self, direction):
+        """Set player task to 'move' with direction arg, finish player turn."""
+        if direction not in {'UP', 'DOWN', 'RIGHT', 'LEFT'}:
+            raise parser.ArgError('Move argument must be one of: '
+                                  'UP, DOWN, RIGHT, LEFT')
+        self.world.get_player().set_task('move', direction=direction)
+        self.proceed()
+    cmd_MOVE.argtypes = 'string'
+
+    def cmd_WAIT(self):
+        """Set player task to 'wait', finish player turn."""
+        self.world.get_player().set_task('wait')
+        self.proceed()
+
+    def cmd_GET_TURN(self, connection_id):
+        """Send world.turn to caller."""
+        self.send_to(connection_id, str(self.world.turn))
+
+    def cmd_ECHO(self, msg, connection_id):
+        """Send msg to caller."""
+        self.send_to(connection_id, msg)
+    cmd_ECHO.argtypes = 'string'
+
+    def cmd_ALL(self, msg, connection_id):
+        """Send msg to all clients."""
+        self.send_all(msg)
+    cmd_ALL.argtypes = 'string'