Пример #1
0
        public InventoryData InitInventory()
        {
            if (Inventory != null)
            {
                return(Inventory);
            }
            TempUserInfo temp;

            try {
                string playerData =
                    File.ReadAllText(Application.streamingAssetsPath + "/defaultPlayerData.json");
                temp = JsonHelper.getObjectFromJson <TempUserInfo>(playerData);
            }
            catch (FileNotFoundException e) {
                SpilLogging.Log("defaultPlayerData.json not found. Creating a placeholder! " + e);
                string placeholder =
                    "{\"wallet\":{\"currencies\":[],\"offset\": 0,\"logic\": \"CLIENT\"},\"inventory\":{\"items\":[],\"offset\":0,\"logic\": \"\"}}";
                temp = JsonHelper.getObjectFromJson <TempUserInfo>(placeholder);
            }

            // Currencies and items loaded from playerdata don't have fields like initialValue because those are defined in the gamedata. Add the missing information.
            // TODO: Should playerdata ever be used to access these fields or only gamedata? A. Check where these fields are being used (they shouldnt be? see SendUpdatePlayerDataEvent tho) and B. Consider maybe this is the wrong inheritance structure / we're exposing fields that shouldn't be exposed for playerdata items/currencies?
            // TODO: Make this prettier? Load both defaultPlayerData and defaultGameData.json and create/initialise wallet+inventory by combining data (instead of deserialising playerdata and adding missing data from gamedata afterwards)?
            if (temp != null && temp.inventory != null && temp.inventory.items != null)
            {
                if (SpilUnityEditorImplementation.gData == null)
                {
                    throw new NotImplementedException("GameData must be initialised before calling this method.");
                }

                foreach (PlayerItemData item in temp.inventory.items)
                {
                    SpilItemData gameDataItem = SpilUnityEditorImplementation.gData.items.FirstOrDefault(a => a.id == item.id);
                    if (gameDataItem != null)
                    {
                        item.content            = gameDataItem.content;
                        item.displayDescription = gameDataItem.displayDescription;
                        item.displayName        = gameDataItem.displayName;
                        item.imageUrl           = gameDataItem.imageUrl;
                        item.initialValue       = gameDataItem.initialValue;
                        item.isGacha            = gameDataItem.isGacha;
                        item.name = gameDataItem.name;
                        item.type = gameDataItem.type;
                    }
                    else
                    {
                        // TODO: Playerdata contains an item that is not defined in the gamedata, should this throw an exception?
                        // TODO: Remove the item from the list or keep it with missing data (as it is now)?
                    }
                }
            }

            return(temp.inventory);
        }
Пример #2
0
        public void OpenGacha(int gachaId, string reason, string reasonDetails, string location)
        {
            PlayerItemData gachaPlayerItem = GetGachaFromInventory(gachaId);
            SpilItemData   gachaItem       = GetGachaFromObjects(gachaId);

            if (gachaPlayerItem == null || gachaItem == null || gachaId <= 0 || reason == null || !gachaPlayerItem.isGacha)
            {
                SpilLogging.Error("Error opening gacha!");
                return;
            }

            if (!gachaPlayerItem.content.All(gachaItem.content.Contains) && gachaPlayerItem.content.Count == gachaItem.content.Count)
            {
                gachaPlayerItem.content = gachaItem.content;
            }

            if (gachaPlayerItem.amount < 1)
            {
                SpilLogging.Error("Not enough gacha boxes in the inventory!");
                return;
            }

            if (gachaPlayerItem.content.Count < 1)
            {
                SpilLogging.Error("Error opening gacha! No content present!");
                return;
            }

            int weightSum = 0;

            foreach (SpilGachaContent gachaContent in gachaPlayerItem.content)
            {
                weightSum = weightSum + gachaContent.weight;
            }

            if (weightSum == 0)
            {
                SpilLogging.Error("Error opening gacha!");
                return;
            }

            int rand = Random.Range(0, weightSum);

            int low  = 0;
            int high = 0;

            for (int i = 0; i < gachaPlayerItem.content.Count; i++)
            {
                SpilGachaContent gachaContent = gachaPlayerItem.content[i];

                if (i != 0)
                {
                    low = high;
                }

                high = low + gachaContent.weight;

                if (rand >= low && rand < high)
                {
                    gachaPlayerItem.amount = gachaPlayerItem.amount - 1;
                    gachaPlayerItem.delta  = gachaPlayerItem.delta - 1;

                    UpdateItem(gachaPlayerItem);

                    switch (gachaContent.type)
                    {
                    case "CURRENCY":
                        WalletOperation("add", gachaContent.id, gachaContent.amount, reason, reasonDetails, location, null);
                        break;

                    case "ITEM":
                        InventoryOperation("add", gachaContent.id, gachaContent.amount, reason, reasonDetails, location, null);
                        break;

                    case "BUNDLE":
                        OpenBundle(gachaContent.id, gachaContent.amount, reason, reasonDetails, location);
                        break;

                    case "GACHA":
                        InventoryOperation("add", gachaContent.id, gachaContent.amount, reason, reasonDetails, location, null);
                        break;

                    case "NONE":
                        UserDataManager.UpdateUserDataVersions();
                        UserDataManager.UpdateUserDataMeta();

                        PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData();
                        updatedData.items.Add(gachaPlayerItem);
                        updatedData.reason = reason;

                        SpilUnityImplementationBase.firePlayerDataEmptyGacha();
                        SpilUnityImplementationBase.firePlayerDataUpdated(JsonHelper.getJSONFromObject(updatedData));
                        break;

                    default:
                        SpilLogging.Error("Error opening gacha!");
                        return;
                    }

                    break;
                }
            }
        }
