From: Christian Heller Date: Sat, 26 Jan 2019 21:42:04 +0000 (+0100) Subject: To command parser, add string options; use TASK syntax for task commands. X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2-experiments;a=commitdiff_plain;h=6237227d771fa5b4cfbcdffb8c74457e2d406bb1 To command parser, add string options; use TASK syntax for task commands. --- diff --git a/client-curses.py b/client-curses.py index ca907db..9a8178d 100755 --- a/client-curses.py +++ b/client-curses.py @@ -108,6 +108,21 @@ class Game(game_common.CommonCommandsMixin): } self.do_quit = False + def get_command_signature(self, command_name): + method_candidate = 'cmd_' + command_name + method = None + argtypes = '' + if hasattr(self, method_candidate): + method = getattr(self, method_candidate) + if hasattr(method, 'argtypes'): + argtypes = method.argtypes + return method, argtypes + + def get_string_options(self, string_option_type): + if string_option_type == 'geometry': + return self.map_manager.get_map_geometries() + return None + def handle_input(self, msg): if msg == 'BYE': self.do_quit = True @@ -120,8 +135,8 @@ class Game(game_common.CommonCommandsMixin): else: command() except ArgError as e: - self.log('ARGUMENT ERROR: ' + msg + '\n' + str(e)) - self.to_update['log'] = True + self.log('ARGUMENT ERROR: ' + msg + '\n' + str(e)) + self.to_update['log'] = True def log(self, msg): """Prefix msg plus newline to self.log_text.""" @@ -385,26 +400,26 @@ class TUI: elif map_mode: if type(self.game.world.map_) == MapSquare: if key == 'a': - plom_socket_io.send(self.socket, 'MOVE LEFT') + plom_socket_io.send(self.socket, 'TASK:MOVE LEFT') elif key == 'd': - plom_socket_io.send(self.socket, 'MOVE RIGHT') + plom_socket_io.send(self.socket, 'TASK:MOVE RIGHT') elif key == 'w': - plom_socket_io.send(self.socket, 'MOVE UP') + plom_socket_io.send(self.socket, 'TASK:MOVE UP') elif key == 's': - plom_socket_io.send(self.socket, 'MOVE DOWN') + plom_socket_io.send(self.socket, 'TASK:MOVE DOWN') elif type(self.game.world.map_) == MapHex: if key == 'w': - plom_socket_io.send(self.socket, 'MOVE UPLEFT') + plom_socket_io.send(self.socket, 'TASK:MOVE UPLEFT') elif key == 'e': - plom_socket_io.send(self.socket, 'MOVE UPRIGHT') + plom_socket_io.send(self.socket, 'TASK:MOVE UPRIGHT') if key == 's': - plom_socket_io.send(self.socket, 'MOVE LEFT') + plom_socket_io.send(self.socket, 'TASK:MOVE LEFT') elif key == 'd': - plom_socket_io.send(self.socket, 'MOVE RIGHT') + plom_socket_io.send(self.socket, 'TASK:MOVE RIGHT') if key == 'x': - plom_socket_io.send(self.socket, 'MOVE DOWNLEFT') + plom_socket_io.send(self.socket, 'TASK:MOVE DOWNLEFT') elif key == 'c': - plom_socket_io.send(self.socket, 'MOVE DOWNRIGHT') + plom_socket_io.send(self.socket, 'TASK:MOVE DOWNRIGHT') else: if len(key) == 1 and key in ASCII_printable and \ len(self.to_send) < len(self.edit): diff --git a/game_common.py b/game_common.py index 1119ce1..3c17bd1 100644 --- a/game_common.py +++ b/game_common.py @@ -80,12 +80,8 @@ class CommonCommandsMixin: def cmd_MAP(self, geometry, yx): """Create new map of grid geometry, size yx and only '?' cells.""" - legal_grids = self.map_manager.get_map_geometries() - if geometry not in legal_grids: - raise ArgError('First map argument must be one of: ' + - ', '.join(legal_grids)) self.world.new_map(geometry, yx) - cmd_MAP.argtypes = 'string yx_tuple:pos' + cmd_MAP.argtypes = 'string:geometry yx_tuple:pos' def cmd_THING_TYPE(self, i, type_): t = self.world.get_thing(i) diff --git a/parser.py b/parser.py index 38283bb..2292f88 100644 --- a/parser.py +++ b/parser.py @@ -45,27 +45,25 @@ class Parser: return tokens def parse(self, msg): - """Parse msg as call to self.game method, return method with arguments. + """Parse msg as call to method, return method with arguments. Respects method signatures defined in methods' .argtypes attributes. """ tokens = self.tokenize(msg) if len(tokens) == 0: return None - method_candidate = 'cmd_' + tokens[0] - if not hasattr(self.game, method_candidate): + method, argtypes = self.game.get_command_signature(tokens[0]) + if method is None: return None - method = getattr(self.game, method_candidate) + if len(argtypes) == 0: + if len(tokens) > 1: + raise ArgError('Command expects no argument(s).') + return method if len(tokens) == 1: - if not hasattr(method, 'argtypes'): - return method - else: - raise ArgError('Command expects argument(s).') + raise ArgError('Command expects argument(s).') args_candidates = tokens[1:] - if not hasattr(method, 'argtypes'): - raise ArgError('Command expects no argument(s).') - args, kwargs = self.argsparse(method.argtypes, args_candidates) - return partial(method, *args, **kwargs) + args = self.argsparse(argtypes, args_candidates) + return partial(method, *args) def parse_yx_tuple(self, yx_string, range_): """Parse yx_string as yx_tuple:nonneg argtype, return result. @@ -93,11 +91,12 @@ class Parser: return (y, x) def argsparse(self, signature, args_tokens): - """Parse into / return args_tokens as args/kwargs defined by signature. + """Parse into / return args_tokens as args defined by signature. Expects signature to be a ' '-delimited sequence of any of the strings 'int:nonneg', 'yx_tuple:nonneg', 'yx_tuple:pos', 'string', - 'seq:int:nonneg', defining the respective argument types. + 'seq:int:nonneg', 'string:' + an option type string accepted by + self.game.get_string_options, defining the respective argument types. """ tmpl_tokens = signature.split() if len(tmpl_tokens) != len(args_tokens): @@ -105,6 +104,7 @@ class Parser: ') not expected number (' + str(len(tmpl_tokens)) + ').') args = [] + string_string = 'string' for i in range(len(tmpl_tokens)): tmpl = tmpl_tokens[i] arg = args_tokens[i] @@ -116,8 +116,6 @@ class Parser: args += [self.parse_yx_tuple(arg, 'nonneg')] elif tmpl == 'yx_tuple:pos': args += [self.parse_yx_tuple(arg, 'pos')] - elif tmpl == 'string': - args += [arg] elif tmpl == 'seq:int:nonneg': sub_tokens = arg.split(',') if len(sub_tokens) < 1: @@ -129,9 +127,22 @@ class Parser: 'non-negative integers.') seq += [int(tok)] args += [seq] + elif tmpl == string_string: + args += [arg] + elif tmpl[:len(string_string) + 1] == string_string + ':': + if not hasattr(self.game, 'get_string_options'): + raise ArgError('No string option directory.') + string_option_type = tmpl[len(string_string) + 1:] + options = self.game.get_string_options(string_option_type) + if options is None: + raise ArgError('Unknown string option type.') + if arg not in options: + msg = 'Argument #%s must be one of: %s' % (i + 1, options) + raise ArgError(msg) + args += [arg] else: raise ArgError('Unknown argument type.') - return args, {} + return args class TestParser(unittest.TestCase): @@ -156,7 +167,6 @@ class TestParser(unittest.TestCase): self.assertEqual(p.parse('x'), None) def test_argsparse(self): - from functools import partial p = Parser() assertErr = partial(self.assertRaises, ArgError, p.argsparse) assertErr('', ['foo']) diff --git a/server_/game.py b/server_/game.py index 5ba3ab2..958bdc8 100644 --- a/server_/game.py +++ b/server_/game.py @@ -68,20 +68,15 @@ class World(game_common.World): class Task: - def __init__(self, thing, name, args=(), kwargs={}): + def __init__(self, thing, name, args=()): self.name = name self.thing = thing self.args = args - self.kwargs = kwargs self.todo = 3 def check(self): - if self.name == 'move': - if len(self.args) > 0: - direction = self.args[0] - else: - direction = self.kwargs['direction'] - test_pos = self.thing.world.map_.move(self.thing.position, direction) + if self.name == 'MOVE': + test_pos = self.thing.world.map_.move(self.thing.position, self.args[0]) if self.thing.world.map_[test_pos] != '.': raise GameError(str(self.thing.id_) + ' would move into illegal terrain') @@ -95,16 +90,17 @@ class Thing(game_common.Thing): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.task = Task(self, 'wait') + self.task = Task(self, 'WAIT') self.last_task_result = None self._stencil = None - def task_wait(self): + def task_WAIT(self): return 'success' - def task_move(self, direction): + def task_MOVE(self, direction): self.position = self.world.map_.move(self.position, direction) return 'success' + task_MOVE.argtypes = 'string:direction' def move_towards_target(self, target): dijkstra_map = type(self.world.map_)(self.world.map_.size) @@ -153,7 +149,7 @@ class Thing(game_common.Thing): direction = dirs[i_dir] #print('DEBUG result', direction) if direction: - self.set_task('move', direction=direction) + self.set_task('MOVE', (direction,)) #self.world.game.io.send('would move ' + direction) def decide_task(self): @@ -169,11 +165,11 @@ class Thing(game_common.Thing): return except GameError: pass - self.set_task('wait') + self.set_task('WAIT') - def set_task(self, task_name, *args, **kwargs): - self.task = Task(self, task_name, args, kwargs) + def set_task(self, task_name, args=()): + self.task = Task(self, task_name, args) self.task.check() # will throw GameError if necessary def proceed(self, is_AI=True): @@ -198,18 +194,18 @@ class Thing(game_common.Thing): try: self.decide_task() except GameError: - self.set_task('wait') + self.set_task('WAIT') return self.task.todo -= 1 if self.task.todo <= 0: task = getattr(self, 'task_' + self.task.name) - self.last_task_result = task(*self.task.args, **self.task.kwargs) + self.last_task_result = task(*self.task.args) self.task = None if is_AI and self.task is None: try: self.decide_task() except GameError: - self.set_task('wait') + self.set_task('WAIT') def get_stencil(self): if self._stencil is not None: @@ -324,20 +320,9 @@ class Game(game_common.CommonCommandsMixin): self.send_gamestate() self.pool_result = self.pool.map_async(fib, (35, 35)) - def cmd_MOVE(self, direction): - """Set player task to 'move' with direction arg, finish player turn.""" - import parser - legal_directions = self.world.map_.get_directions() - if direction not in legal_directions: - raise parser.ArgError('Move argument must be one of: ' + - ', '.join(legal_directions)) - self.world.get_player().set_task('move', direction=direction) - self.proceed() - cmd_MOVE.argtypes = 'string' - def cmd_SWITCH_PLAYER(self): player = self.world.get_player() - player.set_task('wait') + player.set_task('WAIT') thing_ids = [t.id_ for t in self.world.things] player_index = thing_ids.index(player.id_) if player_index == len(thing_ids) - 1: @@ -346,11 +331,6 @@ class Game(game_common.CommonCommandsMixin): self.world.player_id = thing_ids[player_index + 1] self.proceed() - def cmd_WAIT(self): - """Set player task to 'wait', finish player turn.""" - self.world.get_player().set_task('wait') - self.proceed() - def cmd_GET_GAMESTATE(self, connection_id): """Send game state to caller.""" self.send_gamestate(connection_id) @@ -370,9 +350,38 @@ class Game(game_common.CommonCommandsMixin): cmd_TERRAIN_LINE.argtypes = 'int:nonneg string' def cmd_GEN_WORLD(self, geometry, yx, seed): - legal_grids = self.map_manager.get_map_geometries() - if geometry not in legal_grids: - raise ArgError('First map argument must be one of: ' + - ', '.join(legal_grids)) self.world.make_new(geometry, yx, seed) - cmd_GEN_WORLD.argtypes = 'string yx_tuple:pos string' + cmd_GEN_WORLD.argtypes = 'string:geometry yx_tuple:pos string' + + def get_command_signature(self, command_name): + from functools import partial + + def cmd_TASK_colon(task_name, *args): + self.world.get_player().set_task(task_name, args) + self.proceed() + + method = None + argtypes = '' + task_prefix = 'TASK:' + if command_name[:len(task_prefix)] == task_prefix: + task_name = command_name[len(task_prefix):] + task_method_candidate = 'task_' + task_name + if hasattr(Thing, task_method_candidate): + method = partial(cmd_TASK_colon, task_name) + task_method = getattr(Thing, task_method_candidate) + if hasattr(task_method, 'argtypes'): + argtypes = task_method.argtypes + return method, argtypes + method_candidate = 'cmd_' + command_name + if hasattr(self, method_candidate): + method = getattr(self, method_candidate) + if hasattr(method, 'argtypes'): + argtypes = method.argtypes + return method, argtypes + + def get_string_options(self, string_option_type): + if string_option_type == 'geometry': + return self.map_manager.get_map_geometries() + elif string_option_type == 'direction': + return self.world.map_.get_directions() + return None