Esempio n. 1
0
def getItemForUser(cursor, cmdList, roomID, username):
	"""
	Gives a user the item they tried to pickup if possible.
	"""
	query = Q.GetItemsForUser % {DC.RoomID: roomID}
	res = pgDB.fetch(cursor, query)
	print "res = %s" %res
	cmd = cmdList[1]
	print "cmd = %s" %cmd
	desc = "You screw your eyes shut and wish really hard for a %s\r\n" %cmd
	for itemID, keywords, itemCount in res:
		keywords = keywords.split(",")
		print "keywords %s" %keywords
		if cmd in keywords:
			print "cmd in keywords"
			print "itemCount %s"  %itemCount
			if int(itemCount) > 0:
				print "hello" 
				# FIXME: Do all these queries in one transaction.
				# Get The users existing inventory.
				InventoryQ = Q.GetUserInventory % {"UserID":username}
				inventory = pgDB.fetchOne(cursor, InventoryQ)

				# Remove one of the item from the room it was in.
				itemCount = int(itemCount) - 1
				UpdateCount = Q.UpdateItemCount % {"ItemID":itemID,
												"ItemCount":itemCount,}
				pgDB.executeOne(cursor, UpdateCount)

				# Place one of the item into the users inventory.
				newInventory = []
				existing = ""
				if inventory[0]:
					print "inventory %s" %repr(inventory)
					inventory = inventory.split(",")
					for item, count in inventory[0].split(":"):
						if itemID == item:
							count = int(count) + 1
							existing = "1"
							newInventory.append("%s:%s" %(itemID, str(count)))
						else:
							newInventory.append("%s:%s" %(itemID, count))
							
				if not existing:
					newInventory.append("%s:1" %itemID)
				
				newInventory = ",".join(newInventory)
				
				UpdateInvQ = Q.UpdateUserInventory % {"UserID":username,
													"Inventory":newInventory,}
				pgDB.executeOne(cursor, UpdateInvQ)
			else:
				desc = "You pickup a %s\r\n" %cmd
	return desc
Esempio n. 2
0
def getItemForUser(cursor, cmdList, roomID, userID):
    """
	Takes an item from a room and places it in the users inventory.
	"""
    query = Q.GetItemsForUser % {DC.RoomID: roomID}
    res = pgDB.fetch(cursor, query)
    cmd = cmdList[1]
    desc = "You screw your eyes shut and wish really hard for a %s.\r\n" % cmd
    for itemID, keywords, roomItemCount in res:
        keywords = keywords.split(",")
        if cmd in keywords:
            if int(roomItemCount) == -1:
                desc = "I can't do that Dave.\r\n"
            elif int(roomItemCount) > 0:
                # Remove one of the item(s) from the room it was in.
                itemCount = int(roomItemCount) - 1
                updateCount = Q.UpdateRoomItemCount % {
                    "ItemID": itemID,
                    "ItemCount": roomItemCount,
                    "RoomID": roomID
                }
                pgDB.executeOne(cursor, updateCount)

                #Create/update a stack of items if the user already has one.
                userItemCountQ = Q.GetUserItemCount % {
                    "ItemID": itemID,
                    "UserID": userID
                }
                userItemCount = pgDB.fetchOne(cursor, userItemCountQ)

                #increment the item count and update the database
                if not userItemCount:
                    userItemCount = 1

                    updateInvQ = Q.CreateUserItem % {
                        "UserID": userID,
                        "ItemID": itemID,
                        "ItemCount": userItemCount
                    }
                else:
                    userItemCount = int(userItemCount[0]) + 1

                    updateInvQ = Q.UpdateUserItem % {
                        "UserID": userID,
                        "ItemID": itemID,
                        "ItemCount": userItemCount
                    }

                res = pgDB.executeOne(cursor, updateInvQ)
                desc = "You pickup the %s.\r\n" % cmd
    return desc
Esempio n. 3
0
def getUserData(cursor, userid, field = None):
	"""
	Return either one, some, or all of the fields in the UserData
	table for the given userid.
	"""
	if not field:
		query = Q.GetAllUserData % {DC.UserID: userid}
		res = pgDB.fetchDict(cursor, query)
		return res[0]
	else:
		if type(field) in (ListType, TupleType):
			field = ','.join(field)
			query = Q.GetUserData % (field, userid)
			return pgDB.fetchOne(cursor, query)
		else:
			query = Q.GetUserData % (field, userid)
			return pgDB.fetchOneValue(cursor, query)
Esempio n. 4
0
def getUserData(cursor, userid, field=None):
    """
	Return either one, some, or all of the fields in the UserData
	table for the given userid.
	"""
    if not field:
        query = Q.GetAllUserData % {DC.UserID: userid}
        res = pgDB.fetchDict(cursor, query)
        return res[0]
    else:
        if type(field) in (ListType, TupleType):
            field = ','.join(field)
            query = Q.GetUserData % (field, userid)
            return pgDB.fetchOne(cursor, query)
        else:
            query = Q.GetUserData % (field, userid)
            return pgDB.fetchOneValue(cursor, query)
Esempio n. 5
0
def getItemForUser(cursor, cmdList, roomID, userID):
	"""
	Takes an item from a room and places it in the users inventory.
	"""
	query = Q.GetItemsForUser % {DC.RoomID: roomID}
	res = pgDB.fetch(cursor, query)
	cmd = cmdList[1]
	desc = "You screw your eyes shut and wish really hard for a %s.\r\n" %cmd
	for itemID, keywords, roomItemCount in res:
		keywords = keywords.split(",")
		if cmd in keywords:
			if int(roomItemCount) == -1:
				desc = "I can't do that Dave.\r\n"
			elif int(roomItemCount) > 0:
				# Remove one of the item(s) from the room it was in.
				itemCount = int(roomItemCount) - 1
				updateCount = Q.UpdateRoomItemCount % {"ItemID":itemID,
												"ItemCount":roomItemCount,
												"RoomID":roomID}
				pgDB.executeOne(cursor, updateCount)

				#Create/update a stack of items if the user already has one.
				userItemCountQ = Q.GetUserItemCount % { "ItemID" : itemID,
														"UserID" : userID}
				userItemCount = pgDB.fetchOne(cursor, userItemCountQ)

				#increment the item count and update the database
				if not userItemCount:
					userItemCount = 1

					updateInvQ = Q.CreateUserItem % {"UserID":userID,
													"ItemID": itemID,
													"ItemCount": userItemCount}
				else:
					userItemCount = int(userItemCount[0]) + 1

					updateInvQ = Q.UpdateUserItem % {"UserID":userID,
													"ItemID": itemID,
													"ItemCount": userItemCount}

				res	= pgDB.executeOne(cursor, updateInvQ)
				desc = "You pickup the %s.\r\n" %cmd
	return desc
Esempio n. 6
0
def getExits(cursor, roomid):
	query = Q.GetExits % {DC.RoomID: roomid}
	res = pgDB.fetchOne(cursor, query)
	try:
		name1, room1, name2, room2, \
		name3, room3, name4, room4 = res
	except TypeError:
		# Area does not exist.
		print "ERROR! User attempted to go to room %s, which, "\
		"seemingly, does not exist." % roomid
	exits = {}
	if room1:
		area = getArea(cursor, room1)
		exits[lower(name1)] = room1, area
	if room2:
		area = getArea(cursor, room2)
		exits[lower(name2)] = room2, area
	if room3:
		area = getArea(cursor, room3)
		exits[lower(name3)] = room3, area
	if room4:
		area = getArea(cursor, room4)
		exits[lower(name4)] = room4, area
	return exits