lot's of refactoring and packaging
This commit is contained in:
parent
9cdc8d742c
commit
eff057da8c
17 changed files with 190 additions and 134 deletions
90
src/package/games/game_cog.py
Normal file
90
src/package/games/game_cog.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
"""This (abstract) module is a template for Discord Cog's for game specific channel commands"""
|
||||
|
||||
# standard library imports
|
||||
from typing import Dict
|
||||
|
||||
# discord imports
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
# local imports
|
||||
from ..send_message import Send_message
|
||||
from .game import Game
|
||||
|
||||
|
||||
# TODO: take group as argument to add subcommands
|
||||
|
||||
class Game_cog(Send_message, commands.Cog):
|
||||
"""This (abstract) class is are common function for the Game Cog's (setup-game, pre-game, in-game), mainly has checker functions"""
|
||||
|
||||
def __init__(self, bot, game_instances: Dict[discord.TextChannel, Game]):
|
||||
self.bot = bot
|
||||
self.game_instances = game_instances
|
||||
|
||||
async def setup_check(self, ctx):
|
||||
if ctx.channel not in self.game_instances:
|
||||
await self.send_wrong(ctx, f"The channel is not setup yet.")
|
||||
return ctx.channel in self.game_instances
|
||||
|
||||
async def not_running_check(self, ctx):
|
||||
if self.game_instances[ctx.channel].running:
|
||||
await self.send_wrong(ctx, "Sorry! A game is already running")
|
||||
return not self.game_instances[ctx.channel].running
|
||||
|
||||
async def running_check(self, ctx):
|
||||
if not self.game_instances[ctx.channel].running:
|
||||
await self.send_wrong(ctx, "No game is running")
|
||||
return self.game_instances[ctx.channel].running
|
||||
|
||||
|
||||
class Setup_game_cog(Game_cog):
|
||||
"""This (abstract) class is a template for Discord Cog's for game specific channel commands for setting up the channel"""
|
||||
|
||||
async def setup(self, ctx, game: Game):
|
||||
"""This function creates an game instance for this channel"""
|
||||
if ctx.channel in self.game_instances:
|
||||
await self.send_wrong(ctx, f"A game '{game.name}' is already setup in this channel")
|
||||
else:
|
||||
self.game_instances[ctx.channel] = game(self.bot, ctx.channel)
|
||||
await self.send_friendly(ctx, f"This channel can now play: {game.name}")
|
||||
|
||||
async def reset(self, ctx):
|
||||
"""This function deletes the game instance for this channel"""
|
||||
if self.setup_check(ctx):
|
||||
del self.game_instances[ctx.channel]
|
||||
|
||||
# TODO: better info message
|
||||
async def info(self, ctx, game: Game):
|
||||
"""Send information about the subcommands for the game"""
|
||||
embed = discord.Embed(title="How to play?", description="You will need to set up the game and its information in a channel and start the game there. Afterwards the player mainly interact with the bot in DM.", color=0x00ffff)
|
||||
embed.set_author(name=f"With this bot you can play {game.name}")
|
||||
# embed.set_thumbnail(url="https://images-na.ssl-images-amazon.com/images/I/717GrDtFKCL._AC_SL1000_.jpg")
|
||||
embed.add_field(name="$w game setup", value="Make this channel playable.", inline=False)
|
||||
embed.add_field(name="$w game players", value="Set mentioned users as players", inline=False)
|
||||
embed.add_field(name="$w game roles", value="Set the roles to play with", inline=False)
|
||||
embed.add_field(name="$w game start", value="Play one round", inline=False)
|
||||
embed.set_footer(text="Have fun!")
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
|
||||
class Pre_game_cog(Game_cog):
|
||||
"""This (abstract) class is a template for Discord Cog's for game specific channel commands for setting up the game rounds"""
|
||||
async def cog_check(self, ctx):
|
||||
return self.setup_check(ctx) and self.not_running_check(ctx)
|
||||
|
||||
async def players(self, ctx):
|
||||
await self.game_instances[ctx.channel].set_players(ctx.message)
|
||||
|
||||
async def start(self, ctx):
|
||||
self.game_instances[ctx.channel].game = self.bot.loop.create_task(self.game_instances[ctx.channel].round())
|
||||
await self.game_instances[ctx.channel].game
|
||||
|
||||
|
||||
class In_game_goc(Game_cog):
|
||||
"""This (abstract) class is a template for Discord Cog's for game specific channel commands during the game"""
|
||||
async def cog_check(self, ctx):
|
||||
return self.setup_check(ctx) and self.running_check(ctx)
|
||||
|
||||
async def stop(self, ctx):
|
||||
self.game_instances[ctx.channel].game.cancel()
|
||||
await self.send_friendly(ctx, "Game canceled")
|
||||
Loading…
Add table
Add a link
Reference in a new issue