X-Git-Url: https://plomlompom.com/repos/?p=plomrogue2-experiments;a=blobdiff_plain;f=client-curses.py;h=f65d3a0e242c022e1266a3d97c6a27526eaa6532;hp=fa35ffde70578b43a695d90e9db472f657a215ed;hb=HEAD;hpb=e4a1c6b657cc76cb8344b2633e2a469c21b3137c diff --git a/client-curses.py b/client-curses.py index fa35ffd..f65d3a0 100755 --- a/client-curses.py +++ b/client-curses.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 import curses -import plom_socket_io +import plom_socket import socket import threading from parser import ArgError, Parser @@ -53,7 +53,7 @@ class MapHex(Map): def format_to_view(self, map_string, center, size): def map_string_to_lines(map_string): - map_view_chars = ['+'] + map_view_chars = ['0'] x = 0 y = 0 for c in map_string: @@ -64,7 +64,7 @@ class MapHex(Map): x = 0 y += 1 if y % 2 == 0: - map_view_chars += ['+'] + map_view_chars += ['0'] if y % 2 == 0: map_view_chars = map_view_chars[:-1] map_view_chars = map_view_chars[:-1] @@ -77,7 +77,7 @@ class MapHex(Map): return map_lines -map_manager = game_common.MapManager(globals()) +map_manager = game_common.MapManager((MapHex, MapSquare)) class World(game_common.World): @@ -108,20 +108,35 @@ 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 return try: - command = self.parser.parse(msg) + command, args = self.parser.parse(msg) if command is None: self.log('UNHANDLED INPUT: ' + msg) self.to_update['log'] = True else: - command() + command(*args) 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.""" @@ -146,11 +161,13 @@ class Game(game_common.CommonCommandsMixin): pass cmd_TURN_FINISHED.argtypes = 'int:nonneg' - def cmd_NEW_TURN(self, n): + def cmd_TURN(self, n): """Set self.turn to n, empty self.things.""" self.world.turn = n self.world.things = [] - cmd_NEW_TURN.argtypes = 'int:nonneg' + self.to_update['turn'] = False + self.to_update['map'] = False + cmd_TURN.argtypes = 'int:nonneg' def cmd_VISIBLE_MAP_LINE(self, y, terrain_line): self.world.map_.set_line(y, terrain_line) @@ -169,8 +186,8 @@ ASCII_printable = ' !"#$%&\'\(\)*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX' 'YZ[\\]^_\`abcdefghijklmnopqrstuvwxyz{|}~' -def recv_loop(socket, game): - for msg in plom_socket_io.recv(s): +def recv_loop(plom_socket, game): + for msg in plom_socket.recv(): game.handle_input(msg) @@ -336,8 +353,8 @@ class TurnWidget(Widget): class TUI: - def __init__(self, socket, game): - self.socket = socket + def __init__(self, plom_socket, game): + self.socket = plom_socket self.game = game self.parser = Parser(self.game) self.to_update = {'edit': False} @@ -383,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') + self.socket.send('TASK:MOVE LEFT') elif key == 'd': - plom_socket_io.send(self.socket, 'MOVE RIGHT') + self.socket.send('TASK:MOVE RIGHT') elif key == 'w': - plom_socket_io.send(self.socket, 'MOVE UP') + self.socket.send('TASK:MOVE UP') elif key == 's': - plom_socket_io.send(self.socket, 'MOVE DOWN') + self.socket.send('TASK:MOVE DOWN') elif type(self.game.world.map_) == MapHex: if key == 'w': - plom_socket_io.send(self.socket, 'MOVE UPLEFT') + self.socket.send('TASK:MOVE UPLEFT') elif key == 'e': - plom_socket_io.send(self.socket, 'MOVE UPRIGHT') + self.socket.send('TASK:MOVE UPRIGHT') if key == 's': - plom_socket_io.send(self.socket, 'MOVE LEFT') + self.socket.send('TASK:MOVE LEFT') elif key == 'd': - plom_socket_io.send(self.socket, 'MOVE RIGHT') + self.socket.send('TASK:MOVE RIGHT') if key == 'x': - plom_socket_io.send(self.socket, 'MOVE DOWNLEFT') + self.socket.send('TASK:MOVE DOWNLEFT') elif key == 'c': - plom_socket_io.send(self.socket, 'MOVE DOWNRIGHT') + self.socket.send('TASK:MOVE DOWNRIGHT') else: if len(key) == 1 and key in ASCII_printable and \ len(self.to_send) < len(self.edit): @@ -412,7 +429,7 @@ class TUI: self.to_send[:] = self.to_send[:-1] self.to_update['edit'] = True elif key == '\n': # Return key - plom_socket_io.send(self.socket, ''.join(self.to_send)) + self.socket.send(''.join(self.to_send)) self.to_send[:] = [] self.to_update['edit'] = True except curses.error: @@ -422,7 +439,8 @@ class TUI: s = socket.create_connection(('127.0.0.1', 5000)) +plom_socket = plom_socket.PlomSocket(s) game = Game() -t = threading.Thread(target=recv_loop, args=(s, game)) +t = threading.Thread(target=recv_loop, args=(plom_socket, game)) t.start() -TUI(s, game) +TUI(plom_socket, game)