Skip to content

Commit

Permalink
added select any hint
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Aug 23, 2024
1 parent f4c32e4 commit 6c1cfd9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 31 deletions.
3 changes: 2 additions & 1 deletion shvatka/tgbot/dialogs/game_manage/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aiogram_dialog import DialogManager
from aiogram_dialog.api.entities import MediaAttachment, MediaId
from dishka import AsyncContainer
from dishka.integrations.aiogram import CONTAINER_NAME
from telegraph import Telegraph

from shvatka.common.url_factory import UrlFactory
Expand All @@ -30,7 +31,7 @@ async def get_completed_game(dao: HolderDao, dialog_manager: DialogManager, **_)
game_id = (
dialog_manager.dialog_data.get("game_id", None) or dialog_manager.start_data["game_id"]
)
dishka: AsyncContainer = dialog_manager.middleware_data["dishka_container"]
dishka: AsyncContainer = dialog_manager.middleware_data[CONTAINER_NAME]
url_factory = await dishka.get(UrlFactory)
return {
"game": await game.get_game(
Expand Down
1 change: 0 additions & 1 deletion shvatka/tgbot/dialogs/level_manage/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from shvatka.core.services.level import get_by_id, get_level_by_id_for_org, get_all_my_free_levels
from shvatka.core.services.organizers import get_org_by_id, get_by_player
from shvatka.infrastructure.db.dao.holder import HolderDao
from shvatka.tgbot.views.utils import render_time_hints


async def get_level_id(dao: HolderDao, dialog_manager: DialogManager, **_):
Expand Down
5 changes: 3 additions & 2 deletions shvatka/tgbot/dialogs/level_scn/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Button, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Const, Jinja, Format
from aiogram_dialog.widgets.text import Const, Jinja

from shvatka.tgbot import states
from .getters import get_time_hints, get_level_id, get_level_data, get_keys, get_bonus_keys
Expand All @@ -26,7 +26,8 @@
convert_bonus_keys,
on_correct_bonus_keys,
not_correct_bonus_keys,
start_bonus_keys, start_edit_time_hint,
start_bonus_keys,
start_edit_time_hint,
)

level = Dialog(
Expand Down
1 change: 0 additions & 1 deletion shvatka/tgbot/dialogs/level_scn/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from shvatka.core.models.dto import scn
from shvatka.core.models.dto.scn import TimeHint
from shvatka.tgbot.views.utils import render_time_hints


async def get_level_id(dialog_manager: DialogManager, **_):
Expand Down
17 changes: 4 additions & 13 deletions shvatka/tgbot/dialogs/level_scn/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from aiogram_dialog import Data, DialogManager
from aiogram_dialog.widgets.kbd import Button
from dataclass_factory import Factory
from dishka import AsyncContainer

from shvatka.core.models import dto
from shvatka.core.models.dto import scn
Expand All @@ -17,7 +16,6 @@
)
from shvatka.infrastructure.db.dao.holder import HolderDao
from shvatka.tgbot import states
from shvatka.tgbot.views.hint_sender import HintSender


def check_level_id(name_id: str) -> str:
Expand Down Expand Up @@ -135,24 +133,17 @@ async def on_start_hints_edit(start_data: dict[str, Any], manager: DialogManager
manager.dialog_data["time_hints"] = start_data["time_hints"]


async def start_edit_time_hint(c: CallbackQuery, widget: Any, manager: DialogManager, hint_time: str):
async def start_edit_time_hint(
c: CallbackQuery, widget: Any, manager: DialogManager, hint_time: str
):
dcf: Factory = manager.middleware_data["dcf"]
hints = dcf.load(manager.dialog_data.get("time_hints", []), list[scn.TimeHint])
await manager.start(
state=states.TimeHintEditSG.details,
data={"time_hint": dcf.dump(next(filter(lambda x: x.time == int(hint_time), hints)))}
data={"time_hint": dcf.dump(next(filter(lambda x: x.time == int(hint_time), hints)))},
)


async def edit_single_hint(c: CallbackQuery, widget: Any, manager: DialogManager, hint_index: str):
dcf: Factory = manager.middleware_data["dcf"]
dishka: AsyncContainer = manager.middleware_data["dishka_container"]
hint = dcf.load(manager.start_data.get("time_hint"), scn.TimeHint)
hint_sender = await dishka.get(HintSender)
await hint_sender.send_hint(hint.hint[int(hint_index)], c.message.chat.id)



async def start_add_time_hint(c: CallbackQuery, button: Button, manager: DialogManager):
dcf: Factory = manager.middleware_data["dcf"]
hints = dcf.load(manager.dialog_data.get("time_hints", []), list[scn.TimeHint])
Expand Down
32 changes: 21 additions & 11 deletions shvatka/tgbot/dialogs/time_hint/dialogs.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import MessageInput
from aiogram_dialog.widgets.kbd import Select, Button, Group, Back, Cancel, SwitchTo, ScrollingGroup
from aiogram_dialog.widgets.kbd import (
Select,
Button,
Group,
Back,
Cancel,
SwitchTo,
ScrollingGroup,
)
from aiogram_dialog.widgets.text import Const, Format, Case, Jinja

from shvatka.tgbot import states
from .getters import get_available_times, get_hints
from .handlers import process_time_message, select_time, process_hint, on_finish, hint_on_start, hint_edit_on_start, \
process_edit_time_message
from .handlers import (
process_time_message,
select_time,
process_hint,
on_finish,
hint_on_start,
hint_edit_on_start,
process_edit_time_message,
edit_single_hint,
)
from shvatka.tgbot.dialogs.preview_data import TIMES_PRESET
from ..level_scn.handlers import start_edit_time_hint, edit_single_hint

time_hint = Dialog(
Window(
Expand Down Expand Up @@ -60,10 +75,7 @@

time_hint_edit = Dialog(
Window(
Jinja(
"Подсказка выходящая в {{time}}:"
"{{hints | hints}}"
),
Jinja("Подсказка выходящая в {{time}}:" "{{hints | hints}}"),
SwitchTo(
Const("Изменить время"),
id="change_time",
Expand All @@ -86,9 +98,7 @@
state=states.TimeHintEditSG.details,
),
Window(
Jinja(
"Введи новое время выхода подсказки"
),
Jinja("Введи новое время выхода подсказки"),
MessageInput(func=process_edit_time_message),
getter=get_hints,
state=states.TimeHintEditSG.time,
Expand Down
1 change: 0 additions & 1 deletion shvatka/tgbot/dialogs/time_hint/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from dataclass_factory import Factory

from shvatka.core.models.dto.scn.hint_part import AnyHint
from shvatka.tgbot.views.utils import render_hints


async def get_available_times(dialog_manager: DialogManager, **_):
Expand Down
13 changes: 13 additions & 0 deletions shvatka/tgbot/dialogs/time_hint/handlers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from typing import Any

from aiogram import types
from aiogram.types import CallbackQuery, Message
from aiogram_dialog import DialogManager
from aiogram_dialog.widgets.kbd import Button
from dataclass_factory import Factory
from dishka import AsyncContainer
from dishka.integrations.aiogram import CONTAINER_NAME

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.tgbot import states
from shvatka.tgbot.views.hint_factory.hint_parser import HintParser
from shvatka.tgbot.views.hint_sender import HintSender


async def select_time(c: CallbackQuery, widget: Any, manager: DialogManager, item_id: str):
Expand Down Expand Up @@ -38,6 +42,15 @@ async def process_time_message(m: Message, dialog_: Any, manager: DialogManager)
await m.answer("Время выхода данной подсказки должно быть больше, чем предыдущей")


async def edit_single_hint(c: CallbackQuery, widget: Any, manager: DialogManager, hint_index: str):
dishka: AsyncContainer = manager.middleware_data[CONTAINER_NAME]
dcf = await dishka.get(Factory)
hint = dcf.load(manager.start_data.get("time_hint"), scn.TimeHint)
hint_sender = await dishka.get(HintSender)
chat: types.Chat = manager.middleware_data["event_from_chat"]
await hint_sender.send_hint(hint.hint[int(hint_index)], chat.id)


async def set_time(time_minutes: int, manager: DialogManager):
if time_minutes <= int(manager.start_data["previous_time"]):
raise ValueError("Время меньше предыдущего")
Expand Down
7 changes: 6 additions & 1 deletion shvatka/tgbot/views/jinja_filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from .boolean_emoji import bool_render
from .game_status import to_readable_name
from .timezone import datetime_filter, timedelta_filter
from ..utils import render_single_hint, render_hints, render_time_hint, render_time_hints
from shvatka.tgbot.views.utils import (
render_single_hint,
render_hints,
render_time_hint,
render_time_hints,
)


def setup_jinja(bot: Bot):
Expand Down

0 comments on commit 6c1cfd9

Please sign in to comment.