X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=client.py;h=45ef9f859ecf88e14b14cb8dafd81eed138d1917;hb=2966f4632c6c80b5a1a279a908831150092ad09f;hp=a71ac8c224881549bee39aa05211cab6b3339063;hpb=c456cf91332b791cc6c6a685b5043af50e671a02;p=plomrogue2-experiments
diff --git a/client.py b/client.py
index a71ac8c..45ef9f8 100755
--- a/client.py
+++ b/client.py
@@ -6,6 +6,10 @@ import socket
import threading
+class ArgumentError(Exception):
+ pass
+
+
class UrwidSetup:
def __init__(self, socket):
@@ -45,15 +49,15 @@ class UrwidSetup:
Sets up an urwid.Pile inside a returned urwid.Filler; top to bottom:
- an EditToSocketWidget, prefixing self.socket input with 'SEND: '
- - self.reply_widget, a urwid.Text widget printing self.socket replies
- a 50-col wide urwid.Padding container for self.map_widget, which is
to print clipped map representations
+ - self.reply_widget, a urwid.Text widget printing self.socket replies
"""
edit_widget = self.EditToSocketWidget(self.socket, 'SEND: ')
self.reply_widget = urwid.Text('')
self.map_widget = self.MapWidget('', wrap='clip')
map_box = urwid.Padding(self.map_widget, width=50)
- widget_pile = urwid.Pile([edit_widget, self.reply_widget, map_box])
+ widget_pile = urwid.Pile([edit_widget, map_box, self.reply_widget])
return urwid.Filler(widget_pile, valign='top')
class EditToSocketWidget(urwid.Edit):
@@ -73,7 +77,7 @@ class UrwidSetup:
class MapWidget(urwid.Text):
"""Stores/updates/draws game map."""
terrain_map = ' ' * 25
- position = [0,0]
+ position = (0, 0)
def draw_map(self):
"""Draw map view from .terrain_map, .position."""
@@ -89,14 +93,21 @@ class UrwidSetup:
self.terrain_map = terrain_map
self.draw_map()
- def update_position_y(self, position_y_string):
- """Update self.position[0]."""
- self.position[0] = int(position_y_string)
- self.draw_map()
-
- def update_position_x(self, position_x_string):
- """Update self.position[1]."""
- self.position[1] = int(position_x_string)
+ def update_position(self, position_string):
+ """Update self.position."""
+
+ def get_axis_position_from_argument(axis, token):
+ if len(token) < 3 or token[:2] != axis + ':' or \
+ not token[2:].isdigit():
+ raise ArgumentError('Bad arg for ' + axis + ' position.')
+ return int(token[2:])
+
+ tokens = position_string.split(',')
+ if len(tokens) != 2:
+ raise ArgumentError('wrong number of ","-separated arguments')
+ y = get_axis_position_from_argument('y', tokens[0])
+ x = get_axis_position_from_argument('x', tokens[1])
+ self.position = (y, x)
self.draw_map()
class InputHandler:
@@ -125,20 +136,32 @@ class UrwidSetup:
self.message_container, and just pipe the trigger to inform us
about this.
- If the message delivered is 'BYE', quits Urbit.
+ If the message delivered is 'BYE', quits Urwid.
"""
+
+ def mapdraw_command(prefix, func):
+ n = len(prefix)
+ if len(msg) > n and msg[:n] == prefix:
+ m = getattr(self.widget2, func)
+ m(msg[n:])
+ return True
+ return False
+
msg = self.message_container[0]
if msg == 'BYE':
raise urwid.ExitMainLoop()
return
- if len(msg) > 8 and msg[:8] == 'TERRAIN ':
- self.widget2.update_terrain(msg[8:])
- elif len(msg) > 11 and msg[:11] == 'POSITION_Y ':
- self.widget2.update_position_y(msg[11:])
- elif len(msg) > 11 and msg[:11] == 'POSITION_X ':
- self.widget2.update_position_x(msg[11:])
+ found_command = False
+ try:
+ found_command = (
+ mapdraw_command('TERRAIN\n', 'update_terrain') or
+ mapdraw_command('POSITION ', 'update_position'))
+ except ArgumentError as e:
+ self.widget1.set_text('BAD ARGUMENT: ' + msg + '\n' +
+ str(e))
else:
- self.widget1.set_text('SERVER: ' + msg)
+ if not found_command:
+ self.widget1.set_text('UNKNOWN COMMAND: ' + msg)
del self.message_container[0]
def recv_loop(self):