From 17984bc886e3a233b828a53354467bbda2c43692 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 12 Dec 2018 01:30:58 +0100 Subject: [PATCH] Refactor. --- client.py | 2 +- game_common.py | 45 ++++++++++++++++++++++++++++++++++++ server.py | 2 +- game.py => server_/game.py | 47 +++++++++----------------------------- 4 files changed, 58 insertions(+), 38 deletions(-) create mode 100644 game_common.py rename game.py => server_/game.py (74%) diff --git a/client.py b/client.py index b10ba90..fb88c7e 100755 --- a/client.py +++ b/client.py @@ -4,7 +4,7 @@ import plom_socket_io import socket import threading from parser import ArgError, Parser -from game import World +from game_common import World class Thing: diff --git a/game_common.py b/game_common.py new file mode 100644 index 0000000..6a77d9c --- /dev/null +++ b/game_common.py @@ -0,0 +1,45 @@ +from parser import ArgError + + +class World: + + def __init__(self): + self.turn = 0 + self.map_size = (0, 0) + self.terrain_map = '' + self.things = [] + self.Thing = Thing # child classes may use an extended Thing class here + + def set_map_size(self, yx): + y, x = yx + self.map_size = (y, x) + self.terrain_map = '' + for y in range(self.map_size[0]): + self.terrain_map += '?' * self.map_size[1] + + def set_map_line(self, y, line): + width_map = self.map_size[1] + if y >= self.map_size[0]: + raise ArgError('too large row number %s' % y) + width_line = len(line) + if width_line > width_map: + raise ArgError('too large map line width %s' % width_line) + self.terrain_map = self.terrain_map[:y * width_map] + line + \ + self.terrain_map[(y + 1) * width_map:] + + def get_thing(self, i): + for thing in self.things: + if i == thing.id_: + return thing + t = self.Thing(self, i) + self.things += [t] + return t + + +class Thing: + + def __init__(self, world, id_): + self.world = world + self.id_ = id_ + self.type_ = '?' + self.position = [0,0] diff --git a/server.py b/server.py index 63188f3..3e1ae6f 100755 --- a/server.py +++ b/server.py @@ -6,7 +6,7 @@ import queue import sys import os from parser import ArgError, Parser -from game import World, GameError +from server_.game import World, GameError # Avoid "Address already in use" errors. diff --git a/game.py b/server_/game.py similarity index 74% rename from game.py rename to server_/game.py index 83f3643..da2f611 100644 --- a/game.py +++ b/server_/game.py @@ -1,3 +1,8 @@ +import sys +sys.path.append('../') +import game_common + + class GameError(Exception): pass @@ -13,13 +18,11 @@ def move_pos(direction, pos_yx): pos_yx[1] -= 1 -class World: +class World(game_common.World): def __init__(self): - self.turn = 0 - self.map_size = (0, 0) - self.terrain_map = '' - self.things = [] + 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): @@ -45,31 +48,6 @@ class World: if player.task is None: break - def set_map_size(self, yx): - y, x = yx - self.map_size = (y, x) - self.terrain_map = '' - for y in range(self.map_size[0]): - self.terrain_map += '?' * self.map_size[1] - - def set_map_line(self, y, line): - width_map = self.map_size[1] - if y >= self.map_size[0]: - raise ArgError('too large row number %s' % y) - width_line = len(line) - if width_line > width_map: - raise ArgError('too large map line width %s' % width_line) - self.terrain_map = self.terrain_map[:y * width_map] + line + \ - self.terrain_map[(y + 1) * width_map:] - - def get_thing(self, i): - for thing in self.things: - if i == thing.id_: - return thing - t = Thing(self, i, '?', [0,0]) - self.things += [t] - return t - class Task: @@ -98,13 +76,10 @@ class Task: raise GameError('would move into illegal terrain') -class Thing: +class Thing(game_common.Thing): - def __init__(self, world, id_, type_, position): - self.world = world - self.id_ = id_ - self.type_ = type_ - self.position = position + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) self.task = Task(self, 'wait') def task_wait(self): -- 2.30.2