From a87f6dfa71b176af06b103a4f2330565279ad8d5 Mon Sep 17 00:00:00 2001 From: Bobloy Date: Fri, 6 Apr 2018 16:56:40 -0400 Subject: [PATCH] opens and creates a game instance --- werewolf/game.py | 53 ++++++++++++++++++++----------- werewolf/role.py | 19 +++++------ werewolf/roles/vanillawerewolf.py | 25 +++++++-------- werewolf/roles/villager.py | 16 ++-------- werewolf/votegroup.py | 18 +++++------ werewolf/votegroups/wolfvote.py | 29 ++++++++--------- werewolf/werewolf.py | 25 ++++++++++++--- 7 files changed, 98 insertions(+), 87 deletions(-) diff --git a/werewolf/game.py b/werewolf/game.py index a1762e3..cc7b9d0 100644 --- a/werewolf/game.py +++ b/werewolf/game.py @@ -6,7 +6,9 @@ from datetime import datetime, timedelta from random import shuffle -# from .builder import parse_code +from werewolf.player import Player + +from werewolf.builder import parse_code class Game: @@ -20,19 +22,17 @@ class Game: "votegroup": None } - def __new__(cls, game_code): - game_code = ["DefaultWerewolf", "Villager", "Villager"] - return Game(game_code) + # def __new__(cls, guild, game_code): + # game_code = ["DefaultWerewolf", "Villager", "Villager"] + + # return super().__new__(cls, guild, game_code) def __init__(self, guild, game_code): self.guild = guild - self.game_code = game_code + self.game_code = ["VanillaWerewolf", "Villager", "Villager"] self.roles = [] - if self.game_code: - self.get_roles() - self.players = [] self.day_vote = {} # ID, votes @@ -59,8 +59,15 @@ class Game: 3. Check Initial role setup (including alerts) 4. Start game """ + if self.game_code: + await self.get_roles() + else: + await ctx.send("Number of players does not match number of roles, adjust before starting") + return False + if len(self.players) != self.roles: - ctx.send("Player count does not match role count, cannot start") + await ctx.send("Player count does not match role count, cannot start") + self.roles = [] return False # Create category and channel with individual overwrites overwrite = { @@ -284,15 +291,15 @@ class Game: await channel.send("**Game has already started!**") return - if member in self.players: + if await self.get_player_by_member(member): await channel.send("{} is already in the game!".format(member.mention)) return - self.started.append(member) + self.players.append(Player(member)) - channel.send("{} has been added to the game, total players is **{}**".format(member.mention, len(self.players))) + await channel.send("{} has been added to the game, total players is **{}**".format(member.mention, len(self.players))) - async def quit(self, member: discord.Member): + async def quit(self, member: discord.Member, channel: discord.TextChannel = None): """ Have a member quit a game """ @@ -302,7 +309,11 @@ class Game: return "You're not in a game!" if self.started: - await self.kill() + await self.kill(member) + await channel.send("{} has left the game".format(member.mention)) + else: + self.players = [player for player in self.players if player.member != member] + await channel.send("{} chickened out, player count is now **{}**".format(member.mention, len(self.players))) async def vote(self, author, id, channel): """ @@ -311,16 +322,16 @@ class Game: player = self._get_player(author) if player is None: - channel.send("You're not in this game!") + await channel.send("You're not in this game!") return if not player.alive: - channel.send("Corpses can't vote") + await channel.send("Corpses can't vote") return if channel == self.village_channel: if not self.can_vote: - channel.send("Voting is not allowed right now") + await channel.send("Voting is not allowed right now") return elif channel not in self.p_channels.values(): # Not part of the game @@ -332,7 +343,7 @@ class Game: target = None if target is None: - channel.send("Not a valid target") + await channel.send("Not a valid target") return # Now handle village vote or send to votegroup @@ -373,6 +384,12 @@ class Game: if not self.roles: return False + + async def get_player_by_member(self, member): + for player in self.players: + if player.member == member: + return player + return False async def dead_perms(self, channel, member): await channel.set_permissions(member, read_messages=True, send_message=False, add_reactions=False) diff --git a/werewolf/role.py b/werewolf/role.py index b7ef4e5..56df464 100644 --- a/werewolf/role.py +++ b/werewolf/role.py @@ -1,9 +1,5 @@ import asyncio -import discord - -from datetime import datetime,timedelta - class Role: """ Base Role class for werewolf game @@ -52,7 +48,14 @@ class Role: You win by testing the game Lynch players during the day with `[p]ww lynch ` """ - action_list = [ + + def __init__(self, game): + self.game = game + self.player = None + self.blocked = False + self.properties = {} # Extra data for other roles (i.e. arsonist) + + self.action_list = [ (self._at_game_start, 0), # (Action, Priority) (self._at_day_start, 0), (self._at_voted, 0), @@ -62,12 +65,6 @@ class Role: (self._at_night_start, 0), (self._at_night_end, 0) ] - - def __init__(self, game): - self.game = game - self.player = None - self.blocked = False - self.properties = {} # Extra data for other roles (i.e. arsonist) async def on_event(self, event, data): """ diff --git a/werewolf/roles/vanillawerewolf.py b/werewolf/roles/vanillawerewolf.py index 394a575..8db2610 100644 --- a/werewolf/roles/vanillawerewolf.py +++ b/werewolf/roles/vanillawerewolf.py @@ -1,12 +1,8 @@ import asyncio -import discord +from werewolf.role import Role -from datetime import datetime, timedelta - -from cogs.werewolf.Role import Role - -from cogs.werewolf.votegroups.wolfvote import WolfVote +from werewolf.votegroups.wolfvote import WolfVote class VanillaWerewolf(Role): @@ -22,7 +18,14 @@ class VanillaWerewolf(Role): Lynch players during the day with `[p]ww lynch ` Vote to kill players at night with `[p]ww vote ` """ - action_list = [ + + + def __init__(self, game): + # self.game = game + # self.player = None + # self.blocked = False + # self.properties = {} # Extra data for other roles (i.e. arsonist) + self.action_list = [ (self._at_game_start, 0), # (Action, Priority) (self._at_day_start, 0), (self._at_voted, 0), @@ -30,14 +33,8 @@ class VanillaWerewolf(Role): (self._at_hang, 0), (self._at_day_end, 0), (self._at_night_start, 2), - (self._at_night_end, 5) + (self._at_night_end, 0) ] - - # def __init__(self, game): - # self.game = game - # self.player = None - # self.blocked = False - # self.properties = {} # Extra data for other roles (i.e. arsonist) # async def on_event(self, event, data): # """ diff --git a/werewolf/roles/villager.py b/werewolf/roles/villager.py index 47f0c58..a297865 100644 --- a/werewolf/roles/villager.py +++ b/werewolf/roles/villager.py @@ -1,8 +1,6 @@ import asyncio -import discord - -from datetime import datetime, timedelta +from werewolf.role import Role class Villager(Role): @@ -16,17 +14,7 @@ class Villager(Role): You win by lynching all evil in the town Lynch players during the day with `[p]ww lynch ` """ - action_list = [ - (self._at_game_start, 0), # (Action, Priority) - (self._at_day_start, 0), - (self._at_voted, 0), - (self._at_kill, 0), - (self._at_hang, 0), - (self._at_day_end, 0), - (self._at_night_start, 0), - (self._at_night_end, 0) - ] - + # def __init__(self, game): # self.game = game # self.player = None diff --git a/werewolf/votegroup.py b/werewolf/votegroup.py index 04e9ba1..64552b5 100644 --- a/werewolf/votegroup.py +++ b/werewolf/votegroup.py @@ -14,7 +14,14 @@ class VoteGroup: allignment = 0 # 1: Town, 2: Werewolf, 3: Neutral channel_id = "" - action_list = [ + def __init__(self, game, channel): + self.game = game + self.channel = channel + self.players = [] + self.vote_results = {} + self.properties = {} # Extra data for other options + + self.action_list = [ (self._at_game_start, 0), # (Action, Priority) (self._at_day_start, 0), (self._at_voted, 0), @@ -24,15 +31,6 @@ class VoteGroup: (self._at_night_start, 2), (self._at_night_end, 0) ] - - - def __init__(self, game, channel): - self.game = game - self.channel = channel - self.players = [] - self.vote_results = {} - self.properties = {} # Extra data for other options - async def on_event(self, event, data): """ diff --git a/werewolf/votegroups/wolfvote.py b/werewolf/votegroups/wolfvote.py index 2420288..b78d56a 100644 --- a/werewolf/votegroups/wolfvote.py +++ b/werewolf/votegroups/wolfvote.py @@ -4,7 +4,7 @@ import discord import random -from cogs.werewolf.votegroup import VoteGroup +from werewolf.votegroup import VoteGroup class WolfVote(VoteGroup): """ @@ -14,18 +14,6 @@ class WolfVote(VoteGroup): allignment = 2 # 1: Town, 2: Werewolf, 3: Neutral channel_id = "werewolves" - action_list = [ - (self._at_game_start, 0), # (Action, Priority) - (self._at_day_start, 0), - (self._at_voted, 0), - (self._at_kill, 0), - (self._at_hang, 0), - (self._at_day_end, 0), - (self._at_night_start, 2), - (self._at_night_end, 5) # Kill priority - ] - - kill_messages = [ "**{ID}** - {target} was attacked by wolves", "**{ID}** - {target} was found torn to shreds"] @@ -36,9 +24,20 @@ class WolfVote(VoteGroup): # self.players = [] # self.vote_results = {} # self.properties = {} # Extra data for other options - self.killer = None # Added killer super().__init__(game, channel) - + + self.killer = None # Added killer + + self.action_list = [ + (self._at_game_start, 0), # (Action, Priority) + (self._at_day_start, 0), + (self._at_voted, 0), + (self._at_kill, 0), + (self._at_hang, 0), + (self._at_day_end, 0), + (self._at_night_start, 2), + (self._at_night_end, 5) # Kill priority + ] # async def on_event(self, event, data): # """ diff --git a/werewolf/werewolf.py b/werewolf/werewolf.py index 9e025d5..d109fc6 100644 --- a/werewolf/werewolf.py +++ b/werewolf/werewolf.py @@ -7,7 +7,7 @@ from redbot.core import Config from datetime import datetime, timedelta -from .game import Game +from werewolf.game import Game class Werewolf: @@ -44,7 +44,11 @@ class Werewolf: game = self._get_game(ctx.guild, game_code) if not game: - ctx.send("Failed to start a new game") + await ctx.send("Failed to start a new game") + else: + await ctx.send("New game has started") + + @ww.command() async def join(self, ctx): @@ -55,7 +59,7 @@ class Werewolf: game = self._get_game(ctx.guild) if not game: - ctx.send("No game to join!\nCreate a new one with `[p]ww new`") + await ctx.send("No game to join!\nCreate a new one with `[p]ww new`") return await game.join(ctx.author, ctx.channel) @@ -69,7 +73,18 @@ class Werewolf: game = self._get_game(ctx.guild) await game.quit(ctx.author, ctx.channel) - + + @ww.command() + async def start(self, ctx): + """ + Checks number of players and attempts to start the game + """ + game = self._get_game(guild) + if not game: + await ctx.send("No game running, cannot start") + + await game.setup(ctx) + @ww.command() async def vote(self, ctx, id): """ @@ -77,7 +92,7 @@ class Werewolf: """ game = self._get_game(guild) if not game: - ctx.send("No game running, cannot vote") + await ctx.send("No game running, cannot vote") # Game handles response now channel = ctx.channel