From 14037bf5d8a51847db12824bb0a2bca7cc200cbc Mon Sep 17 00:00:00 2001 From: bibin Date: Wed, 15 Jul 2020 18:32:29 +0200 Subject: [PATCH] small fixes + correct checks --- src/package/developer.py | 2 +- src/package/games/game_cog.py | 15 ++++++++++----- src/package/games/werewolf/cog.py | 19 ++++++++++--------- src/package/games/werewolf/game.py | 3 ++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/package/developer.py b/src/package/developer.py index c54a73a..b572206 100644 --- a/src/package/developer.py +++ b/src/package/developer.py @@ -13,7 +13,7 @@ class Developer(commands.Cog): await self.bot.change_presence(status=discord.Status.online, activity=discord.Game('One Night Ultimate Werewolf')) print('We have logged in as {0.user}'.format(self.bot)) - async def is_dev(ctx): + async def is_dev(self, ctx): if ctx.author.id == 461892912821698562: return True await ctx.send("This command is not for you!") diff --git a/src/package/games/game_cog.py b/src/package/games/game_cog.py index 7c9083e..4744b5a 100644 --- a/src/package/games/game_cog.py +++ b/src/package/games/game_cog.py @@ -5,6 +5,7 @@ from typing import Dict, Type # discord imports import discord +from discord.ext import commands # local imports from ..send_message import Send_message @@ -17,7 +18,7 @@ class Game_cog(Send_message): def __init__(self, bot, game_cls: Type[Game]): self.bot = bot self.game_cls = game_cls - self.game_instances = Dict[discord.TextChannel, self.game_cls] + self.game_instances = {} # TODO: type hint? Dict[discord.TextChannel, self.game_cls] async def setup_check(self, ctx): if ctx.channel not in self.game_instances: @@ -60,8 +61,10 @@ class Game_cog(Send_message): embed.set_footer(text="Have fun!") await ctx.send(embed=embed) - async def pre_game_check(self, ctx): - return self.setup_check(ctx) and self.not_running_check(ctx) + def pre_game(): + async def predicate(ctx): + return await ctx.cog.setup_check(ctx) and await ctx.cog.not_running_check(ctx) + return commands.check(predicate) async def players(self, ctx): await self.game_instances[ctx.channel].set_players(ctx.message) @@ -70,8 +73,10 @@ class Game_cog(Send_message): self.game_instances[ctx.channel].game = self.bot.loop.create_task(self.game_instances[ctx.channel].round()) await self.game_instances[ctx.channel].game - async def in_game_check(self, ctx): - return self.setup_check(ctx) and self.running_check(ctx) + def in_game(): + async def predicate(ctx): + return await ctx.cog.setup_check(ctx) and await ctx.cog.running_check(ctx) + return commands.check(predicate) async def stop(self, ctx): self.game_instances[ctx.channel].game.cancel() diff --git a/src/package/games/werewolf/cog.py b/src/package/games/werewolf/cog.py index d210fd1..de02287 100644 --- a/src/package/games/werewolf/cog.py +++ b/src/package/games/werewolf/cog.py @@ -19,12 +19,12 @@ class Werewolf_cog(Game_cog, commands.Cog): @werewolf.command() async def info(self, ctx): """Send information about the subcommands for the game""" - await super().info(ctx, Werewolf_game) + await super().info(ctx) @werewolf.command() async def setup(self, ctx): """This function creates an game instance for this channel""" - await super().setup(ctx, Werewolf_game) + await super().setup(ctx) @werewolf.command() async def reset(self, ctx): @@ -32,37 +32,38 @@ class Werewolf_cog(Game_cog, commands.Cog): await super().reset(ctx) @werewolf.command() - @commands.check(Game_cog.pre_game_check) + @Game_cog.pre_game() async def players(self, ctx): """registers all mentioned players for the game""" await super().players(ctx) @werewolf.command() - @commands.check(Game_cog.pre_game_check) + @Game_cog.pre_game() async def roles(self, ctx, *args): """registers roles you want to play with""" await self.game_instances[ctx.channel].set_roles(args) @werewolf.command() - @commands.check(Game_cog.pre_game_check) - async def minutes(self, ctx, i): + @Game_cog.pre_game() + async def minutes(self, ctx, i: int): """set discussion time""" await self.game_instances[ctx.channel].set_time(i) @werewolf.command() - @commands.check(Game_cog.pre_game_check) + @Game_cog.pre_game() async def start(self, ctx): + print(await self.setup_check(ctx), await self.not_running_check(ctx)) """starts a round of werewolf""" await super().start(ctx) @werewolf.command() - @commands.check(Game_cog.in_game_check) + @Game_cog.in_game() async def stop(self, ctx): """aborts the current round of werewolf""" await super().stop(ctx) @werewolf.command() - @commands.check(Game_cog.in_game_check) + @Game_cog.in_game() async def time(self, ctx): """checks how much discussion time there is left""" await self.send_friendly(ctx, self.game_instances[ctx.channel].remaining_time_string()) diff --git a/src/package/games/werewolf/game.py b/src/package/games/werewolf/game.py index 34fe62d..2be4aab 100644 --- a/src/package/games/werewolf/game.py +++ b/src/package/games/werewolf/game.py @@ -39,6 +39,7 @@ class Werewolf_game: await self.send(f"You have set the discussion time to {self.discussion_time//60} minutes") # send confirmation def check(self): + # TODO: min. player if not 0 <= len(self.player_list) <= 10: raise ValueError(f"Invalid number of players: {len(self.player_list)}") if not len(self.role_list) == (len(self.player_list) + 3): @@ -62,7 +63,7 @@ class Werewolf_game: shuffle(self.role_list) for i in range(len(self.player_list)): role_obj = self.role_list[i](self, self.player_list[i]) - self.player_list[i].setRole(role_obj) + self.player_list[i].set_role(role_obj) role_obj.add_yourself() self.middle_card = [r(self) for r in self.role_list[-3:]]