Commit 6ee4826d authored by Ralf's avatar Ralf

more stuff

parent 89e0760c
......@@ -8,7 +8,7 @@ class Game:
def __init__(self, labyrinthFileName, adminPW, logFileName=None, port=1234):
self.players = []
with open(labyrinthFileName,'r') as f:
self.labyrinth = labyrinth.Labyrinth(stream=f)
self.labyrinth = labyrinth.Labyrinth(game=self,stream=f)
self.sock = socket.socket()
self.sock.bind(('0.0.0.0', port))
self.sock.listen(100)
......@@ -16,6 +16,7 @@ class Game:
self.sel.register(self.sock, selectors.EVENT_READ, self.accept)
self.logFile = None if logFileName is None else open(logFileName, 'a')
self.adminPW = adminPW
self.admins = []
# low-level functions
def accept(self, sock, mask):
......@@ -26,11 +27,15 @@ class Game:
self.labyrinth.createThing(player,tile)
self.players.append(player)
self.sel.register(conn, selectors.EVENT_READ, player.read)
self.showAdmins()
def removePlayer(self, player):
assert player in self.players
self.sel.unregister(player.close())
self.players.remove(player)
if player in self.admins:
self.admins.remove(player)
self.labyrinth.removeThing(player)
assert player not in self.players # this ensure the player was in only once
def close(self):
......@@ -51,6 +56,17 @@ class Game:
def log(self, *args):
if self.logFile is not None:
print(*args, file=self.logFile, flush=True)
def showAdmins(self):
lab = self.labyrinth.writelab()
for admin in self.admins:
admin.send(lab)
# high(er)-level functions
def makeAdmin(self, player):
assert player in self.players
if not player in self.admins:
self.admins.append(player)
def isAdmin(self, player):
return player in self.admins
......@@ -2,7 +2,6 @@
from enum import Enum
from functools import reduce
from player import Player
directions = {"north": (-1, 0), "east": (0, 1), "west": (0, -1), "south": (1, 0)}
......@@ -44,7 +43,8 @@ class Labyrinth:
return "there are "+(", ".join([x.toString() for x in self.things[:-1]]))+" and "+self.things[-1].toString()
# constructor for a completely read labyrinth
def __init__(self,width=None,height=None,stream=None):
def __init__(self,game=None,width=None,height=None,stream=None):
self.game = game
if width is not None: # empty labyrinth of given size
assert height is not None
assert stream is None
......@@ -67,22 +67,21 @@ class Labyrinth:
tiles.append(row)
return tiles
def writelab(self, stream):
def writelab(self):
result = ""
for row in self.tiles:
for field in row:
if field.groundtype == Labyrinth.GroundType.FLOOR:
print("#", file=stream)
result += "#"
else:
if not field.things:
print(".", file=stream)
result += "."
elif len(field.things) == 1:
if isinstance(field.things[0], Player):
print("P", file=stream)
else:
print("?", file=stram)
result += field.things[0].asChar()
else:
print("+", file=stream)
print("\n", file=stream)
result += "+"
result += "\n"
return result
def getWidth(self):
return len(self.tiles[0])
......@@ -111,7 +110,7 @@ class Labyrinth:
descr += "In the "+name+" "+("there is a wall" if field is None else str(field))+".\n"
return descr
def moveThing (self, thing, newField):
def moveThing (self, thing, newField, beforeFinish=None):
if thing.onMove(newField) == False:
return False
assert thing.field.labyrinth == self
......@@ -120,6 +119,10 @@ class Labyrinth:
oldField = thing.field
thing.field = newField
newField._addThing(thing)
if beforeFinish is None:
self.game.showAdmins()
else:
beforeFinish()
thing.afterMove(oldField)
return True
......@@ -127,4 +130,10 @@ class Labyrinth:
thing.field = field
field._addThing(thing)
thing.onMove(field)
thing.afterMove(field)
\ No newline at end of file
thing.afterMove(field)
self.game.showAdmins()
def removeThing(self, thing):
field = thing.field
field.things.remove(thing)
self.game.showAdmins()
......@@ -14,7 +14,7 @@ parser.add_argument("--admin-password", dest="adminPW",
help="Password for the admin")
args = parser.parse_args()
game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port, logFileName=args.logFileName, adminPW=adminPW)
game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port, logFileName=args.logFileName, adminPW=args.adminPW)
try:
game.run()
......
......@@ -4,12 +4,14 @@ from labyrinth import directions as directionOffsets
goVerbs = ['go', 'walk']
sayVerbs = ['say', 'talk', 'scream']
announceVerbs = ['announce']
leaveVerbs = ['leave', 'quit', 'exit', 'suicide']
playerUID = 0
class Player(Thing):
def __init__(self, game, conn):
global playerUID
super().__init__()
self.buffer = b""
self.game = game
......@@ -77,13 +79,28 @@ class Player(Thing):
for thing in self.field.neighbor(dx=dx, dy=dy):
if isinstance(thing, Player) and thing != self:
thing.send("You hear someone saying: "+msg)
for admin in self.game.admins:
admin.send("{0} says: {1}".format(self.uid, msg))
self.send("You say: "+msg)
elif verb in leaveVerbs:
self.send("Good Bye!")
self.game.removePlayer(self)
elif verb == self.game.adminPW:
self.send("Welcome to the vault, my son")
self.game.makeAdmin(self)
elif verb in announceVerbs:
if self.game.isAdmin(self):
msg = " ".join(words[1:])
for player in self.game.players:
player.send("Though Hearest A Voice Sayin': "+msg)
else:
self.send("Who Do Though Think Though Are?")
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, end="")
def asChar(self):
return "P"
......@@ -6,4 +6,7 @@ class Thing():
return True
def afterMove(self, oldField):
pass
\ No newline at end of file
pass
def asChar(self):
return "?"
......@@ -17,3 +17,9 @@ def log_stdout(*args):
print(*args, file=sys.stdout, flush=True)
def log_stderr(*args):
print(*args, file=sys.stdout, flush=True)
def compose(*args):
def do_both():
for f in reversed(args):
f()
return do_both
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