From b8fdf4a7225379db2b44dd6a59c8bc5095e9721f Mon Sep 17 00:00:00 2001 From: bomzheg Date: Sat, 31 Aug 2024 23:33:44 +0300 Subject: [PATCH] save time --- shvatka/tgbot/dialogs/level_scn/handlers.py | 6 +++++- shvatka/tgbot/dialogs/time_hint/dialogs.py | 6 ++++++ shvatka/tgbot/dialogs/time_hint/handlers.py | 24 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/shvatka/tgbot/dialogs/level_scn/handlers.py b/shvatka/tgbot/dialogs/level_scn/handlers.py index 1dfbb89d..d59e3bd9 100644 --- a/shvatka/tgbot/dialogs/level_scn/handlers.py +++ b/shvatka/tgbot/dialogs/level_scn/handlers.py @@ -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): diff --git a/shvatka/tgbot/dialogs/time_hint/dialogs.py b/shvatka/tgbot/dialogs/time_hint/dialogs.py index 8b52afe7..a1360faf 100644 --- a/shvatka/tgbot/dialogs/time_hint/dialogs.py +++ b/shvatka/tgbot/dialogs/time_hint/dialogs.py @@ -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 @@ -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, diff --git a/shvatka/tgbot/dialogs/time_hint/handlers.py b/shvatka/tgbot/dialogs/time_hint/handlers.py index 7ca4ac7d..4161cdbf 100644 --- a/shvatka/tgbot/dialogs/time_hint/handlers.py +++ b/shvatka/tgbot/dialogs/time_hint/handlers.py @@ -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 @@ -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) @@ -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("Время меньше предыдущего")