414 lines
16 KiB
Python
414 lines
16 KiB
Python
import json
|
|
import string
|
|
import logging
|
|
|
|
logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
|
|
|
|
def getName(user : str):
|
|
with open("resources/swcharacters.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
if user in data:
|
|
return data[user]["Name"]
|
|
else:
|
|
return user
|
|
|
|
def setUpDict(cmd : dict):
|
|
if bool(cmd):
|
|
keys = list(cmd)
|
|
values = list(cmd.values())
|
|
result = ""
|
|
if type(values[0]) is dict:
|
|
return ", ".join(values)
|
|
else:
|
|
for x in range(len(keys)):
|
|
if x%3 != 2:
|
|
result += "**" + keys[x] + "**" + ": " + str(values[x]) + " "
|
|
else:
|
|
result += "**" + keys[x] + "**" + ": " + str(values[x]) + "\n"
|
|
return result
|
|
else:
|
|
return "There doesn't seem to be anything here..."
|
|
|
|
def lookUp(data : dict, key : str, cmd : str = ""):
|
|
if cmd == " ":
|
|
cmd = ""
|
|
elif cmd != "":
|
|
while cmd[0] == " ":
|
|
cmd = cmd[1:]
|
|
if cmd == "":
|
|
break
|
|
|
|
if key in data:
|
|
if cmd == "":
|
|
if type(data[key]) is dict and key != "Weapons":
|
|
return setUpDict(data[key])
|
|
elif key == "Weapons":
|
|
if bool(data[key]):
|
|
return ", ".join(list(data[key]))
|
|
else:
|
|
return "There doesn't seem to be anything here..."
|
|
else:
|
|
return data[key]
|
|
elif cmd[0] == '+':
|
|
try:
|
|
cmd = cmd[1:]
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
except:
|
|
return "Can't do that"
|
|
|
|
if type(data[key]) is int:
|
|
try:
|
|
newValue = data[key] + int(cmd)
|
|
data[key] = newValue
|
|
return data
|
|
except:
|
|
return "Can't add that"
|
|
elif type(data[key]) is list:
|
|
try:
|
|
data[key].append(cmd)
|
|
return data
|
|
except:
|
|
return "Can't add that"
|
|
else:
|
|
return "Can't add that"
|
|
elif cmd[0] == '-':
|
|
try:
|
|
cmd = cmd[1:]
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
except:
|
|
return "Can't do that"
|
|
|
|
if type(data[key]) is int:
|
|
try:
|
|
newValue = data[key] - int(cmd)
|
|
data[key] = newValue
|
|
return data
|
|
except:
|
|
return "Can't remove that"
|
|
elif type(data[key]) is list:
|
|
try:
|
|
data[key].remove(cmd)
|
|
return data
|
|
except:
|
|
return "Can't remove that"
|
|
else:
|
|
return "Can't remove that"
|
|
else:
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
if type(data[key]) != list:
|
|
try:
|
|
cmd = type(data[key])(cmd)
|
|
data[key] = cmd
|
|
return data
|
|
except:
|
|
return "Wrong data type"
|
|
else:
|
|
return "Wrong data type"
|
|
else:
|
|
cmd = key + " " + cmd
|
|
words = cmd.split(" ")
|
|
search = ""
|
|
i = 0
|
|
while search not in data:
|
|
try:
|
|
search += " " + words[i]
|
|
i += 1
|
|
except:
|
|
return search + " doesn't exist"
|
|
if search[0] == " ":
|
|
search = search[1:]
|
|
|
|
cmd = cmd[len(search):]
|
|
if cmd != "":
|
|
while cmd[0] == " ":
|
|
cmd = cmd[1:]
|
|
if cmd == "":
|
|
break
|
|
|
|
if cmd == "":
|
|
return setUpDict(data[search])
|
|
else:
|
|
newKey = cmd.split(" ")[0]
|
|
cmd = cmd[len(newKey):]
|
|
while cmd[0] == " ":
|
|
cmd = cmd[1:]
|
|
if cmd == "":
|
|
break
|
|
lookUpResult = lookUp(data[search],newKey,cmd)
|
|
if type(lookUpResult) is dict:
|
|
data[search] = lookUpResult
|
|
return data
|
|
else:
|
|
return lookUpResult
|
|
|
|
def characterSheet(character : dict):
|
|
divider = "--------------------\n"
|
|
name = character["Name"]
|
|
textf = ""
|
|
if character["Force-rating"] != 0:
|
|
textf = "\n**Force Rating**: "+str(character["Force-rating"])
|
|
|
|
text1 = "**Species**: "+character["Species"]+"\n**Career**: "+character["Career"]+"\n**Specialization Trees**: "+", ".join(character["Specialization-trees"])+textf+"\n**Soak**: "+str(character["Soak"])
|
|
text2 = "\n\n**Wounds**: "+str(character["Wounds"])+"/"+str(character["Wound-threshold"])+"\n**Strain**: "+str(character["Strain"])+"/"+str(character["Strain-threshold"])
|
|
text3 = setUpDict(character["Characteristics"])
|
|
text4 = setUpDict(character["Skills"])
|
|
text5 = ""
|
|
text6 = ""
|
|
text7 = ""
|
|
|
|
if bool(character["Talents"]):
|
|
text5 = "**Talents**: "+",".join(list(character["Talents"]))+"\n\n"
|
|
|
|
if bool(character["Force-powers"]):
|
|
text6 = "**Force Powers**: "+",".join(list(character["Force-powers"]))+"\n\n"
|
|
|
|
if bool(character["Weapons"]):
|
|
text7 = "**Weapons**: "+",".join(list(character["Weapons"]))+"\n\n"
|
|
|
|
return name, text1+text2+"\n\n"+text3+divider+text4+"\n"+divider+text5+text6+text7
|
|
|
|
def charData(user : str,cmd : str):
|
|
with open("resources/swcharacters.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
key = string.capwords(cmd.split(" ")[0])
|
|
cmd = cmd[len(key):]
|
|
if cmd == " ":
|
|
cmd = ""
|
|
elif cmd != "":
|
|
while cmd[0] == " ":
|
|
cmd = cmd[1:]
|
|
if cmd == "":
|
|
break
|
|
|
|
if user in data:
|
|
if key in data[user]:
|
|
if type(data[user][key]) is dict:
|
|
if cmd == "":
|
|
if key == "Weapons":
|
|
if bool(data[user][key]):
|
|
return ", ".join(list(data[user][key]))
|
|
else:
|
|
return "There doesn't seem to be anything there..."
|
|
else:
|
|
return setUpDict(data[user][key])
|
|
elif cmd[0] == "+":
|
|
try:
|
|
cmd = cmd[1:]
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
except:
|
|
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:]
|
|
data[user][key][cmd[0]] = cmd[1]
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return cmd[0]+" added to "+key+" for " + data[user]["Name"]
|
|
|
|
elif key == "Weapons":
|
|
with open("resources/swtemplates.json", "r") as f:
|
|
templates = json.load(f)
|
|
newWeapon = templates["Weapon"]
|
|
data[user][key][cmd] = newWeapon
|
|
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return cmd+" added to weapons for " + data[user]["Name"]
|
|
|
|
else:
|
|
return "Can't add that"
|
|
|
|
elif cmd[0] == "-":
|
|
try:
|
|
cmd = cmd[1:]
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
except:
|
|
return "Can't do that"
|
|
|
|
if key == "Talents" or key == "Force-powers" or key == "Weapons":
|
|
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"]
|
|
else:
|
|
return "Can't remove that"
|
|
else:
|
|
return "Can't remove that"
|
|
|
|
else:
|
|
if key != "Talents" and key != "Force-powers":
|
|
newKey = string.capwords(cmd.split(" ")[0])
|
|
newcmd = cmd[len(newKey):]
|
|
else:
|
|
newKey = cmd
|
|
newcmd = ""
|
|
|
|
lookUpResult = lookUp(data[user][key],newKey,newcmd)
|
|
|
|
if type(lookUpResult) is dict:
|
|
data[user][key] = lookUpResult
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return "Changed " + data[user]["Name"] + "'s " + key
|
|
else:
|
|
return lookUpResult
|
|
else:
|
|
if cmd == "":
|
|
if type(data[user][key]) is list:
|
|
return key+":\n"+", ".join(data[user][key])
|
|
else:
|
|
return data[user][key]
|
|
elif cmd[0] == '+':
|
|
try:
|
|
cmd = cmd[1:]
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
except:
|
|
return "Can't do that"
|
|
|
|
if type(data[user][key]) is int:
|
|
try:
|
|
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 " + user + "'s " + key
|
|
except:
|
|
return "Can't add that"
|
|
elif type(data[user][key]) is list:
|
|
try:
|
|
data[user][key].append(cmd)
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return "Added " + cmd + " to " + user + "'s " + key
|
|
except:
|
|
return "Can't add that"
|
|
else:
|
|
return "Can't add that"
|
|
elif cmd[0] == '-':
|
|
try:
|
|
cmd = cmd[1:]
|
|
while cmd[0] == ' ':
|
|
cmd = cmd[1:]
|
|
except:
|
|
return "Can't do that"
|
|
|
|
if type(data[user][key]) is int:
|
|
try:
|
|
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 " + user + "'s " + key
|
|
except:
|
|
return "Can't remove that"
|
|
elif type(data[user][key]) is list:
|
|
try:
|
|
beforeData = data[user][key]
|
|
try:
|
|
data[user][key].remove(cmd)
|
|
except:
|
|
return "Not in list"
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return "Removed " + cmd + " from " + user + "'s " + key
|
|
except:
|
|
return "Can't remove that"
|
|
else:
|
|
return "Can't remove that"
|
|
else:
|
|
if type(data[user][key]) is int:
|
|
try:
|
|
data[user][key] = int(cmd)
|
|
except:
|
|
return "Can't do that"
|
|
elif type(data[user][key]) is str:
|
|
data[user][key] = cmd
|
|
else:
|
|
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:
|
|
return "Couldn't find that data. Are you sure you spelled it correctly?"
|
|
else:
|
|
return "You don't have a character. You can make one with !swchar"
|
|
|
|
def replaceSpaces(cmd : str):
|
|
withSpaces = ["Specialization Trees","Wound Threshold","Strain Threshold","Defense - Ranged","Defense - Melee","Force Rating","Core Worlds","Outer Rim","Piloting - Planetary","Piloting - Space","Ranged - Heavy","Ranged - light","Lightsaber Characteristic","Critical Injuries","Force Powers"]
|
|
withoutSpaces = ["Specialization-trees","Wound-threshold","Strain-threshold","Defense-ranged","Defense-melee","Force-rating","Core-worlds","Outer-rim","Piloting-planetary","Piloting-space","Ranged-heavy","Ranged-light","Lightsaber-characteristic","Critical-injuries","Force-powers"]
|
|
|
|
for x in range(len(withoutSpaces)):
|
|
cmd = cmd.replace(withSpaces[x],withoutSpaces[x])
|
|
|
|
return cmd
|
|
|
|
def replaceWithSpaces(cmd : str):
|
|
withSpaces = ["Specialization Trees","Wound Threshold","Strain Threshold","Defense - Ranged","Defense - Melee","Force Rating","Core Worlds","Outer Rim","Piloting - Planetary","Piloting - Space","Ranged - Heavy","Ranged - light","Lightsaber Characteristic","Critical Injuries","Force Powers"]
|
|
withoutSpaces = ["Specialization-trees","Wound-threshold","Strain-threshold","Defense-ranged","Defense-melee","Force-rating","Core-worlds","Outer-rim","Piloting-planetary","Piloting-space","Ranged-heavy","Ranged-light","Lightsaber-characteristic","Critical-injuries","Force-powers"]
|
|
|
|
for x in range(len(withoutSpaces)):
|
|
cmd = cmd.replace(withoutSpaces[x],withSpaces[x])
|
|
|
|
return cmd
|
|
|
|
def parseChar(user : str, cmd : str):
|
|
|
|
cmd = replaceSpaces(cmd)
|
|
|
|
with open("resources/swcharacters.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
if cmd == " ":
|
|
cmd = ""
|
|
elif cmd != "":
|
|
while cmd[0] == " ":
|
|
cmd = cmd[1:]
|
|
if cmd == "":
|
|
break
|
|
|
|
if cmd == "":
|
|
if user in data:
|
|
text1, text2 = characterSheet(data[user])
|
|
return text1, replaceWithSpaces(text2)
|
|
else:
|
|
with open("resources/swtemplates.json", "r") as f:
|
|
templates = json.load(f)
|
|
newChar = templates["Character"]
|
|
data[user] = newChar
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return "", "Character for " + user + " created"
|
|
else:
|
|
if cmd == "Purge":
|
|
del data[user]
|
|
with open("resources/swcharacters.json", "w") as f:
|
|
json.dump(data,f,indent = 4)
|
|
return "", "Character for " + user + " deleted"
|
|
return "", replaceWithSpaces(charData(user,cmd))
|
|
|
|
def lightsaberChar(user : str):
|
|
with open("resources/swcharacters.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
if user in data:
|
|
return data[user]["Lightsaber Characteristic"]
|
|
|
|
def userHasChar(user : str):
|
|
with open("resources/swcharacters.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
return user in data
|
|
|