This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbartender.py
347 lines (296 loc) · 13.9 KB
/
bartender.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
0] Init UI
1] Waits until the game starts
2] Loads overlay settings
3] Loads overlay
"""
import sys
from PyQt5 import QtWidgets, QtCore, Qt, QtGui
from aoc_game import Game
from aoc_lobby import Lobby
from bartender_save_load import *
from config import *
from interface_offscreen_units import InterfaceOffscreenUnits
from pymemory import pymemory as pm
from ui_overlay import Overlay
class Bartender(QtWidgets.QMainWindow):
BARTENDER_FRAMES_PER_SECOND = 30 # FPS of main window
UPDATE_WINDOW_MS = int(1 / BARTENDER_FRAMES_PER_SECOND)
CONSOLE_PRINT_INFO = True
WINDOW_PRINT_INFO = True
SPAN = 10
WINDOW_TITLE = "Bartender"
WINDOW_WIDTH = 764
WINDOW_HEIGHT = 600
WIDE = WINDOW_WIDTH - SPAN * 2
WIDE_FOURTH = (WINDOW_WIDTH - SPAN * (1 + 4)) / 4
CELL_HEIGHT = 13
GEOMETRY_LABEL_INFO = Qt.QRect(SPAN, SPAN, WIDE, SPAN * 2) # X, Y, WIDTH, HEIGHT
GEOMETRY_CENTER = Qt.QRect(SPAN, SPAN, WIDE, WINDOW_HEIGHT - SPAN * 2) # X, Y, WIDTH, HEIGHT
GEOMETRY_0_0 = Qt.QRect(SPAN, GEOMETRY_LABEL_INFO.bottom() + 1 + SPAN, WIDE_FOURTH, CELL_HEIGHT)
GEOMETRY_0_1 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 1, GEOMETRY_0_0.y(), WIDE_FOURTH, CELL_HEIGHT)
GEOMETRY_0_2 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 2, GEOMETRY_0_0.y(), WIDE_FOURTH, CELL_HEIGHT)
GEOMETRY_0_3 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 3, GEOMETRY_0_0.y() - 4, WIDE_FOURTH, CELL_HEIGHT + 8)
GEOMETRY_00_3 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 3, GEOMETRY_0_0.y() - SPAN - CELL_HEIGHT, WIDE_FOURTH,
CELL_HEIGHT)
GEOMETRY_1_0 = Qt.QRect(SPAN, GEOMETRY_0_0.bottom() + 1 + SPAN, WIDE_FOURTH, CELL_HEIGHT)
GEOMETRY_1_1 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 1, GEOMETRY_1_0.y(), WIDE_FOURTH, CELL_HEIGHT)
GEOMETRY_1_2 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 2, GEOMETRY_1_0.y(), WIDE_FOURTH, CELL_HEIGHT)
GEOMETRY_1_3 = Qt.QRect(SPAN + (SPAN + WIDE_FOURTH) * 3, GEOMETRY_1_0.y() - 4, WIDE_FOURTH, CELL_HEIGHT + 8)
GEOMETRY_TAB_BAR_SETTINGS_HEIGHT = 285 + 26 + 100 + 50
GEOMETRY_TAB_BAR_SETTINGS = Qt.QRect(SPAN, # X
GEOMETRY_1_0.bottom() + 1 + SPAN, # Y
WIDE,
GEOMETRY_TAB_BAR_SETTINGS_HEIGHT)
# Function for printing FSM position
def print_info(self, string):
if string == self.statusbar.lastmsg:
return
if Bartender.CONSOLE_PRINT_INFO:
print(f"<FSM> {string}")
if Bartender.WINDOW_PRINT_INFO:
self.statusbar.showMessage(f"Status: {string}")
self.statusbar.lastmsg = string
def __init__(self, screen_width, screen_height, app):
""" Constuctor, inits a new window with informations about bartender
`screen_width` Screen resolution
`screen_height` Screen resolution
"""
super(Bartender, self).__init__()
### WINDOW SETTINGS
self.setWindowTitle(Bartender.WINDOW_TITLE)
# Set the position of Window to the center.
self.setGeometry((screen_width - Bartender.WINDOW_WIDTH) // 2, # X
(screen_height - Bartender.WINDOW_HEIGHT) // 2, # Y
Bartender.WINDOW_WIDTH, # Window width
Bartender.WINDOW_HEIGHT) # WIndow height
# INFO LABEL
self.w_bartender_title = QtWidgets.QLabel(self)
self.w_bartender_title.setGeometry(Bartender.GEOMETRY_LABEL_INFO)
self.w_bartender_title.setAlignment(QtCore.Qt.AlignCenter)
self.w_bartender_title.setText(Bartender.WINDOW_TITLE)
font = QtGui.QFont(self.w_bartender_title.font().family(), 12)
font.setBold(True)
self.w_bartender_title.setFont(font)
# Status bar
self.statusbar = QtWidgets.QStatusBar()
self.statusbar.lastmsg = ""
self.setStatusBar(self.statusbar)
# Info
self.info_label = QtWidgets.QLabel("Start the game already!", self)
self.info_label.setGeometry(Bartender.GEOMETRY_CENTER)
self.info_label.setAlignment(QtCore.Qt.AlignCenter)
font = QtGui.QFont(self.w_bartender_title.font().family(), 10)
self.info_label.setFont(font)
self.show()
# consts
### WIDGETS SETINGS
# Checkboxes
self.w_checkbox_completed_researches = QtWidgets.QCheckBox("Completed Researches", self)
self.w_checkbox_completed_researches.setGeometry(Bartender.GEOMETRY_1_0)
self.w_checkbox_completed_researches.stateChanged.connect(self.researches_policy)
self.w_checkbox_completed_researches.setHidden(True)
self.w_checkbox_completed_researches_movable = QtWidgets.QCheckBox("Movable", self)
self.w_checkbox_completed_researches_movable.setGeometry(Bartender.GEOMETRY_1_1)
self.w_checkbox_completed_researches_movable.stateChanged.connect(self.researches_policy)
self.w_checkbox_completed_researches_movable.setHidden(True)
self.w_checkbox_completed_researches_times = QtWidgets.QCheckBox("Show Research Times", self)
self.w_checkbox_completed_researches_times.setGeometry(Bartender.GEOMETRY_1_2)
self.w_checkbox_completed_researches_times.stateChanged.connect(self.researches_policy)
self.w_checkbox_completed_researches_times.setHidden(True)
self.w_checkbox_researches = QtWidgets.QCheckBox("Researching Technologies", self)
self.w_checkbox_researches.setGeometry(Bartender.GEOMETRY_0_0)
self.w_checkbox_researches.stateChanged.connect(self.researches_policy)
self.w_checkbox_researches.setHidden(True)
self.w_checkbox_researches_movable = QtWidgets.QCheckBox("Movable", self)
self.w_checkbox_researches_movable.setGeometry(Bartender.GEOMETRY_0_1)
self.w_checkbox_researches_movable.stateChanged.connect(self.researches_policy)
self.w_checkbox_researches_movable.setHidden(True)
self.w_checkbox_researches_show_bars = QtWidgets.QCheckBox("Show Bars", self)
self.w_checkbox_researches_show_bars.setGeometry(Bartender.GEOMETRY_0_2)
self.w_checkbox_researches_show_bars.stateChanged.connect(self.researches_policy)
self.w_checkbox_researches_show_bars.setHidden(True)
self.w_checkbox_editall = QtWidgets.QCheckBox("All Bars Movable", self)
self.w_checkbox_editall.setGeometry(Bartender.GEOMETRY_00_3)
self.w_checkbox_editall.stateChanged.connect(self.moveable)
self.w_checkbox_editall.setHidden(True)
# Buttons
self.w_button_new_header = QtWidgets.QPushButton("New Info Panel", self)
self.w_button_new_header.setGeometry(Bartender.GEOMETRY_0_3)
self.w_button_new_header.clicked.connect(self.add_new_header)
self.w_button_new_header.setHidden(True)
self.w_button_new_bar = QtWidgets.QPushButton("New Bar", self)
self.w_button_new_bar.setGeometry(Bartender.GEOMETRY_1_3)
self.w_button_new_bar.clicked.connect(self.add_new_bar)
self.w_button_new_bar.setHidden(True)
# Tab widget
self.w_tabs_settings = QtWidgets.QTabWidget(self)
self.w_tabs_settings.setGeometry(Bartender.GEOMETRY_TAB_BAR_SETTINGS)
self.w_tabs_settings.addTab(InterfaceOffscreenUnits("Offscreen Units", self), f"Offscreen Units")
self.w_tabs_settings.setHidden(True)
# hide it all
self.app = app
# Consts
self.overlay = None
self.game = None
# self.lobby = Lobby()
self.state = -1
self.process_timer = None
self.timer = None
def load_settings(self):
self.info_label.setHidden(True)
# Checkboxes
self.w_checkbox_completed_researches.setHidden(False)
self.w_checkbox_completed_researches_movable.setHidden(False)
self.w_checkbox_completed_researches_times.setHidden(False)
self.w_checkbox_researches.setHidden(False)
self.w_checkbox_researches_movable.setHidden(False)
self.w_checkbox_researches_show_bars.setHidden(False)
self.w_checkbox_editall.setHidden(False)
# Buttons
self.w_button_new_header.setHidden(False)
self.w_button_new_bar.setHidden(False)
# Tab widget
self.w_tabs_settings.setHidden(False)
# Load Overlay and UI from settings.txt
load(self)
self.start_overlay()
self.update_overlay_widgets()
self.researches_policy()
load_geometry(self)
self.show()
def researches_policy(self):
self.w_checkbox_completed_researches_movable.setEnabled(self.w_checkbox_completed_researches.isChecked())
self.w_checkbox_completed_researches_times.setEnabled(self.w_checkbox_completed_researches.isChecked())
self.w_checkbox_researches_movable.setEnabled(self.w_checkbox_researches.isChecked())
self.w_checkbox_researches_show_bars.setEnabled(self.w_checkbox_researches.isChecked())
if self.overlay is not None:
self.overlay.research_bars.setHidden(not self.w_checkbox_researches.isChecked())
self.overlay.research_bars.set_movable(self.w_checkbox_researches_movable.isChecked())
self.overlay.research_bars.set_magical_bar(self.w_checkbox_researches_show_bars.isChecked())
self.overlay.research_list.setHidden(not self.w_checkbox_completed_researches.isChecked())
self.overlay.research_list.set_movable(self.w_checkbox_completed_researches_movable.isChecked())
self.overlay.research_list.set_magical_timer(self.w_checkbox_completed_researches_times.isChecked())
def setHiddenList(self, widgets, boolean):
for widget in widgets:
widget.setHidden(boolean)
def __load_process_loop__(self):
self.print_info(f"Loading the process {AOK_PROCESS_NAME}. Be sure it is running.")
try:
pm.load_process(AOK_PROCESS_NAME)
except ProcessLookupError:
return
self.state = 0
self.process_timer.stop()
self.app.exit()
def load_process(self):
""" Loading the process timer """
self.process_timer = QtCore.QTimer()
self.process_timer.timeout.connect(self.__load_process_loop__)
self.process_timer.start(Bartender.UPDATE_WINDOW_MS)
self.app.exec_()
def load_game(self):
try:
self.game = Game()
return True
except:
return False
def start_overlay(self):
# Starting overlay UI
self.overlay = Overlay(self)
self.researches_policy()
self.overlay.show()
def closeEvent(self, event):
# Closing the window.
self.print_info("Closing the overlay")
if SAVE_IF_QUITING_THE_GAME and self.overlay is not None:
save(self)
if self.process_timer is not None:
self.process_timer.stop()
if self.timer is not None:
self.timer.stop()
if self.overlay is not None:
self.overlay.setHidden(False)
self.overlay.close()
def load_lobby(self):
# Loads lobby data
try:
self.lobby.update()
except:
self.lobby = Lobby()
def finite_state_machine(self):
# State machine.
if self.state == -1:
self.print_info("Waiting until the game is started.")
# Loading the game
elif self.state == 0:
if self.load_game() and self.game is not None:
self.state = 2
self.lobby = None
self.print_info("Game loaded. Starting the first iteration.")
# Getting first iteration
elif self.state == 2:
self.game.update()
if self.game.running:
self.state = 3
self.print_info("Starting overlay.")
# Starting overlay
elif self.state == 3:
self.overlay.set_game(self.game)
self.update_overlay_widgets()
self.state = 4
self.print_info("Waiting until the game is quitted.")
# Loop
elif self.state == 4:
if not self.game.running:
# self.overlay.set_game(None)
save(self)
self.overlay.deleteLater()
del self.overlay
self.start_overlay()
self.update_overlay_widgets()
self.researches_policy()
load_geometry(self)
self.show()
self.state = 0
def run(self):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.finite_state_machine)
self.timer.start(Bartender.UPDATE_WINDOW_MS)
def moveable(self):
boolean = self.w_checkbox_editall.isChecked()
if self.overlay is not None:
self.overlay.set_movable_widgets(boolean)
def add_new_header(self):
name = f"New Panel"
widget = InterfaceInfoPanel(name, self)
self.w_tabs_settings.addTab(widget, name)
self.update_overlay_widgets()
self.w_tabs_settings.setCurrentIndex(self.w_tabs_settings.count() - 1)
def add_new_bar(self):
name = f"New Bar"
widget = InterfaceBar(name, self)
self.w_tabs_settings.addTab(widget, name)
self.update_overlay_widgets()
self.w_tabs_settings.setCurrentIndex(self.w_tabs_settings.count() - 1)
def update_overlay_widgets(self):
if self.overlay is None:
return
for idx in range(self.w_tabs_settings.count()):
widget = self.w_tabs_settings.widget(idx)
self.overlay.create_overlay_widget(widget.BIND_TYPE, widget)
if __name__ == '__main__' or True:
# Start bartender window.
app = QtWidgets.QApplication(sys.argv)
resolution = app.desktop().screenGeometry()
bartender = Bartender(resolution.width(), resolution.height(), app)
bartender.load_process()
if bartender.state == -1:
sys.exit()
bartender.run()
bartender.load_settings()
result = app.exec_()
sys.exit(result)
# EOF: bartender.py