Commit 8978690b authored by Ralf's avatar Ralf

implement some player commands

parent 30476a55
......@@ -14,6 +14,7 @@ class Game:
self.sel = selectors.DefaultSelector()
self.sel.register(self.sock, selectors.EVENT_READ, self.accept)
# low-level functions
def accept(self, sock, mask):
assert sock == self.sock
conn, addr = sock.accept() # Should be ready
......@@ -40,3 +41,6 @@ class Game:
for key, mask in events:
callback = key.data
callback(key.fileobj, mask)
# high(er)-level functions
from util import *
from thing import Thing
from labyrinth import directions as directionOffsets
class Player:
goVerbs = ['go', 'walk']
sayVerbs = ['say', 'talk', 'scream']
class Player(Thing):
def __init__(self, game, conn):
super().__init__(self)
self.buffer = b""
self.game = game
self.conn = conn
......@@ -20,16 +26,49 @@ class Player:
if data:
self.buffer += data
pos = self.buffer.find(b'\n')
if pos >= 0:
while pos >= 0:
cmd = self.buffer[:pos]
self.buffer = self.buffer[pos+1:]
self.readCmd(cmd.decode('utf-8'))
# maybe we got several lines?
pos = self.buffer.find(b'\n')
else:
self.game.removePlayer(self)
def toString(self):
def __str__(self):
return "another player"
# high(er)-level functions
def readCmd(self, cmd):
self.send("Selber "+cmd)
words = cmd.lower().split()
if not words:
self.send("What did you mean?")
return
verb = words[0]
if verb in goVerbs:
if len(words) >= 3 and words[1:3] == ["to", "the"]:
direction = words[3:]
else:
direction = words[1:]
if len(direction) != 1 or direction[0] not in directionOffsets:
self.send("Where do you want to go?")
return
dy, dx = directionOffsets.get(direction[0])
target = self.field.neighbor(dy=dy, dx=dx)
if target is None or not target.isWalkable():
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():
for thing in self.field.neighbor(dx=dx, dy=dy):
if isinstance(thing, Player) and thing != self:
thing.send("You hear someone saying: "+msg)
self.send("You say: "+msg)
def onMove(self, newField):
desc = self.game.labyrinth.getDescription(newField)
self.send(desc)
return True
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