1 | # This file is part of python-cinema-club-bot
|
---|
2 | # contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
|
---|
3 |
|
---|
4 | # To the extent possible under law, the author(s) have dedicated all copyright
|
---|
5 | # and related and neighboring rights to this software to the public domain
|
---|
6 | # worldwide. This software is distributed without any warranty.
|
---|
7 |
|
---|
8 | # You should have received a copy of the CC0 Public Domain Dedication along
|
---|
9 | # with this software. If not, see:
|
---|
10 | # <http://creativecommons.org/publicdomain/zero/1.0/>
|
---|
11 |
|
---|
12 | from telegram.ext import ContextTypes
|
---|
13 | from collections import deque
|
---|
14 | from typing import Optional
|
---|
15 |
|
---|
16 | def context_init(context: ContextTypes.DEFAULT_TYPE):
|
---|
17 | """
|
---|
18 | Initialize chat context with starting values
|
---|
19 | """
|
---|
20 |
|
---|
21 | if "users" not in context.chat_data:
|
---|
22 | context.chat_data["users"]: list[str] = []
|
---|
23 |
|
---|
24 | if "movies" not in context.chat_data:
|
---|
25 | context.chat_data["movies"]: list[dict] = []
|
---|
26 |
|
---|
27 | if "events" not in context.chat_data:
|
---|
28 | context.chat_data["events"]: list[dict] = []
|
---|
29 |
|
---|
30 | return context
|
---|
31 |
|
---|
32 |
|
---|
33 | def normalize_username(username: str):
|
---|
34 | return username.replace("@", "")
|
---|
35 |
|
---|
36 |
|
---|
37 | def create_users_string(users: list[str]) -> str:
|
---|
38 | return "`" + ", ".join(users) + "`"
|
---|
39 |
|
---|
40 |
|
---|
41 | def choose_next_user(context: ContextTypes.DEFAULT_TYPE) -> list[dict]:
|
---|
42 | users = deque(context.chat_data["users"])
|
---|
43 | users.rotate(-1) # -1 moves list to left by 1 element
|
---|
44 |
|
---|
45 | init_new_event(context, users[0])
|
---|
46 |
|
---|
47 | return list(users)
|
---|
48 |
|
---|
49 |
|
---|
50 | def init_new_event(context: ContextTypes.DEFAULT_TYPE, user: dict, movie: Optional[str] = None):
|
---|
51 | events = context.chat_data["events"]
|
---|
52 |
|
---|
53 | last_event = events[-1] if events != [] else None
|
---|
54 |
|
---|
55 | init_event = dict(
|
---|
56 | when=None,
|
---|
57 | where=None,
|
---|
58 | movie=movie,
|
---|
59 | user=user
|
---|
60 | )
|
---|
61 |
|
---|
62 | if last_event and last_event["movie"] is None:
|
---|
63 | events[-1] = init_event
|
---|
64 | else:
|
---|
65 | events.append(init_event)
|
---|