79 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
| """Has a single class: Game_cog"""
 | |
| 
 | |
| # standard library imports
 | |
| from typing import Dict, Type
 | |
| 
 | |
| # discord imports
 | |
| import discord
 | |
| 
 | |
| # local imports
 | |
| from ..send_message import Send_message
 | |
| from .game import Game
 | |
| 
 | |
| 
 | |
| class Game_cog(Send_message):
 | |
| 	"""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_cls: Type[Game]):
 | |
| 		self.bot = bot
 | |
| 		self.game_cls = game_cls
 | |
| 		self.game_instances = Dict[discord.TextChannel, self.game_cls]
 | |
| 
 | |
| 	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
 | |
| 
 | |
| 	async def setup(self, ctx):
 | |
| 		"""This function creates an game instance for this channel"""
 | |
| 		if ctx.channel in self.game_instances:
 | |
| 			await self.send_wrong(ctx, f"A game '{self.game_cls.name}' is already setup in this channel")
 | |
| 		else:
 | |
| 			self.game_instances[ctx.channel] = self.game_cls(self.bot, ctx.channel)
 | |
| 			await self.send_friendly(ctx, f"This channel can now play: {self.game_cls.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):
 | |
| 		"""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 {self.game_cls.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)
 | |
| 
 | |
| 	async def pre_game_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
 | |
| 
 | |
| 	async def in_game_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")
 |