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); }
public static void ProcessPromotionResponse(ResponseEvent response) { if (response.data == null) { SpilUnityImplementationBase.firePromotionsNotAvailable(); return; } if (response.action.Equals("request")) { if (response.data.HasField("promotions")) { JSONObject promotionsJSON = response.data.GetField("promotions"); for (int i = 0; i < promotionsJSON.Count; i++) { JSONObject promotion = promotionsJSON.list[i]; SpilPromotionData promotionData = new SpilPromotionData(); if (promotion.HasField("id")) { promotionData.id = (int)promotion.GetField("id").i; } if (promotion.HasField("name")) { promotionData.name = promotion.GetField("name").str; } if (promotion.HasField("amountPurchased")) { promotionData.amountPurchased = (int)promotion.GetField("amountPurchased").i; } promotionData.maxPurchase = 0; if (promotion.HasField("maxPurchase")) { promotionData.maxPurchase = (int)promotion.GetField("maxPurchase").i; } if (promotion.HasField("label")) { promotionData.label = promotion.GetField("label").str; } if (promotion.HasField("startDate")) { promotionData.startDate = promotion.GetField("startDate").i; } if (promotion.HasField("endDate")) { promotionData.endDate = promotion.GetField("endDate").i; } promotionData.affectedEntities = new List <SpilPromotionAffectedEntity>(); if (promotion.HasField("affectedEntities")) { List <SpilPromotionAffectedEntity> affectedEntities = new List <SpilPromotionAffectedEntity>(); JSONObject affectedEntitiesJSON = promotion.GetField("affectedEntities"); for (int j = 0; j < affectedEntitiesJSON.Count; j++) { SpilPromotionAffectedEntity affectedEntity = JsonHelper.getObjectFromJson <SpilPromotionAffectedEntity>(affectedEntitiesJSON.list[j].Print()); affectedEntities.Add(affectedEntity); } promotionData.affectedEntities = affectedEntities; } promotionData.affectedEntities = new List <SpilPromotionAffectedEntity>(); if (promotion.HasField("affectedEntities")) { List <SpilPromotionAffectedEntity> affectedEntities = new List <SpilPromotionAffectedEntity>(); JSONObject affectedEntitiesJSON = promotion.GetField("affectedEntities"); for (int j = 0; j < affectedEntitiesJSON.Count; j++) { SpilPromotionAffectedEntity affectedEntity = JsonHelper.getObjectFromJson <SpilPromotionAffectedEntity>(affectedEntitiesJSON.list[j].Print()); affectedEntities.Add(affectedEntity); } promotionData.affectedEntities = affectedEntities; } promotionData.extraEntities = new List <SpilPromotionExtraEntity>(); if (promotion.HasField("extraEntities")) { List <SpilPromotionExtraEntity> extraEntities = new List <SpilPromotionExtraEntity>(); JSONObject extraEntitiesJSON = promotion.GetField("extraEntities"); for (int j = 0; j < extraEntitiesJSON.Count; j++) { SpilPromotionExtraEntity extraEntity = JsonHelper.getObjectFromJson <SpilPromotionExtraEntity>(extraEntitiesJSON.list[j].Print()); extraEntities.Add(extraEntity); } promotionData.extraEntities = extraEntities; } promotionData.priceOverride = new List <SpilPromotionPriceOverride>(); if (promotion.HasField("priceOverride")) { List <SpilPromotionPriceOverride> priceOverride = new List <SpilPromotionPriceOverride>(); JSONObject priceOverrideJSON = promotion.GetField("priceOverride"); for (int j = 0; j < priceOverrideJSON.Count; j++) { SpilPromotionPriceOverride price = JsonHelper.getObjectFromJson <SpilPromotionPriceOverride>(priceOverrideJSON.list[j].Print()); priceOverride.Add(price); } promotionData.priceOverride = priceOverride; } promotionData.gameAssets = new List <SpilPromotionGameAsset>(); if (promotion.HasField("gameAssets")) { List <SpilPromotionGameAsset> gameAssets = new List <SpilPromotionGameAsset>(); JSONObject gameAssetsJSON = promotion.GetField("gameAssets"); for (int j = 0; j < gameAssetsJSON.Count; j++) { SpilPromotionGameAsset gameAsset = JsonHelper.getObjectFromJson <SpilPromotionGameAsset>(gameAssetsJSON.list[j].Print()); gameAssets.Add(gameAsset); } promotionData.gameAssets = gameAssets; } PromotionsManager.PromotionData.Add(promotionData); } SpilUnityImplementationBase.firePromotionsAvailable(); } } else if (response.action.Equals("update")) { SpilPromotionData boughtPromotion = new SpilPromotionData(); if (response.data.HasField("id")) { boughtPromotion.id = (int)response.data.GetField("id").i; } if (response.data.HasField("amountPurchased")) { boughtPromotion.amountPurchased = (int)response.data.GetField("amountPurchased").i; } if (response.data.HasField("maxPurchase")) { boughtPromotion.maxPurchase = (int)response.data.GetField("maxPurchase").i; } PromotionsManager.ProcessBoughtPromotionResponse(boughtPromotion); } }