functional cycle

fight-fixes
Bobloy 7 years ago
parent 67ec9e5cc8
commit 0679d98185

@ -3,7 +3,7 @@ import discord
from datetime import datetime, timedelta from datetime import datetime, timedelta
from random import shuffle import random
from werewolf.player import Player from werewolf.player import Player
@ -21,6 +21,11 @@ class Game:
"votegroup": None # uninitialized VoteGroup "votegroup": None # uninitialized VoteGroup
} }
morning_messages = [
"**The sun rises on the village..**",
"**Morning has arrived..**"
]
# def __new__(cls, guild, game_code): # def __new__(cls, guild, game_code):
# game_code = ["DefaultWerewolf", "Villager", "Villager"] # game_code = ["DefaultWerewolf", "Villager", "Villager"]
@ -46,6 +51,11 @@ class Game:
self.p_channels = {} # uses default_secret_channel self.p_channels = {} # uses default_secret_channel
self.vote_groups = {} # ID : VoteGroup() self.vote_groups = {} # ID : VoteGroup()
self.night_results = []
self.loop = asyncio.get_event_loop() self.loop = asyncio.get_event_loop()
async def setup(self, ctx): async def setup(self, ctx):
@ -101,7 +111,7 @@ class Game:
if self.p_channels[channel_id]["votegroup"] is not None: if self.p_channels[channel_id]["votegroup"] is not None:
vote_group = self.p_channels[channel_id]["votegroup"](self, channel) vote_group = self.p_channels[channel_id]["votegroup"](self, channel)
await vote_group.register_players([player in self.p_channels[channel_id]["players"]]) await vote_group.register_players(*self.p_channels[channel_id]["players"])
self.vote_groups[channel_id] = vote_group self.vote_groups[channel_id] = vote_group
@ -126,25 +136,39 @@ class Game:
and repeat with _at_day_start() again and repeat with _at_day_start() again
""" """
await self._at_day_start() await self._at_day_start()
# Once cycle ends, this will trigger end_game
await self._end_game() # Handle open channels
async def _at_game_start(self): # ID 0 async def _at_game_start(self): # ID 0
if self.game_over: if self.game_over:
return return
await self.village_channel.send("Game is starting, please wait for setup to complete")
await self.village_channel.send(embed=discord.Embed(title="Game is starting, please wait for setup to complete"))
await self._notify(0) await self._notify(0)
async def _at_day_start(self): # ID 1 async def _at_day_start(self): # ID 1
if self.game_over: if self.game_over:
return return
await self.village_channel.send("The sun rises on a new day in the village")
embed=discord.Embed(title=random.choice(self.morning_messages))
for result in self.night_results:
embed.add_field(name=result, value="________", inline=False)
await self.village_channel.send(embed=embed)
await self.generate_targets(self.village_channel)
await self.day_perms(self.village_channel) await self.day_perms(self.village_channel)
await self._notify(1) await self._notify(1)
await self._check_game_over()
if self.game_over:
return
self.can_vote = True self.can_vote = True
await asyncio.sleep(240) # 4 minute days await asyncio.sleep(120) # 4 minute days
await self.village_channel.send(embed=discord.Embed(title="**Two minutes of daylight remain...**"))
await asyncio.sleep(120) # 4 minute days
if not self.can_vote or self.game_over: if not self.can_vote or self.game_over:
return return
@ -160,7 +184,7 @@ class Game:
self.used_votes += 1 self.used_votes += 1
await self.all_but_perms(self.village_channel, target) await self.all_but_perms(self.village_channel, target)
await self.village_channel.send("{} will be put to trial and has 30 seconds to defend themselves".format(target.mention)) await self.village_channel.send("**{} will be put to trial and has 30 seconds to defend themselves**".format(target.mention))
await asyncio.sleep(30) await asyncio.sleep(30)
@ -189,7 +213,7 @@ class Game:
await self.village_channel.send(embed=embed) await self.village_channel.send(embed=embed)
if len(down_votes) > len(up_votes): if len(down_votes) > len(up_votes):
await self.village_channel.send("Voted to lynch {}!".format(target.mention)) await self.village_channel.send("**Voted to lynch {}!**".format(target.mention))
await self.kill(target) await self.kill(target)
self.can_vote = False self.can_vote = False
elif self.used_votes >= 3: elif self.used_votes >= 3:
@ -211,13 +235,15 @@ class Game:
await self._notify(4, data) await self._notify(4, data)
async def _at_day_end(self): # ID 5 async def _at_day_end(self): # ID 5
await self._check_game_over()
if self.game_over: if self.game_over:
return return
self.can_vote = False self.can_vote = False
await self.night_perms(self.village_channel) await self.night_perms(self.village_channel)
await self.village_channel.send("**The sun sets on the village...**") await self.village_channel.send(embed=discord.Embed(title="**The sun sets on the village...**"))
await self._notify(5) await self._notify(5)
await asyncio.sleep(30) await asyncio.sleep(30)
@ -392,11 +418,18 @@ class Game:
async def assign_roles(self): async def assign_roles(self):
"""len(self.roles) must == len(self.players)""" """len(self.roles) must == len(self.players)"""
shuffle(self.roles) random.shuffle(self.roles)
self.players.sort(key=lambda pl: pl.member.display_name.lower())
if len(self.roles) != len(self.players):
await self.village_channel("Unhandled error - roles!=players")
return False
for idx, role in enumerate(self.roles): for idx, role in enumerate(self.roles):
self.roles[idx] = role(self) self.roles[idx] = role(self)
await self.roles[idx].assign_player(self.players[idx]) await self.roles[idx].assign_player(self.players[idx])
# Sorted players, now assign id's
await self.players[idx].assign_id(idx)
async def get_player_by_member(self, member): async def get_player_by_member(self, member):
for player in self.players: for player in self.players:
@ -426,3 +459,11 @@ class Game:
await channel.set_permissions(self.guild.default_role, read_messages=False) await channel.set_permissions(self.guild.default_role, read_messages=False)
for member in member_list: for member in member_list:
await channel.set_permissions(member, read_messages=True) await channel.set_permissions(member, read_messages=True)
async def _check_game_over(self):
#ToDo
pass
async def _end_game(self):
#ToDo
pass

