private void SendUpdatePlayerDataEvent(SpilBundleData bundle, string reason) { SpilEvent spilEvent = Spil.MonoInstance.gameObject.AddComponent<SpilEvent> (); spilEvent.eventName = "updatePlayerData"; JSONObject walletJSON = new JSONObject(); JSONObject currenciesJSON = new JSONObject(JsonHelper.getJSONFromObject(Wallet.currencies)); walletJSON.AddField("currencies", currenciesJSON); walletJSON.AddField("offset", Wallet.offset); spilEvent.customData.AddField("wallet", walletJSON); JSONObject inventoryJSON = new JSONObject(); JSONObject itemsJSON = new JSONObject(JsonHelper.getJSONFromObject(Inventory.items)); inventoryJSON.AddField("items", itemsJSON); inventoryJSON.AddField("offset", Inventory.offset); spilEvent.customData.AddField("inventory", inventoryJSON); spilEvent.customData.AddField("bundle", new JSONObject(JsonHelper.getJSONFromObject(bundle))); spilEvent.customData.AddField("reason", reason); spilEvent.Send(); }
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 void SendUpdatePlayerDataEvent(SpilBundleData bundle, string reason, string reasonDetails, string location, string transactionId) { SpilEvent spilEvent = Spil.MonoInstance.gameObject.AddComponent <SpilEvent>(); spilEvent.eventName = "updatePlayerData"; JSONObject walletObject = new JSONObject(); List <PlayerCurrencyData> currencyList = new List <PlayerCurrencyData>(); foreach (PlayerCurrencyData currencyData in Wallet.currencies) { if (currencyData.delta != 0) { currencyList.Add(currencyData); } } if (currencyList.Count > 0) { JSONObject currenciesJSON = new JSONObject(); foreach (PlayerCurrencyData currencyData in currencyList) { JSONObject obj = new JSONObject(); obj.AddField("id", currencyData.id); obj.AddField("currentBalance", currencyData.currentBalance); obj.AddField("delta", currencyData.delta); currenciesJSON.Add(obj); } walletObject.AddField("currencies", currenciesJSON); walletObject.AddField("offset", Wallet.offset); spilEvent.customData.AddField("wallet", walletObject); foreach (PlayerCurrencyData currencyData in Wallet.currencies) { currencyData.delta = 0; UpdateCurrency(currencyData); } } JSONObject inventoryObject = new JSONObject(); List <PlayerItemData> itemsList = new List <PlayerItemData>(); foreach (PlayerItemData playerItemData in Inventory.items) { if (playerItemData.delta != 0) { itemsList.Add(playerItemData); } } if (itemsList.Count > 0) { JSONObject itemsJSON = new JSONObject(JSONObject.Type.ARRAY); foreach (PlayerItemData playerItemData in itemsList) { JSONObject obj = new JSONObject(); obj.AddField("id", playerItemData.id); obj.AddField("amount", playerItemData.amount); obj.AddField("delta", playerItemData.delta); itemsJSON.Add(obj); } inventoryObject.AddField("items", itemsJSON); inventoryObject.AddField("offset", Inventory.offset); spilEvent.customData.AddField("inventory", inventoryObject); foreach (PlayerItemData playerItemData in Inventory.items) { playerItemData.delta = 0; UpdateItem(playerItemData); } } if (bundle != null) { spilEvent.customData.AddField("bundle", new JSONObject(JsonHelper.getJSONFromObject(bundle))); } spilEvent.customData.AddField("reason", reason); if (reasonDetails != null) { spilEvent.customData.AddField("reasonDetails", reasonDetails); } if (location != null) { spilEvent.customData.AddField("location", location); } if (transactionId != null) { spilEvent.customData.AddField("transactionId", transactionId); } spilEvent.customData.AddField("deviceVersions", UserDataManager.GenerateUserDataVersionsJSON(UserDataManager.userDataVersions)); spilEvent.Send(); }
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(); }