Commit 6ee4826d authored by Ralf's avatar Ralf
Browse files

more stuff

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