Commit 89e0760c authored by Ralf's avatar Ralf
Browse files

more logging

parent 44bbf786
...@@ -5,7 +5,7 @@ import labyrinth ...@@ -5,7 +5,7 @@ import labyrinth
import random import random
class Game: class Game:
def __init__(self, labyrinthFileName, 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(stream=f)
...@@ -15,6 +15,7 @@ class Game: ...@@ -15,6 +15,7 @@ class Game:
self.sel = selectors.DefaultSelector() self.sel = selectors.DefaultSelector()
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
# low-level functions # low-level functions
def accept(self, sock, mask): def accept(self, sock, mask):
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
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)}
...@@ -66,6 +67,23 @@ class Labyrinth: ...@@ -66,6 +67,23 @@ class Labyrinth:
tiles.append(row) tiles.append(row)
return tiles return tiles
def writelab(self, stream):
for row in self.tiles:
for field in row:
if field.groundtype == Labyrinth.GroundType.FLOOR:
print("#", file=stream)
else:
if not field.things:
print(".", file=stream)
elif len(field.things) == 1:
if isinstance(field.things[0], Player):
print("P", file=stream)
else:
print("?", file=stram)
else:
print("+", file=stream)
print("\n", file=stream)
def getWidth(self): def getWidth(self):
return len(self.tiles[0]) return len(self.tiles[0])
......
...@@ -10,9 +10,11 @@ parser.add_argument("-l", "--labyrinth-file", required=True, dest="labyrinthFile ...@@ -10,9 +10,11 @@ parser.add_argument("-l", "--labyrinth-file", required=True, dest="labyrinthFile
help="Name of labyrinth file") help="Name of labyrinth file")
parser.add_argument("--log-file", dest="logFileName", parser.add_argument("--log-file", dest="logFileName",
help="Name of log file") help="Name of log file")
parser.add_argument("--admin-password", dest="adminPW",
help="Password for the admin")
args = parser.parse_args() args = parser.parse_args()
game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port, logFileName=args.logFileName) game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port, logFileName=args.logFileName, adminPW=adminPW)
try: try:
game.run() game.run()
......
...@@ -6,12 +6,17 @@ goVerbs = ['go', 'walk'] ...@@ -6,12 +6,17 @@ goVerbs = ['go', 'walk']
sayVerbs = ['say', 'talk', 'scream'] sayVerbs = ['say', 'talk', 'scream']
leaveVerbs = ['leave', 'quit', 'exit', 'suicide'] leaveVerbs = ['leave', 'quit', 'exit', 'suicide']
playerUID = 0
class Player(Thing): class Player(Thing):
def __init__(self, game, conn): def __init__(self, game, conn):
super().__init__() super().__init__()
self.buffer = b"" self.buffer = b""
self.game = game self.game = game
self.conn = conn self.conn = conn
self.uid = playerUID
playerUID += 1
log_stdout("New player: {0}".format(self.uid))
# low-level functions # low-level functions
def send(self, data, end='\n'): def send(self, data, end='\n'):
...@@ -20,6 +25,7 @@ class Player(Thing): ...@@ -20,6 +25,7 @@ class Player(Thing):
def close(self): # only to be called by Game - it has to remove us from its list def close(self): # only to be called by Game - it has to remove us from its list
self.conn.close() self.conn.close()
log_stdout("Player left: {0}".format(self.uid))
return self.conn return self.conn
def read(self, conn, mask): def read(self, conn, mask):
...@@ -45,7 +51,7 @@ class Player(Thing): ...@@ -45,7 +51,7 @@ class Player(Thing):
# high(er)-level functions # high(er)-level functions
def readCmd(self, cmd): def readCmd(self, cmd):
self.game.log("Someone wrote '{0}'".format(cmd)) self.game.log("Someone [{1}] wrote '{0}'".format(cmd, self.uid))
words = cmd.lower().split() words = cmd.lower().split()
if not words: if not words:
self.send("What did you mean?") self.send("What did you mean?")
......
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