import tkinter as tk
from tkinter import messagebox
import os
import xml.etree.ElementTree as ET
import xml.dom.minidom as minidom
def adjust_rewards(group_chance, rewards):
adjusted_rewards = []
for reward in rewards:
item_id = reward.get('item_id')
if item_id == "57":
adjusted_rewards.append(reward)
else:
adjusted_chance = float(reward.get('chance')) * (float(group_chance) / 100)
adjusted_rewards.append(ET.Element("reward", {
'item_id': item_id,
'min': reward.get('min'),
'max': reward.get('max'),
'chance': f'{adjusted_chance:.4f}'
}))
return adjusted_rewards
def process_file(input_file, output_file):
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
tree = ET.parse(input_file, parser=parser)
root = tree.getroot()
for rewardlist in root.findall('.//rewardlist'):
if rewardlist.get('type') != 'NOT_RATED_GROUPED':
for group in rewardlist.findall('.//group'):
group_chance = group.get('chance')
rewards = group.findall('reward')
adjusted_rewards = adjust_rewards(group_chance, rewards)
for reward in rewards:
group.remove(reward)
for adj_reward in adjusted_rewards:
group.append(adj_reward)
group.set('chance', '100')
# Set chance to 70.0 for item_id="57"
for reward in root.findall('.//reward[@item_id="57"]'):
reward.set('chance', '70.0')
xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ")
xmlstr_no_blank_lines = "\n".join([line for line in xmlstr.split("\n") if line.strip()])
with open(output_file, "w", encoding='utf-8') as f:
f.write(f'<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE list SYSTEM "npc.dtd">\n')
f.write(xmlstr_no_blank_lines.split("<?xml version=\"1.0\" ?>")[1])
def process_all_files(input_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for filename in os.listdir(input_folder):
if filename.endswith(".xml"):
input_file = os.path.join(input_folder, filename)
output_file = os.path.join(output_folder, filename)
process_file(input_file, output_file)
def ask_question():
response = messagebox.askyesno("Question", "Do you love MetaMan?")
if not response:
exit()
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
ask_question()
input_folder = 'npc'
output_folder = 'npc_new'
process_all_files(input_folder, output_folder)