summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralastrim <alasmirt@gmail.com>2023-05-26 20:26:07 -0300
committeralastrim <alasmirt@gmail.com>2023-05-26 20:26:07 -0300
commit743d27c924ec782432cd95867f5db66af7cb72fa (patch)
treefe6428cfc9f4beeb66af26ce188980a7b64a3e37
parent78f7a0464cebb30cf82cdcb5a6311cd0e0b5e6ca (diff)
downloadtools-743d27c924ec782432cd95867f5db66af7cb72fa.tar.gz
tools-743d27c924ec782432cd95867f5db66af7cb72fa.tar.bz2
tools-743d27c924ec782432cd95867f5db66af7cb72fa.tar.xz
tools-743d27c924ec782432cd95867f5db66af7cb72fa.zip
Adapt code to python 3 and use xml.dom.minidom library to parse xml file
-rwxr-xr-xclient/weapons.py150
1 files 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 "<item id=" in l:
- if ctx.id > 0:
- mem.append(ctx)
+ # get root element
+ root = dom.documentElement
- gid=l.replace('\t', '').replace(' ','').replace('<itemid=', '').replace('"', '').replace("'", "")
- rid=0
- if "-" in gid:
- gid="0"
- continue
- try:
- rid=int(gid)
- except:
- print "[CRITICAL] Invalid item ID format: " + l
- exit(1)
-
- ctx=Item(rid)
-
- if "\tlevel=" in l or " level=" in l:
- gid=l.replace('\t', '').replace(' ','').replace('level=', '').replace('"', '').replace("'", "")
- try:
- rid=int(gid)
- except:
- print "[CRITICAL] Invalid item level format: " + l
- exit(1)
- ctx.lvl=0+rid
-
-mem=sorted(mem, key=lambda xcv: xcv.lvl, reverse=True)
-
-for r in mem:
- rid=r.id
- if rid >= 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('<?xml version="1.0" encoding="utf-8"?>\n\
-<!-- Author: 4144, Jesusalva\n\
-Copyright (C) 2015 Evol Online\n\
-Copyright (C) 2018 TMW2: Moubootaur Legends\n -->\n\
-\n\
-<weapons>\n')
-
-b.write(' <swords>\n')
-
-for i in swords:
- b.write(' <item id="%d"/>\n' % i)
-
-b.write(' </swords>\n <bows>\n')
-
-for i in bows:
- b.write(' <item id="%d"/>\n' % i)
-
-b.write(' </bows>\n <shields>\n')
-
-for i in shields:
- b.write(' <item id="%d"/>\n' % i)
+ if not item_level:
+ continue
-b.write(' </shields>\n</weapons>')
+ 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('<?xml version="1.0" encoding="utf-8"?>\n\
+ <!-- Author: 4144, Jesusalva\n\
+ Copyright (C) 2015 Evol Online\n\
+ Copyright (C) 2018 TMW2: Moubootaur Legends\n -->\n\
+ \n\
+ <weapons>', file=f)
+
+ print(' <swords>', file=f)
+ for i in swords:
+ print(f' <item id="{i}"/>', file=f)
+ print(' </swords>', file=f)
+
+ print(' <bows>', file=f)
+ for i in bows:
+ print(f' <item id="{i}"/>', file=f)
+ print(' </bows>', file=f)
+
+ print(' <shields>', file=f)
+ for i in shields:
+ print(f' <item id="{i}"/>', file=f)
+ print(' </shields>', file=f)
+
+ print('</weapons>', file=f)
+
+
+if __name__ == '__main__':
+ main()