2021-09-08 23:38:34 +08:00
|
|
|
# -------------------------------
|
|
|
|
# Difficult Rocket
|
|
|
|
# Copyright © 2021 by shenjackyuanjie
|
|
|
|
# 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-09-08 23:38:34 +08:00
|
|
|
|
2021-09-05 00:50:05 +08:00
|
|
|
import logging
|
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
|
2021-04-03 12:15:24 +08:00
|
|
|
from multiprocessing import Manager as share
|
2021-01-24 22:00:31 +08:00
|
|
|
|
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-01-23 21:43:04 +08:00
|
|
|
|
2021-09-05 00:50:05 +08:00
|
|
|
from Difficult_Rocket import client, server
|
2021-09-08 23:38:34 +08:00
|
|
|
from libs import pyglet
|
2021-09-05 00:50:05 +08:00
|
|
|
from Difficult_Rocket.api import tools
|
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-01-25 17:01:54 +08:00
|
|
|
# share memory
|
2021-01-25 19:23:16 +08:00
|
|
|
self.dicts = share().dict()
|
2021-01-21 15:05:29 +08:00
|
|
|
self.lists = share().list()
|
2021-02-20 21:46:14 +08:00
|
|
|
# lang_config
|
2021-04-02 23:31:54 +08:00
|
|
|
self.language = tools.config('configs/sys_value/basic_config.json5')
|
2021-02-20 21:46:14 +08:00
|
|
|
self.language = self.language['language']
|
2021-08-13 12:25:29 +08:00
|
|
|
self.lang = tools.config('configs/lang/%s.json5' % self.language, 'main')
|
2021-01-25 17:01:54 +08:00
|
|
|
# logger
|
2021-02-14 23:15:54 +08:00
|
|
|
self.log_config = tools.config('configs/logging.json5', 'file')
|
2021-06-26 14:15:33 +08:00
|
|
|
self.log_filename = tools.name_handler(self.log_config['filename']['main'],
|
2021-08-13 12:25:29 +08:00
|
|
|
{'{date}': self.log_config['date_fmt']})
|
2021-02-16 00:08:11 +08:00
|
|
|
self.root_logger_fmt = logging.Formatter(self.log_config['fmt'], self.log_config['date_fmt'])
|
2021-02-15 21:45:20 +08:00
|
|
|
self.root_logger_stream_handler = logging.StreamHandler()
|
|
|
|
self.root_logger_stream_handler.setFormatter(self.root_logger_fmt)
|
2021-02-15 23:48:13 +08:00
|
|
|
self.root_logger_stream_handler.setLevel(tools.log_level(self.log_config['level']))
|
2021-03-24 23:37:10 +08:00
|
|
|
try:
|
|
|
|
self.root_logger_file_handler = logging.FileHandler('logs/' + self.log_filename, encoding='utf-8')
|
|
|
|
except FileNotFoundError:
|
|
|
|
os.mkdir('./logs')
|
|
|
|
self.root_logger_file_handler = logging.FileHandler('logs/' + self.log_filename, encoding='utf-8')
|
2021-02-15 21:45:20 +08:00
|
|
|
self.root_logger_file_handler.setFormatter(self.root_logger_fmt)
|
2021-02-15 23:48:13 +08:00
|
|
|
self.root_logger_file_handler.setLevel(tools.log_level(self.log_config['level']))
|
2021-02-16 00:08:11 +08:00
|
|
|
# root logger setup
|
2021-02-15 23:48:13 +08:00
|
|
|
logging.getLogger().setLevel(tools.log_level(self.log_config['level']))
|
2021-02-15 21:45:20 +08:00
|
|
|
logging.getLogger().addHandler(self.root_logger_stream_handler)
|
|
|
|
logging.getLogger().addHandler(self.root_logger_file_handler)
|
2021-02-16 00:08:11 +08:00
|
|
|
# create logger
|
2021-08-13 12:25:29 +08:00
|
|
|
self.main_logger = logging.getLogger('main')
|
2021-05-24 05:58:16 +08:00
|
|
|
# output info
|
2021-02-20 21:46:14 +08:00
|
|
|
self.main_logger.info(self.lang['logger.created'])
|
|
|
|
self.main_logger.info(self.lang['logger.main_done'])
|
|
|
|
self.log_configs()
|
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:
|
2021-08-13 12:25:29 +08:00
|
|
|
self.client = client.Client(self.lists, self.dicts, net_mode='local')
|
2021-08-24 22:31:52 +08:00
|
|
|
self.server = server.Server(self.lists, self.dicts, net_mode='local')
|
2021-02-16 12:51:07 +08:00
|
|
|
|
2021-08-24 22:31:52 +08:00
|
|
|
def log_configs(self) -> None:
|
2021-02-21 11:58:47 +08:00
|
|
|
self.main_logger.info('%s %s' % (self.lang['logger.language'], self.lang['lang.language']))
|
2021-02-20 21:55:43 +08:00
|
|
|
self.main_logger.info('%s %s' % (self.lang['game_start.at'], self.start_time))
|
|
|
|
self.main_logger.debug('%s %s' % (self.lang['logger.logfile_name'], self.log_filename))
|
|
|
|
self.main_logger.debug('%s %s' % (self.lang['logger.logfile_level'], self.log_config['level']))
|
|
|
|
self.main_logger.debug('%s %s' % (self.lang['logger.logfile_fmt'], self.log_config['fmt']))
|
2021-02-21 11:58:47 +08:00
|
|
|
self.main_logger.debug('%s %s' % (self.lang['logger.logfile_datefmt'], self.log_config['date_fmt']))
|
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
|
2021-02-26 19:05:24 +08:00
|
|
|
self.main_logger.info('%s %s' % (self.lang['version.now_on'], self.on_python_v))
|
2021-02-16 11:35:10 +08:00
|
|
|
if self.on_python_v_info[0] == 2:
|
2021-02-26 19:05:24 +08:00
|
|
|
self.main_logger.critical('%s' % self.lang['version.need3+'])
|
2021-05-24 05:58:16 +08:00
|
|
|
raise SystemError('%s' % self.lang['version.need3+'])
|
2021-08-13 12:25:29 +08:00
|
|
|
elif self.on_python_v_info[1] < 8:
|
2021-06-26 14:15:33 +08:00
|
|
|
warning = tools.name_handler(self.lang['version.best3.8+'])
|
2021-02-26 19:05:24 +08:00
|
|
|
self.main_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) -> int:
|
2021-08-13 12:25:29 +08:00
|
|
|
self.server.run()
|
2021-09-05 00:50:05 +08:00
|
|
|
try:
|
|
|
|
pyglet.app.run()
|
|
|
|
except:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def start(self) -> None:
|
|
|
|
self._start()
|