Files
Gwendolyn/cogs/GamesCog.py
Nikolaj Danger 5151e1faf7 ⚙️ Cogs
2020-08-06 13:01:00 +02:00

269 lines
11 KiB
Python

import discord, asyncio, os, json
from discord.ext import commands
from funcs import logThis, triviaAnswer, triviaCountPoints, triviaStart, deleteGame, checkBalance, giveMoney, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackHit, blackjackDouble, blackjackFinish, blackjackSplit, blackjackStand
from gameLoops import blackjackLoop, fiar, runMonopoly, runHex
class GamesCog(commands.Cog):
"""Cog for game functions"""
def __init__(self,client):
self.client = client
# Checks user balance
@commands.command(aliases = ["b"])
async def balance(self, ctx):
response = checkBalance("#"+str(ctx.message.author.id))
if response == 1:
new_message = ctx.message.author.display_name + " has " + str(response) + " GwendoBuck"
else:
new_message = ctx.message.author.display_name + " has " + str(response) + " GwendoBucks"
await ctx.send(new_message)
# Gives another user an amount of GwendoBucks
@commands.command()
async def give(self, ctx, *, content):
commands = content.split(" ")
if len(commands) == 2:
print(commands)
amount = int(commands[1])
response = giveMoney("#"+str(ctx.message.author.id),commands[0],amount)
await ctx.send(response)
else:
logThis("I didn't understand that (error code 1222)",str(ctx.message.channel.id))
await ctx.send("I didn't understand that (error code 1222)")
# Runs a game of trivia
@commands.command()
async def trivia(self, ctx, *, content = ""):
if content == "":
question, answers, correctAnswer = triviaStart(str(ctx.message.channel.id))
if answers != "":
results = "**"+question+"**\n"
for answer in range(len(answers)):
results += chr(answer+97) + ") "+answers[answer]+"\n"
await ctx.send(results)
await asyncio.sleep(60)
triviaCountPoints(str(ctx.message.channel.id))
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")
else:
await ctx.send(question)
elif content in ["a","b","c","d"]:
response = triviaAnswer("#"+str(ctx.message.author.id),str(ctx.message.channel.id),content)
if response.startswith("Locked in "):
await ctx.message.add_reaction("👍")
else:
await ctx.send(response)
else:
logThis("I didn't understand that (error code 1101)",str(ctx.message.channel.id))
await ctx.send("I didn't understand that (error code 1101)")
# Runs a game of blackjack
@commands.command(aliases = ["bj"])
async def blackjack(self, ctx, *, content = ""):
# Blackjack shuffle variables
blackjackMinCards = 50
blackjackDecks = 4
channel = ctx.message.channel.id
# Starts the game
if content == "":
cardsLeft = 0
if os.path.exists("resources/games/blackjackCards/"+str(channel)+".txt"):
with open("resources/games/blackjackCards/"+str(channel)+".txt","r") as f:
for _ in f:
cardsLeft += 1
# Shuffles if not enough cards
if cardsLeft < blackjackMinCards:
blackjackShuffle(blackjackDecks,str(channel))
logThis("Shuffling the blackjack deck...",str(channel))
await ctx.send("Shuffling the deck...")
new_message = blackjackStart(str(channel))
if new_message == "started":
new_message = "Blackjack game started. Use \"!blackjack bet [amount]\" to enter the game within the next 30 seconds."
await ctx.send(new_message)
oldImage = await ctx.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(channel)+".png"))
with open("resources/games/oldImages/blackjack"+str(channel), "w") as f:
f.write(str(oldImage.id))
await asyncio.sleep(30)
gamedone = False
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if len(data["blackjack games"][str(channel)]["user hands"]) == 0:
gamedone = True
await ctx.send("No one entered the game. Ending the game.")
gameID = data["blackjack games"][str(channel)]["id"]
# Loop of game rounds
if gamedone == False:
logThis("!blackjack calling blackjackLoop()",str(channel))
await blackjackLoop(ctx.message.channel,1,gameID)
else:
new_message = blackjackFinish(str(channel))
await ctx.send(new_message)
else:
await ctx.send(new_message)
# Entering game and placing bet
elif content.startswith("bet"):
commands = content.split(" ")
amount = int(commands[1])
response = blackjackPlayerDrawHand(str(channel),"#"+str(ctx.message.author.id),amount)
await ctx.send(response)
# Hitting
elif content.startswith("hit"):
if content == "hit":
response = blackjackHit(str(channel),"#"+str(ctx.message.author.id))
else:
commands = content.split(" ")
try:
handNumber = int(commands[1])
except:
handNumber = 0
response = blackjackHit(str(channel),"#"+str(ctx.message.author.id),handNumber)
if response.startswith("accept"):
await ctx.message.add_reaction("👍")
try:
if response[6] == "T":
with open("resources/games/games.json", "r") as f:
gameID = json.load(f)["blackjack games"][str(channel)]["id"]
logThis("Hit calling blackjackLoop()",str(channel))
await blackjackLoop(ctx.message.channel,int(response[7:])+1,gameID)
except:
logThis("Something fucked up (error code 1320)",str(channel))
else:
await ctx.send(response)
# Standing
elif content.startswith("stand"):
if content == "hit":
response = blackjackStand(str(channel),"#"+str(ctx.message.author.id))
else:
commands = content.split(" ")
try:
handNumber = int(commands[1])
except:
handNumber = 0
response = blackjackStand(str(channel),"#"+str(ctx.message.author.id),handNumber)
if response.startswith("accept"):
await ctx.message.add_reaction("👍")
try:
if response[6] == "T":
with open("resources/games/games.json", "r") as f:
gameID = json.load(f)["blackjack games"][str(channel)]["id"]
logThis("Stand calling blackjackLoop()",str(channel))
await blackjackLoop(ctx.message.channel,int(response[7:])+1,gameID)
except:
logThis("Something fucked up (error code 1320)",str(channel))
else:
await ctx.send(response)
# Doubling bet
elif content.startswith("blackjack double"):
commands = content.split(" ")
try:
handNumber = int(commands[1])
except:
handNumber = 0
response, roundDone = blackjackDouble(str(channel),"#"+str(ctx.message.author.id),handNumber)
await ctx.send(response)
try:
if roundDone[0] == "T":
with open("resources/games/games.json", "r") as f:
gameID = json.load(f)["blackjack games"][str(channel)]["id"]
logThis("Double calling blackjackLoop()",str(channel))
await blackjackLoop(ctx.message.channel,int(roundDone[1:])+1,gameID)
except:
logThis("Something fucked up (error code 1320)",str(channel))
# Splitting hand
elif content.startswith("blackjack split"):
commands = content.split(" ")
try:
handNumber = int(commands[1])
except:
handNumber = 0
response, roundDone = blackjackSplit(str(channel),"#"+str(ctx.message.author.id),handNumber)
await ctx.send(response)
try:
if roundDone[0] == "T":
with open("resources/games/games.json", "r") as f:
gameID = json.load(f)["blackjack games"][str(channel)]["id"]
logThis("Split calling blackjackLoop()",str(channel))
await blackjackLoop(ctx.message.channel,int(roundDone[1:])+1,gameID)
except:
logThis("Something fucked up (error code 1320)")
# Returning current hi-lo value
elif content.startswith("blackjack hilo") and "#"+str(ctx.message.author.id) == "#266269899859427329":
if os.path.exists("resources/games/blackjackCards/"+str(channel)+".txt"):
with open("resources/games/hilo/"+str(channel)+".txt", "r") as f:
data = f.read()
else:
data = "0"
await ctx.send(data)
# Shuffles the blackjack deck
elif content.startswith("blackjack shuffle"):
blackjackShuffle(blackjackDecks,str(channel))
logThis("Shuffling the blackjack deck...",str(channel))
await ctx.send("Shuffling the deck...")
# Tells you the amount of cards left
elif content.startswith("blackjack cards"):
cardsLeft = 0
if os.path.exists("resources/games/blackjackCards/"+str(channel)+".txt"):
with open("resources/games/blackjackCards/"+str(channel)+".txt","r") as f:
for _ in f:
cardsLeft += 1
decksLeft = round(cardsLeft/52,1)
await ctx.send(str(cardsLeft)+" cards, "+str(decksLeft)+" decks")
else:
logThis("Not a command (error code 1301)")
await ctx.send("I didn't quite understand that (error code 1301)")
# Runs a game of Connect four
@commands.command(aliases = ["fiar","connect4","connectfour","4iar","4inarow"])
async def fourinarow(self, ctx, *, content = ""):
await fiar(ctx.message.channel,content,"#"+str(ctx.message.author.id))
# Runs a game of Hex
@commands.command(name="hex")
async def hexCommand(self, ctx, *, content = ""):
await runHex(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 runMonopoly(ctx.message.channel,content,"#"+str(ctx.message.author.id))
def setup(client):
client.add_cog(GamesCog(client))