@ -25,5 +25,8 @@ class Player:
role.player = self role.player = self
self.role = role self.role = role
async def assign_id(self, id):
self.id = id
async def send_dm(self, message): async def send_dm(self, message):
await self.member.send(message) # Lets do embeds later await self.member.send(message) # Lets do embeds later

@ -43,11 +43,11 @@ class Role:
allignment = 0 # 1: Town, 2: Werewolf, 3: Neutral allignment = 0 # 1: Town, 2: Werewolf, 3: Neutral
channel_id = "" # Empty for no private channel channel_id = "" # Empty for no private channel
unique = False # Only one of this role per game unique = False # Only one of this role per game
game_start_message=""" game_start_message= (
Your role is **Default** "Your role is **Default**\n"
You win by testing the game "You win by testing the game\n"
Lynch players during the day with `[p]ww lynch <ID>` "Lynch players during the day with `[p]ww lynch <ID>`"
""" )
def __init__(self, game): def __init__(self, game):
self.game = game self.game = game

@ -12,19 +12,18 @@ class VanillaWerewolf(Role):
allignment = 2 # 1: Town, 2: Werewolf, 3: Neutral allignment = 2 # 1: Town, 2: Werewolf, 3: Neutral
channel_id = "werewolves" channel_id = "werewolves"
unique = False unique = False
game_start_message = """ game_start_message = (
Your role is **Werewolf** "Your role is **Werewolf**\n"
You win by killing everyone else in the village "You win by killing everyone else in the village\n"
Lynch players during the day with `[p]ww lynch <ID>` "Lynch players during the day with `[p]ww lynch <ID>`\n"
Vote to kill players at night with `[p]ww vote <ID>` "Vote to kill players at night with `[p]ww vote <ID>`"
""" )
def __init__(self, game): def __init__(self, game):
self.game = game super().__init__(game)
self.player = None
self.blocked = False
self.properties = {} # Extra data for other roles (i.e. arsonist)
self.action_list = [ self.action_list = [
(self._at_game_start, 0), # (Action, Priority) (self._at_game_start, 0), # (Action, Priority)
(self._at_day_start, 0), (self._at_day_start, 0),
@ -32,9 +31,10 @@ class VanillaWerewolf(Role):
(self._at_kill, 0), (self._at_kill, 0),
(self._at_hang, 0), (self._at_hang, 0),
(self._at_day_end, 0), (self._at_day_end, 0),
(self._at_night_start, 2), (self._at_night_start, 2), # Get vote priority
(self._at_night_end, 0) (self._at_night_end, 0)
] ]
self.killer = None # Added killer
# async def on_event(self, event, data): # async def on_event(self, event, data):
# """ # """

