Add CommandText.find

This commit is contained in:
shenjack 2023-05-14 20:56:19 +08:00
parent 2d558889f4
commit 1ca49485f0
4 changed files with 30 additions and 32 deletions

View File

@ -210,8 +210,6 @@ class ClientWindow(Window):
def start_game(self) -> None: def start_game(self) -> None:
self.set_icon(pyglet.image.load('./textures/icon.png')) self.set_icon(pyglet.image.load('./textures/icon.png'))
self.run_input = True
# self.read_input()
try: try:
pyglet.app.event_loop.run(1 / self.main_config['runtime']['fps']) pyglet.app.event_loop.run(1 / self.main_config['runtime']['fps'])
except KeyboardInterrupt: except KeyboardInterrupt:
@ -221,22 +219,6 @@ class ClientWindow(Window):
self.dispatch_event("on_close") self.dispatch_event("on_close")
sys.exit(0) sys.exit(0)
@new_thread('window read_input', daemon=True)
def read_input(self):
self.logger.debug('read_input start')
while self.run_input:
try:
get = input(">")
except (EOFError, KeyboardInterrupt):
self.run_input = False
break
if get in ('', ' ', '\n', '\r'):
continue
if get == 'stop':
self.run_input = False
self.command_list.append(get)
self.logger.debug('read_input end')
@new_thread('window save_info') @new_thread('window save_info')
def save_info(self): def save_info(self):
self.logger.info(tr().client.config.save.start()) self.logger.info(tr().client.config.save.start())
@ -266,10 +248,8 @@ class ClientWindow(Window):
@_call_screen_after @_call_screen_after
def on_draw(self, *dt): def on_draw(self, *dt):
if self.command_list: while command := self.game.console.get_command():
for command in self.command_list: self.on_command(line.CommandText(command))
self.on_command(line.CommandText(command))
self.command_list.pop(0)
pyglet.gl.glClearColor(0.1, 0, 0, 0.0) pyglet.gl.glClearColor(0.1, 0, 0, 0.0)
self.clear() self.clear()
self.draw_update(float(self.SPF)) self.draw_update(float(self.SPF))
@ -310,26 +290,26 @@ class ClientWindow(Window):
@_call_screen_after @_call_screen_after
def on_command(self, command: line.CommandText): def on_command(self, command: line.CommandText):
print(command.re_match('/')) print(command.find('/'))
self.logger.info(tr().window.command.text().format(command)) self.logger.info(tr().window.command.text().format(command))
if command.re_match('stop'): if command.find('stop'):
# self.dispatch_event('on_exit') # self.dispatch_event('on_exit')
print("command stop!") print("command stop!")
pyglet.app.platform_event_loop.stop() pyglet.app.platform_event_loop.stop()
self.dispatch_event('on_close', 'command') # source = command self.dispatch_event('on_close', 'command') # source = command
elif command.re_match('fps'): elif command.find('fps'):
if command.re_match('log'): if command.find('log'):
self.logger.debug(self.fps_log.fps_list) self.logger.debug(self.fps_log.fps_list)
elif command.re_match('max'): elif command.find('max'):
self.logger.info(self.fps_log.max_fps) self.logger.info(self.fps_log.max_fps)
self.command.push_line(self.fps_log.max_fps, block_line=True) self.command.push_line(self.fps_log.max_fps, block_line=True)
elif command.re_match('min'): elif command.find('min'):
self.logger.info(self.fps_log.min_fps) self.logger.info(self.fps_log.min_fps)
self.command.push_line(self.fps_log.min_fps, block_line=True) self.command.push_line(self.fps_log.min_fps, block_line=True)
elif command.re_match('default'): elif command.find('default'):
self.set_size(int(self.main_config['window_default']['width']), self.set_size(int(self.main_config['window_default']['width']),
int(self.main_config['window_default']['height'])) int(self.main_config['window_default']['height']))
elif command.re_match('lang'): elif command.find('lang'):
try: try:
lang = command.text[5:] lang = command.text[5:]
tr._language = lang tr._language = lang

View File

@ -43,7 +43,14 @@ class CommandText:
break break
i += 1 i += 1
def find(self, text: str) -> Union[str, bool]: def find(self, text: str) -> bool:
find = self.text.find(text)
if find != -1:
self.text = self.text[find + len(text):]
return True
return False
def re_find(self, text: str) -> Union[str, bool]:
return finding.group() if (finding := re.match(text, self.text)) else False return finding.group() if (finding := re.match(text, self.text)) else False
def re_match(self, text: str) -> bool: def re_match(self, text: str) -> bool:

View File

@ -149,7 +149,7 @@ class Game(Options):
DR_runtime.DR_Mod_List = mod_list DR_runtime.DR_Mod_List = mod_list
def init_console(self) -> None: def init_console(self) -> None:
self.console = Console() self.console = self.console_class()
self.console.start() self.console.start()
def start(self): def start(self):

View File

@ -114,6 +114,13 @@
- 修改 返回值 类型+类型注释 - 修改 返回值 类型+类型注释
- Modify the return value type + type annotation - Modify the return value type + type annotation
- `List[Union[List[Tuple[str, Any, Any]], int, Any]]:` -> `Tuple[List[Tuple[str, Union[Any, Type], Type]], int, int, int]` - `List[Union[List[Tuple[str, Any, Any]], int, Any]]:` -> `Tuple[List[Tuple[str, Union[Any, Type], Type]], int, int, int]`
- `Difficult_Roocket.main.Game`
- 使用 `Options` 完全重构
- 分离 `init mods` `init console` `init logger` `load_file`
- Completely refactored using `Options`
- Separate `init mods` `init console` `init logger` `load_file`
- `Difficult_Rocket.command.api.CommandText`
- `find` -> `re_find`
### Add ### Add
@ -122,6 +129,10 @@
- 用于方便的用人类可读的 Markdown 格式 直接输出一个已经实例化的 `Options` 类的所有字段 - 用于方便的用人类可读的 Markdown 格式 直接输出一个已经实例化的 `Options` 类的所有字段
- Add `as_markdown` method - Add `as_markdown` method
- Used to easily output all fields of an instantiated `Options` class in a human-readable Markdown format - Used to easily output all fields of an instantiated `Options` class in a human-readable Markdown format
- `Difficult_Rocket.command.api.CommandText`
- 添加基于 `str.find``find(text: str) -> bool` 方法
- Add method `find(text: str)` based on `str.find`
### Docs ### Docs