Difficult-Rocket/Difficult_Rocket/main.py

88 lines
2.9 KiB
Python
Raw Normal View History

2021-09-08 23:38:34 +08:00
# -------------------------------
# Difficult Rocket
2022-06-27 16:51:14 +08:00
# Copyright © 2021-2022 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
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')
2021-09-05 00:50:05 +08:00
from Difficult_Rocket import client, server
from Difficult_Rocket.utils import tools
from Difficult_Rocket.utils.translate import tr
2020-12-05 21:24:54 +08:00
2020-12-21 18:46:20 +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
self.language = tools.load_file('configs/main.config', 'runtime')['language']
2021-09-22 20:51:30 +08:00
tr.set_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')
self.logger.info(tr['main']['logger.mkdir'])
self.logger.info(tr['main']['logger.created'])
2021-02-16 11:35:10 +08:00
# version check
2021-02-16 00:08:11 +08:00
self.python_version_check()
2021-08-13 12:25:29 +08:00
self.setup()
2021-08-24 22:31:52 +08:00
def setup(self) -> None:
self.client = client.Client(net_mode='local')
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
self.logger.info('%s %s' % (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:
self.logger.critical('%s' % tr['main']['version.need3+'])
raise SystemError('%s' % tr['main']['version.need3+'])
2021-08-13 12:25:29 +08:00
elif self.on_python_v_info[1] < 8:
warning = tools.name_handler(tr['main']['version.best3.8+'])
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):
2021-08-13 12:25:29 +08:00
self.server.run()
2022-03-22 23:20:07 +08:00
threaded = False
if threaded:
try:
game_process = multiprocessing.Process(target=self.client.start(), name='pyglet app')
game_process.start()
game_process.join()
except:
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()