commit 8f84706546f91a8413b2aa79646191edccf323a1 Author: Olli Graf Date: Tue Jan 18 06:40:26 2022 +0100 Dateien von pykerker übernommen. diff --git a/ActionModul.py b/ActionModul.py new file mode 100644 index 0000000..9ac673a --- /dev/null +++ b/ActionModul.py @@ -0,0 +1,149 @@ +import logging + +class ActionModul: + + def isBlank(self,str): + if str and len(str.strip()) == 0: + return True + return False + + def __init__(self, world): + + self.world = world + + def warte(self): + print('Du wartest') + + + def inventar(self): + + self.world.stdscr.addstr(12,0,'Du besitzt folgende Gegenstände:') + zeile=13 + for itemid in self.world.inventar: + item = self.world.inventar[itemid] + self.world.stdscr.addstr(zeile,0,item.name) + zeile = zeile +1 + + def verliere(self,parsedCommand): + item = self.world.findItemImInventar(parsedCommand.gegenstand); + + + if item != None: + del self.world.inventar[item.id] + self.world.aktuellerRaum.items[item.id] = item + else: + self.setFehler("Diesen Gegenstand besitzt du nicht.") + + def setFehler(self,text): + self.world.fehler =text + + + def stelle(self,parsedCommand): + item = self.world.findItemImInventar(parsedCommand.gegenstand); + + if item != None: + logging.debug('stelle() item.id=' + str(item.id)) + if item.id == '6': + self.world.aktuellerRaum.ausgaenge[self.world.NORD] = '5' + self.verliere(parsedCommand) + self.world.printText('1') + else: + self.setFehler("Diesen Gegenstand besitzt du nicht.") + + def untersuche(self,parsedCommand): + logging.debug('untersuche() suche nach Gegenstand:' + parsedCommand.gegenstand) + item = self.world.findItemImInventarOderAktuellerRaum(parsedCommand.gegenstand) + + if item != None: + if item.id == '10': + schluessel = self.world.findRaumItemById('11') + logging.debug('mache Gegenstand ' + schluessel.name + ' sichtbar') + schluessel.sichtbar = True + self.world.printText('2') + else: + self.world.printText('item-' + item.id) + else: + self.setFehler("diesen Gegenstand sehe ich hier nicht.") + + def ziehe(self,parsedCommand): + item = self.world.findIteminAktuellerRaum(parsedCommand.gegenstand) + + if item != None: + if item.id == 8: + self.world.stdscr.addstr('Du ziehst den Hebel und es passiert.... nichts!') + else: + self.setFehler("diesen Gegenstand sehe ich hier nicht.") + + def nimm(self,parsedCommand): + item = self.world.findIteminAktuellerRaum(parsedCommand.gegenstand) + + if item != None: + if item.imobil: + text = self.world.msg[item.pickupmsg] + + if self.isBlank(item.pickupmsg): + self.setFehler('Das kannst du nicht mitnehmen') + else: + text = self.world.msg[item.pickupmsg] + self.setFehler(text) + else: + del self.world.aktuellerRaum.items[item.id] + self.world.inventar[item.id] = item + else: + self.setFehler("diesen Gegenstand sehe ich hier nicht.") + + + def gehe(self): + richtung = self.world.parsedCommand.gegenstand + print("gehe nach " + richtung) + + if richtung == 'nord': + self.nord() + + def geheNach(self,richtung): + raum = self.world.aktuellerRaum + + if raum.ausgaenge[richtung]: + ausgang = raum.sucheRaumAusgang(richtung) + print("gefundener Ausgang: " + ausgang) + if ausgang != None: + raum = self.world.sucheRaum(ausgang) + print("gefundener Raum "+ raum.name) + else: + self.setFehler("In diese Richtung gibt's keine Ausgang!") + + if raum != None: + self.world.aktuellerRaum = raum + else: + self.setFehler("In diese Richtung gibt's keine Ausgang!") + else: + self.setFehler("In diese Richtung kannst du nicht gehen") + + def nord(self): + self.geheNach(self.world.NORD) + + def sued(self): + self.geheNach(self.world.SUED) + + def west(self): + self.geheNach(self.world.WEST) + + def ost(self): + self.geheNach(self.world.OST) + + def rauf(self): + self.geheNach(self.world.RAUF) + + def runter(self): + self.geheNach(self.world.RUNTER) + + def about(self): + print("Kerker Version " + self.world.VERSION) + self.world.waitForCR + + def raumaction(self): + raumid = self.world.aktuellerRaum.id + + print('Raumaction für Raum: ' + raumid) + + diff --git a/ParsedCommand.py b/ParsedCommand.py new file mode 100644 index 0000000..a933d7e --- /dev/null +++ b/ParsedCommand.py @@ -0,0 +1,9 @@ +class ParsedCommand: + def __init__(self): + self.commandid = '' + self.verb = None + self.gegenstand = None + self.gegenstand2 = None + self.objekt = None + self.key = '-1' + diff --git a/World.py b/World.py new file mode 100644 index 0000000..6575783 --- /dev/null +++ b/World.py @@ -0,0 +1,172 @@ +import WorldParser +import ParsedCommand +import ActionModul +import curses +from curses import wrapper +import logging +from os import system, name + + +class World: + + def clearScreen(self): + self.stdscr.clear() + + def printText(self,textid): + self.clearScreen() + self.stdscr.addstr(1,0,self.texte[textid].name) + self.waitForCR() + self.printRaum() + + def waitForCR(self): + self.stdscr.addstr(0,0,'Taste für Weiter',curses.color_pair(1)) + self.stdscr.getch() + + + def printRaum(self): + raum = self.aktuellerRaum + self.clearScreen() + + if self.fehler != None and self.fehler != '': + self.stdscr.addstr(self.fehler,curses.color_pair(1)) + + self.stdscr.addstr(3,0,'aktueller Raum: ' + raum.name,curses.color_pair(3)) + self.stdscr.addstr(4,0,raum.beschreibung) + self.stdscr.addstr(7,0,'Gegenstände: ' + str(raum.labelsGegenstaende()),curses.color_pair(2)) + self.stdscr.addstr(8,0,'Personen: ' + str(raum.labelsPersonen()),curses.color_pair(2)) + + + aus = raum.ausgaenge + self.stdscr.addstr(9,0,'mögliche Richtungen: ') + r = [] + if aus[self.NORD] != '-1': + r.append('Norden') + if aus[self.OST] != '-1': + r.append('Osten') + if aus[self.WEST] != '-1': + r.append('Westen') + if aus[self.SUED] != '-1': + r.append('Süden') + if aus[self.RAUF] != '-1': + r.append('Rauf') + if aus[self.RUNTER] != '-1': + r.append('Runter') + + richtungen ='' + ixri = 0 + win = curses.newwin(7,35,7,85) + win.box() + win.addstr('Ausgänge:') + for ri in r: + richtungen = richtungen + ri + win.addstr(ixri+1,1,ri) + if ixri +1 < len(r): + richtungen = richtungen + ', ' + ixri = ixri +1 + self.stdscr.addstr(9,22,richtungen) + self.stdscr.refresh() + win.refresh() + + def sucheRaum(self,id): + for raumid in self.raumliste: + raum = self.raumliste[raumid] + if raum.id == id: + return raum + return None + + def findItemImInventarOderAktuellerRaum(self,itemname): + item = self.findItemImInventar(itemname) + + if item == None: + item = self.findIteminAktuellerRaum(itemname) + return item + + + def findIteminAktuellerRaum(self,itemname): + raum = self.aktuellerRaum + + for itemid in raum.items: + item = raum.items[itemid] + logging.debug('raum.id=' + raum.id) + logging.debug('item.raumid=' + item.raumid) + logging.debug('findItemInAktuellerRaum() ' + itemname + '-' + item.name) + if item.raumid == raum.id and item.name == itemname: + return item + + return None + + def findRaumItemById(self,id): + raum = self.aktuellerRaum + + for itemid in raum.items: + item = raum.items[itemid] + if item.raumid == raum.id and item.id == id: + return item + + return None + + def findItemImInventar(self,itemname): + + for itemid in self.inventar: + if self.inventar[itemid].name == itemname: + return self.inventar[itemid] + return None + + def ermittleBefehlId(self,befehl): + for id in self.befehle: + name = self.befehle[id].name + if name == befehl: + return id + return None + + def removeSortouts(self,words): + w = [] + for word in words: + if word not in self.sortouts: + w.append(word) + return w + + + def parseInput(self,input): + words = input.split(' ') + words = self.removeSortouts(words) + parsedCommand = ParsedCommand.ParsedCommand() + befehlid = self.ermittleBefehlId(words[0]) + + if befehlid != None: + befehl = self.befehle[befehlid] + parsedCommand.commandid = befehl.id + parsedCommand.key = befehl.key + + if len(words) > 1: + parsedCommand.gegenstand = words[1] + if len(words) > 2: + parsedCommand.gegenstand2 = words[2] + + return parsedCommand + + def __init__(self): + self.raumliste = {} + self.msg = {} + self.personen = {} + self.bewegungen = {} + self.gegenstaende = {} + self.befehle = {} + self.inventar = {} + self.objekte = {} + self.sortouts = [] + self.texte = {} + self.aktuellerRaum = None + self.parsedCommand = None + self.NOEXIT = '-1' + self.NORD = '0' + self.OST = '1' + self.SUED = '2' + self.WEST = '3' + self.RAUF = '4' + self.RUNTER = '5' + self.VERSION = '0.5' + self.fehler = '' + parser = WorldParser.WorldParser(self) + parser.parseWorld('world.xml') + diff --git a/WorldParser.py b/WorldParser.py new file mode 100644 index 0000000..c8892ac --- /dev/null +++ b/WorldParser.py @@ -0,0 +1,120 @@ +import xml.etree.ElementTree as ET +from data.Raum import Raum, Befehl, Gegenstand,Person,TextNode +import io +import logging + +class WorldParser(): + + def __init__(self,world): + self.neuerRaum = None + self.world = world + self.textCount = 0 + + def parseWorld(self,filename): + tree = ET.parse(filename) + root = tree.getroot() + + for item in root.findall('ebene/raum'): + id = item.attrib['id'] + name = item.attrib['name'] + beschreibung = '' +# Beschreibung + for text in item.iter(): + if text != None and text.text != None: + beschreibung = beschreibung + text.text.strip() +# Ausgaenge + raum = Raum(name,id, beschreibung) + ausgaenge = item.findall('ausgang') + + for ausgang in ausgaenge: + dir = ausgang.attrib['dir'] + nachRaum = ausgang.attrib['nachRaum'] + + print('Raum: ' + raum.name + '- dir=' + dir + ',nachRaum=' + nachRaum) + if dir == 'Nord': + raum.ausgaenge[self.world.NORD] = nachRaum + elif dir == 'Sued': + raum.ausgaenge[self.world.SUED] = nachRaum + elif dir == 'West': + raum.ausgaenge[self.world.WEST] = nachRaum + elif dir == 'Ost': + raum.ausgaenge[self.world.OST] = nachRaum + elif dir == 'Rauf': + raum.ausgaenge[self.world.RAUF] = nachRaum + elif dir == 'Runter': + raum.ausgaenge[self.world.RUNTER] = nachRaum + + + self.world.raumliste[id] = raum + startr = root.find('startraum') + print(startr) + print(startr.attrib['id']) + startraum = self.world.sucheRaum(startr.attrib['id']) + self.world.aktuellerRaum = startraum + print('Startraum gefunden.') + print(startraum) + +#Befehle + for befehl in root.findall('commandset/command'): + name = befehl.attrib['name'] + id = befehl.attrib['id'] + key = befehl.attrib['key'] + command = Befehl(name,id,key) + + self.world.befehle[id] = command + +# Sortouts + for sortout in root.findall('sortouts/sortout'): + name = sortout.attrib['name'] + self.world.sortouts.append(name) +# Messages + for msg in root.findall('messages/pickup'): + id = msg.attrib['id'] + text = msg.attrib['text'] + self.world.msg[id] = text + +# Gegenstände + for item in root.findall('items/item'): + name = item.attrib['name'] + id = item.attrib['id'] + msgid = item.attrib['msgid'] + raumid = item.attrib['raum'] + gegenstand = Gegenstand(name,id,raumid) + imobil = item.attrib['imobil'] + visible = item.attrib['visible'] + raum = self.world.sucheRaum(raumid) + gegenstand.raum = raum.id + gegenstand.imobil = imobil.lower() in ['true','True','1'] + logging.debug('itemid= ' + id + ',visible= ' + visible) + gegenstand.sichtbar = visible.lower() not in ['false','False','0'] + logging.debug('Gegenstand ' + gegenstand.name + ' ist sichtbar: ' + str(gegenstand.sichtbar)) + gegenstand.pickupmsg = msgid + print('Item ' + gegenstand.name + ' - Pickup: ' + gegenstand.pickupmsg) + + raum.items[id] = gegenstand + self.world.gegenstaende[id] = gegenstand + +# Personen + for item in root.findall('personen/person'): + name = item.attrib['name'] + id = item.attrib['id'] + move = item.attrib['bewegung'] + + raumid = item.attrib['raum'] + person = Person(name,id,raum) + self.world.personen[id] = person + raum = self.world.sucheRaum(raumid) + raum.personen[raumid] = person + +# Texte + for item in root.findall('texte/text'): + id = item.attrib['id'] + print('Id: ' + id+ ' Text ' + item.text) + textnode = TextNode(id,item.text) + self.world.texte[id] = textnode + + + + + + diff --git a/data/Raum.py b/data/Raum.py new file mode 100644 index 0000000..d9eb694 --- /dev/null +++ b/data/Raum.py @@ -0,0 +1,88 @@ +import logging + +class SuperNode: + + def __init__(self,name,id): + self.name = name + self.id = id + +class Raum(SuperNode): + + def __init__(self,name,id,beschreibung): + + super().__init__(name,id) + self.items ={} + self.personen = {} + self.beschreibung=beschreibung + self.pickupmsg = '' + self.ausgaenge = { + '0': '-1', # Nord + '1': '-1', # West + '2': '-1', # Sued + '3': '-1', # Ost + '4': '-1', # Rauf + '5': '-1', # Runter + } + + def sucheRaumAusgang(self,richtung): + nachRaum = self.ausgaenge[richtung] + return nachRaum + + def labelsPersonen(self): + labels = [] + + for persid in self.personen: + labels.append(self.personen[persid].name) + + return labels + + def labelsGegenstaende(self): + labels =[] + + for itemid in self.items: + item = self.items[itemid] + logging.debug('labelsGegenstaende(): Item ' + self.items[itemid].name + ' ist ' + str(item.sichtbar)) + if item.sichtbar: + labels.append(item.name) + + return labels + + +class Befehl(SuperNode): + def __init__(self,command,id,key): + super().__init__(command,id) + self.command = command + self.key = key + +class Ausgang(SuperNode): + def __init__(name,id,nachRaum): + super(name,id) + self.nachRaum = nachRaum + self.richtung = name + +class Gegenstand(SuperNode): + def __init__(self,name,id,raumid): + super().__init__(name,id) + self.raumid = raumid + self.zustand = 0 + self.sichtbar = True + self.imobil = False + self.GESCHLOSSEN = 0 + self.OFFEN = 1 + self.zustand = self.GESCHLOSSEN + +class Person(SuperNode): + def __init__(self, name,id,raum): + super().__init__(name,id) + self.raum = raum + self.bewegung = [] + def __str__(self): + return self.name + +class TextNode(SuperNode): + def __init__(self,id, text): + super().__init__(text, id) + + def __str__(self): + return self.name + diff --git a/data/Raum.pyc b/data/Raum.pyc new file mode 100644 index 0000000..b7e718f Binary files /dev/null and b/data/Raum.pyc differ diff --git a/data/__init__.py b/data/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/data/__init__.py @@ -0,0 +1 @@ + diff --git a/data/__init__.pyc b/data/__init__.pyc new file mode 100644 index 0000000..5e2de71 Binary files /dev/null and b/data/__init__.pyc differ diff --git a/data/__pycache__/Raum.cpython-36.pyc b/data/__pycache__/Raum.cpython-36.pyc new file mode 100644 index 0000000..6b05d58 Binary files /dev/null and b/data/__pycache__/Raum.cpython-36.pyc differ diff --git a/data/__pycache__/Raum.cpython-38.pyc b/data/__pycache__/Raum.cpython-38.pyc new file mode 100644 index 0000000..f8b9c52 Binary files /dev/null and b/data/__pycache__/Raum.cpython-38.pyc differ diff --git a/data/__pycache__/SuperNode.cpython-36.pyc b/data/__pycache__/SuperNode.cpython-36.pyc new file mode 100644 index 0000000..6c37816 Binary files /dev/null and b/data/__pycache__/SuperNode.cpython-36.pyc differ diff --git a/data/__pycache__/__init__.cpython-36.pyc b/data/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..43159e3 Binary files /dev/null and b/data/__pycache__/__init__.cpython-36.pyc differ diff --git a/data/__pycache__/__init__.cpython-38.pyc b/data/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..b7d618c Binary files /dev/null and b/data/__pycache__/__init__.cpython-38.pyc differ diff --git a/kerker.py b/kerker.py new file mode 100755 index 0000000..958bcfb --- /dev/null +++ b/kerker.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 +import World +import ActionModul +import signal +import sys +import logging +import curses +from curses import wrapper + +def verarbeiteBefehl(parsedCommand): + id = parsedCommand.key + + logging.debug(' Befehlkey: ' + id) + if id == '0': + sys.exit() + elif id == '1': + actionmodul.gehe(parsedCommand) + elif id == '2': + actionmodul.nimm(parsedCommand) + elif id == '3': + actionmodul.untersuche(parsedCommand) + elif id == '5': + actionmodul.nord() + elif id == '6': + actionmodul.ost() + elif id == '7': + actionmodul.sued() + elif id == '8': + actionmodul.west() + elif id == '9': + actionmodul.rauf() + elif id == '10': + actionmodul.runter() + elif id == '12': + actionmodul.inventar() + world.waitForCR() + elif id == '13': + actionmodul.about() + elif id == '14': + print('verliere: ' + parsedCommand.gegenstand) + actionmodul.verliere(parsedCommand) + elif id == '15': + actionmodul.warte() + elif id == '17': + actionmodul.ziehe(parsedCommand) + elif id == '19': + actionmodul.stelle(parsedCommand) + elif id == '-1': + world.fehler = 'Ich verstehe diesen Befehl nicht' + else: + world.fehler = 'nicht implementierter Befehl' + +def handle_SIGINT(sig,frame): + logging.debug("CTRL-C abgefangen") + sys.exit(0) + + +logging.basicConfig(filename='kerker.log', level=logging.DEBUG) +signal.signal(signal.SIGINT,handle_SIGINT) +world = World.World() +logging.debug('World initialisiert') +actionmodul = ActionModul.ActionModul(world) + +def inputLoop(stdscr): + world.stdscr = stdscr + curses.echo() + curses.init_pair(1,curses.COLOR_RED, curses.COLOR_WHITE) + curses.init_pair(2,curses.COLOR_GREEN, curses.COLOR_BLACK) + curses.init_pair(3,curses.COLOR_YELLOW, curses.COLOR_BLACK) + commandid = '' + while commandid != '0': + world.printRaum() + stdscr.addstr(11,0,'Was nun? ') + command = stdscr.getstr(11,10,40).decode(encoding="utf-8") + command = command.rstrip() + parsedCommand = world.parseInput(command) + commandid = parsedCommand.commandid + verarbeiteBefehl(parsedCommand) + actionmodul.raumaction() + +wrapper(inputLoop) diff --git a/parser/ParsedCommand.py b/parser/ParsedCommand.py new file mode 100644 index 0000000..f9ed580 --- /dev/null +++ b/parser/ParsedCommand.py @@ -0,0 +1,7 @@ +class ParsedCommand(): + def __init__(self): + self.commandid = -1 + self.verb = None + self.gegenstand = None + self.objekt = None + diff --git a/world.xml b/world.xml new file mode 100644 index 0000000..e540e35 --- /dev/null +++ b/world.xml @@ -0,0 +1,150 @@ + + + + + + + Im Raum steht eine Streckbank, mit der du vermutlich bald nähere Bekanntschaft machen wirst. +An der Wand steht eine geöffnete eiserne Jungfrau mit furchterregenden spitzen, rostigen Dornen. + + + + + Hier lagern die Folterinstrumente. Du erkennst verschiede Zangen, Ketten, spitze Gegenstände und einige Utensilien, von denen du gar nicht wissen willst, welchen Zweck sie haben. + + + + + + + + + Der Gang verläuft von Süd nach Nord. Im Norden versperrt ein massives Gittertor den Weg. + + + + + + Der Aufenthaltsraum der Wächter, hier stehen ein Tisch und vier Stühle. + An der Wand steht ein Gestell für Schwerter. + + + + + + Treppenhaus im Keller + + + + + + + Treppenhaus in der Kerkerebene. Eine Tür führt nach Süden. Über der Tür hängt ein Hinweisschild. + + + + + + + Vom Gang im Zellblock führt eine Tür nach Osten in die Kerkerzelle. Über der Tür hängt ein Hinweisschild. + + + + + + Vom Gang im Zellblock führt eine Tür nach Osten in die Kerkerzelle. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Als du die Fackel in den Fackelhalter stellst, kippt der Halter zur Seite. Hinter der Wand hörst du Seilzüge und Zahnräder + anlaufen. Über den verborgenen Mechanismus wird das Gittertor in die Höhe gezogen und der Weg nach Norden ist frei. + + + In einer Schreibtischschublade findest du einen Schlüssel. + + + Der Schlüssel ist etwa 10cm lang und hat eine ringförmige Reide. Der Bart hat drei Zacken und zwei Kerben. + + + Zellblock 11-38 + + + Das Tor besteht aus schmideeisernen Stäben, die geschmiedete Querträger halten. Das Tor ist an den Seiten in Führungsschienen + eingelassen. Auf der rechten Seite befindet sich etwa in der Mitte ein Schloss. Hinter dem Tor scheint ein Treppenhaus zu sein. + + +