Пример #3
0
        public void BuyBundle(int bundleId, string reason, string reasonDetails, string location, string transactionId)
        {
            PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData();

            SpilBundleData bundle = GetBundleFromObjects(bundleId);

            if (bundle == null || reason == null)
            {
                SpilLogging.Error("Error adding bundle to player inventory!");
                return;
            }

            Promotion promotion        = Spil.Instance.GetPromotions().GetBundlePromotion(bundleId);
            bool      isPromotionValid = false;

            if (promotion != null)
            {
                isPromotionValid = promotion.IsValid();
            }

            List <SpilBundlePriceData> bundlePrices = new List <SpilBundlePriceData>();

            if (isPromotionValid)
            {
                foreach (PriceOverride priceOverride in promotion.PriceOverride)
                {
                    SpilBundlePriceData bundlePriceData = new SpilBundlePriceData();
                    bundlePriceData.currencyId = priceOverride.Id;
                    bundlePriceData.value      = priceOverride.Amount;

                    bundlePrices.Add(bundlePriceData);
                }
            }
            else
            {
                bundlePrices = bundle.prices;
            }

            foreach (SpilBundlePriceData bundlePrice in bundlePrices)
            {
                PlayerCurrencyData currency = GetCurrencyFromWallet(bundlePrice.currencyId);

                if (currency == null)
                {
                    SpilLogging.Error("Currency does not exist!");
                    return;
                }

                int currentBalance = currency.currentBalance;
                int updatedBalance = currentBalance - bundlePrice.value;

                if (updatedBalance < 0)
                {
                    SpilLogging.Error("Not enough balance for currency!");
                    return;
                }

                int updatedDelta = -bundlePrice.value + currency.delta;

                if (updatedDelta == 0)
                {
                    updatedDelta = -bundlePrice.value;
                }

                currency.delta          = updatedDelta;
                currency.currentBalance = updatedBalance;

                UpdateCurrency(currency);
                updatedData.currencies.Add(currency);
            }

            foreach (SpilBundleItemData bundleItem in bundle.items)
            {
                SpilItemData gameItem = GetItemFromObjects(bundleItem.id);

                if (gameItem == null)
                {
                    SpilLogging.Error("Item does not exist!");
                    return;
                }
                ;
                PlayerItemData item = new PlayerItemData();
                item.id                 = gameItem.id;
                item.name               = gameItem.name;
                item.type               = gameItem.type;
                item.displayName        = gameItem.displayName;
                item.displayDescription = gameItem.displayDescription;
                item.isGacha            = gameItem.isGacha;
                item.content            = gameItem.content;

                PlayerItemData inventoryItem = GetItemFromInventory(bundleItem.id);

                int inventoryItemAmount;

                if (inventoryItem != null)
                {
                    inventoryItemAmount = inventoryItem.amount;

                    inventoryItemAmount = inventoryItemAmount + bundleItem.amount;

                    inventoryItem.delta  = bundleItem.amount;
                    inventoryItem.amount = inventoryItemAmount;

                    UpdateItem(inventoryItem);

                    updatedData.items.Add(inventoryItem);
                }
                else
                {
                    inventoryItemAmount = bundleItem.amount;

                    item.delta  = inventoryItemAmount;
                    item.amount = inventoryItemAmount;

                    Inventory.items.Add(item);

                    updatedData.items.Add(item);
                }
            }

            if (isPromotionValid)
            {
                foreach (ExtraEntity extraEntity in promotion.ExtraEntities)
                {
                    if (extraEntity.Type.Equals("CURRENCY"))
                    {
                        PlayerCurrencyData currency = GetCurrencyFromWallet(extraEntity.Id);

                        if (currency == null)
                        {
                            SpilLogging.Error("Currency does not exist!");
                            return;
                        }

                        currency.currentBalance = currency.currentBalance + extraEntity.Amount;
                        currency.delta          = currency.delta + extraEntity.Amount;

                        UpdateCurrency(currency);

                        PlayerCurrencyData temp = null;

                        foreach (PlayerCurrencyData playerCurrency in updatedData.currencies)
                        {
                            if (playerCurrency.id == extraEntity.Id)
                            {
                                temp = playerCurrency;
                            }
                        }

                        if (temp != null)
                        {
                            updatedData.currencies.Remove(temp);
                        }

                        updatedData.currencies.Add(currency);
                    }
                    else if (extraEntity.Type.Equals("ITEM") || extraEntity.Type.Equals("GACHA"))
                    {
                        SpilItemData gameItem = GetItemFromObjects(extraEntity.Id);

                        if (gameItem == null)
                        {
                            SpilLogging.Error("Item does not exist!");
                            return;
                        }
                        ;
                        PlayerItemData item = new PlayerItemData();
                        item.id                 = gameItem.id;
                        item.name               = gameItem.name;
                        item.type               = gameItem.type;
                        item.displayName        = gameItem.displayName;
                        item.displayDescription = gameItem.displayDescription;
                        item.isGacha            = gameItem.isGacha;
                        item.content            = gameItem.content;

                        PlayerItemData inventoryItem = GetItemFromInventory(extraEntity.Id);

                        int inventoryItemAmount;

                        if (inventoryItem != null)
                        {
                            inventoryItemAmount = inventoryItem.amount;

                            inventoryItemAmount = inventoryItemAmount + extraEntity.Amount;

                            inventoryItem.delta  = extraEntity.Amount;
                            inventoryItem.amount = inventoryItemAmount;

                            UpdateItem(inventoryItem);

                            PlayerItemData temp = null;

                            foreach (PlayerItemData playerItem in updatedData.items)
                            {
                                if (playerItem.id == extraEntity.Id)
                                {
                                    temp = playerItem;
                                }
                            }

                            if (temp != null)
                            {
                                updatedData.items.Remove(temp);
                            }

                            updatedData.items.Add(inventoryItem);
                        }
                        else
                        {
                            inventoryItemAmount = extraEntity.Amount;

                            item.delta  = inventoryItemAmount;
                            item.amount = inventoryItemAmount;

                            Inventory.items.Add(item);

                            updatedData.items.Add(item);
                        }
                    }
                }
            }

            UserDataManager.UpdateUserDataVersions();
            UserDataManager.UpdateUserDataMeta();

            updatedData.reason = reason;

            SpilUnityImplementationBase.firePlayerDataUpdated(JsonHelper.getJSONFromObject(updatedData));

            if (isPromotionValid)
            {
                PromotionsManager.PromotionData.First(a => a.id == promotion.Id).amountPurchased++;

                PromotionsManager.SendBoughtPromotion(promotion.Id);
            }

            SendUpdatePlayerDataEvent(bundle, reason, reasonDetails, location, transactionId);
        }
