talkomatic.py 0.1.2
An easy-to-use Python versatile wrapper for the Talkomatic Bot interface ensuring best practices for making bots, and handling the REST API.
 
Loading...
Searching...
No Matches
room.py
Go to the documentation of this file.
1from .user import User
2
3from dataclasses import dataclass
4from enum import Enum
5
6
7class RoomType(Enum):
8 """
9 The visibility of the room.
10
11 Composed of:
12 - PUBLIC: The room is public and can be joined by anyone, with no password required.
13 - SEMI_PRIVATE: The room **can be seen** on the room list but can only be joined with a password.
14 - PRIVATE: The room **cannot be seen** on the room list and can only be joined with a password.
15 """
16
17 PUBLIC = "public"
18 SEMI_PRIVATE = "semi-private"
19 PRIVATE = "private"
20
21class RoomLayoutType(Enum):
22 """
23 Whether the textboxes of the user in the room are stacked vertically or horizontally.
24
25 Composed of:
26 - VERTICAL: The textboxes of the users in the room are stacked vertically.
27 - HORIZONTAL: The textboxes of the users in the room are stacked horizontally.
28 """
29
30 VERTICAL = "vertical"
31 HORIZONTAL = "horizontal"
32
33@dataclass
34class Room:
35 """
36 A class representing room information.
37
38 Composed of:
39 - room_id (int): The ID of the room.
40 - name (str): The name of the room.
41 - room_type (RoomType): The type of the room.
42 - layout (RoomLayoutType): The layout of the room.
43 - users (list[User]): The users in the room.
44 - votes (dict[User, list[User]]): The votes in the room.
45 - banned_users (list[User]): The banned users in the room.
46 - last_time_active (int): The last time the room was active.
47 - is_full (bool): Whether the room is full.
48 """
49
50 room_id: int
51 name: str
52 room_type: RoomType
53 layout: RoomLayoutType | None
54 users: list[User]
55 votes: dict[User, list[User]] | None
56 banned_users: list[User] | None
57 last_time_active: int | None
58 is_full: bool | None
59
60 @classmethod
61 def _parse_votes(cls, data: dict) -> dict[User, list[User]]:
62 # HACK: votes are formatted with {"voted user id": "voter user id"}, we need to reformat them
63 # with "id-only" user objects (users only used to compare)
64 votes = {}
65 for voted_user_id, voter_user_id in data["votes"].items():
66 if voted_user_id not in data["users"]:
67 votes[voted_user_id] = [voter_user_id]
68 else:
69 votes[voted_user_id].append(voter_user_id)
70 return {User.from_id_only(voted): list(map(User.from_id_only, voters)) for voted, voters in votes.items()}
71
72 @classmethod
73 def from_raw_json(cls, data: dict) -> "Room":
74 return cls(
75 room_id = int(data["id" if "id" in data else "roomId"]),
76 name = data["name" if "name" in data else "roomName"],
77 room_type = RoomType(data["type" if "type" in data else "roomType"]),
78 layout = RoomLayoutType(data["layout"]) if "layout" in data else None,
79 users = [User.from_raw_json(user) for user in data["users"]],
80 votes = cls._parse_votes(data) if "votes" in data else None,
81 banned_users = list(map(User.from_id_only, data["bannedUserIds"])) if "bannedUserIds" in data else None,
82 last_time_active = data["lastActiveTime"] if "lastActiveTime" in data else None,
83 is_full = data["isFull"] if "isFull" in data else None
84 )
85
86 def __eq__(self, other: "Room") -> bool:
87 if not isinstance(other, Room):
88 return False
89
90 return self.room_id == other.room_id
91
92 def __ne__(self, other: "Room") -> bool:
93 return not self.__eq__(other)
94
95 def __str__(self) -> str:
96 return f"""Room "{self.name}" (id: {self.room_id}):
97 - room type: {self.room_type.value}
98 - layout: {self.layout if self.layout else "N/A"}
99 - users: {", ".join(map(str, self.users))}
100 - votes: {", ".join([f"{voted} -> {map(str, voters)}" for voted, voters in self.votes.items()]) if self.votes else "N/A"}
101 - banned users: {", ".join(map(str, self.banned_users)) if self.banned_users else "N/A"}
102 - last time active (epoch time): {self.last_time_active if self.last_time_active else "N/A"}
103 - is full: {self.is_full}"""
104
105
106__all__ = ["Room", "RoomType", "RoomLayoutType"]
bool __eq__(self, "Room" other)
Definition room.py:86
"Room" from_raw_json(cls, dict data)
Definition room.py:73
dict[User, list[User]] _parse_votes(cls, dict data)
Definition room.py:61
bool __ne__(self, "Room" other)
Definition room.py:92