Ejemplo n.º 1
0
    internal void CmdTransferFromStoreToInventory(NetworkInstanceId store_id, string item_name)
    {
        /* Get the item that the player is trying to purchase */
        Item item = SojournDBM.odbm.GetItemByName(item_name);

        if (Item.IsNull(item))
        {
            Debug.LogError("BarteringMenuManager Server: Client sent us a bad item uuid.");
            return;
        }

        if (debug)
        {
            Debug.Log("BarteringMenuManager Server: Got a request to move an item from the inventory to the store.");
        }

        /* Get the player's attribute manager */
        PlayerEntityController attrib = GetComponent <PlayerEntityController>();

        /* Get this player's inventory */
        PlayerInventory inventory = attrib.GetComponent <PlayerInventoryManager>().GetInventory();

        /* Get the object for the store */
        GameObject storeObject = NetworkServer.FindLocalObject(store_id);

        if (storeObject == null)
        {
            Debug.LogError("BarteringMenuManager Server: The client send us a bad object id!");
            return;
        }

        /* Get the store manager for this store */
        StoreManager storeManager = storeObject.GetComponent <StoreManager>();

        if (storeManager == null)
        {
            Debug.LogError("BarteringMenuManager Server: The client sent us an ID for something that isn't a store!");
            return;
        }

        /* Does the store carry the given item? */
        if (storeManager.GetCurrentInventory().GetQuantity(item) <= 0)
        {
            Debug.LogError("BarteringMenuManager Server: The store doesn't carry this item.");
            return;
        }

        /* Take coins from store and give them to the player */
        long value = item.item_value;

        if (inventory.gold < value)
        {
            if (debug)
            {
                Debug.LogWarning("BarteringMenuManager Server: The player doesn't have enough coins to buy this item!");
            }
            return;
        }

        inventory.gold -= value;
        storeManager.AddCoins(value);

        /* Remove the item from the store */
        if (!storeManager.RemoveItem(item))
        {
            Debug.LogError("BarteringMenuManager Server: Failed to remove the item from the store!");
            return;
        }

        /* Give the item to the player */
        if (!inventory.Add(item))
        {
            Debug.LogError("BarteringMenuManager Server: Failed to add item to the player's inventory!!");

            /* If this happens we should really restore the store's item. */
            if (storeManager.AddItem(item))
            {
                Debug.LogWarning("BarteringMenuManager Server: We were able to restore the store's item.");
            }
            else
            {
                Debug.LogError("BarteringMenuManager Server: We failed to restore the store's item!!");
            }

            return;
        }

        /* The player's inventory has been updated */
        // attrib.UpdateClientInventory();

        if (debug)
        {
            Debug.Log("BarteringMenuManager Server: Item transferred to the inventory.");
        }
    }