@ -9,28 +9,29 @@ class Villager(Role):
allignment = 0 # 1: Town, 2: Werewolf, 3: Neutral allignment = 0 # 1: Town, 2: Werewolf, 3: Neutral
channel_id = "" # Empty for no private channel channel_id = "" # Empty for no private channel
unique = False # Only one of this role per game unique = False # Only one of this role per game
game_start_message=""" game_start_message=(
Your role is **Villager** "Your role is **Villager**\n"
You win by lynching all evil in the town "You win by lynching all evil in the town\n"
Lynch players during the day with `[p]ww lynch <ID>` "Lynch players during the day with `[p]ww lynch <ID>`\n"
""" )
def __init__(self, game): def __init__(self, game):
self.game = game super().__init__()
self.player = None # self.game = game
self.blocked = False # self.player = None
self.properties = {} # Extra data for other roles (i.e. arsonist) # self.blocked = False
# self.properties = {} # Extra data for other roles (i.e. arsonist)
self.action_list = [
(self._at_game_start, 0), # (Action, Priority) # self.action_list = [
(self._at_day_start, 0), # (self._at_game_start, 0), # (Action, Priority)
(self._at_voted, 0), # (self._at_day_start, 0),
(self._at_kill, 0), # (self._at_voted, 0),
(self._at_hang, 0), # (self._at_kill, 0),
(self._at_day_end, 0), # (self._at_hang, 0),
(self._at_night_start, 0), # (self._at_day_end, 0),
(self._at_night_end, 0) # (self._at_night_start, 0),
] # (self._at_night_end, 0)
# ]
# async def on_event(self, event, data): # async def on_event(self, event, data):
# """ # """

@ -40,7 +40,7 @@ class VoteGroup:
await self.action_list[event][0](data) await self.action_list[event][0](data)
async def _at_game_start(self, data=None): async def _at_game_start(self, data=None):
pass await self.channel.send(" ".join(player.mention for player in self.players))
async def _at_day_start(self, data=None): async def _at_day_start(self, data=None):
pass pass

@ -46,8 +46,7 @@ class WolfVote(VoteGroup):
# await action_list[event][0](data) # await action_list[event][0](data)
# async def _at_game_start(self, data=None): # async def _at_game_start(self, data=None):
# if self.channel_id: # await self.channel.send(" ".join(player.mention for player in self.players))
# self.channel = await self.game.register_channel(self.channel_id)
# async def _at_day_start(self, data=None): # async def _at_day_start(self, data=None):
# pass # pass
@ -74,7 +73,7 @@ class WolfVote(VoteGroup):
self.killer = random.choice(self.players) self.killer = random.choice(self.players)
await channel.send("{} has been selected as tonight's killer") await self.channel.send("{} has been selected as tonight's killer".format(self.killer.member.display_name))
async def _at_night_end(self, data=None): async def _at_night_end(self, data=None):
if self.channel is None: if self.channel is None:

Loading…
Cancel
Save