Пример #1
0
        public IPromise <BuyItemResult> BuyItemGroup(string itemGroupId)
        {
            var promise = new Promise <BuyItemResult>();

            ItemGroupConfigData itemGroupData = itemGroupConfigModel.GetGroup(itemGroupId);

            if (itemGroupData == null)
            {
                logger.LogError(Tag, "Attempted to buy a non existing group with id " + itemGroupId);
                promise.Resolve(new BuyItemResult(BuyItemResultCode.Error));
                return(promise);
            }

            switch (itemGroupData.type)
            {
            case ItemGroupConfigType.Pack:
                BuyItemPack(itemGroupData, promise);
                break;

            case ItemGroupConfigType.Upgrades:
                BuyUpgrade(itemGroupData, promise);
                break;
            }

            return(promise);
        }
Пример #2
0
        private void BuyItemPack(ItemGroupConfigData itemGroupData, Promise <BuyItemResult> promise)
        {
            if (itemGroupData.items.Count == 0)
            {
                logger.LogError(Tag, "BuyItemPack - Group contains no items");
                promise.Resolve(new BuyItemResult(BuyItemResultCode.Error));
                return;
            }

            string itemIdToBuy = itemGroupData.items[0];

            // For item groups with more than
            if (itemGroupData.items.Count > 1)
            {
                ItemConfigData packCost = itemGroupConfigModel.GetPackCost(itemGroupData.id);
                if (packCost == null)
                {
                    logger.LogError(Tag, "Cannot find Pack cost - item groups with more than one item require a pack cost item");
                    promise.Resolve(new BuyItemResult(BuyItemResultCode.Error));
                    return;
                }

                itemIdToBuy = packCost.id;
            }

            // Pay the costs, then give the player the rest of the items in the group
            BuyItem(itemIdToBuy).Done(buyItemResult => {
                if (buyItemResult.resultCode.Equals(BuyItemResultCode.Success))
                {
                    foreach (string itemId in itemGroupConfigModel.GetItemIds(itemGroupData.id))
                    {
                        ItemConfigData item = itemConfigModel.GetItem(itemId);

                        if (item.itemType == ItemType.PackCost)
                        {
                            continue;
                        }

                        GameElementData ge = itemConfigModel.ToGameElement(item);
                        var items          = new List <GameElementData> {
                            ge
                        };

                        collectGameElementsSignal.Dispatch(items);
                    }
                }
                else
                {
                    logger.Log(Tag, "BuyItemPack - Will not collect game elements since buy attempt failed. Result code: " + buyItemResult.resultCode);
                }

                promise.Resolve(buyItemResult);
            });
        }
Пример #3
0
        public IConfigData Clone()
        {
            ItemGroupConfigData c = new ItemGroupConfigData();

            c.id           = id;
            c.name         = name;
            c.category     = category;
            c.index        = index;
            c.type         = type;
            c.items        = new List <string>(items.Clone());
            c.restrictions = new List <GameElementData>(restrictions.Clone());
            c.assets       = new List <AssetData>(assets.Clone());
            c.properties   = new List <GenericPropertyData>(properties.Clone());

            return(c);
        }
Пример #4
0
        private void BuyUpgrade(ItemGroupConfigData itemGroupData, Promise <BuyItemResult> promise)
        {
            // Handle upgrade: If not final item in upgrade list, Remove the item from inUse in inventory, add the next item to the inventory and set it inUse

            string currentItemId = itemGroupConfigModel.GetUsedItemId(itemGroupData);
            string nextUpgradeId = itemGroupConfigModel.GetNextUpgradeItemId(currentItemId, itemGroupData.id);

            bool isGroupItemInInventory = !string.IsNullOrEmpty(currentItemId);
            bool itemHasUpgrade         = !string.IsNullOrEmpty(nextUpgradeId);

            string itemIdToBuy = null;

            if (!isGroupItemInInventory)
            {
                itemIdToBuy = itemGroupData.items[0];
            }
            else if (itemHasUpgrade)
            {
                itemIdToBuy = nextUpgradeId;
            }
            else
            {
                logger.LogError(Tag, "Cannot upgrade item since - no upgrade available for item in group " + itemGroupData.id);
                promise.Resolve(new BuyItemResult(BuyItemResultCode.Error));
                return;
            }

            BuyItem(itemIdToBuy).Done(buyItemResult => {
                if (buyItemResult.resultCode.Equals(BuyItemResultCode.Success))
                {
                    if (isGroupItemInInventory)
                    {
                        inventoryStateModel.SetItemUnUsed(currentItemId);
                    }

                    inventoryStateModel.SetItemInUse(itemIdToBuy);
                }
                else
                {
                    logger.Log(Tag, "BuyUpgrade - Will not collect item since buy attempt failed. Result code: " + buyItemResult.resultCode);
                }

                promise.Resolve(buyItemResult);
            });
        }