opens and creates a game instance

fight-fixes
Bobloy 7 years ago
parent 9447dd87fc
commit a87f6dfa71

@ -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
@ -374,6 +385,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)

@ -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 <ID>`
"""
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),
@ -63,12 +66,6 @@ class Role:
(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):
"""
See Game class for event guide

@ -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 <ID>`
Vote to kill players at night with `[p]ww vote <ID>`
"""
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,15 +33,9 @@ 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):
# """
# See Game class for event guide

@ -1,8 +1,6 @@
import asyncio
import discord
from datetime import datetime, timedelta
from werewolf.role import Role
class Villager(Role):
@ -16,16 +14,6 @@ class Villager(Role):
You win by lynching all evil in the town
Lynch players during the day with `[p]ww lynch <ID>`
"""
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

@ -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),
@ -25,15 +32,6 @@ class VoteGroup:
(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):
"""
See Game class for event guide

@ -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):
# """

@ -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)
@ -70,6 +74,17 @@ class Werewolf:
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

Loading…
Cancel
Save