From 82e8c4a0622368052390c3822e22454ab9111eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E7=91=97=E6=9D=B0?= <3695888@qq.com> Date: Mon, 25 Oct 2021 22:08:00 +0800 Subject: [PATCH] 0.6.0 comming! --- Difficult_Rocket/api/calculation.py | 36 +++++++++++-------- Difficult_Rocket/api/command.py | 35 +++++++++++++----- Difficult_Rocket/client.py | 51 +++++++++++++++++--------- Difficult_Rocket/fps/__init__.py | 12 +++++++ Difficult_Rocket/fps/fps_log.py | 55 +++++++++++++++++++++++++++++ configs/logger.json5 | 2 +- configs/main.config | 6 ++-- docs/update_logs.md | 19 ++++++++-- 8 files changed, 170 insertions(+), 46 deletions(-) create mode 100644 Difficult_Rocket/fps/__init__.py create mode 100644 Difficult_Rocket/fps/fps_log.py diff --git a/Difficult_Rocket/api/calculation.py b/Difficult_Rocket/api/calculation.py index 2d02f6c..0181062 100644 --- a/Difficult_Rocket/api/calculation.py +++ b/Difficult_Rocket/api/calculation.py @@ -14,7 +14,7 @@ gitee: @shenjackyuanjie import math import decimal -from typing import List, Optional +from typing import List, Optional, Union from decimal import Decimal @@ -283,18 +283,26 @@ class ScientificNumber: """ def __init__(self, - floats: Optional[float] = 1.0, - integer: Optional[int] = 0, - multi_unit: list = None, - divide_unit: list = None): - if divide_unit is None: - self.divide_unit = [] + 小数: Union[float, Decimal] = 1.0, + 指数: int = 0, + 乘单位: list = list, + 除单位: list = list): + if not isinstance(小数, Decimal): + self.小数 = Decimal(小数) else: - self.divide_unit = divide_unit - if multi_unit is None: - self.multi_unit = [] - else: - self.multi_unit = multi_unit - self.floats = floats - self.integer = integer + self.小数 = 小数 + self.指数 = 指数 + self.乘单位 = 乘单位 + self.除单位 = 除单位 + self.check() + + def check(self): + while self.小数 > 10: + self.小数 /= 10 + self.指数 += 1 + while self.小数 < 1: + self.小数 *= 10 + self.指数 -= 1 + self.除单位.sort() + self.乘单位.sort() diff --git a/Difficult_Rocket/api/command.py b/Difficult_Rocket/api/command.py index 96ba8e1..8eeb1dd 100644 --- a/Difficult_Rocket/api/command.py +++ b/Difficult_Rocket/api/command.py @@ -13,9 +13,12 @@ gitee: @shenjackyuanjie import time +from typing import Union +from decimal import Decimal + # from DR -from Difficult_Rocket.api import translate -from Difficult_Rocket.api.new_thread import new_thread +from . import translate +from .new_thread import new_thread # from libs.pyglet from libs import pyglet @@ -49,7 +52,7 @@ class CommandLine(widgets.WidgetBase): # normal values self.length = length self.command_list = ['' for line in range(length)] - self.command_text = command_text + self._command_text = command_text self._text_position = 0 self._command_view = 0 self._value = 0 @@ -78,7 +81,7 @@ class CommandLine(widgets.WidgetBase): color = (100, 100, 100, 100) self._pad = p = 5 self._outline = pyglet.shapes.Rectangle(x=x - p, y=y - p, - width=width + p + p, height=height + p + p, + width=width + p, height=height + p, color=color[:3], batch=batch, group=fg_group) self._outline.opacity = color[3] @@ -135,7 +138,7 @@ class CommandLine(widgets.WidgetBase): pass else: # move downwards pass - self._command_view = value + # self._command_view = value @property def editing(self): @@ -157,16 +160,22 @@ class CommandLine(widgets.WidgetBase): if self._label[0].visible and not self.editing: self._label[0].visible = False + """ + events + """ + def on_text(self, text): if self.editing: if text in ('\r', '\n'): # goto a new line if not self.text: pass - elif self.text[0] == self.command_text: + elif self.text[0] == self._command_text: self.dispatch_event('on_command', self.text[1:]) else: self.dispatch_event('on_message', self.text) - self.command_view = -1 + # on_message 和 on_command 可能会覆盖 self.text 需要再次判定 + if self.text: + self.command_view = -1 self.editing = False self.wait(1) else: @@ -204,7 +213,6 @@ class CommandLine(widgets.WidgetBase): self.command_view -= 1 else: pass - print(self._text_position) def on_text_motion_select(self, motion): if self.editing: @@ -218,6 +226,10 @@ class CommandLine(widgets.WidgetBase): if self.editing: pass + """ + custom event + """ + def on_command(self, command: text): if self.editing: return @@ -228,6 +240,13 @@ class CommandLine(widgets.WidgetBase): return """give message to it""" + def push_line(self, line: Union[str, int, float, Decimal], block_line: bool = False): + _text = self.text + self.text = str(line) + self.command_view = -1 + if not block_line: + self.text = _text + CommandLine.register_event_type('on_command') CommandLine.register_event_type('on_message') diff --git a/Difficult_Rocket/client.py b/Difficult_Rocket/client.py index 36f461b..990e769 100644 --- a/Difficult_Rocket/client.py +++ b/Difficult_Rocket/client.py @@ -16,6 +16,7 @@ import os import sys import time import logging +import traceback import configparser from decimal import Decimal @@ -25,10 +26,9 @@ if __name__ == '__main__': # been start will not run this sys.path.append('/bin') # Difficult_Rocket function -from . import guis from .api import command -from .api.Exp import * from .api.translate import tr +from .fps.fps_log import FpsLogger from .api import tools, new_thread, translate # libs function @@ -103,6 +103,8 @@ class ClientWindow(pyglet.window.Window): self.max_fps = [self.FPS, time.time()] self.min_fps = [self.FPS, time.time()] self.fps_wait = 5 + self.fps_log = FpsLogger(stable_fps=int(self.FPS), + wait_time=5) # batch self.part_batch = pyglet.graphics.Batch() self.label_batch = pyglet.graphics.Batch() @@ -137,20 +139,19 @@ class ClientWindow(pyglet.window.Window): def setup(self): self.load_fonts().join() - # print(pyglet.font.load(translate.鸿蒙简体)) - @new_thread('client load_fonts') + @new_thread('window load_fonts') def load_fonts(self): file_path = './libs/fonts/HarmonyOS_Sans/' ttf_files = os.listdir(file_path) self.logger.info(tr.lang('window', 'fonts.found').format(ttf_files)) for ttf_folder in ttf_files: for ttf_file in os.listdir(f'{file_path}{ttf_folder}'): - if not ttf_file[-4:] == '.ttf': continue + if not ttf_file[-4:] == '.ttf': + continue pyglet.font.add_file(f'{file_path}{ttf_folder}/{ttf_file}') - # print(f'{file_path}{ttf_file}') - # @new_thread('client load_editor') + # @new_thread('window load_editor') def load_Editor(self): pass @@ -159,27 +160,37 @@ class ClientWindow(pyglet.window.Window): self.read_input() pyglet.app.run() - @new_thread('read_input', daemon=True) + @new_thread('window read_input', daemon=True) def read_input(self): self.logger.debug('read_input start') while self.run_input: - self.logger.info('<<<') get = input() if get in ('', ' ', '\n', '\r'): continue - self.logger.info(get) if get == 'stop': self.run_input = False - self.dispatch_event('on_close', 'a') # source = 'command' + try: + self.on_command(get) + except: + self.logger.error(traceback.format_exc()) self.logger.debug('read_input end') + @new_thread('window save_info') + def save_info(self): + config_file = configparser.ConfigParser() + config_file.read('configs/main.config') + config_file['window']['width'] = str(self.width) + config_file['window']['height'] = str(self.height) + config_file.write(open('configs/main.config', 'w', encoding='utf-8')) + """ draws and some event """ def update(self, tick: float): - decimal_tick = Decimal(tick) + decimal_tick = Decimal(str(tick)[:10]) self.FPS_update(decimal_tick) + self.fps_log.update_tick(Decimal(tick)) def FPS_update(self, tick: Decimal): now_FPS = pyglet.clock.get_fps() @@ -214,6 +225,16 @@ class ClientWindow(pyglet.window.Window): self.logger.info(tr.lang('window', 'command.text').format(command)) if command == 'stop': self.dispatch_event('on_close', 'command') # source = command + elif command == 'log_tick': + self.logger.debug(self.fps_log.fps_list) + elif command == 'max_fps': + self.logger.info(self.fps_log.max_fps) + self.command.push_line(self.fps_log.max_fps, block_line=True) + elif command == 'min_fps': + self.logger.info(self.fps_log.min_fps) + self.command.push_line(self.fps_log.min_fps, block_line=True) + elif command == 'default': + self.set_size(int(self.config_file['window_default']['width']), int(self.config_file['window_default']['height'])) def on_message(self, message: command.CommandLine.text): self.logger.info(tr.lang('window', 'message.text').format(message)) @@ -263,10 +284,6 @@ class ClientWindow(pyglet.window.Window): self.logger.info(tr.lang('window', 'game.stop')) if self.run_input: self.run_input = False - config_file = configparser.ConfigParser() - config_file.read('configs/main.config') - config_file['window']['width'] = str(self.width) - config_file['window']['height'] = str(self.height) - config_file.write(open('configs/main.config', 'w', encoding='utf-8')) + self.save_info() super().on_close() self.logger.info(tr.lang('window', 'game.end')) diff --git a/Difficult_Rocket/fps/__init__.py b/Difficult_Rocket/fps/__init__.py new file mode 100644 index 0000000..80dcc9a --- /dev/null +++ b/Difficult_Rocket/fps/__init__.py @@ -0,0 +1,12 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + +""" +writen by shenjackyuanjie +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie +""" diff --git a/Difficult_Rocket/fps/fps_log.py b/Difficult_Rocket/fps/fps_log.py new file mode 100644 index 0000000..7f905f7 --- /dev/null +++ b/Difficult_Rocket/fps/fps_log.py @@ -0,0 +1,55 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2021 by shenjackyuanjie +# All rights reserved +# ------------------------------- + +""" +writen by shenjackyuanjie +mail: 3695888@qq.com +github: @shenjackyuanjie +gitee: @shenjackyuanjie +""" + +import time +import decimal + +from decimal import Decimal + +from libs import pyglet + +""" +fps_list -> +[ +[fps, time_ns, tick time_ns, pyglet tick] +] +""" + + +class FpsLogger: + def __init__(self, + stable_fps: int = 60, + wait_time: int = 5): + self.stable_fps = stable_fps + self.wait_time = wait_time + self.fps_list = [[stable_fps, time.time_ns()]] + + def update_tick(self, + tick: Decimal): + now_time = time.time_ns() + now_fps = pyglet.clock.get_fps() + tick_time = now_time - self.fps_list[-1][1] + self.fps_list.append([now_fps, now_time, tick_time, tick]) + if now_time - self.fps_list[0][1] >= self.wait_time * (10 ** 9): + self.fps_list.pop(0) + + @property + def max_fps(self): + fps_list = [fpss[0] for fpss in self.fps_list] + return max(fps_list) + + @property + def min_fps(self): + fps_list = [fpss[0] for fpss in self.fps_list] + return min(fps_list) + diff --git a/configs/logger.json5 b/configs/logger.json5 index 6fd3f07..a602693 100644 --- a/configs/logger.json5 +++ b/configs/logger.json5 @@ -35,7 +35,7 @@ }, 'loggers': { 'client': { - 'level': 'DEBUG', + 'level': 'INFO', 'handlers': [] }, 'server': { diff --git a/configs/main.config b/configs/main.config index de20bff..9673b7d 100644 --- a/configs/main.config +++ b/configs/main.config @@ -1,14 +1,14 @@ [runtime] fps = 60 -version = 0.5.2 +version = 0.6.0 language = zh-CN date_fmt = %%Y-%%m-%%d %%H-%%M-%%S write_py_v = 3.8.10 [window] style = None -width = 1217 -height = 896 +width = 1024 +height = 768 visible = true caption = Difficult Rocket {version} resizable = true diff --git a/docs/update_logs.md b/docs/update_logs.md index cb673ba..8d032ec 100644 --- a/docs/update_logs.md +++ b/docs/update_logs.md @@ -3,20 +3,23 @@ ## Readme ##### most badge can be clicked and jump [![Generic badge](https://img.shields.io/badge/SemVer-2.0.0-blue.svg)](https://Semver.org/) -![Generic badge](https://img.shields.io/badge/Version-0.5.3-yellow.svg) +![Generic badge](https://img.shields.io/badge/Version-0.6.0-yellow.svg) - [![Readme-github](https://img.shields.io/badge/Readme-Github-blue.svg?style=flat-square&logo=Github)](https://github.com/shenjackyuanjie/Difficult-Rocket) - [![Readme-gitee](https://img.shields.io/badge/Readme-Gitee-blue.svg?style=flat-square&logo=Gitee)](https://gitee.com/shenjackyuanjie/Difficult-Rocket) - [![Readme-gitee](https://img.shields.io/badge/Readme-中文(点我!)-blue.svg?style=flat-square)](README-cn.md) - Using [SemVer 2.0.0](https://semver.org/) to manage version -## 202110 V 0.5.3 +## 20211025 V 0.6.0 + +#### Command Line Update! ### Change - now `Difficult Rocket` will only fit python3.8+ - because `:=` - now main crash report handler have new way to handler crash +- now fonts' folder's name is `HarmonyOS_Sans` ### Add @@ -29,6 +32,7 @@ - `translate/Lang.翻译` same as `Lang.lang` - `command/CommandLine` to render command line - `@new_thread` now can option if log this thread to `crash` or not +- `start.cmd` witch could debug easier ### Translate @@ -54,7 +58,16 @@ - now you can press `t` to call out a message window - or press `/` to open command line - not done - - useless until now + - ~~useless until now~~ +- new command + - `/min_fps` + - get min fps in 5 second + - `/max_fps` + - get max fps in 5 second + - `/stop` + - stop the game + - `/default` + - switch window size to default size ## 20210928 V 0.5.2