コード例 #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
コード例 #2
0
ファイル: interface.py プロジェクト: Cloudxtreme/pymud
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
コード例 #3
0
ファイル: interface.py プロジェクト: BackupTheBerlios/pymud
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