-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pathfinder_Spell_Slot_Tracker.pyw
397 lines (332 loc) · 15.9 KB
/
Pathfinder_Spell_Slot_Tracker.pyw
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
from tkinter import Tk, ttk, Label, LabelFrame, Listbox, Checkbutton, IntVar,END
from functools import partial
import logging as lg
import subprocess
import json
import sys
import os
class Startup:
def __init__(self):
self.profile = ''
self.extras = 1
def create_profile_list(self):
'''
Creates a list of the .json files in the profile folder.
'''
profile_list = []
for file in os.scandir(f'{os.getcwd()}/Profiles'):
if file.name.endswith('.json'):
profile_list.append(file.name)
if len(profile_list) == 0:
print('No profiles found')
self.open_profile_location()
else:
return profile_list
def refresh_profiles(self):
'''
Refreshes profile listbox.
'''
profileList = self.create_profile_list()
if len(profileList) == 1:
App = Tracker(profileList[0])
App.create_window()
return
self.profilelist.delete(0, END)
for item in profileList:
self.profilelist.insert(END, os.path.splitext(item)[0])
self.profilelist.select_set(0)
def load_profile(self):
'''
Sets the profile based on the listbox selection.
'''
self.profile = self.profilelist.get(self.profilelist.curselection()) + '.json'
App = Tracker(self.profile, self.extras)
self.Prompt.destroy()
App.create_window()
def new_profile(self):
'''
Creates a new profile and refreshes the list.
'''
template = {
'settings':
{'character_name': 'Dain Olaren',
'character_class': 'Sorcerer',
'spell_level_range': 3,
'excel': 'Insert Excel Path Here',
'ahk': 'Insert AHK Path Here'},
'spells': {
'level_1': {'spells_per_day': 8, 'spells_left': 8},
'level_2': {'spells_per_day': 7, 'spells_left': 7},
'level_3': {'spells_per_day': 3, 'spells_left': 3},
'level_4': {'spells_per_day': 0, 'spells_left': 0},
'level_5': {'spells_per_day': 0, 'spells_left': 0},
'level_6': {'spells_per_day': 0, 'spells_left': 0},
'level_7': {'spells_per_day': 0, 'spells_left': 0},
'level_8': {'spells_per_day': 0, 'spells_left': 0},
'level_9': {'spells_per_day': 0, 'spells_left': 0}},
'spell_like':
{'Silver Tongue': {'per_day': 8, 'uses_left': 8}}}
json_object = json.dumps(template, indent = 4) # Serializing json
with open('Profiles/profile.json', "w") as outfile: # Writing to sample.json
outfile.write(json_object)
self.refresh_profiles()
@staticmethod
def open_profile_location():
'''
Opens Profile folder in explorer.
'''
subprocess.Popen(f'explorer "{os.getcwd()}\\Profiles\\"')
def profile_select(self):
self.Prompt = Tk()
self.Prompt.title("Spell Slot Counter")
self.Prompt.iconbitmap(self.Prompt, 'Fireball Icon.ico')
window_width = 369
window_height = 194
width = int((self.Prompt.winfo_screenwidth()-window_width)/2)
height = int((self.Prompt.winfo_screenheight()-window_height)/2)
self.Prompt.geometry(f'+{width}+{height}')
# Bindings for interface.
self.Prompt.bind("<Key-Return>", self.load_profile)
self.Prompt.bind(f"<Escape>", sys.exit)
# Todo Update spacing to increase size of combobox.
text = "Profile Selector"
PromptTitle = Label(text=text, font=('Arial Bold', 14))
PromptTitle.grid(columnspan=2, row=0, pady=(10, 5), padx=50)
self.profilelist = Listbox(self.Prompt, exportselection=False)
self.profilelist.grid(rowspan=4, column=0, pady=(5,15), padx=(15, 0))
self.refresh_profiles()
button_width = 15
padx_spacing = 15
self.extras = IntVar(value=1)
Extra_Launch = Checkbutton(self.Prompt, text="Launch Extras", variable=self.extras)
Extra_Launch.grid(column=1, row=1, padx=padx_spacing)
Load_Profile = ttk.Button(self.Prompt, text='Load Profile', width=button_width, command=self.load_profile)
Load_Profile.grid(column=1, row=2, padx=padx_spacing)
New_Profile = ttk.Button(self.Prompt, text='New Profile', width=button_width, command=self.new_profile)
New_Profile.grid(column=1, row=3, padx=padx_spacing)
Open_Fold = ttk.Button(self.Prompt, text='Open Folder', width=button_width, command=self.open_profile_location)
Open_Fold.grid(column=1, row=4, padx=padx_spacing)
self.Prompt.mainloop()
class Tracker:
def __init__(self, profile_name, extras):
# Logging setup
format = '%(asctime)-15s %(message)s'
lg.basicConfig(filename='Spell_Tracker.log', level=lg.INFO, format=format, datefmt='%m/%d/%Y %I:%M:%S %p')
self.spell_log = lg.getLogger()
# Profile system
self.current_profile = f'Profiles/{profile_name}'
with open(self.current_profile) as json_file:
self.data = json.load(json_file)
self.launch_extras = extras
self.character_name = self.data['settings']['character_name']
self.character_class = self.data['settings']['character_class']
self.spells_known = self.data['settings']['spell_level_range']
if self.spells_known > 9:
self.spells_known = 9
self.folder = self.data['settings']['folder']
self.debug = 1
# defaul label text
self.spells_left_info = 'spells left of'
self.no_spells_left_info = '0 spells left of'
self.spell_like_left_info = 'uses left of'
self.no_spell_like_left_info = '0 uses left of'
def run_excel(self):
'''
Opens the excel doc from the config json.
'''
excel_path = ''
for item in os.scandir(self.folder):
if item.name.endswith('.xlsx'):
excel_path = item.path
cwd = os.getcwd()
head_tail = os.path.split(excel_path)
os.chdir(head_tail[0])
os.system(f'start excel.exe "{head_tail[1]}"')
os.chdir(cwd)
def run_ahk(self):
'''
Runs the AHK script from the config json.
'''
pathfinder_dir = os.path.split(self.folder)
print(pathfinder_dir[0])
for item in os.scandir(pathfinder_dir[0]):
print(item.name)
if item.name.endswith('.ahk'):
os.system(f'start "C:/Program Files/AutoHotkey/AutoHotkey.exe" "{item.path}"')
def extra_startup(self):
'''
runs some extra programs and files that are also used during pathfinder.
'''
print(self.launch_extras)
if self.launch_extras.get() == 1 and os.path.exists(self.folder):
self.run_ahk()
self.run_excel()
def write_to_json(self):
'''
Writes updated json data into profile.
'''
json_object = json.dumps(self.data, indent = 4) # Serializing json
with open(self.current_profile, "w") as outfile: # Writing to sample.json
outfile.write(json_object)
def spell_button_used(self, spell_level):
'''
Decreases entered spell level uses by 1 and updates labels and buttons for the level.
Arguments:
spell_level -- spell level that is being cast
'''
spells_left = self.data['spells'][f'level_{spell_level}']['spells_left']
spells_per_day = self.data['spells'][f'level_{spell_level}']['spells_per_day']
if spells_left > 1:
self.data['spells'][f'level_{spell_level}']['spells_left'] = spells_left - 1
text = f"Level {spell_level} - {spells_left-1} {self.spells_left_info} {spells_per_day}"
self.spell_info_list[spell_level-1].config(text=text)
self.write_to_json()
else:
self.data['spells'][f'level_{spell_level}']['spells_left'] = 0
text = f'Level {spell_level} - {self.no_spells_left_info} {spells_per_day}'
self.spell_info_list[spell_level-1].config(text=text)
self.spell_button_list[spell_level-1].config(state='disabled')
self.write_to_json()
lg.info(f'Level {spell_level} spell used.')
def spell_like_button_used(self, index, spell_like):
'''
Decreases entered spell-like ability uses by 1 and updates labels and buttons for the ability.
Arguments:
index -- index number for spell like abilities for updating the labels and buttons
spell_like -- ability that was used
'''
uses_left = int(self.data['spell_like'][spell_like]['uses_left'])
uses_per_day = int(self.data['spell_like'][spell_like]['per_day'])
if uses_left > 1:
self.data['spell_like'][spell_like]['uses_left'] = uses_left - 1
text = f"{spell_like} - {uses_left-1} {self.spell_like_left_info} {uses_per_day}"
self.spell_like_info_list[index].config(text=text)
self.write_to_json()
else:
self.data['spell_like'][spell_like]['uses_left'] = 0
text = f"{spell_like} - {self.no_spell_like_left_info} {uses_per_day}"
self.spell_like_info_list[index].config(text=text)
self.spell_like_button_list[index].config(state='disabled')
self.write_to_json()
lg.info(f'{spell_like} used.')
def reset_slots(self):
'''
Resers slots for all known spell levels to the current spells per day for each level.
'''
lg.info(f'Spell slots reset for {self.current_profile}.')
# Spells
for spell_level in range(1, self.spells_known+1):
spells_left = self.data['spells'][f'level_{spell_level}']['spells_left']
spells_per_day = self.data['spells'][f'level_{spell_level}']['spells_per_day']
lg.info(f'Previous Spells for Level {spell_level} - {spells_left}.')
self.data['spells'][f'level_{spell_level}']['spells_left'] = spells_per_day
text = f"Level {spell_level} - {spells_per_day} {self.spells_left_info} {spells_per_day}"
self.spell_info_list[spell_level-1].config(text=text)
self.spell_button_list[spell_level-1].config(state='normal')
index=0
# Spell Like Abilities
for spell_like in self.data['spell_like']:
uses_per_day = self.data['spell_like'][spell_like]['per_day']
self.data['spell_like'][spell_like]['uses_left'] = uses_per_day
text = f"{spell_like} - {uses_per_day} {self.spell_like_left_info} {uses_per_day}"
self.spell_like_info_list[index].config(text=text)
self.spell_like_button_list[index].config(state='normal')
index += 1
self.write_to_json()
lg.info('Long Rest Completed')
def create_window(self):
'''
Creates Tkinter interface.
'''
self.extra_startup()
SpellSlots = Tk()
SpellSlots.title("Spell Slot Counter")
window_width = 370
window_height = 500
width = int((SpellSlots.winfo_screenwidth()-window_width)/2)
height = int((SpellSlots.winfo_screenheight()-window_height)/2)
SpellSlots.geometry(f'+{width}+{height}')
SpellSlots.iconbitmap(SpellSlots, 'Fireball Icon.ico')
SpellSlots.resizable(width=False, height=False)
SpellSlots.focus_force()
# Bindings for interface.
SpellSlots.bind(f"<Escape>", sys.exit)
text = f'{self.character_name}\n{self.character_class}'
Title = Label(SpellSlots, text=text, font=('Arial Bold', 18))
Title.grid(column=0, row=0, pady=5)
SpellFrame = LabelFrame(SpellSlots, text='Spells', font=('Arial Bold', 14))
SpellFrame.grid(column=0, row=1, padx=40, pady=5)
# Spell Level Info and Buttons
self.spell_info_list = []
self.spell_button_list = []
for _ in range(1, self.data['settings']['spell_level_range']+1):
self.spell_info_list.append(Label(SpellFrame))
self.spell_button_list.append(ttk.Button(SpellFrame))
# Spell Level Tkinter Labels
for num in range(1, self.spells_known+1):
self.data['spells'][f'level_{num}']['spells_per_day']
spells_left = self.data['spells'][f'level_{num}']['spells_left']
spells_per_day = self.data['spells'][f'level_{num}']['spells_per_day']
if spells_left > 0:
text = f"Level {num} - {spells_left} {self.spells_left_info} {spells_per_day}"
self.spell_info_list[num-1].config(text=text, font=('Arial', 13))
else:
text = f'Level {num} - {self.no_spells_left_info} {spells_per_day}'
self.spell_info_list[num-1].config(text=text, font=('Arial', 13))
self.spell_info_list[num-1].grid(column=0, row=num+1, padx=(20, 10))
# Spell Level Tkinter Buttons for using Spells
for num in range(1, self.spells_known+1):
spells_left = self.data['spells'][f'level_{num}']['spells_left']
text = f"Use Level {num} Spell"
button_width = 18
if spells_left > 0:
self.spell_button_list[num-1].config(text=text, width=button_width,
command=partial(self.spell_button_used, num))
self.spell_button_list[num-1].grid(column=1, row=num+1, pady=5, padx=(4, 20))
else:
self.spell_button_list[num-1].config(text=text, width=button_width, state='disabled',
command=partial(self.spell_button_used, num))
self.spell_button_list[num-1].grid(column=1, row=num+1, pady=5, padx=(4, 20))
SpellLikeFrame = LabelFrame(SpellSlots, text='Spell Like Abilities', font=('Arial Bold', 14))
SpellLikeFrame.grid(column=0, row=2, padx=10, pady=10)
# Spell Level Info and Buttons
spell_likes = self.data['spell_like']
self.spell_like_info_list = []
self.spell_like_button_list = []
for _ in spell_likes:
self.spell_like_info_list.append(Label(SpellLikeFrame))
self.spell_like_button_list.append(ttk.Button(SpellLikeFrame))
# Spell-like Tkinter Labels
index=0
for spell_like in spell_likes:
uses_left = int(self.data['spell_like'][spell_like]['uses_left'])
uses_per_day = int(self.data['spell_like'][spell_like]['per_day'])
if uses_left > 0:
text = f"{spell_like} - {uses_left} {self.spell_like_left_info} {uses_per_day}"
self.spell_like_info_list[index].config(text=text, font=('Arial', 13))
else:
text = f'{spell_like} - {self.no_spell_like_left_info} {uses_per_day}'
self.spell_like_info_list[index].config(text=text, font=('Arial', 13))
self.spell_like_info_list[index].grid(column=0, row=index, padx=(20, 10))
index += 1
# Spell-like Tkinter Buttons for using Spell like abilties
index=0
for spell_like in spell_likes:
uses_left = int(self.data['spell_like'][spell_like]['uses_left'])
uses_per_day = int(self.data['spell_like'][spell_like]['per_day'])
text = f"Use Ability"
button_width = 12
if uses_left > 0:
self.spell_like_button_list[index].config(text=text, width=button_width,
command=partial(self.spell_like_button_used, index, spell_like))
else:
self.spell_like_button_list[index].config(text=text, width=button_width, state='disabled',
command=partial(self.spell_like_button_used, index, spell_like))
self.spell_like_button_list[index].grid(column=1, row=index, pady=5, padx=(4, 20))
index += 1
LongRest = ttk.Button(SpellSlots, text="Long Rest - Reset Slots", width=24, command=self.reset_slots)
LongRest.grid(columnspan=2, row=3, pady=(5, 15))
SpellSlots.mainloop()
if __name__ == '__main__':
Selection = Startup()
Selection.profile_select()