-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBingoDisplay.py
357 lines (296 loc) · 12 KB
/
BingoDisplay.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
import argparse
import time
import sys
import json
import os.path
import urllib.request
import urllib.parse
import re
from tkinter import filedialog as fd
from tkinter import font
from tkinter import messagebox
from tkinter import *
from pathlib import Path
BUTTON_BORDER_WIDTH = 4
BUTTON_BORDER_WIDTH_TOTAL=15*BUTTON_BORDER_WIDTH
MAGIC_GREEN="#1e641e"
BRIGHT_GREEN="#00CC00"
BINGO_VARIABLE_CONFIG_NAME="bingoexport"
BINGO_MOD_LINE_DETECT="PlayerDataItem"
NEWLY_COMPLETED_DISPLAY_TIME=2.8 # we only redraw every second, so this will keep it closer to about 3 seconds
WINDOW_TITLE="Deus Ex Randomizer Bingo Board"
if os.name == 'nt': # Windows works correctly (for once)
BORDER_WIDTH_SCALE=1
BORDER_HEIGHT_SCALE=1
else: # Linux needs fixing
BORDER_WIDTH_SCALE=2.85 # idk why these numbers
BORDER_HEIGHT_SCALE=1.7
JSON_DEST_FILENAME="pushjson.txt"
class Bingo:
def __init__(self,targetFile):
self.targetFile = targetFile
self.board = [[None]*5 for i in range(5)]
self.tkBoard = [[None]*5 for i in range(5)]
self.tkBoardText = [[None]*5 for i in range(5)]
self.width=500
self.height=500
self.selectedMod=""
self.prevLines=None
self.bingoLineMatch = re.compile(
r'bingoexport\[(?P<key>\d+)\]=\(Event="(?P<event>.*)",Desc="(?P<desc>.*)",Progress=(?P<progress>\d+),Max=(?P<max>\d+),Active=(?P<active>-?\d+)\)',
re.IGNORECASE
)
self.initDrawnBoard()
def closeWindow(self):
self.win.destroy()
self.win=None
def isWindowOpen(self):
return self.win!=None
def getFontSizeByWindowSize(self):
width = min(self.width, self.height)
return int(width / 37)
def resize(self,event):
if event.widget == self.win:
self.width=event.width - BUTTON_BORDER_WIDTH_TOTAL * BORDER_WIDTH_SCALE
self.height=event.height - BUTTON_BORDER_WIDTH_TOTAL * BORDER_HEIGHT_SCALE
self.font = font.Font(size=self.getFontSizeByWindowSize())
for x in range(5):
for y in range(5):
self.tkBoard[x][y].config(
width=self.width/5,height=self.height/5,wraplength=self.width/5,font=self.font
)
def initDrawnBoard(self):
self.win = Tk()
self.win.protocol("WM_DELETE_WINDOW",self.closeWindow)
self.win.bind("<Configure>",self.resize)
self.win.title(WINDOW_TITLE)
self.win.geometry(str(self.width+BUTTON_BORDER_WIDTH_TOTAL)+"x"+str(self.height+BUTTON_BORDER_WIDTH_TOTAL))
self.win.config(bg="black")
self.pixel = PhotoImage() #Needed to allow the button width/height to be configured in pixels
self.font = font.Font(size=self.getFontSizeByWindowSize())
for x in range(5):
for y in range(5):
self.tkBoardText[x][y]=StringVar()
self.tkBoardText[x][y].set("("+str(x)+","+str(y)+")")
self.tkBoard[x][y]=Button(self.win,
textvariable=self.tkBoardText[x][y],
image=self.pixel,compound="c",
width=self.width/5, height=self.height/5,
wraplength=self.width/5, font=self.font,fg='white',
disabledforeground="white", bd=BUTTON_BORDER_WIDTH
)
self.tkBoard[x][y]["state"]='disabled'
self.tkBoard[x][y].finished_time=None
self.tkBoard[x][y].grid(column=x,row=y)
def drawBoard(self):
self.win.title(WINDOW_TITLE+" "+translateMod(self.selectedMod))
for x in range(5):
for y in range(5):
self.drawTile(self.tkBoard[x][y], self.tkBoardText[x][y], self.board[x][y])
def drawTile(self, tkTile, tkText, boardEntry):
if boardEntry is None or tkTile is None:
return
desc = boardEntry["desc"]
if boardEntry["max"]>1:
desc=desc+"\n("+str(boardEntry["progress"])+"/"+str(boardEntry["max"])+")"
tkText.set(desc)
isActive = boardEntry.get('active', 1)
if boardEntry["progress"]>=boardEntry["max"] and boardEntry["max"]>0:
if tkTile.finished_time is None:
tkTile.finished_time=time.time() + NEWLY_COMPLETED_DISPLAY_TIME
tkTile.config(bg=BRIGHT_GREEN)
elif(tkTile.finished_time>time.time()):
tkTile.config(bg=BRIGHT_GREEN)
else:
tkTile.config(bg=MAGIC_GREEN)
elif isActive == 2:# 2 is for active
tkTile.config(bg="#505050")
elif isActive == 1:# 1 is for maybe
tkTile.config(bg="#303030")
elif isActive == -1:# -1 is for impossible
tkTile.config(bg="#300000")
else:
tkTile.config(bg="#000000", fg="#c8c8c8") # text color adjustment isn't working
def printBoard(self):
for x in range(5):
for y in range(5):
print(str(x)+","+str(y)+": "+str(self.board[x][y]))
def bingoNumberToCoord(self,bingoNumber):
x = bingoNumber//5
y = bingoNumber%5
return (x,y)
def parseBingoLine(self,bingoLine):
bingoMatches=self.bingoLineMatch.match(bingoLine)
if (bingoMatches==None):
return
bingoNumber=int(bingoMatches.group('key'))
bingoCoord = self.bingoNumberToCoord(bingoNumber)
bingoItem = dict()
bingoItem["event"]=bingoMatches.group('event')
bingoItem["desc"]=bingoMatches.group('desc')
bingoItem["progress"]=int(bingoMatches.group('progress'))
bingoItem["max"]=int(bingoMatches.group('max'))
bingoItem["active"]=int(bingoMatches.group('active'))
self.board[bingoCoord[0]][bingoCoord[1]] = bingoItem
def readBingoFile(self):
allLines = dict()
try:
try:
with open(self.targetFile) as file:
bingoFile = file.readlines()
except Exception as e:
print("Couldn't read file, ignoring - "+str(e));
return False
for line in bingoFile:
if BINGO_MOD_LINE_DETECT in line:
currentMod=line.strip()
allLines[currentMod]=[]
elif line.startswith(BINGO_VARIABLE_CONFIG_NAME):
bingoLine = line.strip()
allLines[currentMod].append(bingoLine)
except Exception as e:
print(e)
pass
mods=list(allLines.keys())
if self.selectedMod not in mods:
self.selectedMod=""
if not self.selectedMod:
if len(mods)>1:
for mod in mods:
if messagebox.askyesno("Select your mod","Do you want to use "+translateMod(mod)):
self.selectedMod=mod
break
elif len(mods)==1:
self.selectedMod=mods[0]
else:
print("No mods")
sys.exit(0)
if self.selectedMod:
for line in allLines[self.selectedMod]:
self.parseBingoLine(line)
changed = (allLines!=self.prevLines)
if changed:
self.prevLines=allLines
return changed
def generateBingoStateJson(self):
board = []
for y in range(5):
for x in range(5):
square = dict()
square["x"]=x
square["y"]=y
if self.board[x][y]==None:
return {}
square["name"]=self.board[x][y]["desc"]
if self.board[x][y]["max"]>1:
square["name"]+="\n"+str(self.board[x][y]["progress"])+"/"+str(self.board[x][y]["max"])
square["completed"]=self.board[x][y]["progress"] >= self.board[x][y]["max"]
square["possible"]=self.board[x][y]["active"]!=-1
#print(square)
board.append(square)
#return json.dumps(board,indent=4)
return {"bingo":json.dumps({"bingo":board},indent=4)}
def sendBingoState(self):
if not os.path.isfile(JSON_DEST_FILENAME):
return
f = open(JSON_DEST_FILENAME,'r')
desturl=f.readline()
f.close()
if (desturl==""):
print("Make sure to specify where you want to push your json!")
return
bingoState = self.generateBingoStateJson()
#print(bingoState)
try:
r = urllib.request.urlopen(desturl,data=urllib.parse.urlencode(bingoState).encode('utf-8'))
#print(r.status)
#print(r.read().decode('utf-8'))
except Exception as e:
print("Couldn't push JSON to "+desturl+" - "+str(e))
def saveLastUsedBingoFile(f):
p = Path(f)
# TODO: save last used file and reuse it for getDefaultPath()
def getDefaultPath():
try:
checks = [
Path.home() / "Documents" / "Deus Ex" / "System",
Path("C:\\") / "Program Files (x86)" / "Steam" / "steamapps" / "common" / "Deus Ex" / "Revision" / "System",
Path("D:\\") / "Program Files (x86)" / "Steam" / "steamapps" / "common" / "Deus Ex" / "Revision" / "System",
Path("C:\\") / "Program Files (x86)" / "Steam" / "steamapps" / "common" / "Deus Ex" / "System",
Path("D:\\") / "Program Files (x86)" / "Steam" / "steamapps" / "common" / "Deus Ex" / "System",
# Linux
Path.home() /'snap'/'steam'/'common'/'.local'/'share'/'Steam'/'steamapps'/'common'/'Deus Ex'/'System',
Path.home() /'.steam'/'steam'/'SteamApps'/'common'/'Deus Ex'/'System',
Path.home() /'.local'/'share'/'Steam'/'steamapps'/'compatdata'/'6910'/'pfx'/'drive_c'/'users'/'steamuser'/'Documents'/'Deus Ex'/'System',
Path.home() /'.local'/'share'/'Steam'/'steamapps'/'common'/'Deus Ex'/'System',
]
modified_times = {}
for p in checks:
try:
f:Path = p / "DXRBingo.ini"
if f.exists():
modified_times[p] = os.path.getmtime(f)
except Exception as e:
print(e)
sorted_paths = sorted(modified_times.keys(), key=lambda f: modified_times[f])
if len(sorted_paths) > 0:
return sorted_paths[-1]
p:Path
for p in checks:
try:
if p.is_dir():
return p
except Exception as e:
print(e)
except Exception as e:
print(e)
return None
def findBingoFile():
root = Tk()
root.withdraw()
filetype = (("DXRBingo File","DXRBingo.ini"),("all files","*.*"))
initdir = getDefaultPath()
target = fd.askopenfilename(title="Locate your DXRBingo File",filetypes=filetype, initialdir=initdir)
root.destroy()
return target
def translateMod(modName):
if "DeusEx" in modName:
return "[Deus Ex Randomizer - Vanilla]"
elif "GMDXRandomizer" in modName:
return "[GMDX Randomizer]"
elif "RevRandomizer" in modName:
return "[Revision Randomizer]"
elif "VMDRandomizer" in modName:
return "[Vanilla? Madder. Randomizer]"
elif "HXRandomizer" in modName:
return "[HX Randomizer]"
else:
return modName
#####################################################################################
parser = argparse.ArgumentParser(description='DXRando Bingo Viewer')
parser.add_argument('--version', action="store_true", help='Output version')
args = parser.parse_args()
def GetVersion():
return 'v2.7'
if args.version:
print('DXRando Bingo Viewer version:', GetVersion(), file=sys.stderr)
print('Python version:', sys.version_info, file=sys.stderr)
sys.exit(0)
targetFile = findBingoFile()
if targetFile=='':
sys.exit(0)
b = Bingo(targetFile)
saveLastUsedBingoFile(targetFile)
lastFileUpdate=0
while True:
if (time.time()>(lastFileUpdate+1)):
changed = b.readBingoFile()
#b.printBoard()
lastFileUpdate=time.time()
b.drawBoard()
if (changed):
b.sendBingoState()
b.win.update()
if not b.isWindowOpen():
sys.exit(0)
time.sleep(0.01)