You've already forked HeurAMS-legacy
fix: 完善
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
# 单项选择题
|
||||
from textual.widgets import (
|
||||
Label,
|
||||
Button,
|
||||
)
|
||||
from textual.containers import (
|
||||
Container
|
||||
)
|
||||
from textual.widget import Widget
|
||||
import heurams.kernel.particles as pt
|
||||
import heurams.kernel.puzzles as pz
|
||||
from .base_puzzle_widget import BasePuzzleWidget
|
||||
from typing import TypedDict
|
||||
|
||||
from bidict import bidict
|
||||
from heurams.services.hasher import hash
|
||||
|
||||
class Setting(TypedDict):
|
||||
__origin__: str
|
||||
@@ -42,7 +47,7 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
)
|
||||
self.inputlist = []
|
||||
self.alia = alia
|
||||
self.hashtable = {}
|
||||
self.hashmap = bidict()
|
||||
self._load()
|
||||
|
||||
def _load(self):
|
||||
@@ -53,9 +58,7 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
self.puzzle.refresh()
|
||||
|
||||
def compose(self):
|
||||
setting: Setting = self.atom.registry["nucleon"].metadata["orbital"]["puzzle"][
|
||||
self.alia
|
||||
]
|
||||
setting: Setting = self.atom.registry["nucleon"].metadata["orbital"]["puzzle"][self.alia]
|
||||
yield Label(setting["primary"], id="sentence")
|
||||
yield Label(self.puzzle.wording[len(self.inputlist)], id="puzzle")
|
||||
yield Label(f"当前输入: {self.inputlist}", id="inputpreview")
|
||||
@@ -63,8 +66,8 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
# 渲染当前问题的选项
|
||||
current_options = self.puzzle.options[len(self.inputlist)]
|
||||
for i in current_options:
|
||||
self.hashtable[str(hash(i))] = i
|
||||
yield Button(i, id=f"select{hash(i)}")
|
||||
self.hashmap[str(hash(i))] = i
|
||||
yield Button(i, id=f"select-{hash(i)}")
|
||||
|
||||
yield Button("退格", id="delete")
|
||||
|
||||
@@ -79,12 +82,9 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
if current_question_index < len(self.puzzle.wording):
|
||||
puzzle_label.update(self.puzzle.wording[current_question_index]) # type: ignore
|
||||
|
||||
# 发送输入变化消息
|
||||
|
||||
# 如果还有下一题,发送题目切换消息
|
||||
|
||||
def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""处理按钮点击事件"""
|
||||
event.stop()
|
||||
button_id = event.button.id
|
||||
|
||||
if button_id == "delete":
|
||||
@@ -93,9 +93,10 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
self.inputlist.pop()
|
||||
self.refresh_buttons()
|
||||
self.update_display()
|
||||
|
||||
elif button_id.startswith("select"): # type: ignore
|
||||
# 选项选择处理
|
||||
answer_text = self.hashtable[button_id[6:]] # type: ignore
|
||||
answer_text = self.hashmap[button_id[7:]] # type: ignore
|
||||
self.inputlist.append(answer_text)
|
||||
|
||||
# 检查是否完成所有题目
|
||||
@@ -103,16 +104,7 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
is_correct = self.inputlist == self.puzzle.answer
|
||||
rating = 4 if is_correct else 2
|
||||
|
||||
# 发送完成消息
|
||||
self.post_message(
|
||||
self.PuzzleCompleted(
|
||||
atom=self.atom,
|
||||
rating=rating,
|
||||
is_correct=is_correct,
|
||||
user_answers=self.inputlist.copy(),
|
||||
correct_answers=self.puzzle.answer.copy(),
|
||||
)
|
||||
)
|
||||
self.screen.rating = rating # type: ignore
|
||||
|
||||
# 重置输入(如果回答错误)
|
||||
if not is_correct:
|
||||
@@ -132,6 +124,7 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
for child in self.children
|
||||
if hasattr(child, "id") and child.id and child.id.startswith("select")
|
||||
]
|
||||
|
||||
for button in buttons_to_remove:
|
||||
self.remove_child(button) # type: ignore
|
||||
|
||||
@@ -140,8 +133,8 @@ class MCQPuzzle(BasePuzzleWidget):
|
||||
if current_question_index < len(self.puzzle.options):
|
||||
current_options = self.puzzle.options[current_question_index]
|
||||
for option in current_options:
|
||||
button_id = f"select{hash(option)}"
|
||||
if button_id not in self.hashtable:
|
||||
self.hashtable[button_id] = option
|
||||
button_id = f"select-{hash(option)}"
|
||||
if button_id not in self.hashmap:
|
||||
self.hashmap[button_id] = option
|
||||
new_button = Button(option, id=button_id)
|
||||
self.mount(new_button)
|
||||
|
||||
Reference in New Issue
Block a user