From 743d27c924ec782432cd95867f5db66af7cb72fa Mon Sep 17 00:00:00 2001 From: alastrim Date: Fri, 26 May 2023 20:26:07 -0300 Subject: Adapt code to python 3 and use xml.dom.minidom library to parse xml file --- client/weapons.py | 150 ++++++++++++++++++++++++++---------------------------- 1 file changed, 71 insertions(+), 79 deletions(-) diff --git a/client/weapons.py b/client/weapons.py index 8dc03d7..1d90c39 100755 --- a/client/weapons.py +++ b/client/weapons.py @@ -1,89 +1,81 @@ -#!/usr/bin/python2.7 +#!/usr/bin/python3 +# -*- coding: utf8 -*- -class Item: - def __init__(self, xid): - self.id=xid - self.lvl=0 +import xml.dom.minidom -a=open("../../client-data/items.xml", "r") -swords=[] -bows=[] -shields=[] +def main(): + swords = [] + bows = [] + shields = [] -gid="0" -rid=0 -ctx=Item(0) -mem=[] + dom = xml.dom.minidom.parse('../../client-data/items.xml') -for l in a: - if "= 2700 and rid <= 2899: - shields.append(rid) - elif rid >= 3500 and rid <= 3999: - swords.append(rid) - elif rid >= 6000 and rid <= 6499: - bows.append(rid) - -a.close() - -#shields=sorted(shields, reverse=True) -#bows=sorted(bows, reverse=True) -#swords=sorted(swords, reverse=True) + # get item elements + items = root.getElementsByTagName("item") + list_items = [] -b=open("weapons.tmp", "w") + for item in items: + item_id = item.getAttribute("id") + item_level = item.getAttribute("level") -b.write('\n\ -\n\ -\n\ -\n') - -b.write(' \n') - -for i in swords: - b.write(' \n' % i) - -b.write(' \n \n') - -for i in bows: - b.write(' \n' % i) - -b.write(' \n \n') - -for i in shields: - b.write(' \n' % i) + if not item_level: + continue -b.write(' \n') + try: + item_id = int(item_id) + except Exception as e: + print(f"{e} - [CRITICAL] Invalid item ID format: {item_id}") + return -b.close() + try: + item_level = int(item_level) + except Exception as e: + print(f"{e} - [CRITICAL] Invalid item level format: {item_level}") + return + + list_items.append(item_id) + + for item_id in sorted(list_items): + if item_id >= 2700 and item_id <= 2899: + shields.append(item_id) + elif item_id >= 3500 and item_id <= 3999: + swords.append(item_id) + elif item_id >= 6000 and item_id <= 6499: + bows.append(item_id) + + # shields = sorted(shields, reverse=True) + # bows = sorted(bows, reverse=True) + # swords = sorted(swords, reverse=True) + + with open('weapons.tmp', 'w', encoding='UTF-8') as f: + print('\n\ + \n\ + \n\ + ', file=f) + + print(' ', file=f) + for i in swords: + print(f' ', file=f) + print(' ', file=f) + + print(' ', file=f) + for i in bows: + print(f' ', file=f) + print(' ', file=f) + + print(' ', file=f) + for i in shields: + print(f' ', file=f) + print(' ', file=f) + + print('', file=f) + + +if __name__ == '__main__': + main() -- cgit v1.2.3-70-g09d2