Beispiel #1
0
        /// <summary>
        /// <para>Attampts to consume the indicated number of items from the instance provided.</para>
        /// <para>Note this method must look up the instance's related Item Definition which can take time and can be error prone. It is recomended that you provide the ItemDefinition with your call to the consume method.</para>
        /// </summary>
        /// <param name="instanceId"></param>
        /// <param name="count"></param>
        public void ConsumeItem(SteamItemInstanceID_t instanceId, int count)
        {
            var itemDefinition = ItemDefinitions.FirstOrDefault(p => p.Instances.Any(i => i.m_itemId == instanceId));

            if (itemDefinition == null)
            {
                Debug.LogError("Unable to locate the Item Definition for Item Instance " + instanceId.m_SteamItemInstanceID.ToString());
                return;
            }

            var target = itemDefinition.Instances.FirstOrDefault(p => p.m_itemId == instanceId);
            var result = SteamworksPlayerInventory.ConsumeItem(instanceId, (status, results) =>
            {
                if (status)
                {
                    itemDefinition.Instances.RemoveAll(p => p.m_itemId == target.m_itemId);
                    target.m_unQuantity -= System.Convert.ToUInt16(count);
                    itemDefinition.Instances.Add(target);
                    ItemInstancesUpdated.Invoke();
                    ItemsConsumed.Invoke(status, results);
                }
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.ConsumeItem] - Call failed");
            }
        }
 public void TriggerDrop(Action <bool, SteamItemDetails_t[]> callback)
 {
     if (!SteamworksPlayerInventory.TriggerItemDrop(DefinitionID, callback))
     {
         Debug.LogWarning("[ItemGeneratorDefinition.TriggerDrop] - Call failed.");
     }
 }
 /// <summary>
 /// Grants a copy of this item if available in the items definition on the Steam backend.
 /// </summary>
 public void GrantPromoItem()
 {
     SteamworksPlayerInventory.AddPromoItem(DefinitionID, (status, results) =>
     {
         if (SteamworksInventorySettings.Current != null)
         {
             SteamworksInventorySettings.Current.ItemsGranted.Invoke(status, results);
         }
     });
 }
Beispiel #4
0
        /// <summary>
        /// <para>Grants the user a promotional item</para>
        /// <para>This will trigger the Item Instances Updated event after steam responds with the users inventory items and the items have been updated to reflect the correct instances.</para>
        /// <para> <para>
        /// <para>NOTE: this additivly updates the Instance list for effected items and is not a clean refresh!
        /// Consider a call to Refresh Inventory to resolve a complete and accurate refresh of all player items.</para>
        /// </summary>
        /// <paramref name="itemDefinition">The item type to grant to the user.</paramref>
        public void GrantPromotionalItem(InventoryItemDefinition itemDefinition)
        {
            var result = SteamworksPlayerInventory.AddPromoItem(itemDefinition.DefinitionID, (status, results) =>
            {
                ItemsGranted.Invoke(status, results);
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.GrantPromotionalItem] - Call failed");
            }
        }
