summaryrefslogtreecommitdiff
path: root/utils.py
blob: c9a549547204ed7cae27afebea19ae4e9827c606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# This file is part of python-cinema-club-bot
# contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>

# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.

# You should have received a copy of the CC0 Public Domain Dedication along
# with this software. If not, see:
# <http://creativecommons.org/publicdomain/zero/1.0/>

from telegram.ext import ContextTypes
from collections import deque
from typing import Optional

def context_init(context: ContextTypes.DEFAULT_TYPE):
    """
    Initialize chat context with starting values
    """

    if "users" not in context.chat_data:
        context.chat_data["users"]: list[str] = []

    if "movies" not in context.chat_data:
        context.chat_data["movies"]: list[dict] = []

    if "events" not in context.chat_data:
        context.chat_data["events"]: list[dict] = []

    return context


def normalize_username(username: str):
    return username.replace("@", "")


def create_users_string(users: list[str]) -> str:
    return "`" + ", ".join(users) + "`"


def choose_next_user(context: ContextTypes.DEFAULT_TYPE) -> list[dict]:
    users = deque(context.chat_data["users"])
    users.rotate(-1)  # -1 moves list to left by 1 element

    init_new_event(context, users[0])

    return list(users)


def init_new_event(context: ContextTypes.DEFAULT_TYPE, user: dict, movie: Optional[str] = None):
    events = context.chat_data["events"]

    last_event = events[-1] if events != [] else None

    init_event = dict(
        when=None,
        where=None,
        movie=movie,
        user=user
    )

    if last_event and last_event["movie"] is None:
        events[-1] = init_event
    else:
        events.append(init_event)