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

almost finished

parent bfccf5bf
from roles import Role
import asyncio
liebespaar_id = 696323604605960213
class Amor(Role):
@classmethod
def get_name(cls): return 'Amor'
@classmethod
def get_channel_id(cls): return 697047699006029884
def __init__(self, game):
Role.__init__(self, game)
self.amor_event = asyncio.Event()
async def task(self):
if not self.amor_event.is_set():
self.channel.send('Wähle das Liebespaar, mit $love @p1 @p2')
await self.amor_event.wait()
async def love(self, p1, p2):
if p1 == p2:
self.game.amor_channel.send('Da muss ich dich enttäuschen, das Liebespaar muss aus zwei verschiedenen Personen bestehen!')
self.game.couple = { p1, p2 }
await self.game.server.get_channel(liebespaar_id).set_permissions(p1, read_messages=True, send_messages=True)
await self.game.server.get_channel(liebespaar_id).set_permissions(p2, read_messages=True, send_messages=True)
self.amor_event.set()
\ No newline at end of file
import discord
import asyncio
from pprint import pprint
from discord.ext import commands
client = commands.Bot(command_prefix='.')
client.load_extension('werewolf_commands')
client.load_extension('utility_commands')
client.load_extension('cog_werewolf')
client.run('Njk2MTEyNTQ1MDgxMDY1NTU0.Xoj_iQ.VXMY9AUo56o_k-QgLSUWtPldhjE')
\ No newline at end of file
from discord.ext import commands
import asyncio
import random
import roles
from discord import Permissions
from discord import Member
from werewolf import Werewolf
from villager import Villager
from seer import Seer
from witch import Witch
from hunter import Hunter
from amor import Amor
from whore import Whore
from girl import Girl
from game import Game
welcome_id = 696079221684175005
lobby_id = 696130304884211733
guild_id = 696079221684175001
role_alive_id = 697048340889731134
role_dead_id = 697048466978897990
village_id = 696105930336632963
class Werwolf(commands.Cog):
def __init__(self, client):
self.client = client
self.init_event = asyncio.Event()
self.game_event = asyncio.Event()
@commands.Cog.listener()
async def on_message(self, message):
await self.init_event.wait()
if message.channel.id == Werewolf.get_channel_id():
await self.server.get_channel(Girl.get_channel_id()).send(message.content)
@commands.Cog.listener()
async def on_ready(self):
self.server = self.client.guilds[0]
self.role_alive = self.server.get_role(role_alive_id)
self.role_dead = self.server.get_role(role_dead_id)
self.welcome_channel = self.server.get_channel(welcome_id)
self.lobby_channel = self.server.get_channel(lobby_id)
self.village_channel = self.server.get_channel(village_id)
self.init_event.set()
await self.welcome_channel.send('[status] ready')
@commands.command()
async def start(self, ctx):
await self.init_event.wait()
print(self.game_event.is_set())
if self.game_event.is_set():
await ctx.send('Spiel läuft bereits!')
return
self.game_event.set()
members = self.server.get_channel(lobby_id).members
random.shuffle(members)
self.game = Game()
await self.game.play(members, self.server, self.role_alive, self.role_dead, self.village_channel)
self.game_event.clear()
@commands.command()
async def accuse(self, ctx, player:Member):
await self.game.accuse(ctx, player)
@commands.command()
async def confirm(self, ctx):
await self.game.confirm(ctx)
@commands.command()
async def vote(self, ctx, player:Member):
if ctx.channel.id == Werewolf.get_channel_id():
await self.game.role[ctx.message.author].vote(player)
elif ctx.channel == self.village_channel:
await self.game.vote(ctx, ctx.message.author, player)
@commands.command()
async def captain(self, ctx):
await self.village_channel.send(f'Captain: {self.game.captain}')
@commands.command()
async def heal(self, ctx):
if ctx.channel.id == Witch.get_channel_id():
self.game.role[ctx.message.author].heal()
@commands.command()
async def poison(self, ctx, player:Member):
if ctx.channel.id == Witch.get_channel_id():
self.game.role[ctx.message.author].poison(player)
@commands.command()
async def see(self, ctx, player:Member):
if ctx.channel.id == Seer.get_channel_id():
self.game.role[ctx.message.author].see(player)
@commands.command()
async def kill(self, ctx, player:Member):
if isinstance(self.game.role[ctx.message.author], Hunter):#TODO hunter only kill when dead!
await self.game.role[ctx.message.author].kill(player)
@commands.command()
async def love(self, ctx, p1:Member, p2:Member):
if ctx.channel.id == Amor.get_channel_id():
await self.game.role[ctx.message.author].love(p1, p2)
@commands.command()
async def sleep(self, ctx, player:Member):
if ctx.channel.id == Whore.get_channel_id():
await self.game.role[ctx.message.author].sleep(player)
@commands.command()
async def tasks(self, ctx):
for task in self.game.tasks:
await ctx.send(str(task))
def setup(client):
client.add_cog(Werwolf(client))
\ No newline at end of file
import werewolf_roles
import random
import asyncio
from asyncio import Event
from asyncio import create_task
import roles
from werewolf import Werewolf
game_category_id = 696079221684175004
class Game():
async def start(ctx, _players, _channels, _server_roles):
global role, players, channels, server_roles
role = dict(zip(random.sample(_players,len(_players)), werewolf_roles.get_roles()))
players = set(_players)
channels = _channels
server_roles = _server_roles
await ctx.send(f'starting new game with {[member.mention for member in players]}')
await set_night()
l = list()
for player in players:
l.append(asyncio.create_task(player.add_roles([server_roles['alive']])))
for t in l:
await t
async def play(self, members, server, role_alive, role_dead, village_channel):
self.accusations_event = Event()
self.accusations_event.set()
self.proposed_event = Event()
self.proposed_event.set()
self.confirm_event = Event()
self.confirm_event.set()
self.votes_event = Event()
self.votes_event.set()
self.couple = {}
self.server = server
self.village_channel = village_channel
self.players = set(members)
self.role = dict(zip(members, roles.get_roles(self)))
self.role_by_name = { self.role[player].get_name() for player in self.players }
self.captain = None
self.game_over = False
l = list()
for player in self.players:
l.append(create_task(player.remove_roles(role_dead)))
l.append(create_task(player.add_roles(role_alive)))
l.append(create_task(self.set_channel_for_member(self.role[player].get_channel_id(), player)))
for channel in self.server.get_channel(game_category_id).text_channels:
l.append(create_task(channel.purge()))
for task in l:
await task
await self.village_channel.send(f'Starte neues Spiel mit { [p.mention for p in self.players] }')
while True:
await self.night()
if await self.check_winconditions(): return
await self.day()
if await self.check_winconditions(): return
async def check_winconditions(self):
if len(self.players) == 0:
await self.village_channel.send('Somit ist das Spiel vorbei!\nNiemand hat gewonnen!')
return True
elif self.players == self.couple:
await self.village_channel.send('Somit ist das Spiel vorbei!\nDas Liebespaar hat gewonnen!')
return True
elif len({ self.role[p].get_team() for p in self.players}) == 1:
await self.village_channel.send(f'Somit ist das Spiel vorbei!\nDie {self.role[next(iter(self.players))].get_team()} haben gewonnen!')
return True
return False
async def check_time_state():
if tasks == {}:
if state == 'day':
await set_night()
else:
await set_day()
async def set_night():
global state, tasks, werewolf_votes, killed
state = 'night'
tasks = { task for task in [tasks for tasks in [role[player]['tasks'] for player in players]]}
werewolf_votes = dict()
killed = dict()
await channels['village'].send('Wieder mal wird es dunkel im Düsterwald und das Dorf schläft ein...')
def check_killed():
global killed
tmp = killed.values()
for killed in tmp:
players.remove(killed)
return tmp
async def set_day():
dead = await check_killed()
await channels['village'].send(f'Das Dorf erwacht ... ohne {[p.mention for p in dead]}')
global state, accusations, votes, tasks
state = 'day'
accusations = set()
votes = dict()
tasks = {'accusation'}
await channels['village'].send('Das Dorf erwacht...\n'
'Die Anklage ist eröffnet!\n'
'\n'
'Um eine Person anzuklagen schreibe $accuse @name!\n'
'Mit $withdraw @name können Anklagen zurückgezogen werden!\n')
async def accuse(ctx, player):
if 'accusation' in tasks:
if player in players:
if len(accusations) == 3:
await ctx.send(f'Es sind bereits drei Spieler angeklagt!')
async def set_channel_for_member(self, channel_id, member):
l = list()
for channel in self.server.get_channel(game_category_id).channels:
l.append(create_task(channel.set_permissions(member, overwrite=None)))
channel = self.server.get_channel(channel_id)
l.append(channel.set_permissions(member, read_messages=True, send_messages=True))
for t in l:
await t
async def day(self):
self.victims = dict()
await self.village_channel.send(f'Das Dorf erwacht ... ohne {[player.mention for player in self.victims.values()]}')
if not self.captain in self.players:
await self.village_channel.send('Ein neuer Hauptmann muss gewählt werden!\n'
'Dazu gibt jeder seine Stimme mit $vote @name ab!')
self.votes = dict()
self.vote_type = 'captain'
self.votes_event.clear()
await self.votes_event.wait()
votes = { player : self.votes.values().count(player) for player in set(self.votes.values()) }
self.captain = max(votes, key=votes.get)
await self.village_channel.send(f'Der neue Hauptmann ist {self.captain.mention}')
await self.village_channel.send('Und nun zur Anklage:\n'
'Anklagen können mit $accuse @name vorgeschlagen werden\n'
'Mit $withdraw @name können Angklagen zurück gezogen werden!\n'
'Nachdem mindestes zwei Leute angeklagt sind kann mit $confirm die Anklage bestätigt werden')
self.accused = set()
self.can_accuse = True
while True:
self.confirm_event.clear()
await self.confirm_event.wait()
if len(self.accused) > 1:
self.can_accuse = False
break
else:
accusations.add(player)
await ctx.send(f'Angeklagt: {[m.mention for m in accusations]}')
else:
await ctx.send(f'{player.mention} ist nicht (mehr) im Spiel')
async def withdraw(ctx, player):
if 'accusation' in tasks:
if player not in accusations:
ctx.send(f'{player.mention} ist nicht angeklagt!')
accusations.remove(player)
await ctx.send(f'Angeklagt: {[m.mention for m in accusations]}')
async def confirm_accusations(ctx):
if 'accusation' in tasks:
if len(accusations) < 2:
await ctx.send('Es müssen mindestens 2 Personen angeklagt werden!')
else:
tasks.add('voting')
tasks.remove('accusation')
await ctx.send('Die Angeklagen wurden bestätig!'
'Votet nun mit $vote @name für einen der Angeklagten!')
async def citizen_vote(ctx, player):
if 'voting' in tasks:
if player in accusations:
global votes
votes[ctx.message.author] = player
await ctx.send(f'Abstimmung: { {k.mention: v.mention for (k, v) in votes.items()} }')
if votes.keys() == players:
tasks.remove('voting')
votes = {player : votes.values().count(player) for player in set(votes.values())}
victim = max(votes, key=votes.get)
await ctx.send('Die Abstimmung ist beendet:'
f'{victim.name} wird hingerichtet!')
else:
await ctx.send(f'{player.mention} ist nicht angeklagt!')
async def werewolf_vote(ctx, player):
if 'werewolfs' in tasks:
werewolf_votes[ctx.message.author] = player
if werewolf_votes.keys() == { p for p in players if role[p] == werewolf_roles.werwolf }: #all werewolfs have submitted their vote
if len(set(werewolf_votes.values())) == 1: #all votes are equal
tasks.remove('werewolfs')
global killed
killed['werewolfs'] = list(werewolf_votes.values())[0]
await notify_werewolf_vote()
async def see(ctx, player):
if 'seer' in tasks:
if player in players:
await ctx.send(f'{player.mention} gehört zum Team {role[player]["team"]}')
tasks.remove('seer')
await self.village_channel.send('Es müssen mindestens 2 Personen angeklagt werden!')
await self.village_channel.send('Jetzt muss jeder noch lebende Spieler mit $vote @name seine Stimme abgeben!')
self.vote_type = 'victim'
self.votes = dict()
self.votes_event.clear()
await self.votes_event.wait()
votes = { player : self.votes.values().count(player) for player in set(self.votes.values()) }
votes[self.captain] += 1
victim = max(votes, key=votes.get)
print(f'{victim.mention} wird hingerichtet!')
self.players.remove(victim)
async def accuse(self, ctx, player):
if ctx.channel == self.village_channel:
if self.can_accuse:
if len(self.accused) == 3:
await self.village_channel.send(f'Es können nicht mehr als 3 Leute aufgestellt werden!')
else:
self.accused.add(player)
await self.village_channel.send(f'Aufgestellt sind: {[member.mention for member in self.accused]}')
async def withdraw(self, ctx, player):
if ctx.chanel == self.village_channel:
if not self.proposed_event.is_set():
if player not in self.accused:
await self.village_channel.send(f'{player.mention} ist nicht angeklagt!')
else:
self.accused.remove(player)
await self.village_channel.send(f'Angeklagt sind: {[member.mention for member in self.accused]}')
async def confirm(self, ctx):
self.confirm_event.set()
async def vote(self, ctx, voter, player):
if not self.votes_event.is_set():
if self.vote_type == 'captain':
self.captain_vote(ctx, voter, player)
elif self.vote_type == 'victim':
self.victim_vote(ctx, voter, player)
async def captain_vote(self, ctx, voter, player):
self.votes[voter] = player
if self.votes.keys() == self.players:
self.votes_event.set()
async def victim_vote(self, ctx, voter, player):
if player not in self.accused:
await self.village_channel.send(f'{player.mention} ist nicht angeklagt!')
else:
await ctx.send(f'{player.mention} ist nicht (mehr) im Spiel!')
async def heal(ctx, player):
if 'heal' in tasks:
global killed
healed = killed['werewolfs']
del killed['werewolfs']
await ctx.send(f'{healed.mention} wurde geheilt!')
tasks.remove('heal')
async def poison(ctx, player):
if 'poison' in tasks:
if player in players:
killed['witch'] = player
async def notify_werewolf_vote():
pass
\ No newline at end of file
self.votes[voter] = player
if self.votes.keys() == self.players:
self.votes_event.set()
async def night(self):
self.tasks = list()
self.victims = dict()
await self.village_channel.send('Es wird dunkel im Düsterwald und das Dorf schläft ein')
for player in self.players:
self.tasks.append(create_task(self.role[player].task()))
for task in self.tasks:
await task
for v in self.victims.values():
self.players.remove(v)
def get_living(self):
return { p for p in self.players if isinstance(self.role[p], self) }
\ No newline at end of file
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