-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (144 loc) · 5.68 KB
/
main.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
# Standard
import sys
import threading as th
import traceback as tb
import shutil
# External
import nikocraft as nc
# Local
from constants import *
from configs import MainConfig, GameConfig, UserConfig, CacheConfig
from window.window import Window
from user.user_manager import UserManager
from game.game_manager import GameManager
from listener import Listener
import email_utils
class Main(nc.App):
def __init__(self, args):
super(Main, self).__init__(args,
name="Msp Arcade",
author="Makerspace",
version="1.3.0",
short_description="A Makerspace project for an arcade machine in the school",
description="This is the main software of the arcade machine, which controls " +
"all games and provides a menu. The gaming machine is a project of the " +
"Makerspace for the Bodensee Gymnasium Lindau in Germany.",
details="Database: Lernplattform Mebis Bayern (Moodle)\n\n" +
"TEAM\n" +
"Management: Dr. Andre Scherl\n"
"Software: Nikogenia (Nikolas Beyer)\n" +
"Hardware: Valis World (Valentin Sutter)\n" +
"Games: Makerspace (see menu)",
log_path=PATH_LOG,
log_thread=True)
self.running = True
# Create directories
for path in [PATH_CONFIG, PATH_CONFIG_BACKUP, PATH_GAME]:
if not nc.file.exists(path):
nc.file.make_dir(path, self.logger)
# Load configs
self.main_config: MainConfig = MainConfig(self.logger)
self.game_config: GameConfig = GameConfig(self.logger)
self.user_config: UserConfig = UserConfig(self.logger)
self.cache_config: CacheConfig = CacheConfig(self.logger)
self.logger.info("Backup configs ...")
for conf in (self.main_config, self.game_config, self.user_config, self.cache_config):
try:
self.logger.debug(f"Copy config '{conf.path}' to '{PATH_CONFIG_BACKUP}' ...")
shutil.copy(conf.path, PATH_CONFIG_BACKUP)
except OSError:
pass
self.logger.info("Load configs ...")
self.main_config.load()
self.game_config.load()
self.user_config.load()
self.cache_config.load()
self.main_config.save()
self.game_config.save()
self.user_config.save()
self.cache_config.save()
# Initialize window
self.window = Window(self)
# Initialize user manager
self.user_manager: UserManager = UserManager(self)
# Initialize game manager
self.game_manager: GameManager = GameManager(self)
# Initialize listener
self.listener: Listener = Listener(self)
def run(self):
# Start user manager
self.user_manager.start()
# Start game manager
self.game_manager.start()
# Start listener
self.listener.start()
try:
# Open window
self.window.open()
except Exception:
# Handle crash
self.handle_crash()
# Quit window
self.logger.info("Close window ...")
self.window.scene.quit()
self.window.scene.deactivate_event_hooks()
self.window.quit()
except KeyboardInterrupt:
# Log warning
self.logger.warning("Keyboard interrupted! Shutting down!")
# Quit all components
self.running = False
self.window.running = False
self.game_manager.running = False
self.user_manager.running = False
self.listener.running = False
# Quit window
self.logger.info("Close window ...")
self.window.scene.quit()
self.window.scene.deactivate_event_hooks()
self.window.quit()
def quit(self):
# Quit game manager
self.game_manager.running = False
self.game_manager.join()
# Quit user manager
self.user_manager.running = False
self.user_manager.join()
# Quit listener
self.listener.running = False
self.listener.join()
# Save configs
self.logger.info("Save configs ...")
self.cache_config.save()
def handle_crash(self):
# Set exit code
self.exit_code = 1
# Log error
self.logger.critical("\n" +
"----------------------------------------\n" +
" CRITICAL UNEXPECTED ERROR\n" +
" Exit\n" +
"----------------------------------------\n" +
tb.format_exc() +
"----------------------------------------")
# Quit all components
self.running = False
self.window.running = False
self.game_manager.running = False
self.user_manager.running = False
self.listener.running = False
# Send email
if self.main_config.email_active:
email_utils.send_error(self.main_config, tb.format_exc().strip("\n"))
def main():
# Set main thread name
th.main_thread().name = "Main"
# Initialize main
m = Main(sys.argv)
# Start main
exit_code = m.start()
# Exit
sys.exit(exit_code)
# Main
if __name__ == '__main__':
main()