-
Notifications
You must be signed in to change notification settings - Fork 94
/
comp_editor.py
430 lines (357 loc) · 14.7 KB
/
comp_editor.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
CompEditor Application
This script defines a Tkinter-based GUI application for editing champion compositions.
"""
import tkinter as tk
from tkinter import ttk, simpledialog
import json
import os
from comps import COMP
from game_assets import FULL_ITEMS, CHAMPIONS
CHAMPION_NAMES = list(CHAMPIONS.keys())
ITEM_OPTIONS = list(FULL_ITEMS.keys())
class CompEditor(tk.Tk):
"""
Class representing the CompEditor application.
Attributes:
comp_tree (ttk.Treeview): Treeview widget for displaying champion details.
COMP (dict): Dictionary containing champion data.
trait_vars (list): List of StringVar instances for trait dropdowns.
"""
# pylint: disable=too-many-instance-attributes,too-many-public-methods
def __init__(self, comp_data):
super().__init__()
self.title("Comp Editor")
self.geometry("1280x720")
self.comp_tree = ttk.Treeview(
self, columns=("board_position", "level", "items", "final_comp")
)
self.comp_tree.heading("#0", text="Champion")
self.comp_tree.heading("board_position", text="Board Position")
self.comp_tree.heading("level", text="Level")
self.comp_tree.heading("items", text="Items")
self.comp_tree.heading("final_comp", text="Final Comp")
self.comp_tree.grid(row=0, column=1, rowspan=8, sticky="nsew")
self.comp = comp_data
self.trait_vars = [tk.StringVar() for _ in range(3)]
self.populate_tree()
# Left side (Add Champion)
left_frame = ttk.Frame(self)
left_frame.grid(row=0, column=0, rowspan=8, padx=10, pady=10, sticky="nsew")
self.champion_name_var = tk.StringVar(value=CHAMPION_NAMES[0])
self.champion_dropdown = ttk.Combobox(
left_frame, textvariable=self.champion_name_var, values=CHAMPION_NAMES
)
self.champion_dropdown.grid(
row=0, column=0, columnspan=2, pady=5, padx=5, sticky="w"
)
self.board_position_var = tk.StringVar()
self.create_label_entry(
left_frame, "Board Position:", self.board_position_var, row=1
)
self.level_var = tk.StringVar()
self.create_label_entry(left_frame, "Level:", self.level_var, row=2)
self.item_dropdowns = []
for i in range(3):
item_var = tk.StringVar()
item_label = f"Item {i+1}:"
item_dropdown = ttk.Combobox(
left_frame, textvariable=item_var, values=[""] + ITEM_OPTIONS
)
ttk.Label(left_frame, text=item_label).grid(
row=i + 3, column=0, sticky="w", padx=5
)
item_dropdown.grid(row=i + 3, column=1, columnspan=2, pady=5, sticky="w")
self.item_dropdowns.append(item_var)
self.final_comp_var = tk.BooleanVar()
self.create_checkbox(
left_frame, "Final Composition:", self.final_comp_var, row=9
)
self.add_button = tk.Button(
left_frame,
text="Add Champion",
command=self.add_champion,
state=tk.DISABLED,
)
self.add_button.grid(row=10, column=0, columnspan=2, pady=10, sticky="w")
# Right side (Remove Champion)
remove_button = tk.Button(
self, text="Remove Champion", command=self.remove_champion
)
remove_button.grid(row=8, column=1, sticky="e", pady=10, padx=10)
# Save button
save_button = tk.Button(self, text="Save", command=self.save_changes)
save_button.grid(row=2, column=0, sticky="e", pady=10, padx=10)
# Configure grid weights for resizing
self.grid_columnconfigure(1, weight=1)
self.grid_rowconfigure(0, weight=1)
left_frame.grid_columnconfigure(1, weight=1)
left_frame.grid_rowconfigure(11, weight=1)
self.board_position_var.trace_add("write", lambda *args: self.validate_inputs())
self.level_var.trace_add("write", lambda *args: self.validate_inputs())
self.comp_tree.bind("<Double-1>", lambda event: self.on_tree_double_click())
def on_tree_double_click(self):
"""
Handle double-click event on the Treeview widget.
"""
selected_item = self.comp_tree.selection()
if selected_item:
champion = self.comp_tree.item(selected_item, "text")
self.load_champion_details(champion)
def load_champion_details(self, champion):
"""
Load champion details into the input fields.
"""
details = self.comp.get(champion)
if details is None:
print(f"Champion '{champion}' not found in comp.")
return
# Set champion name
self.champion_name_var.set(champion)
# Set other input fields
self.board_position_var.set(details.get("board_position", ""))
self.level_var.set(details.get("level", ""))
self.final_comp_var.set(details.get("final_comp", ""))
# Set item dropdowns
for i, item_var in enumerate(self.item_dropdowns):
items = details.get("items", [])
if i < len(items):
item_var.set(items[i])
else:
item_var.set("")
def create_label_entry(self, frame, label_text, variable, row=None):
"""
Create a label and entry widget pair in the given frame.
Args:
frame (ttk.Frame): The frame in which to create the widgets.
label_text (str): The text for the label widget.
variable: The variable associated with the entry widget.
var_type: The type of variable to create (default is tk.StringVar).
row (int): The row in which to place the widgets.
"""
ttk.Label(frame, text=label_text).grid(row=row, column=0, sticky="w", padx=5)
tk.Entry(frame, textvariable=variable).grid(
row=row, column=1, sticky="w", padx=5
)
def create_checkbox(self, frame, label_text, variable, row=None):
"""
Create a checkbox widget in the given frame.
Args:
frame (ttk.Frame): The frame in which to create the checkbox.
label_text (str): The text for the checkbox.
variable: The variable associated with the checkbox.
row (int): The row in which to place the checkbox.
"""
checkbox = ttk.Checkbutton(frame, text=label_text, variable=variable)
checkbox.grid(row=row, column=1, pady=5, sticky="e")
def populate_tree(self):
"""
Populate the Treeview widget with champion data.
"""
for champion, details in sorted(
self.comp.items(), key=lambda x: x[1]["board_position"]
):
self.comp_tree.insert(
"",
"end",
text=champion,
values=(
details["board_position"],
details["level"],
", ".join(details["items"]),
details["final_comp"],
),
)
def validate_inputs(self):
"""
Validate user inputs for adding a champion.
"""
champion_selected = self.champion_name_var.get()
board_position_str = self.board_position_var.get()
level_str = self.level_var.get()
if (
champion_selected
and self.is_valid_board_position_str(board_position_str)
and self.is_valid_level_str(level_str)
):
self.add_button["state"] = tk.NORMAL
else:
self.add_button["state"] = tk.DISABLED
def is_valid_board_position_str(self, board_position_str):
"""
Check if the board position string is valid.
Args:
board_position_str (str): The string to check.
Returns:
bool: True if the string is a valid board position, False otherwise.
"""
try:
board_position = int(board_position_str)
return self.is_valid_board_position(board_position)
except ValueError:
return False
def is_valid_board_position(self, board_position):
"""
Check if the board position is valid.
Args:
board_position (int): The board position to check.
Returns:
bool: True if the board position is valid, False otherwise.
"""
selected_champion = self.champion_name_var.get()
# Exclude the currently chosen champion from the check
champions_to_check = {
name: champion["board_position"]
for name, champion in self.comp.items()
if name != selected_champion
}
return 0 <= board_position <= 27 and not any(
position == board_position for position in champions_to_check.values()
)
def is_valid_level_str(self, level_str):
"""
Check if the level string is valid.
Args:
level_str (str): The string to check.
Returns:
bool: True if the string is a valid level, False otherwise.
"""
try:
level = int(level_str)
return self.is_valid_level(level)
except ValueError:
return False
def is_valid_level(self, level):
"""
Check if the level is valid.
Args:
level (int): The level to check.
Returns:
bool: True if the level is valid, False otherwise.
"""
return level in {1, 2, 3}
def add_champion(self):
"""
Add a new champion based on user inputs.
Retrieves information entered by the user, validates it,
and then adds a new champion to the COMP data structure.
"""
board_position = self.validate_board_position()
items = self.validate_and_filter_items()
level = self.validate_level()
final_comp = self.final_comp_var.get()
selected_champion = self.champion_name_var.get()
new_champion = {
"board_position": board_position,
"items": items,
"level": level,
"final_comp": final_comp,
}
self.comp[selected_champion] = new_champion
self.comp_tree.delete(*self.comp_tree.get_children())
self.populate_tree()
def validate_board_position(self):
"""
Validate and retrieve the board position entered by the user.
Returns:
int or None: The validated board position or None if validation fails.
"""
board_position_str = self.board_position_var.get()
try:
board_position = int(board_position_str)
if not self.is_valid_board_position(board_position):
simpledialog.messagebox.showerror(
"Error",
"Board Position must be a valid number between 0 and 27 and not already taken.",
)
return None
return board_position
except ValueError:
simpledialog.messagebox.showerror(
"Error", "Board Position must be a valid number."
)
return None
def validate_and_filter_items(self):
"""
Validate and filter the selected items entered by the user.
Returns:
list or None: The filtered list of items or None if validation fails.
"""
items_selected = [item_var.get() for item_var in self.item_dropdowns]
filtered_items = list(filter(lambda item: item, items_selected))
if not all(self.is_valid_item(item) for item in items_selected):
simpledialog.messagebox.showerror(
"Error", "Items can only contain letters (a-zA-Z) and commas."
)
return None
return filtered_items
def validate_level(self):
"""
Validate and retrieve the level entered by the user.
Returns:
int or None: The validated level or None if validation fails.
"""
level_str = self.level_var.get()
if not self.is_valid_level_str(level_str):
simpledialog.messagebox.showerror(
"Error", "Level must be a valid number between 1 and 3."
)
return None
return int(level_str)
def is_valid_item(self, item):
"""
Check if the item string is valid.
Args:
item (str): The item string to check.
Returns:
bool: True if the item string is valid, False otherwise.
"""
return all(c.isalpha() or c.isnumeric() or c == "," for c in item)
def remove_champion(self):
"""
Remove the selected champion from the Treeview and data.
"""
selected_item = self.comp_tree.selection()
if selected_item:
champion = self.comp_tree.item(selected_item, "text")
del self.comp[champion]
self.comp_tree.delete(selected_item)
def save_changes(self):
"""
Save changes made in the application to the comps.py file.
"""
current_file_path = os.path.abspath(__file__)
comps_file_path = os.path.join(os.path.dirname(current_file_path), "comps.py")
with open(comps_file_path, "r", encoding="utf-8", newline='') as file:
file_content = file.read()
comp_line_start = file_content.find("COMP = {")
if comp_line_start == -1:
print("Error: COMP variable not found in the file.")
return
comp_line_end = comp_line_start
brace_count = 0
for _, char in enumerate(file_content[comp_line_start:], start=1):
comp_line_end += 1
if char == "{":
brace_count += 1
elif char == "}":
brace_count -= 1
if brace_count == 0 and char == "}":
break
updated_file_content = (
file_content[:comp_line_start]
+ "COMP = "
+ json.dumps(self.comp, indent=4)
.replace("false", "False")
.replace("true", "True")
.replace(" ", " ")
.replace("\n ", "")
.replace("\n ]", "]")
.replace("[ ", "[")
+ file_content[comp_line_end:]
)
with open(comps_file_path, "w", encoding="utf-8", newline='') as file:
file.write(updated_file_content)
if __name__ == "__main__":
app = CompEditor(COMP)
app.mainloop()