Archived
0
0

fix: 改进

This commit is contained in:
2025-12-16 03:28:29 +08:00
parent 5e4b0508eb
commit 5f2e8f6523
19 changed files with 1076 additions and 61 deletions

View File

@@ -8,8 +8,19 @@ import heurams.kernel.puzzles as pz
from .base_puzzle_widget import BasePuzzleWidget
import copy
import random
from textual.containers import Container
from textual.message import Message
from heurams.services.logger import get_logger
from typing import TypedDict
logger = get_logger(__name__)
class Setting(TypedDict):
__origin__: str
__hint__: str
text: str
delimiter: str
min_denominator: str
class ClozePuzzle(BasePuzzleWidget):
@@ -36,52 +47,37 @@ class ClozePuzzle(BasePuzzleWidget):
self.inputlist = list()
self.hashtable = {}
self.alia = alia
self._work()
self._load()
self.hashmap = dict()
def _work(self):
cfg = self.atom.registry["orbital"]["puzzles"][self.alia]
def _load(self):
setting = self.atom.registry["orbital"]["puzzles"][self.alia]
self.puzzle = pz.ClozePuzzle(
text=cfg["content"],
delimiter=cfg["delimiter"],
min_denominator=cfg["min_denominator"],
text=setting["text"],
delimiter=setting["delimiter"],
min_denominator=int(setting["min_denominator"]),
)
self.puzzle.refresh()
self.ans = copy.copy(self.puzzle.answer)
self.ans = copy.copy(self.puzzle.answer) # 乱序
random.shuffle(self.ans)
class RatingChanged(Message):
def __init__(self, atom: pt.Atom, rating: int, is_correct: bool) -> None:
self.atom = atom
self.rating = rating # 评分
self.is_correct = is_correct # 是否正确
super().__init__()
class InputChanged(Message):
"""输入变化消息"""
def __init__(self, current_input: list, max_length: int) -> None:
self.current_input = current_input # 当前输入
self.max_length = max_length # 最大长度
self.progress = len(current_input) / max_length # 进度
super().__init__()
def compose(self):
yield Label(self.puzzle.wording, id="sentence")
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
for i in self.ans:
self.hashtable[str(hash(i))] = i
yield Button(i, id=f"{hash(i)}")
# 渲染当前问题的选项
with Container(id="btn-container"):
for i in self.ans:
self.hashmap[str(hash(i))] = i
btnid = f"sel000-{hash(i)}"
logger.debug(f"建立按钮 {btnid}")
yield Button(i, id=f"{btnid}")
yield Button("退格", id="delete")
def update_preview(self):
def update_display(self):
preview = self.query_one("#inputpreview")
preview.update(f"当前输入: {self.inputlist}") # type: ignore
self.post_message(
self.InputChanged(
current_input=self.inputlist.copy(), max_length=len(self.puzzle.answer)
)
)
def on_button_pressed(self, event: Button.Pressed) -> None:
button_id = event.button.id
@@ -89,22 +85,18 @@ class ClozePuzzle(BasePuzzleWidget):
if button_id == "delete":
if len(self.inputlist) > 0:
self.inputlist.pop()
self.update_preview()
self.update_display()
else:
answer_text = self.hashtable[button_id]
answer_text = self.hashmap[button_id[7:]] # type: ignore
self.inputlist.append(answer_text)
self.update_preview()
self.update_display()
if len(self.inputlist) >= len(self.puzzle.answer):
is_correct = self.inputlist == self.puzzle.answer
rating = 4 if is_correct else 2
self.post_message(
self.RatingChanged(
atom=self.atom, rating=rating, is_correct=is_correct
)
)
self.screen.rating = rating # type: ignore
if not is_correct:
self.inputlist = []
self.update_preview()
self.update_display()