Commit bec41e9b authored by Ferdinand Schober's avatar Ferdinand Schober 💬
Browse files

adding a number system for quicker userinputs

parent 56755ad0
from discord.ext import commands
from discord import Permissions, PermissionOverwrite, Member, utils, Colour
from discord.ext.commands import CommandInvokeError, CommandError
from asyncio import create_task, Event, Lock, gather
from functools import wraps
from ..roles import Amor, Girl, Hunter, Seer, Villager, Werewolf, Whore, Witch, Role
from ..game import Game
def delete_command_message(command):
@wraps(command)
async def wrapper(self, ctx, *args, **kwargs):
await command(self, ctx, *args, **kwargs)
await ctx.message.delete()
return wrapper
def is_alive(ctx):
if ctx.author not in ctx.cog.game[ctx.guild].alive:
raise CommandError('You must be alive to invoke this command!')
return True
def is_not_alive(ctx):
if ctx.author in ctx.cog.game[ctx.guild].alive:
raise CommandError('You must be dead to invoke this command!')
return True
def args_alive(command):
@wraps(command)
async def wrapper(self, ctx, *args, **kwargs):
for arg in args:
if type(arg) == Member and arg not in ctx.cog.game[ctx.guild].alive:
raise CommandError(f'Member {arg.mention} lebt nicht mehr!')
for arg in kwargs.values():
if type(arg) == Member and arg not in ctx.cog.game[ctx.guild].alive:
raise CommandError(f'Member {arg.mention} lebt nicht mehr!')
await command(self, ctx, *args, **kwargs)
return wrapper
def is_role(Role):
def is_role(ctx):
if ctx.cog.game[ctx.guild].role_of_type.get(Role) == None:
raise CommandError(f'Die Rolle "{Role.get_name()}" ist nicht im Spiel!')
if ctx.author not in ctx.cog.game[ctx.guild].role_of_type[Role].members:
raise CommandError(f'Deine Rolle ist nicht {Role.get_name()}, {ctx.author.mention}!')
return True
return is_role
def is_running(ctx):
if not ctx.cog.running[ctx.guild]:
raise CommandInvokeError('Game ist not running')
return True
class WerwolfCommands(commands.Cog):
def __init__(self, client):
self.client = client
self.init_event = Event()
self.game = dict()
self.running = dict()
self.lock = Lock()
@commands.Cog.listener()
async def on_ready(self):
for guild in self.client.guilds:
self.game[guild] = None
self.running[guild] = False
self.init_event.set()
async def create_channels(self, guild, channel_names, role_alive, role_dead):
if not self.lock.locked():
async with self.lock:
overwrites = {
role_alive : PermissionOverwrite(read_messages=False, send_messages=False),
role_dead : PermissionOverwrite(read_messages=True, send_messages=False),
}
ww_category = await guild.create_category('werwolf')
await gather(*[ww_category.create_text_channel(name, overwrites=overwrites) for name in channel_names])
await ww_category.create_voice_channel('talk')
dead_talk = await ww_category.create_voice_channel('dead')
await dead_talk.set_permissions(role_alive, connect=False)
await utils.get(ww_category.text_channels, name='dorf').set_permissions(role_alive, read_messages=True, send_messages=True)
return ww_category
@commands.command()
async def clear_channels(self, ctx):
await self.uninstall(ctx.guild)
async def uninstall(self, guild):
if not self.lock.locked():
async with self.lock:
ww_category = utils.get(guild.categories, name='werwolf')
if not ww_category: return
await gather(*[channel.delete() for channel in ww_category.channels])
await ww_category.delete()
@commands.command()
async def start(self, ctx):
async with ctx.channel.typing():
await self.init_event.wait()
if self.running[ctx.guild]:
await ctx.send('Spiel läuft bereits!')
else:
async with ctx.channel.typing():
self.running[ctx.guild] = True
lobby = utils.get(ctx.guild.channels, name='lobby')
members = lobby.members
role_alive = utils.get(ctx.guild.roles, name='lebendig')
role_dead = utils.get(ctx.guild.roles, name='tot')
if not role_alive:
role_alive = await ctx.guild.create_role(name='lebendig', color=Colour.green(), hoist=True, mentionable=True)
if not role_dead:
role_dead = await ctx.guild.create_role(name='tot', color=Colour.red(), hoist=True, mentionable=True)
#roles = [Girl, Werewolf, Hunter, Witch, Whore, Seer]
roles = [Werewolf, Hunter, Witch, Whore, Amor, Werewolf, Girl, Seer, Villager, Werewolf]
category = await self.create_channels(ctx.guild, ['info', 'dorf'] + [role.get_channel_name() for role in set(roles[:len(members)]) ] + ['liebespaar'], role_alive, role_dead)
#await gather(*[member.move_to(category.voice_channels[0]) for member in members])
self.game[ctx.guild] = Game(category, members, roles, role_alive, role_dead)
await self.game[ctx.guild].play(ctx)
self.running[ctx.guild] = False
#await gather(*[member.move_to(lobby) for member in members])
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
await ctx.send(str(error))
print(error)
@commands.Cog.listener()
async def on_message(self, message):
game = self.game[message.guild]
if not game:
return
role = game.role_of_member.get(message.author)
if not role:
return
if message.author not in self.game.alive:
return
await role.command(message.author, message.content)
def setup(client):
client.add_cog(WerwolfCommands(client))
import itertools
import random
from asyncio import Event, Lock
from abc import ABC, abstractmethod
from discord import Embed
from discord import utils
def distribute_roles(game, members, roles):
random.shuffle(members)
members_for_role = { role : set() for role in roles[:len(members)] }
for member, role in zip(members, roles):
members_for_role[role].add(member)
roles = [ role(game, members_for_role[role]) for role in members_for_role.keys() ]
return { member : role for role in roles for member in role.members }
class Role(ABC):
@classmethod
@abstractmethod
def get_name(cls): return None
@classmethod
@abstractmethod
def get_channel_name(cls): return None
@classmethod
@abstractmethod
def get_team(cls): return None
@classmethod
@abstractmethod
def get_max_members(cls): return None
@classmethod
@abstractmethod
def get_command_help_embed(cls): return None
def info(self):
pass
def __init__(self, game, members):
if self.get_max_members() != None and len(members) > self.get_max_members():
raise Exception(f'There can only be {self.get_max_members()} members with the role {self.get_name()}')
self.game = game
self.members = set(members)
self.channel = utils.get(game.category.text_channels, name=self.get_channel_name())
if not self.channel:
raise Exception(f'Channel {self.get_channel_name()} not found')
self.event = Event()
self.lock = Lock()
async def on_kill(self, member): pass
async def on_night_begin(self):
self.event.clear()
await self.channel.send(f'Die {self.game.round}te Nacht Beginnt...')
async def help(self):
await self.channel.send(embed=Embed.from_dict(self.get_command_help_embed()))
async def task(self):
await self.task_()
self.notify()
@classmethod
async def command(cls, message, member):
pass
@abstractmethod
async def task_(self): pass
async def on_night_end(self): pass
async def wait(self):
await self.event.wait()
async def wait_for(self, role):
role = self.game.role_of_type.get(role)
if role:
await role.wait()
def notify(self):
self.event.set()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment