From d03ae749375dd742c4bd3fc2e9519e63993cbff9 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Thu, 3 Dec 2020 15:48:02 +0100 Subject: [PATCH] Set some stuff up for werewolf :wolf: --- cogs/GamesCog.py | 43 +++++++------ funcs/funcs.py | 1 + funcs/games/blackjack.py | 3 - funcs/games/games.py | 2 + funcs/games/werewolf.py | 133 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 funcs/games/werewolf.py diff --git a/cogs/GamesCog.py b/cogs/GamesCog.py index 51d0e45..1900774 100644 --- a/cogs/GamesCog.py +++ b/cogs/GamesCog.py @@ -5,14 +5,14 @@ from funcs import logThis class GamesCog(commands.Cog): - def __init__(self,client): + def __init__(self,bot): """Runs game stuff.""" - self.client = client + self.bot = bot # Checks user balance @commands.command(aliases = ["b"]) async def balance(self, ctx): - response = self.client.money.checkBalance("#"+str(ctx.message.author.id)) + response = self.bot.money.checkBalance("#"+str(ctx.message.author.id)) if response == 1: new_message = ctx.message.author.display_name + " has " + str(response) + " GwendoBuck" else: @@ -25,19 +25,19 @@ class GamesCog(commands.Cog): commands = content.split(" ") amount = int(commands[-1]) username = " ".join(commands[:-1]) - if self.client.funcs.getID(username) == None: + if self.bot.funcs.getID(username) == None: async for member in ctx.guild.fetch_members(limit=None): if member.display_name.lower() == username.lower(): username = member.display_name userID = "#" + str(member.id) - self.client.database["users"].insert_one({"_id":userID,"user name":username,"money":0}) - response = self.client.money.giveMoney("#"+str(ctx.message.author.id),username,amount) + self.bot.database["users"].insert_one({"_id":userID,"user name":username,"money":0}) + response = self.bot.money.giveMoney("#"+str(ctx.message.author.id),username,amount) await ctx.send(response) # Invest GwendoBucks in the stock market @commands.command(aliases=["i"]) async def invest(self, ctx, *, content = "check"): - response = self.client.invest.parseInvest(content,"#"+str(ctx.message.author.id)) + response = self.bot.invest.parseInvest(content,"#"+str(ctx.message.author.id)) if response.startswith("**"): responses = response.split("\n") em = discord.Embed(title=responses[0],description="\n".join(responses[1:]),colour=0x00FF00) @@ -49,7 +49,7 @@ class GamesCog(commands.Cog): @commands.command() async def trivia(self, ctx, *, content = ""): if content == "": - question, answers, correctAnswer = self.client.trivia.triviaStart(str(ctx.message.channel.id)) + question, answers, correctAnswer = self.bot.trivia.triviaStart(str(ctx.message.channel.id)) if answers != "": results = "**"+question+"**\n" for x, answer in enumerate(answers): @@ -59,9 +59,9 @@ class GamesCog(commands.Cog): await asyncio.sleep(60) - self.client.trivia.triviaCountPoints(str(ctx.message.channel.id)) + self.bot.trivia.triviaCountPoints(str(ctx.message.channel.id)) - self.client.funcs.deleteGame("trivia questions",str(ctx.message.channel.id)) + self.bot.funcs.deleteGame("trivia questions",str(ctx.message.channel.id)) logThis("Time's up for the trivia question",str(ctx.message.channel.id)) await ctx.send("Time's up The answer was \""+chr(correctAnswer)+") "+answers[correctAnswer-97]+"\". Anyone who answered that has gotten 1 GwendoBuck") @@ -69,7 +69,7 @@ class GamesCog(commands.Cog): await ctx.send(question) elif content in ["a","b","c","d"]: - response = self.client.trivia.triviaAnswer("#"+str(ctx.message.author.id),str(ctx.message.channel.id),content) + response = self.bot.trivia.triviaAnswer("#"+str(ctx.message.author.id),str(ctx.message.channel.id),content) if response.startswith("Locked in "): await ctx.message.add_reaction("👍") else: @@ -81,32 +81,37 @@ class GamesCog(commands.Cog): # Runs a game of blackjack @commands.command(aliases = ["bj"]) async def blackjack(self, ctx, *, content = ""): - await self.client.blackjack.parseBlackjack(content,ctx) + await self.bot.blackjack.parseBlackjack(content,ctx) # Runs a game of Connect four @commands.command(aliases = ["fiar","connect4","connectfour","4iar","4inarow"]) async def fourinarow(self, ctx, *, content = ""): - await self.client.gameLoops.fiar(ctx.message.channel,content,"#"+str(ctx.message.author.id)) + await self.bot.gameLoops.fiar(ctx.message.channel,content,"#"+str(ctx.message.author.id)) # Runs a game of Monopoly @commands.command(aliases = ["m","mon","mono"]) async def monopoly(self, ctx, *, content = ""): - await self.client.gameLoops.runMonopoly(ctx.message.channel,content,"#"+str(ctx.message.author.id)) + await self.bot.gameLoops.runMonopoly(ctx.message.channel,content,"#"+str(ctx.message.author.id)) # Runs a game of Hangman @commands.command(aliases = ["hm"]) async def hangman(self, ctx, *, content = "start"): - await self.client.gameLoops.runHangman(ctx.message.channel,"#"+str(ctx.message.author.id),content) + await self.bot.gameLoops.runHangman(ctx.message.channel,"#"+str(ctx.message.author.id),content) # Runs a game of Hex @commands.command(name="hex") async def hexCommand(self, ctx, *, content = ""): - await self.client.gameLoops.runHex(ctx.message.channel,content,"#"+str(ctx.message.author.id)) + await self.bot.gameLoops.runHex(ctx.message.channel,content,"#"+str(ctx.message.author.id)) # Runs a game of Love Letter @commands.command(aliases = ["ll"]) async def loveletter(self, ctx, *, content = ""): - await self.client.gameLoops.runLoveletter(ctx.message.channel,content,"#"+str(ctx.message.author.id),ctx.message.author) + await self.bot.gameLoops.runLoveletter(ctx.message.channel,content,"#"+str(ctx.message.author.id),ctx.message.author) -def setup(client): - client.add_cog(GamesCog(client)) \ No newline at end of file + # Runs a game of Werewolf + @commands.command() + async def werewolf(self,ctx, *,content = "start"): + await self.bot.werewolf.parseWerewolf(ctx,content.lower()) + +def setup(bot): + bot.add_cog(GamesCog(bot)) \ No newline at end of file diff --git a/funcs/funcs.py b/funcs/funcs.py index 6c53822..01e1178 100644 --- a/funcs/funcs.py +++ b/funcs/funcs.py @@ -30,6 +30,7 @@ class Funcs(): def stopServer(self): self.bot.database["trivia questions"].delete_many({}) self.bot.database["blackjack games"].delete_many({}) + self.bot.database["werewolf games"].delete_many({}) g = git.cmd.Git("") g.pull() diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index e082c59..5594bf0 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -50,7 +50,6 @@ class Blackjack(): else: for x in range(len(values)): values[x] += int(cardValue) - print(values[x]) values.sort() @@ -91,9 +90,7 @@ class Blackjack(): else: done = True - print(dealerHand) if self.calcHandValue(dealerHand) > 21: - print("yeet") self.bot.database["blackjack games"].update_one({"_id":channel},{"$set":{"dealer busted":True}}) return done diff --git a/funcs/games/games.py b/funcs/games/games.py index fe5246a..a59d07a 100644 --- a/funcs/games/games.py +++ b/funcs/games/games.py @@ -6,6 +6,7 @@ from .gameLoops import GameLoops from .monopoly import Monopoly from .hangman import Hangman from .hex import HexGame +from .werewolf import Werewolf class Games(): def __init__(self, bot): @@ -19,3 +20,4 @@ class Games(): bot.monopoly = Monopoly(bot) bot.hangman = Hangman(bot) bot.hex = HexGame(bot) + bot.werewolf = Werewolf(bot) diff --git a/funcs/games/werewolf.py b/funcs/games/werewolf.py new file mode 100644 index 0000000..abd4f2f --- /dev/null +++ b/funcs/games/werewolf.py @@ -0,0 +1,133 @@ +import datetime +import discord +import asyncio +from funcs import logThis +from enum import Enum + +minimumPlayers = 1 +minimumPlayersWithAI = 5 + +class Team(Enum): + Villager = 1 + Werewolf = 2 + +class Role(): + def __init__(self,team,isWerewolf=False,isSeer=False,isSorcerer=False): + self.alive = True + self.team = team + self.isWerewolf = isWerewolf + self.isSeer = isSeer + self.isSorcerer = isSorcerer + +class VillagerRole(Role): + def __init__(self): + super().__init__(Team.Villager) + +class WerewolfRole(Role): + def __init__(self): + super.__init__(Team.Werewolf,isWerewolf=True) + +class SeerRole(Role): + def __init__(self): + super.__init__(Team.Villager,isSeer=True) + +class SorcererRole(Role): + def __init__(self): + super.__init__(Team.Werewolf,isSorcerer=True) + +class SorcererWerewolfRole(Role): + def __init__(self): + super.__init__(Team.Werewolf,isWerewolf=True,isSorcerer=True) + +class Werewolf(): + def __init__(self,bot): + self.bot = bot + + # Starts a game of werewolf + def werewolfStart(self,channel:str): + game = self.bot.database["werewolf games"].find_one({"_id":channel}) + + logThis("Trying to start a werewolf game in "+channel) + + if game == None: + + gameID = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + + newGame = {"_id":channel,"users": {},"round":0,"turn":0,"turnOrder":[],"current message":None,"gameID":gameID} + + self.bot.database["werewolf games"].insert_one(newGame) + + return "Started a game of werewolf. Use \"!werewolf join\" to join the game", True + else: + logThis("There is already a werewolf game going on in "+channel) + return "There's already a werewolf game going on in this channel. Try again in a few minutes.", False + + # Stops the game of werewolf + def werewolfStop(self,channel:str): + game = self.bot.database["werewolf games"].find_one({"_id":channel}) + + if game != None: + self.bot.database["werewolf games"].delete_one({"_id":channel}) + return "Ended the werewolf game" + else: + return "There's no game going on right now" + + async def werewolfLoop(self,ctx,round): + logThis(f"Starting loop {round} of Werewolf game") + channel = "#"+str(ctx.channel.id) + if round == 0: + await asyncio.sleep(120) + game = self.bot.database["werewolf games"].find_one({"_id":channel}) + AINumber = 1 + while len(game["users"].keys()) >= minimumPlayers and len(game["users"].keys()) < minimumPlayersWithAI: + user = {"role": None} + self.bot.database["werewolf games"].update_one({"_id":"#"+str(ctx.channel.id)}, + {"$set":{"users."+str(AINumber):user}}) + game = self.bot.database["werewolf games"].find_one({"_id":channel}) + AINumber += 1 + self.bot.database["werewolf games"].update_one({"_id":channel},{"$inc":{"round":1}}) + await self.werewolfLoop(ctx,round+1) + else: + await asyncio.sleep(60) + + + async def werewolfJoin(self,ctx): + currentGames = list(self.bot.database["werewolf games"].find({})) + for game in currentGames: + if "#"+str(ctx.message.author.id) in game["users"].keys(): + return f"{ctx.message.author.display_name} is already in a werewolf game", False + + game = self.bot.database["werewolf games"].find_one({"_id":"#"+str(ctx.channel.id)}) + if game["round"] == 0: + user = {"role": None} + self.bot.database["werewolf games"].update_one({"_id":"#"+str(ctx.channel.id)}, + {"$set":{"users."+"#"+str(ctx.message.author.id):user}}) + return "You joined the werewolf game", True + else: + return "It's too late to join", False + + async def parseWerewolf(self,ctx,content): + if content == "start": + response, started = self.werewolfStart("#"+str(ctx.channel.id)) + await ctx.send(response) + if started: + await self.werewolfLoop(ctx,0) + + elif content == "stop": + await ctx.send(self.werewolfStop("#"+str(ctx.channel.id))) + + elif content == "join": + response, joined = await self.werewolfJoin(ctx) + await ctx.send(response) + if joined: + game = self.bot.database["werewolf games"].find_one({"_id":"#"+str(ctx.channel.id)}) + if game["current message"] != None: + await ctx.channel.fetch_message(game["current message"]).delete() + users = "" + for x, user in enumerate(list(game["users"].keys())): + users += str(x+1)+") "+self.bot.funcs.getName(user)+"\n" + + if len(list(game["users"].keys())) < minimumPlayers: + users += f"You need at least {minimumPlayers} players to play werewolf." + em = discord.Embed(title="Current Players",description=users,colour=0x00FF00) + await ctx.send(embed=em)