From d7f58305acf1faf6893c732c11a4ecf39775d305 Mon Sep 17 00:00:00 2001 From: Nikolaj Danger Date: Mon, 3 Aug 2020 16:58:37 +0200 Subject: [PATCH] :card_file_box: Better modules and init files --- Gwendolyn.py | 53 ++++++++---- funcs/__init__.py | 4 +- funcs/games/__init__.py | 2 + funcs/games/blackjack.py | 176 ++++++++++++++++---------------------- funcs/lookup/__init__.py | 2 + funcs/miscFuncs.py | 51 +++++------ funcs/other/__init__.py | 2 + funcs/other/movie.py | 6 +- funcs/roll/__init__.py | 2 + funcs/swfuncs/__init__.py | 2 + resources/errorCodes.txt | 14 ++- 11 files changed, 162 insertions(+), 152 deletions(-) diff --git a/Gwendolyn.py b/Gwendolyn.py index 36e4f97..4bd4d90 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -7,7 +7,7 @@ import codecs import string import json import random -import math +#import math import os from funcs import * @@ -122,10 +122,14 @@ async def blackjackLoop(channel,gameRound,gameID): oldImage = await channel.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)) - if allStanding: - await asyncio.sleep(5) - else: - await asyncio.sleep(120) + + try: + if allStanding: + await asyncio.sleep(5) + else: + await asyncio.sleep(120) + except: + logThis("Loop "+str(gameRound)+" interrupted (error code 1321)") with open("resources/games/games.json", "r") as f: data = json.load(f) @@ -139,7 +143,10 @@ async def blackjackLoop(channel,gameRound,gameID): logThis("Loop "+str(gameRound)+" calling blackjackLoop()",str(channel)) await blackjackLoop(channel,gameRound+1,gameID) else: - new_message = blackjackFinish(str(channel)) + try: + new_message = blackjackFinish(str(channel)) + except: + logThis("Something fucked up (error code 1310)") await channel.send(new_message) else: logThis("Ending loop on round "+str(gameRound),str(channel)) @@ -216,7 +223,12 @@ async def parseCommands(message,content): # Looks up a spell with the spellFunc function from funcs/lookup/lookuppy elif content.startswith("spell "): try: - await message.channel.send(spellFunc(cap(content.replace("spell","")))) + spell = spellFunc(cap(content.replace("spell",""))) + if len(spell) > 2000: + await message.channel.send(spell[:2000]) + await message.channel.send(spell[2000:]) + else: + await message.channel.send(spell) except: logThis("Something fucked up (error code 500)",str(message.channel)) await message.channel.send("Something fucked up (error code 500)") @@ -282,7 +294,8 @@ async def parseCommands(message,content): # funcs/gwendolynpy elif content.startswith("image"): try: - await message.channel.send(imageFunc()) + randomImage = imageFunc() + await message.channel.send(randomImage) except: logThis("Something fucked up (error code 700)",str(message.channel)) await message.channel.send("Something fucked up (error code 700)") @@ -290,19 +303,22 @@ async def parseCommands(message,content): # Sends information about a random movie with the movieFunc function from # funcs/other/movie.py elif content.startswith("movie"): - try: - async with message.channel.typing(): - title, plot, cover, cast = movieFunc() - if title == "error": - await message.channel.send("An error occurred. Try again (error code "+plot+")") - else: + #try: + async with message.channel.typing(): + title, plot, cover, cast = movieFunc() + if title == "error": + await message.channel.send("An error occurred. Try again (error code "+plot+")") + else: + try: embed = discord.Embed(title=title, description=plot, color=0x24ec19) embed.set_thumbnail(url=cover) embed.add_field(name="Cast", value=cast,inline = True) await message.channel.send(embed = embed) - except: - logThis("Something fucked up (error code 800)",str(message.channel)) - await message.channel.send("Something fucked up (error code 800)") + except: + logThis("Error embedding (error code 805)") + #except: + # logThis("Something fucked up (error code 800)",str(message.channel)) + # await message.channel.send("Something fucked up (error code 800)") # Generates a random name with the nameGen function from funcs/other/generators.py elif content.startswith("name"): @@ -564,8 +580,7 @@ async def parseCommands(message,content): logThis("Hit calling blackjackLoop()",str(message.channel)) await blackjackLoop(message.channel,int(response[7:])+1,gameID) except: - logThis("Something fucked up") - await message.channel.send("something fucked up",str(message.channel)) + logThis("Something fucked up",str(message.channel)) else: await message.channel.send(response) diff --git a/funcs/__init__.py b/funcs/__init__.py index 4229c99..d8a53c7 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -1,3 +1,5 @@ +__all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToNumber", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney","triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI", "spellFunc", "monsterFunc", "nameGen", "tavernGen", "movieFunc", "roll_dice", "parseChar", "parseRoll", "critRoll", "parseDestiny"] + from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer from .swfuncs import * @@ -8,4 +10,4 @@ from .other import * from .games import * -from .roll import * \ No newline at end of file +from .roll import * diff --git a/funcs/games/__init__.py b/funcs/games/__init__.py index 76ab117..ab5e931 100644 --- a/funcs/games/__init__.py +++ b/funcs/games/__init__.py @@ -1,3 +1,5 @@ +__all__ = ["checkBalance", "giveMoney", "addMoney","triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI"] + from .money import checkBalance, giveMoney, addMoney from .trivia import triviaCountPoints, triviaStart, triviaAnswer from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index 8dbe3ff..a3d9be8 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -677,113 +677,32 @@ def blackjackFinish(channel): data = json.load(f) dealerValue = calcHandValue(data["blackjack games"][channel]["dealer hand"]) - reason = "" + dealerBlackjack = data["blackjack games"][channel]["dealer blackjack"] + dealerBusted = data["blackjack games"][channel]["dealer busted"] - for user in data["blackjack games"][channel]["user hands"]: - winnings = -1 * data["blackjack games"][channel]["user hands"][user]["bet"] - if data["blackjack games"][channel]["user hands"][user]["blackjack"] and data["blackjack games"][channel]["dealer blackjack"] == False: - reason = "(blackjack)" - winnings += math.floor(2.5 * data["blackjack games"][channel]["user hands"][user]["bet"]) - elif data["blackjack games"][channel]["dealer blackjack"]: - reason += "(dealer blackjack)" - elif data["blackjack games"][channel]["user hands"][user]["busted"]: - reason = "(busted)" - else: - if data["blackjack games"][channel]["dealer busted"]: - reason = "(dealer busted)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) > dealerValue: - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["bet"] - reason = "(highest value)" - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) == dealerValue: - reason = "(pushed)" - winnings += data["blackjack games"][channel]["user hands"][user]["bet"] - else: - reason = "(highest value)" + try: + for user in data["blackjack games"][channel]["user hands"]: - - if data["blackjack games"][channel]["user hands"][user]["split"] > 0: - winnings -= data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"] - if data["blackjack games"][channel]["user hands"][user]["other hand"]["blackjack"] and data["blackjack games"][channel]["dealer blackjack"] == False: - reason += "(blackjack)" - winnings += math.floor(2.5 * data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"]) - elif data["blackjack games"][channel]["dealer blackjack"]: - reason += "(dealer blackjack)" - elif data["blackjack games"][channel]["user hands"][user]["other hand"]["busted"]: - reason += "(busted)" - else: - if data["blackjack games"][channel]["dealer busted"]: - reason += "(dealer busted)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["other hand"]["hand"]) > dealerValue: - reason += "(highest value)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["other hand"]["hand"]) == dealerValue: - reason += "(pushed)" - winnings += data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"] + try: + winnings, netWinnings, reason = calcWinnings(data["blackjack games"][channel]["user hands"][user],dealerValue,True,dealerBlackjack,dealerBusted) + except: + logThis("Error calculating winnings for "+str(user)+" (error code 1312)") + + if winnings < 0: + if winnings == -1: + finalWinnings += user+" lost "+str(-1 * winnings)+" GwendoBuck "+reason+"\n" else: - reason += "(highest value)" - - if data["blackjack games"][channel]["user hands"][user]["split"] > 1: - winnings -= data["blackjack games"][channel]["user hands"][user]["third hand"]["bet"] - if data["blackjack games"][channel]["user hands"][user]["third hand"]["blackjack"] and data["blackjack games"][channel]["dealer blackjack"] == False: - reason += "(blackjack)" - winnings += math.floor(2.5 * data["blackjack games"][channel]["user hands"][user]["third hand"]["bet"]) - elif data["blackjack games"][channel]["dealer blackjack"]: - reason += "(dealer blackjack)" - elif data["blackjack games"][channel]["user hands"][user]["third hand"]["busted"]: - reason += "(busted)" - else: - if data["blackjack games"][channel]["dealer busted"]: - reason += "(dealer busted)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["third hand"]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["third hand"]["hand"]) > dealerValue: - reason += "(highest value)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["third hand"]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["third hand"]["hand"]) == dealerValue: - reason += "(pushed)" - winnings += data["blackjack games"][channel]["user hands"][user]["third hand"]["bet"] - else: - reason += "(highest value)" - - if data["blackjack games"][channel]["user hands"][user]["split"] > 2: - winnings -= data["blackjack games"][channel]["user hands"][user]["fourth hand"]["bet"] - if data["blackjack games"][channel]["user hands"][user]["fourth hand"]["blackjack"] and data["blackjack games"][channel]["dealer blackjack"] == False: - reason += "(blackjack)" - winnings += math.floor(2.5 * data["blackjack games"][channel]["user hands"][user]["fourth hand"]["bet"]) - elif data["blackjack games"][channel]["dealer blackjack"]: - reason += "(dealer blackjack)" - elif data["blackjack games"][channel]["user hands"][user]["fourth hand"]["busted"]: - reason += "(busted)" - else: - if data["blackjack games"][channel]["dealer busted"]: - reason += "(dealer busted)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["fourth hand"]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["fourth hand"]["hand"]) > dealerValue: - reason += "(highest value)" - winnings += 2 * data["blackjack games"][channel]["user hands"][user]["fourth hand"]["bet"] - elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["fourth hand"]["hand"]) == dealerValue: - reason += "(pushed)" - winnings += data["blackjack games"][channel]["user hands"][user]["fourth hand"]["bet"] - else: - reason += "(highest value)" - - - - if winnings < 0: - if winnings == -1: - finalWinnings += user+" lost "+str(-1 * winnings)+" GwendoBuck "+reason+"\n" + finalWinnings += user+" lost "+str(-1 * winnings)+" GwendoBucks "+reason+"\n" else: - finalWinnings += user+" lost "+str(-1 * winnings)+" GwendoBucks "+reason+"\n" - else: - if winnings == 1: - finalWinnings += user+" won "+str(winnings)+" GwendoBuck "+reason+"\n" - else: - finalWinnings += user+" won "+str(winnings)+" GwendoBucks "+reason+"\n" - - netWinnings = winnings + data["blackjack games"][channel]["user hands"][user]["bet"] + data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"] + if winnings == 1: + finalWinnings += user+" won "+str(winnings)+" GwendoBuck "+reason+"\n" + else: + finalWinnings += user+" won "+str(winnings)+" GwendoBucks "+reason+"\n" - money.addMoney(user,netWinnings) + money.addMoney(user,netWinnings) + + except: + logThis("Error calculating winnings (error code 1311)") with open("resources/games/games.json", "r") as f: data = json.load(f) @@ -794,3 +713,56 @@ def blackjackFinish(channel): json.dump(data,f,indent=4) return finalWinnings + + +def calcWinnings(hand, dealerValue, topLevel, dealerBlackjack, dealerBusted): + logThis("Calculating winnings") + reason = "" + bet = hand["bet"] + winnings = -1 * bet + netWinnings = 0 + handValue = calcHandValue(hand["hand"]) + + if hand["blackjack"] and dealerBlackjack == False: + reason += "(blackjack)" + winnings += math.floor(2.5 * bet) + netWinnings += math.floor(2.5 * bet) + elif dealerBlackjack: + reason += "(dealer blackjack)" + elif hand["busted"]: + reason += "(busted)" + else: + if dealerBusted: + reason = "(dealer busted)" + winnings += 2 * bet + netWinnings += 2 * bet + elif handValue > dealerValue: + winnings += 2 * bet + netWinnings += 2 * bet + reason = "(highest value)" + elif handValue == dealerValue: + reason = "(pushed)" + winnings += bet + netWinnings += bet + else: + reason = "(highest value)" + + + if topLevel: + if hand["split"] >= 1: + winningsTemp, netWinningsTemp, reasonTemp = calcWinnings(hand["other hand"],dealerValue,False,dealerBlackjack,dealerBusted) + winnings += winningsTemp + netWinnings += netWinningsTemp + reason += reasonTemp + if hand["split"] >= 2: + winningsTemp, netWinningsTemp, reasonTemp = calcWinnings(hand["third hand"],dealerValue,False,dealerBlackjack,dealerBusted) + winnings += winningsTemp + netWinnings += netWinningsTemp + reason += reasonTemp + if hand["split"] >= 3: + winningsTemp, netWinningsTemp, reasonTemp = calcWinnings(hand["fourth hand"],dealerValue,False,dealerBlackjack,dealerBusted) + winnings += winningsTemp + netWinnings += netWinningsTemp + reason += reasonTemp + + return winnings, netWinnings, reason \ No newline at end of file diff --git a/funcs/lookup/__init__.py b/funcs/lookup/__init__.py index 5c748ff..a4f6b5a 100644 --- a/funcs/lookup/__init__.py +++ b/funcs/lookup/__init__.py @@ -1 +1,3 @@ +__all__ = ["spellFunc", "monsterFunc"] + from .lookupFuncs import spellFunc, monsterFunc \ No newline at end of file diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index aef30a9..2c8f55d 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -2,7 +2,7 @@ import lxml.etree # Used by imageFunc import re # Used by roll_dice import datetime # Used by helloFunc import json # Used by spellFunc -#import random # Used by imageFunc +import random # Used by imageFunc import urllib # Used by imageFunc import imdb # Used by movieFunc import time # Used for logging @@ -54,29 +54,32 @@ def helloFunc(author): # Finds a random picture online def imageFunc(): # Picks a type of camera, which decides the naming scheme - cams = ("one","two","three","four") - cam = random.choice(cams) - logThis("Chose cam type "+cam) - if cam == "one": - a = str(random.randint(0 ,9)) - b = str(random.randint(0,9)) - c = str(random.randint(0,9)) - d = str(random.randint(0,9)) - search = ("img_"+a+b+c+d) - elif cam == "two": - a = str(random.randint(2012,2016)) - b = str(random.randint(1,12)).zfill(2) - c = str(random.randint(1,29)).zfill(2) - search = ("IMG_"+a+b+c) - elif cam == "three": - a = str(random.randint(1,500)).zfill(4) - search = ("IMAG_"+a) - elif cam == "four": - a = str(random.randint(0,9)) - b = str(random.randint(0,9)) - c = str(random.randint(0,9)) - d = str(random.randint(0,9)) - search = ("DSC_"+a+b+c+d) + try: + cams = ("one","two","three","four") + cam = random.choice(cams) + logThis("Chose cam type "+cam) + if cam == "one": + a = str(random.randint(0 ,9)) + b = str(random.randint(0,9)) + c = str(random.randint(0,9)) + d = str(random.randint(0,9)) + search = ("img_"+a+b+c+d) + elif cam == "two": + a = str(random.randint(2012,2016)) + b = str(random.randint(1,12)).zfill(2) + c = str(random.randint(1,29)).zfill(2) + search = ("IMG_"+a+b+c) + elif cam == "three": + a = str(random.randint(1,500)).zfill(4) + search = ("IMAG_"+a) + elif cam == "four": + a = str(random.randint(0,9)) + b = str(random.randint(0,9)) + c = str(random.randint(0,9)) + d = str(random.randint(0,9)) + search = ("DSC_"+a+b+c+d) + except: + logThis("error picking camera type (error code 702)") logThis("Searching for "+search) diff --git a/funcs/other/__init__.py b/funcs/other/__init__.py index 8f788c5..9d2c2a8 100644 --- a/funcs/other/__init__.py +++ b/funcs/other/__init__.py @@ -1,2 +1,4 @@ +__all__ = ["nameGen", "tavernGen", "movieFunc"] + from .generators import nameGen, tavernGen from .movie import movieFunc \ No newline at end of file diff --git a/funcs/other/movie.py b/funcs/other/movie.py index a5eb782..fc448b0 100644 --- a/funcs/other/movie.py +++ b/funcs/other/movie.py @@ -17,7 +17,7 @@ def movieFunc(): movs.close() except: logThis("Problem picking the movie (error code 801)") - return("error","801","","") + return("error","804","","") try: logThis("Searching for "+mov) @@ -42,5 +42,5 @@ def movieFunc(): logThis("Successfully ran !movie") return(movie['title'], movie['plot'][0].split("::")[0], movie['cover url'].replace("150","600").replace("101","404"), pcast[:-2]) except: - logThis("Something bad happened... (error code 800)") - return("error","800","","") + logThis("Something bad happened... (error code 801)") + return("error","801","","") diff --git a/funcs/roll/__init__.py b/funcs/roll/__init__.py index 314efdd..835ae34 100644 --- a/funcs/roll/__init__.py +++ b/funcs/roll/__init__.py @@ -1 +1,3 @@ +__all__ = ["roll_dice"] + from .dice import roll_dice \ No newline at end of file diff --git a/funcs/swfuncs/__init__.py b/funcs/swfuncs/__init__.py index 8937758..bfbc7c3 100644 --- a/funcs/swfuncs/__init__.py +++ b/funcs/swfuncs/__init__.py @@ -1,3 +1,5 @@ +__all__ = ["parseChar", "parseRoll", "critRoll", "parseDestiny"] + from .swchar import parseChar from .swroll import parseRoll, critRoll from .swdestiny import parseDestiny \ No newline at end of file diff --git a/resources/errorCodes.txt b/resources/errorCodes.txt index e1061c1..44e9016 100644 --- a/resources/errorCodes.txt +++ b/resources/errorCodes.txt @@ -32,12 +32,15 @@ 7 - Image 700 - Unspecified error 701 - Can't connect to Bing +702 - Error picking camera type/image name 8 - Movie 800 - Unspecified error -801 - Can't pick movie +801 - Error in function 802 - Can't find movie on imdb 803 - Can't extract data +804 - Can't pick movie +805 - Embed error 9 - Star Wars 910 - Unspecified swroll error @@ -84,10 +87,15 @@ 1223b - Not enough money 13 - Blackjack -1300 - Unspecified +1300 - Unspecified error +1310 - Unspecified finishing error +1311 - Error calculating winnings +1312 - Error in calcWinnings function +1320 - Unspecified loop error +1321 - Loop interrupted while waiting 14 - Four in a row -1400 - Unspecified +1400 - Unspecified error 1401 - Error deleting old image 1410 - Unspecified parsing error 1420 - Unspecified AI error \ No newline at end of file