🗃️ Better modules and init files

This commit is contained in:
Nikolaj Danger
2020-08-03 16:58:37 +02:00
parent 6ca2c80432
commit d7f58305ac
11 changed files with 162 additions and 152 deletions

View File

@ -7,7 +7,7 @@ import codecs
import string import string
import json import json
import random import random
import math #import math
import os import os
from funcs import * 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")) 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: with open("resources/games/oldImages/blackjack"+str(channel), "w") as f:
f.write(str(oldImage.id)) f.write(str(oldImage.id))
try:
if allStanding: if allStanding:
await asyncio.sleep(5) await asyncio.sleep(5)
else: else:
await asyncio.sleep(120) await asyncio.sleep(120)
except:
logThis("Loop "+str(gameRound)+" interrupted (error code 1321)")
with open("resources/games/games.json", "r") as f: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
@ -139,7 +143,10 @@ async def blackjackLoop(channel,gameRound,gameID):
logThis("Loop "+str(gameRound)+" calling blackjackLoop()",str(channel)) logThis("Loop "+str(gameRound)+" calling blackjackLoop()",str(channel))
await blackjackLoop(channel,gameRound+1,gameID) await blackjackLoop(channel,gameRound+1,gameID)
else: else:
try:
new_message = blackjackFinish(str(channel)) new_message = blackjackFinish(str(channel))
except:
logThis("Something fucked up (error code 1310)")
await channel.send(new_message) await channel.send(new_message)
else: else:
logThis("Ending loop on round "+str(gameRound),str(channel)) 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 # Looks up a spell with the spellFunc function from funcs/lookup/lookuppy
elif content.startswith("spell "): elif content.startswith("spell "):
try: 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: except:
logThis("Something fucked up (error code 500)",str(message.channel)) logThis("Something fucked up (error code 500)",str(message.channel))
await message.channel.send("Something fucked up (error code 500)") await message.channel.send("Something fucked up (error code 500)")
@ -282,7 +294,8 @@ async def parseCommands(message,content):
# funcs/gwendolynpy # funcs/gwendolynpy
elif content.startswith("image"): elif content.startswith("image"):
try: try:
await message.channel.send(imageFunc()) randomImage = imageFunc()
await message.channel.send(randomImage)
except: except:
logThis("Something fucked up (error code 700)",str(message.channel)) logThis("Something fucked up (error code 700)",str(message.channel))
await message.channel.send("Something fucked up (error code 700)") 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 # Sends information about a random movie with the movieFunc function from
# funcs/other/movie.py # funcs/other/movie.py
elif content.startswith("movie"): elif content.startswith("movie"):
try: #try:
async with message.channel.typing(): async with message.channel.typing():
title, plot, cover, cast = movieFunc() title, plot, cover, cast = movieFunc()
if title == "error": if title == "error":
await message.channel.send("An error occurred. Try again (error code "+plot+")") await message.channel.send("An error occurred. Try again (error code "+plot+")")
else: else:
try:
embed = discord.Embed(title=title, description=plot, color=0x24ec19) embed = discord.Embed(title=title, description=plot, color=0x24ec19)
embed.set_thumbnail(url=cover) embed.set_thumbnail(url=cover)
embed.add_field(name="Cast", value=cast,inline = True) embed.add_field(name="Cast", value=cast,inline = True)
await message.channel.send(embed = embed) await message.channel.send(embed = embed)
except: except:
logThis("Something fucked up (error code 800)",str(message.channel)) logThis("Error embedding (error code 805)")
await message.channel.send("Something fucked up (error code 800)") #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 # Generates a random name with the nameGen function from funcs/other/generators.py
elif content.startswith("name"): elif content.startswith("name"):
@ -564,8 +580,7 @@ async def parseCommands(message,content):
logThis("Hit calling blackjackLoop()",str(message.channel)) logThis("Hit calling blackjackLoop()",str(message.channel))
await blackjackLoop(message.channel,int(response[7:])+1,gameID) await blackjackLoop(message.channel,int(response[7:])+1,gameID)
except: except:
logThis("Something fucked up") logThis("Something fucked up",str(message.channel))
await message.channel.send("something fucked up",str(message.channel))
else: else:
await message.channel.send(response) await message.channel.send(response)

View File

@ -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 .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer
from .swfuncs import * from .swfuncs import *

View File

@ -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 .money import checkBalance, giveMoney, addMoney
from .trivia import triviaCountPoints, triviaStart, triviaAnswer from .trivia import triviaCountPoints, triviaStart, triviaAnswer
from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit

View File

