Fun lil' commit

This commit is contained in:
NikolajDanger
2020-03-22 14:17:42 +01:00
parent 5f3f37dc0a
commit 4f046d7ab2
8 changed files with 150 additions and 9 deletions

View File

@ -1,18 +1,72 @@
import json
import string
def getName(user : str):
with open("characters.json", "r") as f:
data = json.load(f)
if user in data:
return data[user]["Name"]
else:
return user
def setUpDict(cmd : dict):
keys = list(cmd)
values = list(cmd.values())
result = ""
for x in range(len(keys)):
if x%3 != 2:
result += keys[x] + ": " + str(values[x]) + "\t"
else:
result += keys[x] + ": " + str(values[x]) + "\n"
return result
def lookUp(data : dict, key : str, cmd : str = ""):
if key in data:
if cmd == "":
return data[key]
else:
while cmd[0] == ' ':
cmd = cmd[1:]
try:
cmd = type(data[key])(cmd)
data[key] = cmd
return data
except:
return "Wrong data type"
def charData(user : str, key : str,cmd : str = ""):
with open("characters.json", "r") as f:
data = json.load(f)
key = string.capwords(key)
if user in data:
if key in data[user]:
if cmd == "":
return data[user][key]
if type(data[user][key]) is dict:
if cmd == "":
return setUpDict(data[user][key])
else:
newKey = cmd.split(" ")[0]
newcmd = cmd[len(newKey):]
lookUpResult = lookUp(data[user][key],newKey,newcmd)
if type(lookUpResult) is dict:
data[user][key] = lookUpResult
with open("characters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Changed " + data[user]["Name"] + "'s " + key
else:
return lookUpResult
else:
data[user]["Name"] = cmd
with open("characters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Changed " + user + "'s character's name to " + cmd
if cmd == "":
return data[user][key]
else:
data[user][key] = cmd
with open("characters.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: