Commit e74dbd75 authored by Ralf's avatar Ralf

small fixes; leave verbs; exception handling

parent 8204abe0
......@@ -27,7 +27,7 @@ class Game:
def removePlayer(self, player):
assert player in self.players
player.close()
self.sel.unregister(player.close())
self.players.remove(player)
assert player not in self.players # this ensure the player was in only once
......
......@@ -4,6 +4,7 @@ from labyrinth import directions as directionOffsets
goVerbs = ['go', 'walk']
sayVerbs = ['say', 'talk', 'scream']
leaveVerbs = ['leave', 'quit', 'exit', 'suicide']
class Player(Thing):
def __init__(self, game, conn):
......@@ -13,12 +14,13 @@ class Player(Thing):
self.conn = conn
# low-level functions
def send(self, data):
def send(self, data, end='\n'):
assert type(data) == str
send_async(self.conn, (data+"\n").encode('utf-8'))
send_async(self.conn, (data+end).encode('utf-8'))
def close(self): # only to be called by Game - it has to remove us from its list
self.conn.close()
return self.conn
def read(self, conn, mask):
assert self.conn == conn, "The player's connection changed?"
......@@ -29,7 +31,10 @@ class Player(Thing):
while pos >= 0:
cmd = self.buffer[:pos]
self.buffer = self.buffer[pos+1:]
self.readCmd(cmd.decode('utf-8'))
try:
self.readCmd(cmd.decode('utf-8'))
except UnicodeDecodeError:
self.send("These are bad bytes...")
# maybe we got several lines?
pos = self.buffer.find(b'\n')
else:
......@@ -59,7 +64,6 @@ class Player(Thing):
self.send("Sorry, you cannot go there")
return
self.game.labyrinth.moveThing(self, target)
return
elif verb in sayVerbs:
msg = " ".join(words[1:])
for dx, dy in [(0, 0)]+directionOffsets.values():
......@@ -67,7 +71,12 @@ class Player(Thing):
if isinstance(thing, Player) and thing != self:
thing.send("You hear someone saying: "+msg)
self.send("You say: "+msg)
elif verb in leaveVerbs:
self.send("Good Bye!")
self.game.removePlayer(self)
else:
self.send("I don't know what you are talking about")
def afterMove(self, oldField):
desc = self.game.labyrinth.getDescription(self.field)
self.send(desc)
self.send(desc, end="")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment