Commit 89e0760c authored by Ralf's avatar Ralf

more logging

parent 44bbf786
......@@ -5,7 +5,7 @@ import labyrinth
import random
class Game:
def __init__(self, labyrinthFileName, logFileName=None, port=1234):
def __init__(self, labyrinthFileName, adminPW, logFileName=None, port=1234):
self.players = []
with open(labyrinthFileName,'r') as f:
self.labyrinth = labyrinth.Labyrinth(stream=f)
......@@ -15,6 +15,7 @@ class Game:
self.sel = selectors.DefaultSelector()
self.sel.register(self.sock, selectors.EVENT_READ, self.accept)
self.logFile = None if logFileName is None else open(logFileName, 'a')
self.adminPW = adminPW
# low-level functions
def accept(self, sock, mask):
......
......@@ -2,6 +2,7 @@
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)}
......@@ -66,6 +67,23 @@ class Labyrinth:
tiles.append(row)
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):
return len(self.tiles[0])
......
......@@ -10,9 +10,11 @@ parser.add_argument("-l", "--labyrinth-file", required=True, dest="labyrinthFile
help="Name of labyrinth file")
parser.add_argument("--log-file", dest="logFileName",
help="Name of log file")
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)
game = Game(labyrinthFileName=args.labyrinthFileName, port=args.port, logFileName=args.logFileName, adminPW=adminPW)
try:
game.run()
......
......@@ -6,12 +6,17 @@ goVerbs = ['go', 'walk']
sayVerbs = ['say', 'talk', 'scream']
leaveVerbs = ['leave', 'quit', 'exit', 'suicide']
playerUID = 0
class Player(Thing):
def __init__(self, game, conn):
super().__init__()
self.buffer = b""
self.game = game
self.conn = conn
self.uid = playerUID
playerUID += 1
log_stdout("New player: {0}".format(self.uid))
# low-level functions
def send(self, data, end='\n'):
......@@ -20,6 +25,7 @@ class Player(Thing):
def close(self): # only to be called by Game - it has to remove us from its list
self.conn.close()
log_stdout("Player left: {0}".format(self.uid))
return self.conn
def read(self, conn, mask):
......@@ -45,7 +51,7 @@ class Player(Thing):
# high(er)-level functions
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()
if not words:
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