Difficult-Rocket/Difficult_Rocket/main.py

99 lines
3.2 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2023-01-20 14:08:12 +08:00
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
2021-09-08 23:38:34 +08:00
# All rights reserved
# -------------------------------
2021-02-16 00:15:29 +08:00
"""
2020-12-05 21:24:54 +08:00
writen by shenjackyuanjie
2021-09-08 23:38:34 +08:00
mail: 3695888@qq.com
github: @shenjackyuanjie
gitee: @shenjackyuanjie
2021-02-16 00:15:29 +08:00
"""
2020-12-05 21:24:54 +08:00
2021-03-24 23:37:10 +08:00
import os
2021-02-16 00:08:11 +08:00
import sys
2021-01-25 12:22:55 +08:00
import time
import logging
import logging.config
import multiprocessing
2023-04-09 03:21:11 +08:00
from io import StringIO
2021-08-24 22:31:52 +08:00
if __name__ == '__main__': # been start will not run this
sys.path.append('/bin/libs')
sys.path.append('/bin')
2022-12-25 23:15:49 +08:00
from Difficult_Rocket import client, server, DR_option
2023-01-31 22:07:18 +08:00
2023-04-09 03:21:11 +08:00
from Difficult_Rocket.crash import write_info_to_cache
from Difficult_Rocket.utils import tools
from Difficult_Rocket.utils.translate import tr
2020-12-05 21:24:54 +08:00
2022-08-12 21:07:36 +08:00
2021-08-24 22:31:52 +08:00
class Game:
2020-12-05 21:24:54 +08:00
def __init__(self):
2021-01-25 17:01:54 +08:00
# basic config
2021-02-16 11:35:10 +08:00
self.on_python_v_info = sys.version_info
2021-02-26 19:05:24 +08:00
self.on_python_v = sys.version.split(' ')[0]
2021-02-15 23:52:56 +08:00
self.start_time = time.strftime('%Y-%m-%d %H-%M-%S', time.gmtime(time.time()))
2021-02-20 21:46:14 +08:00
# lang_config
2022-12-22 10:54:28 +08:00
self.language = tools.load_file('configs/main.toml', 'runtime')['language']
2022-08-12 21:07:36 +08:00
DR_option.language = self.language
# logging config
log_config = tools.load_file('configs/logger.toml')
file_name = log_config['handlers']['file']['filename']
del log_config['handlers']['file']['datefmt']
log_config['handlers']['file']['filename'] = f'logs/{file_name.format(self.start_time)}'
try:
logging.config.dictConfig(log_config)
self.logger = logging.getLogger('main')
2021-09-22 20:51:30 +08:00
except ValueError: # it should be no 'logs/' folder
os.mkdir('logs')
logging.config.dictConfig(log_config)
self.logger = logging.getLogger('main')
2023-02-03 20:31:44 +08:00
self.logger.info(tr().main.logger.mkdir())
2023-04-05 16:00:38 +08:00
self.logger.info(tr().language_set_to())
2023-02-03 20:31:44 +08:00
self.logger.info(tr().main.logger.created())
2021-02-16 11:35:10 +08:00
# version check
2023-04-09 03:21:11 +08:00
self.log_env()
2021-02-16 00:08:11 +08:00
self.python_version_check()
2021-08-13 12:25:29 +08:00
self.setup()
2023-04-09 03:21:11 +08:00
def log_env(self) -> None:
cache_steam = StringIO()
write_info_to_cache(cache_steam)
text = cache_steam.getvalue()
self.logger.info(text)
2021-08-13 12:25:29 +08:00
2021-08-24 22:31:52 +08:00
def setup(self) -> None:
self.client = client.Client(net_mode='local')
2022-12-25 23:15:49 +08:00
self.server = server.Server(net_mode='local')
2021-02-06 16:41:54 +08:00
2021-08-24 22:31:52 +08:00
def python_version_check(self) -> None: # best 3.8+ and write at 3.8.10
2023-02-03 20:31:44 +08:00
self.logger.info(f"{tr().main.version.now_on()} {self.on_python_v}")
2021-02-16 11:35:10 +08:00
if self.on_python_v_info[0] == 2:
2023-02-03 20:31:44 +08:00
self.logger.critical(tr().main.version.need3p())
raise SystemError(tr().main.version.need3p())
2021-08-13 12:25:29 +08:00
elif self.on_python_v_info[1] < 8:
2023-02-03 20:31:44 +08:00
warning = tools.name_handler(tr.main.version.best38p())
self.logger.warning(warning)
2021-02-16 00:08:11 +08:00
2021-09-05 00:50:05 +08:00
# @new_thread('main')
def _start(self):
2022-12-25 23:15:49 +08:00
self.server.run()
2023-01-24 23:59:07 +08:00
if DR_option.use_multiprocess:
try:
2023-02-18 19:19:15 +08:00
game_process = multiprocessing.Process(target=self.client.start, name='pyglet app')
game_process.start()
game_process.join()
2023-01-27 21:09:37 +08:00
except Exception:
return -1
else:
return 1
2021-09-05 00:50:05 +08:00
else:
self.client.start()
2021-09-05 00:50:05 +08:00
def start(self) -> None:
self._start()