Пример #4
0
        public void InventoryOperation(string action, int itemId, int amount, string reason, string reasonDetails, string location, string transactionId)
        {
            SpilItemData gameItem = GetItemFromObjects(itemId);

            if (gameItem == null || itemId <= 0 || action == null || reason == null)
            {
                SpilLogging.Error("Error updating item to player inventory!");
                return;
            }

            PlayerItemData item = new PlayerItemData();

            item.id                 = gameItem.id;
            item.name               = gameItem.name;
            item.type               = gameItem.type;
            item.displayName        = gameItem.displayName;
            item.displayDescription = gameItem.displayDescription;
            item.isGacha            = gameItem.isGacha;
            item.content            = gameItem.content;
            item.amount             = amount;
            item.delta              = amount;

            PlayerItemData inventoryItem = GetItemFromInventory(itemId);

            if (inventoryItem != null)
            {
                int inventoryItemAmount = inventoryItem.amount;

                if (action.Equals("add"))
                {
                    inventoryItemAmount = inventoryItemAmount + amount;
                }
                else if (action.Equals("subtract"))
                {
                    inventoryItemAmount = inventoryItemAmount - amount;

                    if (inventoryItemAmount < 0)
                    {
                        SpilLogging.Error("Could not remove item as amount is too low!");
                        return;
                    }
                }

                inventoryItem.delta  = amount;
                inventoryItem.amount = inventoryItemAmount;
                UpdateItem(inventoryItem);
            }
            else
            {
                if (action.Equals("add"))
                {
                    Inventory.items.Add(item);
                }
                else if (action.Equals("subtract"))
                {
                    SpilLogging.Error("Could not remove item as amount is too low!");
                }
            }

            UserDataManager.UpdateUserDataVersions();
            UserDataManager.UpdateUserDataMeta();

            PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData();

            updatedData.items.Add(item);
            updatedData.reason = reason;

            SpilUnityImplementationBase.firePlayerDataUpdated(JsonHelper.getJSONFromObject(updatedData));

            SendUpdatePlayerDataEvent(null, reason, reasonDetails, location, transactionId);
        }
