-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.py
109 lines (85 loc) · 3.31 KB
/
App.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import tkinter
from tkinter import filedialog
import csv
class item:
def __init__(self, item, count, prob):
self.item = item
self.prob = str(round(prob * 100, 2)) + "%"
self.count = count
def __str__(self):
return f'{self.prob} {self.count} {self.item}'
items = []
def calcProb(r, maxR, w, totalW):
if maxR == None:
return 1 - (1 - w / totalW) ** r
else:
num = 0
for n in range(int(r), int(maxR) + 1, 1):
num += 1 - ((1 - (w / totalW)) ** n)
return num / (maxR - r + 1)
def readJson(loottable):
for pool in loottable['pools']:
rolls = 0
maxRolls = None
totalWeight = 0
# if it has max and min rolls
if type(pool['rolls']) == dict:
rolls = pool['rolls']['min']
maxRolls = pool['rolls']['max']
else:
rolls = pool['rolls']
#count total weight
for entry in pool['entries']:
totalWeight += 1
# if has special weight, else it just should add 1
if "weight" in entry:
totalWeight += entry['weight'] - 1
# declaring each entry
for entry in pool['entries']:
# pass if empty
if entry['type'] == "minecraft:empty":
continue
itemCount = "1"
if "weight" in entry:
itemProb = calcProb(rolls, maxRolls, entry['weight'], totalWeight)
else:
itemProb = calcProb(rolls, None, 1, totalWeight)
itemId = ""
# if has functions
if "functions" in entry:
for function in entry['functions']:
#if has count
if function['function'] == "minecraft:set_count":
# if varies in count
if "min" in function['count']:
itemCount = str(function['count']['min']) + "-" + str(function['count']['max'])
else:
itemCount = str(function['count'])
if function['function'] == "minecraft:enchant_randomly":
itemId = "randomly encahnted "
# non changing variables and adding the item to the list of items
itemId += entry['name']
items.append(item(itemId, itemCount, itemProb))
for i in items:
textarea.insert("end", str(i))
textarea.insert("end", "\n")
def uploadFile():
f = filedialog.askopenfilename(initialdir = "/",title = "Select file")
if f != "":
loottable = json.loads(open(f, "r").read().replace('\n', ' '))
readJson(loottable)
def exportCSV():
if items != []:
writer = csv.DictWriter(open("/exportedLootTable.csv", "w"), fieldnames=["probability", "count", "item"])
for i in items:
writer.writerow({"probability": i.prob, "count": i.count, "item": i.item})
root = tkinter.Tk()
btn_upload_json = tkinter.Button(root, command=uploadFile, text="Upload a loot table file to read")
btn_upload_json.pack()
btn_export_csv = tkinter.Button(root, command=exportCSV, text="Export to .csv")
btn_export_csv.pack()
textarea = tkinter.Text(root,height=50,width=100)
textarea.insert("end", "probabilty count item\n")
textarea.pack()
root.mainloop()