def cheat(levelDict, stateDict, num): """ Automatically clears a level for you. """ data = game.readSession() if num == "5": print("You cannot cheat here.") else: i = 0 for key, value in stateDict["objects"].items(): try: if value["keyAction"] is False: i += 1 value["visibility"] = 1 value["keyAction"] = True except KeyError: pass stateDict["state"] += i stateDict["finished"] = True stateDict["allDone"] = True stateDict["praised"] = True data["levels"][num] = stateDict game.writeSession(data) if num != "4": for item in levelDict["objects"]: for key, value in item.items(): if "Statue" in key: target = key objDict = dict_handler.objectTake(target, num) object_actions.itemTake(objDict, stateDict, num) print("You have automatically cleared this level.")
def itemTake(objDict, stateDict, num): """ Places an item in your inventory. """ inventory = objDict["inventory"] name = objDict.pop("itemName", None) data = game.readSession() if name in data["inventory"]: print("You've already picked this up.") elif name in data["dropped"]: ohJoy = data["dropped"] data["inventory"][name] = ohJoy.pop(name, None) level = data["inventory"][name]["level"] objectKey = data["inventory"][name]["key"] levelObjects = data["levels"][level]["objects"] levelObjects[objectKey]["inInv"] = True print(data["inventory"][name]["take"]) game.writeSession(data) else: thing = objDict["thing"] data["levels"][num]["objects"][thing]["inInv"] = True data["inventory"][name] = inventory game.writeSession(data) try: if objDict["key"] == "take" and stateDict["objects"][thing][ "keyAction"] is False: keyAction(objDict, num) elif stateDict["objects"][thing]["keyAction"] is True: print(objDict["take"]) else: print(objDict["take"]) except KeyError: print(objDict["take"])
def basicCommands(command, objDict, stateDict, num): """ Processes the object commands, and in case of the few "special" ones, sends them along to their second destination. """ if command == "take" and "take" in objDict["commands"]: itemTake(objDict, stateDict, num) elif command == "drop" and "drop" in objDict["commands"]: target = objDict["itemName"] itemDrop(target, num) elif command == "use" and "use" in objDict["commands"]: itemUse(objDict, num, stateDict) elif command in objDict["commands"]: try: thing = objDict["thing"] if objDict["key"] == command and stateDict["objects"][thing][ "keyAction"] is False: keyAction(objDict, num) elif objDict["key"] is not command: if command == "kick" and stateDict["objects"][thing][ "inInv"] is True: print("You cannot kick an object you are carrying.") else: print(objDict[command]) except KeyError: if command == "kick" and stateDict["objects"][thing][ "inInv"] is True: print("You cannot kick an object you are carrying.") else: print(objDict[command]) try: if command == "kick" and objDict["whenDone"] == "allDone": data = game.readSession() data["gameStats"]["kicks"] += 1 game.writeSession(data) except KeyError: pass try: if command == "kick" and objDict["breaks"] is True: data = game.readSession() data["levels"][num]["objects"][thing]["visibility"] = -1 game.writeSession(data) except KeyError: pass else: print( "Joko forbids you from doing this with {target}. Shame on you for trying." .format(target=objDict["itemName"]))
def getCommands(commandType): """ Compiles commands, for easy reference. """ cmdDict = dict() data = game.readSession("schema/info.json") for key, value in data.items(): if key == commandType: cmdDict = value return cmdDict
def getLevel(num): """ Get the dict for the appropriate level """ num = str(num) levelDict = dict() data = game.readSession("schema/levels.json") for key, value in data.items(): if key == num: levelDict = value return levelDict
def fwd(stateDict): """ Attemps to move you one level up. """ if stateDict["finished"] is True: data = game.readSession() data["gameStats"]["level"] += 1 upOne = data["gameStats"]["level"] game.writeSession(data) game.level(upOne) else: print("You have not yet completed this chapter in your story.")
def back(): """ Attemps to move you down one level. """ data = game.readSession() data["gameStats"]["level"] -= 1 if data["gameStats"]["level"] == -1: print("You are already at the beginning. You can only move forward.") else: downOne = data["gameStats"]["level"] game.writeSession(data) game.level(downOne)
def inventory(): """ Lists your inventory items. """ data = game.readSession() pInventory = data["inventory"] print("In your satchel, or on your person, you currently have:\n") if pInventory: for key, value in pInventory.items(): print("{key}: {desc}\n".format(key=key.capitalize(), desc=value["inInv"])) else: print("You are not carrying anything. You are naked.")
def getState(num): """ Get the dict of the player state. """ num = str(num) stateDict = dict() save = game.readSession() for key, value in save.items(): if key == "levels": for k, level in value.items(): if k == num: stateDict = level return stateDict
def save(savename=""): """ Saves current session to save file. """ if savename: if savename[-5:] != ".json": print( "You need to enter the filename in this format: \"name.json\"") else: copyfile("saves/currentSession.json", "saves/" + savename) else: data = game.readSession() savename = data["gameStats"]["saveFile"] copyfile("saves/currentSession.json", "saves/" + savename) print("You succesfully saved your progress to the file " + savename)
def itemDrop(target, num): """ Removes an item from your inventory. """ data = game.readSession() inventory = data["inventory"] data["dropped"][target] = inventory.pop(target, None) try: data["dropped"][target]["droppedAt"] = num level = data["dropped"][target]["level"] objectKey = data["dropped"][target]["key"] levelObjects = data["levels"][level]["objects"] levelObjects[objectKey]["inInv"] = False if data["dropped"][target]["droppedAt"] != data["dropped"][target][ "level"]: levelObjects[objectKey]["droppedElsewhere"] = True game.writeSession(data) print(data["dropped"][target]["drop"]) except TypeError: print("You cannot drop what you aren't carrying.")
def keyAction(objDict, num): """ Handles increasing the state and such. """ thing = objDict["thing"] data = game.readSession() activeLevel = data["levels"][num] activeLevel["state"] += 1 activeLevel["objects"][thing]["keyAction"] = True if objDict["whenDone"] != "allDone": revealed = objDict["whenDone"] if isinstance(revealed, list): for item in revealed: activeLevel["objects"][item]["visibility"] = 1 else: activeLevel["objects"][revealed]["visibility"] = 1 else: activeLevel["allDone"] = True data["levels"][num] = activeLevel game.writeSession(data) print("\n" + objDict["keyAction"])
def objects(levelDict, stateDict, num): """ Lists all the (currently visible) objects on a level. """ print("You look around you, and this is what you see:\n") data = game.readSession() if data["dropped"]: dropped = data["dropped"] try: for key, value in dropped.items(): if value["droppedAt"] == num and value["level"] != num: print(key.capitalize() + ": {desc}".format(desc=value["inInv"])) except KeyError: pass for key, value in stateDict["objects"].items(): if value["visibility"] >= 0 and value["inInv"] is False and value[ "droppedElsewhere"] is False: key = int(key) for k, v in levelDict["objects"][key].items(): print("{item}: {desc}".format(item=k, desc=v["description"]))
def praiseJoko(levelDict, stateDict, num): """ It is so important, to praise Joko. """ praises = dict_handler.getCommands("praises") if stateDict["allDone"] is True and stateDict["praised"] is False: print(praises["allDone"]) data = game.readSession() data["levels"][num]["praised"] = True data["levels"][num]["finished"] = True game.writeSession(data) print(levelDict["whenDone"]) elif num == "4": game.lastLevel() else: print("Praise Joko!") maxInt = len(praises["PSA"]) - 1 number = randint(0, maxInt) msg = "And in answer to your praise of King Joko, your mind stirs" msg2 = "with memory of what you have heard many times before..." print("{msg1}, {msg2}".format(msg1=msg, msg2=msg2)) print("\n\"" + praises["PSA"][number] + "\"")
def objectDropped(target, action): """ Since dropped objects don't always get dropped on their level, we have to deal with it separately. Also, handy for use-items. """ tempDict = dict() data = game.readSession() inventory = data["inventory"] item = target.lower() for thing, values in inventory.items(): if thing == item: tempDict = values try: level = tempDict["level"] levelDict = getLevel(level) stateDict = getState(level) objDict = objectChecker(item, levelDict, stateDict) return objDict except KeyError: if action == "drop": print("You cannot drop what you aren't carrying.") elif action == "use": print("You can't use something you haven't got.")
def objectTake(target, level): """ Because inventory just made things complicated. """ tempDict = dict() data = game.readSession() target = target.lower() dropped = data["dropped"] for thing, values in dropped.items(): if thing == target: tempDict = values if not tempDict: stateDict = getState(level) levelDict = getLevel(level) objDict = objectChecker(target, levelDict, stateDict) elif tempDict["droppedAt"] == level: orgLevel = int(tempDict["level"]) stateDict = getState(orgLevel) levelDict = getLevel(orgLevel) objDict = objectChecker(target, levelDict, stateDict) try: return objDict except UnboundLocalError: print("What you're trying to pick up isn't here.")
def keyActionUse(objDict, num): """ Again, the problem with doing things with inventory items on a level they didn't originate from. """ thing = objDict["thing"] level = objDict["level"] data = game.readSession() activeLevel = data["levels"][num] activeLevel["state"] += 1 objLevel = data["levels"][level] objLevel["objects"][thing]["keyAction"] = True if objDict["whenDone"] != "allDone": revealed = objDict["whenDone"] if isinstance(revealed, list): for item in revealed: activeLevel["objects"][item]["visibility"] = 1 else: activeLevel["objects"][revealed]["visibility"] = 1 else: activeLevel["allDone"] = True data["levels"][num] = activeLevel data["levels"][level] = objLevel game.writeSession(data) print("\n" + objDict["keyAction"])