Пример #5
0
        public void CalculatePlayerDataResponse(WalletData receivedWallet, InventoryData receivedInventory, bool fromInit)
        {
            bool updated = false;
            PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData();

            if (receivedWallet != null)
            {
                foreach (PlayerCurrencyData playerCurrency in Wallet.currencies)
                {
                    playerCurrency.delta = 0;
                }

                if (Wallet.offset < receivedWallet.offset && receivedWallet.currencies.Count > 0)
                {
                    foreach (PlayerCurrencyData playerCurrency in receivedWallet.currencies)
                    {
                        if (receivedWallet.logic.Equals("CLIENT"))
                        {
                            PlayerCurrencyData currency =
                                Wallet.currencies.FirstOrDefault(a => a.id == playerCurrency.id);

                            if (currency == null)
                            {
                                continue;
                            }
                            if (Wallet.offset == 0 && receivedWallet.offset != 0)
                            {
                                currency.currentBalance = playerCurrency.currentBalance;
                            }
                            else
                            {
                                if (playerCurrency.delta != 0)
                                {
                                    int updatedBalance =
                                        currency.currentBalance + playerCurrency.delta;

                                    if (updatedBalance < 0)
                                    {
                                        updatedBalance = 0;
                                    }

                                    currency.currentBalance = updatedBalance;
                                }
                            }

                            updated = true;
                            updatedData.currencies.Add(currency);
                        }
                        else if (receivedWallet.logic.Equals("SERVER"))
                        {
                        }
                    }
                }

                Wallet.offset = receivedWallet.offset;
                Wallet.logic  = receivedWallet.logic;
            }

            if (receivedInventory != null)
            {
                foreach (PlayerItemData playerItem in Inventory.items)
                {
                    playerItem.delta = 0;
                }

                if (Inventory.offset < receivedInventory.offset && receivedInventory.items.Count > 0)
                {
                    List <PlayerItemData> itemsToBeAdded = new List <PlayerItemData>();

                    foreach (PlayerItemData playerItem in receivedInventory.items)
                    {
                        if (receivedInventory.logic.Equals("CLIENT"))
                        {
                            PlayerItemData item = Inventory.items.FirstOrDefault(a => a.id == playerItem.id);
                            if (item != null && playerItem.delta != 0)
                            {
                                item.amount = item.amount + playerItem.delta;
                            }
                            else
                            {
                                itemsToBeAdded.Add(playerItem);
                            }

                            updated = true;
                        }
                        else if (receivedInventory.logic.Equals("SERVER"))
                        {
                        }

                        updatedData.items.Add(playerItem);
                    }

                    foreach (PlayerItemData itemToAdd in itemsToBeAdded)
                    {
                        SpilItemData item = GetItemFromObjects(itemToAdd.id);

                        if (item != null && itemToAdd.amount > 0)
                        {
                            PlayerItemData playerItem = new PlayerItemData();
                            playerItem.id     = item.id;
                            playerItem.name   = item.name;
                            playerItem.type   = item.type;
                            playerItem.amount = itemToAdd.amount;
                            playerItem.value  = itemToAdd.value;
                            playerItem.delta  = 0;

                            playerItem.displayName        = item.displayName;
                            playerItem.displayDescription = item.displayDescription;
                            playerItem.isGacha            = item.isGacha;
                            playerItem.content            = item.content;

                            Inventory.items.Add(playerItem);

                            updated = true;
                        }
                    }
                }

                Inventory.offset = receivedInventory.offset;
                Inventory.logic  = receivedInventory.logic;
            }

            if (updated)
            {
                UserDataManager.UpdateUserDataVersions();
                updatedData.reason = PlayerDataUpdateReasons.ServerUpdate;

                SpilUnityImplementationBase.firePlayerDataUpdated(JsonHelper.getJSONFromObject(updatedData));
            }

            if (!fromInit)
            {
                SpilUnityImplementationBase.fireUserDataAvailable();
            }
        }
        public static void ProcessGameDataResponse(ResponseEvent response)
        {
            if (response.data.HasField("currencies"))
            {
                if (SpilUnityEditorImplementation.gData.currencies == null)
                {
                    SpilUnityEditorImplementation.gData.currencies = new List <SpilCurrencyData>();
                }

                if (SpilUnityEditorImplementation.gData.currencies.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.currencies.Clear();
                }

                JSONObject currenciesJSON = response.data.GetField("currencies");

                for (int i = 0; i < currenciesJSON.Count; i++)
                {
                    SpilCurrencyData currency =
                        JsonHelper.getObjectFromJson <SpilCurrencyData>(currenciesJSON.list[i].Print(false));
                    SpilUnityEditorImplementation.gData.currencies.Add(currency);
                }
            }

            if (response.data.HasField("items"))
            {
                if (SpilUnityEditorImplementation.gData.items == null)
                {
                    SpilUnityEditorImplementation.gData.items = new List <SpilItemData>();
                }

                if (SpilUnityEditorImplementation.gData.items.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.items.Clear();
                }

                JSONObject itemsJSON = response.data.GetField("items");

                for (int i = 0; i < itemsJSON.Count; i++)
                {
                    SpilItemData item = JsonHelper.getObjectFromJson <SpilItemData>(itemsJSON.list[i].Print(false));
                    SpilUnityEditorImplementation.gData.items.Add(item);
                }
            }

            if (response.data.HasField("bundles"))
            {
                if (SpilUnityEditorImplementation.gData.bundles == null)
                {
                    SpilUnityEditorImplementation.gData.bundles = new List <SpilBundleData>();
                }

                if (SpilUnityEditorImplementation.gData.bundles.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.bundles.Clear();
                }

                JSONObject bundlesJSON = response.data.GetField("bundles");

                for (int i = 0; i < bundlesJSON.Count; i++)
                {
                    SpilBundleData bundle =
                        JsonHelper.getObjectFromJson <SpilBundleData>(bundlesJSON.list[i].Print(false));
                    SpilUnityEditorImplementation.gData.bundles.Add(bundle);
                }
            }

            if (response.data.HasField("shop"))
            {
                if (SpilUnityEditorImplementation.gData.shop == null)
                {
                    SpilUnityEditorImplementation.gData.shop = new List <SpilShopTabData>();
                }

                if (SpilUnityEditorImplementation.gData.shop.Count > 0)
                {
                    SpilUnityEditorImplementation.gData.shop.Clear();
                }

                JSONObject shopJSON = response.data.GetField("shop");

                for (int i = 0; i < shopJSON.Count; i++)
                {
                    SpilShopTabData tab = JsonHelper.getObjectFromJson <SpilShopTabData>(shopJSON.list[i].Print(false));

                    SpilUnityEditorImplementation.gData.shop.Add(tab);
                }
            }

            GameDataManager.updatedFromServer = true;

            Spil.GameData.RefreshData(Spil.Instance);
            SpilUnityImplementationBase.fireSpilGameDataAvailable();
        }