From caca8bc2e5917e2e22a5fcfc16e6e0d55e7d4a04 Mon Sep 17 00:00:00 2001 From: bibin Date: Wed, 15 Apr 2020 02:04:37 +0200 Subject: [PATCH] added stop command to restart round --- src/werewolf_bot.py | 25 ++++++++++++++++++++++--- src/werewolf_game.py | 2 +- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/werewolf_bot.py b/src/werewolf_bot.py index 0f398a3..4c5ee4a 100644 --- a/src/werewolf_bot.py +++ b/src/werewolf_bot.py @@ -70,12 +70,21 @@ async def setup(ctx): await send_friendly(ctx, "This channel can now play Werewolf") -def game_running(command): +def channel_setup(command): @functools.wraps(command) async def wrapper(ctx): if ctx.channel not in game_instances: await send_wrong(ctx, f"No game setup yet. Use {PREFIX}game setup") - elif game_instances[ctx.channel].running: + else: + await command(ctx) + return wrapper + + +def game_running(command): + @functools.wraps(command) + @channel_setup + async def wrapper(ctx): + if game_instances[ctx.channel].running: await send_wrong(ctx, "Sorry! A game is already running") else: await command(ctx) @@ -98,7 +107,17 @@ def error_handling(command): @game_running @error_handling async def start(ctx): - await game_instances[ctx.channel].game() + game_instances[ctx.channel].game = bot.loop.create_task(game_instances[ctx.channel].round()) + + +@game.command() +@channel_setup +async def stop(ctx): + if not game_instances[ctx.channel].running: + await send_wrong(ctx, "No game is running") + else: + game_instances[ctx.channel].game.cancel() + await send_friendly(ctx, "Game canceled") @game.command() diff --git a/src/werewolf_game.py b/src/werewolf_game.py index 8abf581..4f5388f 100644 --- a/src/werewolf_game.py +++ b/src/werewolf_game.py @@ -165,7 +165,7 @@ class Game: def end(self): self.running = False - async def game(self): + async def round(self): try: self.check() self.running = True