Skip to content

Commit

Permalink
save time
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Sep 2, 2024
1 parent b33ef55 commit b8fdf4a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion shvatka/tgbot/dialogs/level_scn/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ async def on_correct_bonus_keys(
async def process_time_hint_result(start_data: Data, result: Any, manager: DialogManager):
if not result:
return
if new_hint := result["time_hint"]:
if new_hint := result.get("time_hint", None):
manager.dialog_data.setdefault("time_hints", []).append(new_hint)
elif (edited_hint := result.get("edited_time_hint")) and isinstance(start_data, dict):
old_hint = start_data["time_hint"]
assert edited_hint != old_hint
# TODO save me


async def process_level_result(start_data: Data, result: Any, manager: DialogManager):
Expand Down
6 changes: 6 additions & 0 deletions shvatka/tgbot/dialogs/time_hint/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
hint_edit_on_start,
process_edit_time_message,
edit_single_hint,
save_edited_time_hint,
)
from shvatka.tgbot.dialogs.preview_data import TIMES_PRESET

Expand Down Expand Up @@ -93,6 +94,11 @@
width=1,
height=10,
),
Button(
text=Const("Сохранить изменения"),
id="save_time_hint",
on_click=save_edited_time_hint,
),
Cancel(text=Const("Вернуться, ничего не менять")),
getter=get_hints,
state=states.TimeHintEditSG.details,
Expand Down
24 changes: 24 additions & 0 deletions shvatka/tgbot/dialogs/time_hint/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from shvatka.core.models.dto import scn
from shvatka.core.models.dto.scn import TimeHint
from shvatka.core.models.dto.scn.hint_part import AnyHint
from shvatka.core.utils import exceptions
from shvatka.tgbot import states
from shvatka.tgbot.views.hint_factory.hint_parser import HintParser
from shvatka.tgbot.views.hint_sender import HintSender
Expand All @@ -26,6 +27,15 @@ async def process_edit_time_message(m: Message, dialog_: Any, manager: DialogMan
except ValueError:
await m.answer("Некорректный формат времени. Пожалуйста введите время в формате ЧЧ:ММ")
return
dcf: Factory = manager.middleware_data["dcf"]
hint = dcf.load(manager.start_data["time_hint"], scn.TimeHint)
if not hint.can_update_time():
await m.reply(
"Увы, отредактировать время данной подсказки не получится. "
"Скорее всего это загадка уровня (Подсказка 0 мин.). "
"Придётся переделать прямо тут текст (или медиа, или что там)"
)
return
manager.dialog_data["time"] = time_
await manager.switch_to(states.TimeHintEditSG.details)

Expand All @@ -52,6 +62,20 @@ async def edit_single_hint(c: CallbackQuery, widget: Any, manager: DialogManager
await hint_sender.send_hint(hint.hint[int(hint_index)], chat.id)


async def save_edited_time_hint(c: CallbackQuery, widget: Any, manager: DialogManager):
dishka: AsyncContainer = manager.middleware_data[CONTAINER_NAME]
dcf = await dishka.get(Factory)
time_hint = dcf.load(manager.start_data["time_hint"], scn.TimeHint)
try:
time_hint.update_time(manager.dialog_data["time"])
time_hint.update_hint(dcf.load(manager.dialog_data["hints"], list[AnyHint]))
except exceptions.LevelError as e:
assert isinstance(c.message, Message)
await c.message.reply(e.text)
return
await manager.done({"edited_time_hint": dcf.dump(time_hint)})


async def set_time(time_minutes: int, manager: DialogManager):
if time_minutes <= int(manager.start_data["previous_time"]):
raise ValueError("Время меньше предыдущего")
Expand Down

0 comments on commit b8fdf4a

Please sign in to comment.