def transfer(itemid, toid, fromid=["player"], action="give", val=""): toSlot = find.invSlotByItemID(None, toid) if toSlot < 11: fromSlot = find.invSlotByItemID(itemid, fromid) db.cur.execute("update inventory set item" + str(toSlot) + " = \"" + itemid[0] + "\" where charid = \"" + toid[0] + "\";") db.cur.execute("update inventory set item" + str(fromSlot) + " = NULL where charid = \"" + fromid[0] + "\";") else: print("That character's inventory is full.") if action == "give": print( find.nameFromID(itemid, "item")[0] + " given to " + find.nameFromID(toid, "people")[0] + ".") elif action == "buy": print( find.nameFromID(itemid, "item")[0] + " bought from " + find.nameFromID(fromid, "people")[0] + " for " + str(val) + ".") elif action == "rent": print( find.nameFromID(itemid, "item")[0] + " rented from " + find.nameFromID(fromid, "people")[0] + " for " + str(val) + ".") else: sysmsg.show("noaction")
def text(txtid): """String -> String.""" db.cur.execute("select txt from text where txtid = \"" + txtid + "\";") result = db.cur.fetchall() if db.cur.rowcount > 0: for row in result: return row[0] + "\n" else: sysmsg.show("notext")
def expandCmd(plinput): """Alias to command. Returns a word list.""" if len(plinput) > 0: # Something was input. if verifyCmd(plinput): # The function returns True if the command exists. Do the thing! return g.cmdlist[g.cmdalias.index(plinput)] # Replace player input command alias with the real command. else: # Unknown command. sysmsg.show("invalidcmd",plinput[0]) else: # Nothing was input. sysmsg.show("emptycmd")
def which(idlist,listType="",charid=[]): """When a query returned more than one result and you need to ask which one to use. Returns a list with a single item. idlist List: List of IDs. listType String: Type of IDs. Valid values: item/people/location/topic charid List: charid of character whose inventory/topic to search if listType is item or topic.""" if g.debug: print("[DBG] ask.which:",idlist,listType,charid) valid = False names = find.nameFromID(idlist,listType) while not valid: if g.debug: print("[DBG] ask.which NAMES",names) if len(names) > 1 and len(set(names)) == 1: # Two things listed, they have the same name -> same type, use the first one. if g.debug: print("[DBG] ask.which FOUND ONLY ONE UNIQUE IN:",names) plinput = idlist[:1] valid = True elif len(charid) == 0 and not valid: print("Did you mean "+parse.naturalList(names, "or")+"? (empty to cancel)") plinput = find.idFromName(parse.sanitize(str(input("> "))),listType) if g.debug: print("[DBG] ask.which PLINPUT:",plinput) if len(plinput) == 1 or len(plinput) == 0: valid = True else: names = find.nameFromID(plinput,listType) elif len(charid) > 0 and not valid: print("Did you mean "+parse.naturalList(names, "or")+"? (empty to cancel)") plinput = find.idFromName(parse.sanitize(str(input("> "))),listType,charid) if g.debug: print("[DBG] ask.which PLINPUT:",plinput) if len(plinput) == 1 or len(plinput) == 0: valid = True else: names = find.nameFromID(plinput,listType) else: sysmsg.show("argerror") if g.debug: print("[DBG] ask.which RETURNS:",plinput) return plinput
def nameFromID(inlist, nameType): """Find item/char/loc names from a list of IDs. Returns a list with the names.""" inputwords = [] # List of input words. namelist = [] # List of output IDs. invalid = False # Invalid input name? done = False # Done with the search? inputlist = inlist.copy( ) # Don't ask. It's because of objects and referencing and things that I don't fully understand. With this, you can still print the original input. while invalid == False and done == False and len( inputlist ) > 0: # Keep searching until we come across an invalid input item or we are done with the search. inputwords = [] inputwords.append( inputlist.pop(0)) # Take out first search term from input list. inputid = "%".join(a for a in inputwords) # Add to search string. if nameType == "item": db.cur.execute( "select itemtype.name from item, itemtype where item.itemid = \"" + inputid + "\" and itemtype.typeid = item.typeid;") result = parse.multiTupleListToList(db.cur.fetchall()) for a in result: namelist.append(a) elif nameType == "people": db.cur.execute("select name from people where charid = \"" + inputid + "\";") result = parse.multiTupleListToList(db.cur.fetchall()) for a in result: namelist.append(a) elif nameType == "location": db.cur.execute("select name from location where locid =\"" + inputid + "\";") result = parse.multiTupleListToList(db.cur.fetchall()) for a in result: namelist.append(a) elif nameType == "topic": #print("looking topic",inputname,charid[0]) db.cur.execute("select topic from dialogue where dlgid = \"" + inputid + "\";") result = parse.multiTupleListToList(db.cur.fetchall()) for a in result: namelist.append(a) #print("potential topics",idlist) else: sysmsg.show("notype") #print("namelist with duplicates",namelist,"and without",parse.removeDuplicates(namelist)) return namelist
def listValueByID(idlist, table="item"): """List -> List""" vallist = [] if table == "item": for a in idlist: db.cur.execute( "select itemtype.val from item, itemtype where item.itemid = \"" + a + "\" and item.typeid = itemtype.typeid;") vallist.append(db.cur.fetchall()[0][0]) return vallist elif table == "people": for a in idlist: db.cur.execute("select val from people where charid = \"" + a + "\";") vallist.append(db.cur.fetchall()[0][0]) return vallist else: sysmsg.show("notype")
def parse(plinput): """String -> List.""" wordlist = sanitize(plinput) # Sanitize player input, take out whitespace, return a list of words. if len(wordlist) > 0: # Something was input. if verifyCmd(wordlist[0]): # The function returns True if the command exists. func = expandCmd(wordlist.pop(0)) # Delete data from list at index 0. Place data into variable func. if func not in g.multiArgCmds: # Some special cases. wordlist = cmdStrip(wordlist,False) else: wordlist = cmdStrip(wordlist,True) # Use alternate stripping for these commands. if g.debug: print("[DBG] parse STRIPPED INPUT:",func,wordlist) if func != "quit" and func != "help": # Special cases are needed because "quit" and "help" are Python reserved name. if wordlist: # Returns True is wordlist is not empty. #try: # Try to do: getattr(cmd, func)(wordlist) getattr(cmd, func)(wordlist) # Is the same as: cmd.func(wordlist) #except AttributeError: # getattr(cmd, func)(wordlist) failed with a Python error, do this instead: # sysmsg.show("unimplementedcmd",func) else: # wordlist is empty so we're calling the function with no arguments since none were given. #try: # Try to do: getattr(cmd, func)() getattr(cmd, func)() # Is the same as: cmd.func() | For example the user input "look" will call the function named "look" in cmd.py with no arguments. #except AttributeError: # getattr(cmd, func)() failed with a Python error, do this instead: # sysmsg.show("unimplementedcmd",func) elif func == "help": # Help command. cmd.helpcmd(wordlist) elif func == "quit": # Quit command. cmd.quitgame() else: sysmsg.show("cmdcheck") else: # Unknown command. sysmsg.show("invalidcmd",wordlist[0]) else: # Nothing was input. sysmsg.show("emptycmd")
def idFromName(inlist, nameType, charid=[], local=True): """Find itemid/charid/locid from a list of words. Returns a list with the IDs. inlist List: Search words. nameType String: Name of database table to search from. One per search. Valid values: item/people/location/topic charid List: charid of character whose inventory/topic to search if listType is item or topic. local Boolean: Search in the location where the player currently is?""" if g.debug: print("[DBG] idFromName ARGUMENTS:", inlist, nameType, charid, local) inputwords = [] # List of input words. idlist = [] # List of output IDs. inputlist = inlist.copy( ) # Don't ask. It's because of objects and referencing and things that I don't fully understand. With this, you can still print the original input. inputname = "" while len(inputlist) > 0: idlist = [] # Empty data for names with more than one word. inputname = "" # Empty data for names with more than one word. if g.debug: print("[DBG] idFromName INPUTLIST:", inputlist) inputwords.append( inputlist.pop(0)) # Take out first search term from input list. if g.debug: print("[DBG] idFromName INPUTWORDS POPPED:", inputwords) checkname = "%".join( a for a in inputwords) # Add to search string. % is a wildcard. for letter in checkname: # For character in string. if letter != "'": # Letter is not '. inputname += letter # Add it to the new string. else: inputname += "%" # Replace ' with wildcard % so the MySQL queries work. if g.debug: print("[DBG] idFromName INPUTNAME CLEANED:", inputname) if nameType == "item": if len(charid) > 0: db.cur.execute( "select item.itemid from item, itemtype where itemtype.name like \"%" + inputname + "%\" and itemtype.typeid = item.typeid;") result = parse.multiTupleListToList(db.cur.fetchall()) for a in result: if a in listInvItemIDs( charid): # If itemid in player inventory. idlist.append(a) elif len(charid) == 0 and local: db.cur.execute( "select item.itemid from item, itemtype, people where itemtype.name like \"%" + inputname + "%\" and itemtype.typeid = item.typeid and item.locid = people.locid and people.charid = \"player\";" ) idlist = parse.multiTupleListToList(db.cur.fetchall()) else: # Global search. db.cur.execute( "select item.itemid from item, itemtype where itemtype.name like \"%" + inputname + "%\" and itemtype.typeid = item.typeid;") idlist = parse.multiTupleListToList(db.cur.fetchall()) elif nameType == "people": if local: db.cur.execute( "select charid from people where name like \"%" + inputname + "%\" and locid = \"" + plLoc() + "\" and charid != \"player\";") idlist = parse.multiTupleListToList(db.cur.fetchall()) else: db.cur.execute( "select charid from people where name like \"%" + inputname + "%\" and charid != \"player\";") idlist = parse.multiTupleListToList(db.cur.fetchall()) elif nameType == "location": db.cur.execute("select locid from location where name like \"%" + inputname + "%\";") result = parse.multiTupleListToList(db.cur.fetchall()) if local: for a in result: if a in localSpecLoc(): if get.visCheck(a): idlist.append(a) else: idlist = result elif nameType == "topic": #print("looking topic",inputname,charid[0]) db.cur.execute("select dlgid from dialogue where topic like \"%" + inputname + "%\" and charid = \"" + charid[0] + "\";") idlist = parse.multiTupleListToList(db.cur.fetchall()) #print("potential topics",idlist) else: sysmsg.show("notype") #print("idlist with duplicates",idlist,"and no dupes",parse.removeDuplicates(idlist)) if g.debug: print("[DBG] idFromName RETURNS:", idlist) return idlist
def helpcmd(*args): if not args[ 0]: # Returns True when the list given as argument (which is in the args list in index 0) is empty. In other words the player input help and nothing else. print( "\nCATEGORIZED COMMAND LIST\n------------------------\nUse \"help <command>\" to see the all the aliases of a specific command or \"help full\" for a full, uncategorized list.\n" ) # \n is shorthand for new line. print("\nMOVEMENT\n--------") maxmove = max(len(s) for s in g.cmdMovement) maxinter = max(len(s) for s in g.cmdInteraction) maxnoargs = max(len(s) for s in g.cmdNoArgs) for command in g.cmdMovement: db.cur.execute("select alias, dsc from command where alias = \"" + str(command) + "\";") result = db.cur.fetchall() if db.cur.rowcount > 0: # MySQL query returned something. for row in result: spacelist = [" "] * ( maxmove - len(row[0]) + 1 ) # Create a list filled with " " of length "length of longest command - length of current command + 1". spacer = "".join( str(a) for a in spacelist ) # Concatenate created list into a string to use a spacer when priting. print(row[0] + spacer + row[1]) # Print a pretty command list. else: # This should not happen. sysmsg.show("emptylist") print("\nINTERACTION\n-----------") for command in g.cmdInteraction: db.cur.execute("select alias, dsc from command where alias = \"" + str(command) + "\";") result = db.cur.fetchall() if db.cur.rowcount > 0: # MySQL query returned something. for row in result: spacelist = [" "] * ( maxinter - len(row[0]) + 1 ) # Create a list filled with " " of length "length of longest command - length of current command + 1". spacer = "".join( str(a) for a in spacelist ) # Concatenate created list into a string to use a spacer when priting. print(row[0] + spacer + row[1]) # Print a pretty command list. else: # This should not happen. sysmsg.show("emptylist") print("\nNO ARGUMENTS\n------------") for command in g.cmdNoArgs: db.cur.execute("select alias, dsc from command where alias = \"" + str(command) + "\";") result = db.cur.fetchall() if db.cur.rowcount > 0: # MySQL query returned something. for row in result: spacelist = [" "] * ( maxnoargs - len(row[0]) + 1 ) # Create a list filled with " " of length "length of longest command - length of current command + 1". spacer = "".join( str(a) for a in spacelist ) # Concatenate created list into a string to use a spacer when priting. print(row[0] + spacer + row[1]) # Print a pretty command list. else: # This should not happen. sysmsg.show("emptylist") print() # For clarity. elif args[0][ 0] == "full": # Returns True when the list given as argument (which is in the args list in index 0) is empty. In other words the player input help and nothing else. db.cur.execute("select alias, dsc from command;") result = db.cur.fetchall() print( "\nFULL COMMAND LIST\n-----------------\nUse \"help <command>\" to see the description of a specific command and it's aliases or just \"help\" for a categorized list.\n" ) # \n is shorthand for new line. if db.cur.rowcount > 0: # MySQL query returned something. #maxlength = max(len(a[0]) for a in result) # Get the length of the longest command. for row in result: #spacer = " " * (maxlength - len(row[0]) + 1) # Create a string filled with spaces "length of longest command - length of current command + 1" times. print(row[0] + " : " + row[1] + "\n") # Print a pretty command list. print() # For clarity. else: # This should not happen. sysmsg.show("emptylist") elif args[0][0] == "move" or args[0][0] == "movement" or args[0][ 0] == "moving": # Special case for the clueless player. args[0][0] refers to the command player wants help with. #maxlength = max(len(a) for a in movement) # Get the length of the longest command. for command in g.cmdMovement: db.cur.execute("select alias, dsc from command where cmd = \"" + str(command) + "\";") result = db.cur.fetchall() if db.cur.rowcount > 0: # MySQL query returned something. for row in result: #spacelist = [" "] * (maxlength - len(row[0]) + 1) # Create a list filled with " " of length "length of longest command - length of current command + 1". #spacer = "".join(str(a) for a in spacelist) # Concatenate created list into a string to use a spacer when priting. print(row[0] + " : " + row[1] + "\n") # Print a pretty command list. else: # This should not happen. sysmsg.show("emptylist") print() # For clarity. else: if parse.verifyCmd( args[0] [0]): # The function returns True if the command exists. command = g.cmdlist[g.cmdalias.index(args[0][0])] db.cur.execute("select alias, dsc from command where cmd = \"" + str(command) + "\";") result = db.cur.fetchall() if db.cur.rowcount > 1: # MySQL query returned something. #maxlength = max(len(a[0]) for a in result) # Get the length of the longest command. for row in result: #spacelist = [" "] * (maxlength - len(row[0]) + 1) # Create a list filled with " " of length "length of longest command - length of current command + 1". #spacer = "".join(str(a) for a in spacelist) # Concatenate created list into a string to use a spacer when priting. print(row[0] + " : " + row[1] + "\n") # Print a pretty command list. print() # For clarity. elif db.cur.rowcount == 1: # MySQL query returned 1 line. for row in result: print(row[0] + " : " + row[1]) # Print the command with a simple spacer. print() # For clarity. else: # This should not happen. sysmsg.show("emptylist") else: # Unknown command. sysmsg.show("invalidcmd", args[0][0])