@ -677,98 +677,16 @@ def blackjackFinish(channel):
data = json.load(f) data = json.load(f)
dealerValue = calcHandValue(data["blackjack games"][channel]["dealer hand"]) dealerValue = calcHandValue(data["blackjack games"][channel]["dealer hand"])
reason = "" dealerBlackjack = data["blackjack games"][channel]["dealer blackjack"]
dealerBusted = data["blackjack games"][channel]["dealer busted"]
try:
for user in data["blackjack games"][channel]["user hands"]: 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)"
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"]
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)"
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 < 0:
if winnings == -1: if winnings == -1:
@ -781,10 +699,11 @@ def blackjackFinish(channel):
else: else:
finalWinnings += user+" won "+str(winnings)+" GwendoBucks "+reason+"\n" 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"]
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: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
@ -794,3 +713,56 @@ def blackjackFinish(channel):
json.dump(data,f,indent=4) json.dump(data,f,indent=4)
return finalWinnings 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

View File

@ -1 +1,3 @@
__all__ = ["spellFunc", "monsterFunc"]
from .lookupFuncs import spellFunc, monsterFunc from .lookupFuncs import spellFunc, monsterFunc

View File

@ -2,7 +2,7 @@ import lxml.etree # Used by imageFunc
import re # Used by roll_dice import re # Used by roll_dice
import datetime # Used by helloFunc import datetime # Used by helloFunc
import json # Used by spellFunc import json # Used by spellFunc
#import random # Used by imageFunc import random # Used by imageFunc
import urllib # Used by imageFunc import urllib # Used by imageFunc
import imdb # Used by movieFunc import imdb # Used by movieFunc
import time # Used for logging import time # Used for logging
@ -54,6 +54,7 @@ def helloFunc(author):
# Finds a random picture online # Finds a random picture online
def imageFunc(): def imageFunc():
# Picks a type of camera, which decides the naming scheme # Picks a type of camera, which decides the naming scheme
try:
cams = ("one","two","three","four") cams = ("one","two","three","four")
cam = random.choice(cams) cam = random.choice(cams)
logThis("Chose cam type "+cam) logThis("Chose cam type "+cam)
@ -77,6 +78,8 @@ def imageFunc():
c = str(random.randint(0,9)) c = str(random.randint(0,9))
d = str(random.randint(0,9)) d = str(random.randint(0,9))
search = ("DSC_"+a+b+c+d) search = ("DSC_"+a+b+c+d)
except:
logThis("error picking camera type (error code 702)")
logThis("Searching for "+search) logThis("Searching for "+search)

View File

@ -1,2 +1,4 @@
__all__ = ["nameGen", "tavernGen", "movieFunc"]
from .generators import nameGen, tavernGen from .generators import nameGen, tavernGen
from .movie import movieFunc from .movie import movieFunc

View File

@ -17,7 +17,7 @@ def movieFunc():
movs.close() movs.close()
except: except:
logThis("Problem picking the movie (error code 801)") logThis("Problem picking the movie (error code 801)")
return("error","801","","") return("error","804","","")
try: try:
logThis("Searching for "+mov) logThis("Searching for "+mov)
@ -42,5 +42,5 @@ def movieFunc():
logThis("Successfully ran !movie") logThis("Successfully ran !movie")
return(movie['title'], movie['plot'][0].split("::")[0], movie['cover url'].replace("150","600").replace("101","404"), pcast[:-2]) return(movie['title'], movie['plot'][0].split("::")[0], movie['cover url'].replace("150","600").replace("101","404"), pcast[:-2])
except: except:
logThis("Something bad happened... (error code 800)") logThis("Something bad happened... (error code 801)")
return("error","800","","") return("error","801","","")

View File

@ -1 +1,3 @@
__all__ = ["roll_dice"]
from .dice import roll_dice from .dice import roll_dice

View File

@ -1,3 +1,5 @@
__all__ = ["parseChar", "parseRoll", "critRoll", "parseDestiny"]
from .swchar import parseChar from .swchar import parseChar
from .swroll import parseRoll, critRoll from .swroll import parseRoll, critRoll
from .swdestiny import parseDestiny from .swdestiny import parseDestiny

View File

@ -32,12 +32,15 @@
7 - Image 7 - Image
700 - Unspecified error 700 - Unspecified error
701 - Can't connect to Bing 701 - Can't connect to Bing
702 - Error picking camera type/image name
8 - Movie 8 - Movie
800 - Unspecified error 800 - Unspecified error
801 - Can't pick movie 801 - Error in function
802 - Can't find movie on imdb 802 - Can't find movie on imdb
803 - Can't extract data 803 - Can't extract data
804 - Can't pick movie
805 - Embed error
9 - Star Wars 9 - Star Wars
910 - Unspecified swroll error 910 - Unspecified swroll error
@ -84,10 +87,15 @@
1223b - Not enough money 1223b - Not enough money
13 - Blackjack 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 14 - Four in a row
1400 - Unspecified 1400 - Unspecified error
1401 - Error deleting old image 1401 - Error deleting old image
1410 - Unspecified parsing error 1410 - Unspecified parsing error
1420 - Unspecified AI error 1420 - Unspecified AI error