This commit is contained in:
NikolajDanger
2020-04-01 00:59:02 +02:00
parent 9088aaf932
commit eb8fb0c2c2
11 changed files with 254 additions and 185 deletions

View File

@ -1,19 +1,22 @@
import json
import string
import logging
logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
from funcs import logThis
def getName(user : str):
logThis("Getting name for "+user+"'s character")
with open("resources/swcharacters.json", "r") as f:
data = json.load(f)
if user in data:
logThis("Name is "+data[user]["Name"])
return data[user]["Name"]
else:
logThis("Just using "+user)
return user
def setUpDict(cmd : dict):
logThis("Setting up a dictionary in a nice way")
if bool(cmd):
keys = list(cmd)
values = list(cmd.values())
@ -22,12 +25,20 @@ def setUpDict(cmd : dict):
return ", ".join(values)
else:
for x in range(len(keys)):
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + " "
if type(keys[x]) is list:
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + ", ".join(values[x]) + " "
else:
result += "**" + keys[x] + "**" + ": " + ", ".join(values[x]) + "\n"
else:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + "\n"
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + " "
else:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + "\n"
logThis("Returning a dictionary, but well formatted")
return result
else:
logThis("Couldn't find anything")
return "There doesn't seem to be anything here..."
def lookUp(data : dict, key : str, cmd : str = ""):
@ -39,62 +50,87 @@ def lookUp(data : dict, key : str, cmd : str = ""):
if cmd == "":
break
logThis("Looking up "+key)
if key in data:
logThis(key+" exists")
if cmd == "":
if type(data[key]) is dict and key != "Weapons":
return setUpDict(data[key])
elif key == "Weapons":
logThis("Does this even get used? I'm too scared to delete it")
if bool(data[key]):
logThis("Found "+(", ".join(list(data[key]))))
return ", ".join(list(data[key]))
else:
logThis("There is nothing here")
return "There doesn't seem to be anything here..."
else:
return data[key]
if str(data[key]) != "":
logThis("Returning "+str(data[key]))
return data[key]
else:
logThis("There was nothing there")
return "There doesn't seem to be anything here"
elif cmd[0] == '+':
logThis("Trying to add to "+key)
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Yeah, that fucked up")
return "Can't do that"
if type(data[key]) is int:
try:
newValue = data[key] + int(cmd)
data[key] = newValue
logThis("Added "+cmd+" to "+key)
return data
except:
logThis("Couldn't add "+cmd+" to "+key)
return "Can't add that"
elif type(data[key]) is list:
try:
data[key].append(cmd)
logThis("Added "+cmd+" to "+key)
return data
except:
logThis("Couldn't add "+cmd+" to "+key)
return "Can't add that"
else:
logThis("Yeah, I can't add that to "+key)
return "Can't add that"
elif cmd[0] == '-':
logThis("Trying to remove/subtract from "+key)
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Yeah, that fucked up")
return "Can't do that"
if type(data[key]) is int:
try:
newValue = data[key] - int(cmd)
data[key] = newValue
logThis("Subtracted "+cmd+" from "+key)
return data
except:
logThis("Couldn't subtract "+cmd+" from "+key)
return "Can't remove that"
elif type(data[key]) is list:
try:
data[key].remove(cmd)
logThis("Removed "+cmd+" from "+key)
return data
except:
logThis("Couldn't remove "+cmd+" from "+key)
return "Can't remove that"
else:
logThis("Yeah, I can't remove/subtract that from "+key)
return "Can't remove that"
else:
while cmd[0] == ' ':
@ -103,22 +139,34 @@ def lookUp(data : dict, key : str, cmd : str = ""):
if type(data[key]) is dict:
newKey = cmd.split(" ")[0]
cmd = cmd[len(newKey):]
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
data[key] = lookUp(data[key],newKey,cmd)
return data
if cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking up "+newKey+" in "+key)
lookUpResult = lookUp(data[key],newKey,cmd)
if type(lookUpResult) is dict:
data[key] = lookUpResult
return data
else:
return lookUpResult
elif type(data[key]) != list:
logThis("Trying to change "+key+" to "+cmd)
try:
cmd = type(data[key])(cmd)
data[key] = cmd
logThis("Did that")
return data
except:
logThis("No. That did not work")
return "Wrong data type"
else:
logThis("Yeah, that didn't work")
return "Wrong data type"
else:
logThis("Couldn't find "+key)
logThis("Testing to see if it's a multi-word key")
cmd = key + " " + cmd
words = cmd.split(" ")
search = ""
@ -128,9 +176,11 @@ def lookUp(data : dict, key : str, cmd : str = ""):
search += " " + words[i]
i += 1
except:
logThis("It wasn't. "+search+" doesn't exist")
return search + " doesn't exist"
if search[0] == " ":
search = search[1:]
logThis("Yeah! Did that! The key was "+search)
cmd = cmd[len(search):]
if cmd != "":
@ -138,16 +188,18 @@ def lookUp(data : dict, key : str, cmd : str = ""):
cmd = cmd[1:]
if cmd == "":
break
if cmd == "":
logThis("Returning "+search)
return setUpDict(data[search])
else:
newKey = cmd.split(" ")[0]
cmd = cmd[len(newKey):]
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
if cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
lookUpResult = lookUp(data[search],newKey,cmd)
if type(lookUpResult) is dict:
data[search] = lookUpResult
@ -156,6 +208,7 @@ def lookUp(data : dict, key : str, cmd : str = ""):
return lookUpResult
def characterSheet(character : dict):
logThis("Setting up a character sheet for "+character["Name"])
divider = "--------------------\n"
name = character["Name"]
textf = ""
@ -197,30 +250,40 @@ def charData(user : str,cmd : str):
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking for "+user+"'s character")
if user in data:
logThis("Foundt it! Looking for "+key+" in the data")
if key in data[user]:
logThis("Found it!")
if type(data[user][key]) is dict:
logThis("It's a dictionary!")
if cmd == "":
logThis("Retrieving data")
if key == "Weapons":
if bool(data[user][key]):
logThis("Returning a list of weapons")
return ", ".join(list(data[user][key]))
else:
logThis("The character doesn't have any weapons. Which is probably for the best. Like, who just walks around with weapons?")
return "There doesn't seem to be anything there..."
else:
return setUpDict(data[user][key])
elif cmd[0] == "+":
logThis("Gonna add something!!!")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Nope. That didn't happen")
return "Can't do that"
if (key == "Talents" or key == "Force-powers") and "," in cmd:
cmd = cmd.split(",")
while cmd[1][0] == " ":
cmd[1] = cmd[1][1:]
logThis("Adding "+cmd[0]+" to "+key)
data[user][key][cmd[0]] = cmd[1]
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
@ -230,9 +293,11 @@ def charData(user : str,cmd : str):
cmd = cmd.split(",")
while cmd[1][0] == " ":
cmd[1] = cmd[1][1:]
logThis("Adding "+cmd[0]+" to "+key)
try:
data[user][key][cmd[0]] = int(cmd[1])
except:
logThis("Fucked that up")
return "Wrong data type"
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
@ -242,6 +307,7 @@ def charData(user : str,cmd : str):
with open("resources/swtemplates.json", "r") as f:
templates = json.load(f)
newWeapon = templates["Weapon"]
logThis("Adding "+cmd+" to "+key)
data[user][key][cmd] = newWeapon
with open("resources/swcharacters.json", "w") as f:
@ -249,34 +315,42 @@ def charData(user : str,cmd : str):
return cmd+" added to weapons for " + data[user]["Name"]
else:
logThis("That's not happening")
return "Can't add that"
elif cmd[0] == "-":
logThis("Gonna subtract/remove something")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("AAAAAAAAAAAA")
return "Can't do that"
if key == "Talents" or key == "Force-powers" or key == "Weapons" or key == "Obligations":
logThis("Trying to remove "+cmd+" from "+key)
if cmd in data[user][key]:
del data[user][key][cmd]
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return cmd+" removed from "+key+" for "+data[user]["Name"]
logThis("I did that")
return cmd+" removed from "+key+" from "+data[user]["Name"]
else:
logThis("Welp. I fucked that up")
return "Can't remove that"
else:
logThis("Urgh!")
return "Can't remove that"
else:
if key != "Talents" and key != "Force-powers":
newKey = string.capwords(cmd.split(" ")[0])
newcmd = cmd[len(newKey):]
else:
logThis("Looking up "+cmd+" in "+key)
if key == "Talents" or key == "Force-powers":
newKey = cmd
newcmd = ""
else:
newKey = string.capwords(cmd.split(" ")[0])
newcmd = cmd[len(newKey):]
lookUpResult = lookUp(data[user][key],newKey,newcmd)
@ -289,69 +363,86 @@ def charData(user : str,cmd : str):
return lookUpResult
else:
if cmd == "":
logThis("Retrieving data")
if type(data[user][key]) is list:
return key+":\n"+", ".join(data[user][key])
else:
return data[user][key]
elif cmd[0] == '+':
logThis("Adding")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Error message")
return "Can't do that"
if type(data[user][key]) is int:
try:
logThis("Adding "+cmd+" to "+key)
beforeData = data[user][key]
data[user][key] = beforeData+ int(cmd)
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Added " + cmd + " to " + data[user]["Name"] + "'s " + key
except:
logThis("BITCH SANDWICH")
return "Can't add that"
elif type(data[user][key]) is list:
try:
logThis("Adding "+cmd+" to "+key)
data[user][key].append(cmd)
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Added " + cmd + " to " + data[user]["Name"] + "'s " + key
except:
logThis("tstststststs")
return "Can't add that"
else:
logThis("Help")
return "Can't add that"
elif cmd[0] == '-':
logThis("Removing/subtracting")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("lalalala")
return "Can't do that"
if type(data[user][key]) is int:
try:
logThis("Subtracting "+cmd+" from "+key)
beforeData = data[user][key]
data[user][key] = beforeData - int(cmd)
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Subtracted " + cmd + " from " + data[user]["Name"] + "'s " + key
except:
logThis("Tried it. Didn't want to")
return "Can't remove that"
elif type(data[user][key]) is list:
try:
logThis("removing "+cmd+" from "+key)
beforeData = data[user][key]
try:
data[user][key].remove(cmd)
except:
logThis("They can only remove stuff that's actually in the list")
return "Not in list"
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Removed " + cmd + " from " + data[user]["Name"] + "'s " + key
except:
logThis("nah")
return "Can't remove that"
else:
logThis("nyope")
return "Can't remove that"
else:
logThis("Changing "+key+" to "+cmd)
if type(data[user][key]) is int:
try:
data[user][key] = int(cmd)
@ -360,14 +451,17 @@ def charData(user : str,cmd : str):
elif type(data[user][key]) is str:
data[user][key] = cmd
else:
logThis("I don't wanna tho")
return "Can't do that"
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Changed " + data[user]["Name"] + "'s " + key +" to " + cmd
else:
logThis(key+" isn't in there")
return "Couldn't find that data. Are you sure you spelled it correctly?"
else:
logThis(user+" doesn't have a character")
return "You don't have a character. You can make one with !swchar"
def replaceSpaces(cmd : str):
@ -408,6 +502,7 @@ def parseChar(user : str, cmd : str):
text1, text2 = characterSheet(data[user])
return text1, replaceWithSpaces(text2)
else:
logThis("Makin' a character for "+user)
with open("resources/swtemplates.json", "r") as f:
templates = json.load(f)
newChar = templates["Character"]
@ -417,6 +512,7 @@ def parseChar(user : str, cmd : str):
return "", "Character for " + user + " created"
else:
if cmd == "Purge":
logThis("Deleting "+user+"'s character")
del data[user]
with open("resources/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)

View File

@ -1,6 +1,9 @@
from . import swroll
from funcs import logThis
def destinyNew(num : int):
logThis("Creating a new destiny pool with "+str(num)+" players")
roll = swroll.roll(0,0,0,0,0,0,num)
with open("resources/destinyPoints.txt","wt") as f:
@ -13,20 +16,26 @@ def destinyUse(user : str):
points = f.read()
if user == "Nikolaj":
logThis("Trying to use a dark side destiny point")
if 'B' in points:
points = points.replace("B","L",1)
with open("resources/destinyPoints.txt","wt") as f:
f.write(points)
logThis("Did it")
return "Used a dark side destiny point. Destiny pool is now:\n"+swroll.resultToEmoji(points)
else:
logThis("There were no dark side destiny points")
return "No dark side destiny points"
else:
logThis("Trying to use a light side destiny point")
if 'L' in points:
points = points.replace("L","B",1)
with open("resources/destinyPoints.txt","wt") as f:
f.write(points)
logThis("Did it")
return "Used a light side destiny point. Destiny pool is now:\n"+swroll.resultToEmoji(points)
else:
logThis("There were no dark side destiny points")
return "No light side destiny points"
def parseDestiny(user : str, cmd : str):
@ -38,6 +47,7 @@ def parseDestiny(user : str, cmd : str):
if cmd == "":
logThis("Retrieving destiny pool info")
with open("resources/destinyPoints.txt","rt") as f:
return swroll.resultToEmoji(f.read())
else:

View File

@ -5,6 +5,7 @@ import json
import os
from . import swchar
from funcs import logThis
with open("resources/swskills.json", "r") as f:
skillData = json.load(f)
@ -35,6 +36,7 @@ def roll(abi : int = 1, prof : int = 0, dif : int = 3, cha : int = 0, boo : int
return result
def simplify(result : str):
logThis("Simplifying "+result)
simp = ""
success = (result.count('S') + result.count('R')) - (result.count('F') + result.count('D'))
advantage = result.count('A') - result.count('H')
@ -110,10 +112,8 @@ def diceToEmoji(dice : list):
return emoji
def getDice(user : str, skill : str):
return "yes"
def obligationRoll():
logThis("Rolling for obligation")
with open("resources/swcharacters.json", "r") as f:
data = json.load(f)
@ -144,10 +144,13 @@ def parseRoll(user : str,cmd : str = ""):
return obligationRoll()
elif string.capwords(commands[0]) in skillData:
logThis("Oh look! This guy has skills!")
if swchar.userHasChar:
logThis("They have a character. That much we know")
skillLevel = swchar.charData(user,"Skills " + string.capwords(commands[0]))
if string.capwords(commands[0]) == "Lightsaber":
logThis("The skill is lightsaber")
charLevel = swchar.charData(user,"Characteristics " + swchar.lightsaberChar(user))
else:
charLevel = swchar.charData(user,"Characteristics " + skillData[string.capwords(commands[0])])
@ -156,16 +159,20 @@ def parseRoll(user : str,cmd : str = ""):
proficiencyDice = min(skillLevel,charLevel)
commands = [str(abilityDice)] + [str(proficiencyDice)] + commands[1:]
logThis("Converted skill to dice")
else:
logThis("Okay, no they don't i guess")
return "You don't have a user. You can make one with !swchar"
elif string.capwords(commands[0]) in ["Ranged","Piloting"]:
logThis("They fucked up writing the name of a ranged or piloting skill")
if string.capwords(commands[0]) == "Ranged":
return "Did you mean \"Ranged - Heavy\" or \"Ranged - Light\""
else:
return "Did you mean \"Piloting - Planetary\" or \"Piloting - Space\""
try:
logThis("Converting commands to dice")
for x in range(len(commands)):
if commands[x-1] != "":
if commands[x-1][0] == "A":
@ -185,8 +192,10 @@ def parseRoll(user : str,cmd : str = ""):
else:
rollParameters[x-1] = int(commands[x-1])
except:
logThis("Someone fucked u-up! (it was them)")
return "Invalid input!"
logThis("Rolling "+str(rollParameters))
rollResults = roll(rollParameters[0],rollParameters[1],rollParameters[2],rollParameters[3],rollParameters[4],rollParameters[5],rollParameters[6])
simplified = simplify(rollResults)
@ -194,7 +203,9 @@ def parseRoll(user : str,cmd : str = ""):
name = swchar.getName(user)
if simplified != rollResults:
return name + " rolls " + diceToEmoji(rollParameters) + "\nResult: " + resultToEmoji(rollResults) + "\nSimplified: " + resultToEmoji(simplify(rollResults))
logThis("Returns results and simplified results")
return name + " rolls " + diceToEmoji(rollParameters) + "\nResult: " + resultToEmoji(rollResults) + "\nSimplified: " + resultToEmoji(simplify(rollResults))
else:
logThis("Returns results")
return name + " rolls " + diceToEmoji(rollParameters) + "\nResult: " + resultToEmoji(rollResults)