forked from andreiapostoae/dota2-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_gui.py
249 lines (190 loc) · 6.09 KB
/
basic_gui.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from Tkinter import *
import ttk
import json
import logging
import pickle
import os
from os import listdir
from sklearn.externals import joblib
from query import process_query_list, give_result
MAX_MMR = 9000
MMR_INIT = 10000
heroes_json_data = {}
def get_hero_id(localized_name):
for hero in heroes_json_data:
if hero["localized_name"] == localized_name:
return hero["id"]
def get_full_hero_list():
global heroes_json_data
json_data = json.load(open(os.path.join('preprocessing', 'heroes.json'), "rt"))
hero_list = []
heroes_json_data = json_data["heroes"]
for hero in heroes_json_data:
hero_list.append(hero["localized_name"])
return hero_list
suggest_button = None
predict_button = None
boxes = []
unique_heroes = []
mmr_box = None
predict_result_label = None
suggest_result_label = None
no_mmr = True
def check_boxes_completed():
global no_mmr
global boxes
global suggest_button
global predict_button
global mmr_box
global unique_heroes
completed_valid = 0
total_completed = 0
unique_heroes = []
for box in boxes:
box_text = box.get()
if box_text != "":
total_completed += 1
if box_text != "" and box_text not in unique_heroes:
unique_heroes.append(box_text)
completed_valid = len(unique_heroes)
if total_completed - completed_valid != 0:
predict_button.config(state="disabled")
suggest_button.config(state="disabled")
return
if completed_valid == 9 and no_mmr == False:
suggest_button.config(state="normal")
predict_button.config(state="disabled")
elif completed_valid == 10 and no_mmr == False:
predict_button.config(state="normal")
suggest_button.config(state="disabled")
else:
predict_button.config(state="disabled")
suggest_button.config(state="disabled")
def ComboBoxSelected(event):
check_boxes_completed()
def validate(action, index, value_if_allowed, prior_value, text, validation_type, trigger_type, widget_name):
global no_mmr
if text in '0123456789':
try:
if value_if_allowed == "":
no_mmr = True
check_boxes_completed()
return True
val = int(value_if_allowed)
if val >= 0 and val < 10000:
no_mmr = False
check_boxes_completed()
return True
except ValueError:
return False
return False
def process_query():
global mmr_box
global boxes
global predict_result_label
global suggest_result_label
mmr = int(mmr_box.get())
file_list = [int(valid_file[:-4]) for valid_file in listdir('pretrained') \
if 'dicts' not in valid_file and 'results' not in valid_file]
file_list.sort()
min_distance = MMR_INIT
final_mmr = MMR_INIT
for model_mmr in file_list:
if abs(mmr - model_mmr) < min_distance:
min_distance = abs(mmr - model_mmr)
final_mmr = model_mmr
model = joblib.load("pretrained/" + str(final_mmr) + ".pkl")
query_list = []
faction = "Radiant"
for i in range(10):
name = boxes[i].get()
if name != "":
hero_id = get_hero_id(name)
query_list.append(hero_id)
else:
if i < 5:
faction = "Radiant"
else:
faction = "Dire"
print query_list
logging.basicConfig(level=logging.INFO, format='%(name)-10s %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)
if len(query_list) == 9:
sorted_dict = process_query_list(query_list, heroes_json_data, faction, model, logger)
i = 0
label_text = ""
for (hero, value) in sorted_dict:
value = round(value, 2)
hero_name = ""
for json_hero in heroes_json_data:
if json_hero["id"] == hero + 1:
hero_name = json_hero["localized_name"]
break
label_text += "%s: %.2f%%\n" % (hero_name, value)
i += 1
if(i == 10):
break
suggest_result_label['text'] = label_text
predict_result_label['text'] = ""
else:
result = give_result(query_list, faction, model, logger)
suggest_result_label['text'] = ""
if result < 50.0:
predict_result_label['text'] = "Dire has a %.2f%% chance to win" % (100 - result)
else:
predict_result_label['text'] = "Radiant has a %.2f%% chance to win" % result
def main():
global suggest_button
global predict_button
global boxes
global mmr_box
global predict_result_label
global suggest_result_label
root = Tk()
hero_list = get_full_hero_list()
hero_list = sorted(hero_list)
hero_list.insert(0, "")
root.title("Dota 2 predictor")
root.minsize(width=450, height=480)
root.maxsize(width=450, height=480)
radiant_label = Label(root, text="Radiant team")
radiant_label.place(relx=0.15, rely=0.02)
dire_label = Label(root, text="Dire team")
dire_label.place(relx=0.67, rely=0.02)
predict_button = Button(root, text="Predict winner", command=process_query)
predict_button.place(relx=0.22, rely=0.45)
predict_button.config(state="disabled")
suggest_button = Button(root, text="Suggest hero", command=process_query)
suggest_button.place(relx=0.5, rely=0.45)
suggest_button.config(state="disabled")
boxes = []
for i in range(10):
box_value = StringVar()
box = ttk.Combobox(root, textvariable=box_value, state='readonly')
box['values'] = hero_list
box.current(0)
box.place(relx=(i / 5) * 0.5 + 0.07, rely=(i % 5) * 0.05 + 0.1)
box.bind("<<ComboboxSelected>>", ComboBoxSelected)
boxes.append(box)
avg_mmr = Label(root, text="Average MMR:")
avg_mmr.place(relx=0.4, rely=0.4, anchor=CENTER)
vcmd = (root.register(validate), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
mmr_box = Entry(root, validate="key", validatecommand=vcmd)
mmr_box.place(relx=0.56, rely=0.4, anchor=CENTER, width=50)
predict_result_label = Label(root, text="")
predict_result_label.place(relx = 0.5, anchor=CENTER, y=350)
suggest_result_label = Label(root, text="")
suggest_result_label.place(relx = 0.5, rely=0.8, anchor=CENTER, )
info_label1 = Label(root, text="For predicting the winner, select all the heroes in the game.")
info_label1.place(x=10, y=250)
info_label2 = Label(root, text="For getting last pick suggestions, select the other 9 heroes.")
info_label2.place(x=10, y=270)
label = Label(root, text="Andrei Apostoae, July 2017")
label.place(x=300, y=460)
label.configure(foreground="gray")
label = Label(root, text="Current patch: 7.06d")
label.place(x=10,y=460)
label.configure(foreground="gray")
root.mainloop()
if __name__ == "__main__":
main()