Beispiel #5
0
        /// <summary>
        /// <para>Grants the user all available promotional items</para>
        /// <para>This will trigger the Item Instances Updated event after steam responds with the users inventory items and the items have been updated to reflect the correct instances.</para>
        /// <para> <para>
        /// <para>NOTE: this additivly updates the Instance list for effected items and is not a clean refresh!
        /// Consider a call to Refresh Inventory to resolve a complete and accurate refresh of all player items.</para>
        /// </summary>
        public void GrantAllPromotionalItems()
        {
            var result = SteamworksPlayerInventory.GrantPromoItems((status, results) =>
            {
                ItemsGranted.Invoke(status, results);
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.GrantAllPromotionalItems] - Call failed");
            }
        }
        public void TriggerDrop()
        {
            var result = SteamworksPlayerInventory.TriggerItemDrop(DefinitionID, (status, results) =>
            {
                if (!status)
                {
                    Debug.LogWarning("[ItemGeneratorDefinition.TriggerDrop] - Call returned an error status.");
                }
            });

            if (!result)
            {
                Debug.LogWarning("[ItemGeneratorDefinition.TriggerDrop] - Call failed.");
            }
        }
        /// <summary>
        /// Splits an instance quantity, if the destination instance is -1 this will create a new stack of the defined quantity.
        /// </summary>
        /// <param name="source">The instance to split</param>
        /// <param name="quantity">The number of items to remove from the source stack</param>
        /// <param name="destination">The target to move the quanity to</param>
        /// <returns></returns>
        public bool TransferQuantity(SteamItemDetails_t source, uint quantity, SteamItemInstanceID_t destination)
        {
            if (source.m_unQuantity >= quantity)
            {
                var ret = SteamworksPlayerInventory.TransferQuantity(source.m_itemId, quantity, destination, (result) =>
                {
                    Instances.RemoveAll(p => p.m_itemId == source.m_itemId);
                    source.m_unQuantity -= Convert.ToUInt16(quantity);
                    Instances.Add(source);
                });

                return(ret);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Consolodate all stacks of this into a single stack
        /// </summary>
        /// <returns></returns>
        public void Consolidate()
        {
            if (Instances != null)
            {
                if (Instances.Count > 1)
                {
                    List <SteamItemInstanceID_t> removedInstances = new List <SteamItemInstanceID_t>();
                    var primary = Instances[0];
                    for (int i = 1; i < Instances.Count; i++)
                    {
                        var toMove = Instances[i];
                        var ret    = SteamworksPlayerInventory.TransferQuantity(toMove.m_itemId, toMove.m_unQuantity, primary.m_itemId, (result) =>
                        {
                            if (!result)
                            {
                                Debug.LogError("Failed to stack an instance, please refresh the item instances for item definition [" + name + "].");
                            }
                            else
                            {
                                removedInstances.Add(toMove.m_itemId);
                            }
                        });

                        if (!ret)
                        {
                            Debug.LogError("Steam activly refused a TransferItemQuantity request during the Consolodate operation. No further requests will be sent.");
                        }
                    }

                    foreach (var instance in removedInstances)
                    {
                        Instances.RemoveAll(p => p.m_itemId == instance);
                    }
                }
                else
                {
                    Debug.LogWarning("Unable to consolodate items, this item only has 1 instance. No action will be taken.");
                }
            }
            else
            {
                Debug.LogWarning("Unable to consolodate items, this item only has no instances. No action will be taken.");
            }
        }
Beispiel #9
0
        /// <summary>
        /// <para>Grants the user the promotional items indicated</para>
        /// <para>This will trigger the Item Instances Updated event after steam responds with the users inventory items and the items have been updated to reflect the correct instances.</para>
        /// <para>NOTE: this additivly updates the Instance list for effected items and is not a clean refresh!
        /// Consider a call to Refresh Inventory to resolve a complete and accurate refresh of all player items.</para>
        /// </summary>
        /// <param name="itemDefinitions">The list of items to be granted if available</param>
        public void GrantPromotionalItems(IEnumerable <InventoryItemDefinition> itemDefinitions)
        {
            List <SteamItemDef_t> items = new List <SteamItemDef_t>();

            foreach (var itemDef in itemDefinitions)
            {
                items.Add(itemDef.DefinitionID);
            }

            var result = SteamworksPlayerInventory.AddPromoItems(items, (status, results) =>
            {
                ItemsGranted.Invoke(status, results);
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.GrantPromotionalItems] - Call failed");
            }
        }
        /// <summary>
        /// Moves the quantity from the source into a new stack
        /// </summary>
        /// <param name="source">Source instance to move units from</param>
        /// <param name="quantity">The number of units to move</param>
        /// <returns></returns>
        public bool SplitInstance(SteamItemDetails_t source, uint quantity)
        {
            if (source.m_unQuantity >= quantity)
            {
                var ret = SteamworksPlayerInventory.TransferQuantity(source.m_itemId, quantity, SteamItemInstanceID_t.Invalid, (result) =>
                {
                    Instances.RemoveAll(p => p.m_itemId == source.m_itemId);
                    source.m_unQuantity -= Convert.ToUInt16(quantity);
                    Instances.Add(source);
                });

                return(ret);
            }
            else
            {
                Debug.LogWarning("Unable to split instance, insufficent units available to move.");
                return(false);
            }
        }
Beispiel #11
0
        /// <summary>
        /// <para>Consumes the indicated number of units of this item from this specific instance stack</para>
        /// <para>NOTE: this is the most efficent way to consume multiple units of an item at a time.</para>
        /// </summary>
        /// <param name="itemDefinition"></param>
        /// <param name="instanceId"></param>
        /// <param name="count"></param>
        public void ConsumeItem(InventoryItemDefinition itemDefinition, SteamItemInstanceID_t instanceId, int count)
        {
            var target = itemDefinition.Instances.FirstOrDefault(p => p.m_itemId == instanceId);
            var result = SteamworksPlayerInventory.ConsumeItem(instanceId, (status, results) =>
            {
                if (status)
                {
                    itemDefinition.Instances.RemoveAll(p => p.m_itemId == target.m_itemId);
                    target.m_unQuantity -= System.Convert.ToUInt16(count);
                    itemDefinition.Instances.Add(target);
                    ItemInstancesUpdated.Invoke();
                    ItemsConsumed.Invoke(status, results);
                }
            });

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.ConsumeItem] - Call failed");
            }
        }
