136 lines
4.2 KiB
Python
136 lines
4.2 KiB
Python
import xml.etree.ElementTree as ET
|
|
from data.Raum import Raum, Befehl, Adjektiv,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
|
|
# Adjektive
|
|
for adj in root.findall('adjektive/adjektiv'):
|
|
name = adj.attrib['name']
|
|
id = adj.attrib['id']
|
|
key = adj.attrib['key']
|
|
adjektiv = Adjektiv(name,id,key)
|
|
|
|
self.world.adjektive[id] = adjektiv
|
|
|
|
# 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']
|
|
adjektiv = item.attrib['adjektiv']
|
|
raum = self.world.sucheRaum(raumid)
|
|
if raum is not None:
|
|
gegenstand.raum = raum.id
|
|
gegenstand.adjektiv = adjektiv
|
|
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
|
|
# logging.debug('Item ' + gegenstand.name + ' - Pickup: ' + gegenstand.pickupmsg)
|
|
|
|
raum.items[id] = gegenstand
|
|
self.world.gegenstaende[id] = gegenstand
|
|
else:
|
|
if raum != '-1':
|
|
logging.error(f'Kann Raum für Gegenstand {gegenstand.name} nicht finden')
|
|
logging.error(f'Kein Raum für {gegenstand.name}')
|
|
|
|
# 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
|
|
|
|
|
|
|
|
|
|
|
|
|