Skip to content

Game registry

Registry managing active Werewolf games per Discord guild.

This class acts as a central storage for all running games, indexed by guild ID. It ensures that only one game instance exists per guild.

Source code in sources/bot/game_registry.py
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
66
class GameRegistry:
    """Registry managing active Werewolf games per Discord guild.

    This class acts as a central storage for all running games,
    indexed by guild ID. It ensures that only one game instance
    exists per guild.
    """

    def __init__(self, bot: "DiscordClient", settings: GameSettings | None = None) -> None:
        self.bot = bot
        self.settings = settings or GameSettings()
        self.games: dict[int, GameManager] = {}
        # One lock per guild: the setup of a guild must not wait for another one
        self._create_locks: defaultdict[int, asyncio.Lock] = defaultdict(asyncio.Lock)

    def get(self, guild_id: int) -> GameManager | None:
        return self.games.get(guild_id)

    async def create(self, guild: discord.Guild) -> GameManager:
        """Return the guild's game, creating and setting it up if needed."""
        game, _ = await self.get_or_create(guild)
        return game

    async def get_or_create(self, guild: discord.Guild) -> tuple[GameManager, bool]:
        """Return the guild's game and whether this call created it.

        Creation is serialized per guild so that concurrent calls for the same
        guild cannot set up two games while the first setup is still running,
        while different guilds are set up in parallel. Only the call that
        actually created the game gets True.
        """
        async with self._create_locks[guild.id]:
            if guild.id in self.games:
                return self.games[guild.id], False

            game = GameManager(guild=guild, bot=self.bot, settings=self.settings)
            await game.setup()
            if self.bot.get_guild(guild.id) is None:
                # Left during the setup: on_guild_remove found no game to drop
                raise GuildLeftError(guild.id)
            self.games[guild.id] = game
            return game, True

    def delete(self, guild_id: int) -> None:
        self.games.pop(guild_id, None)

create(guild) async

Return the guild's game, creating and setting it up if needed.

Source code in sources/bot/game_registry.py
40
41
42
43
async def create(self, guild: discord.Guild) -> GameManager:
    """Return the guild's game, creating and setting it up if needed."""
    game, _ = await self.get_or_create(guild)
    return game

get_or_create(guild) async

Return the guild's game and whether this call created it.

Creation is serialized per guild so that concurrent calls for the same guild cannot set up two games while the first setup is still running, while different guilds are set up in parallel. Only the call that actually created the game gets True.

Source code in sources/bot/game_registry.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
async def get_or_create(self, guild: discord.Guild) -> tuple[GameManager, bool]:
    """Return the guild's game and whether this call created it.

    Creation is serialized per guild so that concurrent calls for the same
    guild cannot set up two games while the first setup is still running,
    while different guilds are set up in parallel. Only the call that
    actually created the game gets True.
    """
    async with self._create_locks[guild.id]:
        if guild.id in self.games:
            return self.games[guild.id], False

        game = GameManager(guild=guild, bot=self.bot, settings=self.settings)
        await game.setup()
        if self.bot.get_guild(guild.id) is None:
            # Left during the setup: on_guild_remove found no game to drop
            raise GuildLeftError(guild.id)
        self.games[guild.id] = game
        return game, True