Beispiel #12
0
 /// <summary>
 /// Grant promotion items and print the results to the console.
 /// </summary>
 public void grantPromoTest()
 {
     if (SteamworksPlayerInventory.GrantPromoItems((status, results) =>
     {
         if (status)
         {
             Debug.Log("Granted " + results.Length + " promo items.");
         }
         else
         {
             Debug.Log("Grant Promo Items Failed.");
         }
     }))
     {
         Debug.Log("Grant Promo Items request sent to Steam.");
     }
     else
     {
         Debug.Log("Grant Promo Items failed to send to Steam.");
     }
 }
Beispiel #13
0
 /// <summary>
 /// Fetch all items and print the results to the console.
 /// </summary>
 public void getAllTest()
 {
     if (SteamworksPlayerInventory.GetAllItems((status, results) =>
     {
         if (status)
         {
             Debug.Log("Query returned " + results.Length + " items.");
         }
         else
         {
             Debug.Log("Query failed.");
         }
     }))
     {
         Debug.Log("Get All Items request sent to Steam.");
     }
     else
     {
         Debug.Log("Get All Items failed to send to Steam.");
     }
 }
Beispiel #14
0
        /// <summary>
        /// <para>Updates the InventoryItemDefinition.Instances list of each of the referenced Item Definitions with the results of a 'Get All Items' query against the current user's Steam Inventory.</para>
        /// <para>This will cause the Instances member of each item to reflect the current state of the users inventory.</para>
        /// <para> <para>
        /// <para>This will trigger the Item Instances Updated event after steam responds with the users inventory items and the items have been updated to reflect the correct instances.</para>
        /// </summary>
        public void RefreshInventory()
        {
            foreach (var item in ItemDefinitions)
            {
                if (item.Instances == null)
                {
                    item.Instances = new List <SteamItemDetails_t>();
                }
                else
                {
                    item.Instances.Clear();
                }
            }

            var result = SteamworksPlayerInventory.GetAllItems(null);

            if (!result)
            {
                Debug.LogWarning("[SteamworksInventorySettings.RefreshInventory] - Call failed");
            }
        }
        /// <summary>
        /// This cannot be undone and will remove items from the palyer's inventory
        /// Be very sure the player wants to do this!
        /// </summary>
        /// <param name="count"></param>
        public void Consume(int count)
        {
            if (Count > count)
            {
                var ConsumedSoFar = 0;

                List <SteamItemDetails_t> Edits = new List <SteamItemDetails_t>();

                foreach (var instance in Instances)
                {
                    if (count - ConsumedSoFar >= instance.m_unQuantity)
                    {
                        //We need to consume all of these
                        ConsumedSoFar += instance.m_unQuantity;
                        SteamworksPlayerInventory.ConsumeItem(instance.m_itemId, instance.m_unQuantity,
                                                              (status, results) =>
                        {
                            if (!status)
                            {
                                Debug.LogWarning("Failed to consume (" + instance.m_unQuantity.ToString() + ") units of item [" + instance.m_iDefinition.m_SteamItemDef.ToString() + "]");
                                SteamworksInventorySettings.Current.ItemsConsumed.Invoke(status, results);
                            }
                        });

                        var edit = instance;
                        edit.m_unQuantity = 0;
                        Edits.Add(edit);
                    }
                    else
                    {
                        //We only need some of these
                        var need = count - ConsumedSoFar;
                        ConsumedSoFar += need;
                        SteamworksPlayerInventory.ConsumeItem(instance.m_itemId, Convert.ToUInt32(need),
                                                              (status, results) =>
                        {
                            if (!status)
                            {
                                Debug.LogWarning("Failed to consume (" + need.ToString() + ") units of item [" + instance.m_iDefinition.m_SteamItemDef.ToString() + "]");
                            }

                            if (SteamworksInventorySettings.Current != null)
                            {
                                SteamworksInventorySettings.Current.ItemsConsumed.Invoke(status, results);
                            }
                        });

                        var edit = instance;
                        edit.m_unQuantity -= Convert.ToUInt16(need);
                        Edits.Add(edit);

                        break;
                    }
                }

                //Manually update our instances to account for the quantity changes we expect to see
                foreach (var edit in Edits)
                {
                    Instances.RemoveAll(p => p.m_itemId == edit.m_itemId);
                    Instances.Add(edit);
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// <para>Attempts to consume the requested units of the indicated item</para>
        /// <para>NOTE: this may need to iterate over multiple instances and may need to send multiple consume requests any of which may fail and each of which will trigger an Item Instance Update event call.</para>
        /// <para>You are recomended to use the the SteamItemInstance_t overload of this method when consuming more than 1 unit of an item.</para>
        /// </summary>
        /// <param name="itemDefinition">The item to consume for</param>
        /// <param name="count">The number of item units to try and consume</param>
        public void ConsumeItem(InventoryItemDefinition itemDefinition, int count)
        {
            if (count < 1)
            {
                Debug.LogWarning("Attempted to consume a number of items less than 1; this is not possible and was note requested.");
                return;
            }

            List <ExchangeItemCount> counts = new List <ExchangeItemCount>();
            int currentCount = 0;

            foreach (var details in itemDefinition.Instances)
            {
                if (details.m_unQuantity >= count - currentCount)
                {
                    counts.Add(new ExchangeItemCount()
                    {
                        InstanceId = details.m_itemId, Quantity = System.Convert.ToUInt32(count - currentCount)
                    });
                    currentCount = count;
                    break;
                }
                else
                {
                    counts.Add(new ExchangeItemCount()
                    {
                        InstanceId = details.m_itemId, Quantity = details.m_unQuantity
                    });
                    currentCount += details.m_unQuantity;
                }
            }

            bool noErrors = true;

            foreach (var exchange in counts)
            {
                var result = SteamworksPlayerInventory.ConsumeItem(exchange.InstanceId, exchange.Quantity, (status, results) =>
                {
                    if (status)
                    {
                        var target = itemDefinition.Instances.FirstOrDefault(p => p.m_itemId == exchange.InstanceId);
                        itemDefinition.Instances.RemoveAll(p => p.m_itemId == exchange.InstanceId);
                        target.m_unQuantity -= System.Convert.ToUInt16(exchange.Quantity);
                        itemDefinition.Instances.Add(target);
                        ItemInstancesUpdated.Invoke();
                        ItemsConsumed.Invoke(status, results);
                    }
                });

                if (!result)
                {
                    noErrors = false;
                    Debug.LogWarning("Failed to consume all requested items");
                    break;
                }
            }

            if (!noErrors)
            {
                Debug.LogWarning("[SteamworksInventorySettings.ConsumeItem] - Call failed");
            }
        }
Beispiel #17
0
 /// <summary>
 /// <para>Determins if the result handle belongs to the user</para>
 /// </summary>
 /// <param name="resultHandle">The inventory result handle to check the user on</param>
 /// <param name="user">The user to check against</param>
 public bool CheckUserResult(SteamInventoryResult_t resultHandle, SteamUserData user)
 {
     return(SteamworksPlayerInventory.CheckResultSteamID(resultHandle, user));
 }
        /// <summary>
        /// Attempts to exchange the required items for a new copy of this item
        /// This is subject to checks by valve as to rather or not this is a legitimate recipie and that the use has sufficent items available for exchange
        /// </summary>
        /// <param name="recipe"></param>
        /// <returns>True if the request is successfuly sent to Steam for processing</returns>
        public void Craft(CraftingRecipe recipe)
        {
            var itemRecipe = PrepareItemExchange(recipe, out Dictionary <InventoryItemDefinition, List <SteamItemDetails_t> > edits);

            if (itemRecipe.ItemsToConsume == null || itemRecipe.ItemsToConsume.Count < 1)
            {
                Debug.LogWarning("Attempted to craft item [" + name + "] with no items to consume selected!\nThis will be refused by Steam so will not be sent!");
                return;
            }

            if (itemRecipe != null)
            {
                var result = SteamworksPlayerInventory.ExchangeItems(itemRecipe, (status, results) =>
                {
                    if (status)
                    {
                        //Remove the counts for the consumed items
                        foreach (var kvp in edits)
                        {
                            foreach (var item in kvp.Value)
                            {
                                kvp.Key.Instances.RemoveAll(p => p.m_itemId == item.m_itemId);
                                kvp.Key.Instances.Add(item);
                            }
                        }

                        if (SteamworksInventorySettings.Current != null && SteamworksInventorySettings.Current.LogDebugMessages)
                        {
                            StringBuilder sb = new StringBuilder("Inventory Item [" + name + "] Crafted,\nItems Consumed:\n");
                            foreach (var item in recipe.Items)
                            {
                                sb.Append("\t" + item.Count + " [" + item.Item.name + "]");
                            }
                        }
                    }
                    else
                    {
                        if (SteamworksInventorySettings.Current != null && SteamworksInventorySettings.Current.LogDebugMessages)
                        {
                            Debug.LogWarning("Request to craft item [" + name + "] failed, confirm the item and recipie configurations are correct in the app settings.");
                        }
                    }

                    if (SteamworksInventorySettings.Current != null)
                    {
                        SteamworksInventorySettings.Current.ItemsExchanged.Invoke(status, results);
                    }
                });

                if (!result)
                {
                    if (SteamworksInventorySettings.Current != null)
                    {
                        SteamworksInventorySettings.Current.ItemsExchanged.Invoke(false, new SteamItemDetails_t[] { });
                        if (SteamworksInventorySettings.Current.LogDebugMessages)
                        {
                            Debug.LogWarning("Request to craft item [" + name + "] was refused by Steam.");
                        }
                    }
                }
            }
        }