private void PlayerDataUpdatedHandler(string reason, PlayerDataUpdatedData updatedData) { if (reason.Equals(PlayerDataUpdateReasons.ItemBought)) { txtErrorMessage.text = "Transaction successfull"; pnlErrorMessage.SetActive(true); } updateUI(); }
private void CalculatePlayerDataResponse(WalletData receivedWallet, InventoryData receivedInventory) { bool updated = false; PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData (); if (receivedWallet != null) { for (int i = 0; i < Wallet.currencies.Count; i++) { Wallet.currencies[i].delta = 0; } if (Wallet.offset < receivedWallet.offset && receivedWallet.currencies.Count > 0) { for (int i = 0; i < receivedWallet.currencies.Count; i++) { for (int j = 0; j < Wallet.currencies.Count; j++) { if (receivedWallet.logic.Equals ("CLIENT")) { if (Wallet.currencies [j].id == receivedWallet.currencies [i].id && receivedWallet.currencies [i].delta != 0) { int updatedBalance = 0; if (Wallet.offset == 0 && receivedWallet.offset != 0) { updatedBalance = receivedWallet.currencies [i].currentBalance; } else { updatedBalance = Wallet.currencies [j].currentBalance + receivedWallet.currencies [i].delta; if (updatedBalance < 0) { updatedBalance = 0; } } Wallet.currencies [j].currentBalance = updatedBalance; updated = true; updatedData.currencies.Add (Wallet.currencies [j]); } } else if (receivedWallet.logic.Equals ("SERVER")) { } } } } Wallet.offset = receivedWallet.offset; Wallet.logic = receivedWallet.logic; } if (receivedInventory != null) { for (int i = 0; i < Inventory.items.Count; i++) { Inventory.items[i].delta = 0; } if(Inventory.offset < receivedInventory.offset && receivedInventory.items.Count > 0){ List<PlayerItemData> itemsToBeAdded = new List<PlayerItemData>(); for(int i = 0; i < receivedInventory.items.Count; i++){ for(int j = 0; j < Inventory.items.Count; j++){ if(receivedInventory.logic.Equals("CLIENT")){ if(Inventory.items[j].id == receivedInventory.items[i].id && receivedInventory.items[i].delta != 0){ int updatedAmount = Inventory.items[j].amount + receivedInventory.items[i].delta; Inventory.items[j].amount = updatedAmount; } else { itemsToBeAdded.Add(receivedInventory.items[i]); } updated = true; } else if(receivedInventory.logic.Equals("SERVER")){ } } updatedData.items.Add(receivedInventory.items[i]); } for(int i = 0; i < itemsToBeAdded.Count; i++){ SpilItemData item = GetItemFromObjects(itemsToBeAdded[i].id); if(item != null && itemsToBeAdded[i].amount > 0){ PlayerItemData playerItem = new PlayerItemData(); playerItem.id = item.id; playerItem.name = item.name; playerItem.type = item.type; playerItem.amount = itemsToBeAdded[i].amount; playerItem.value = itemsToBeAdded[i].value; playerItem.delta = 0; Inventory.items.Add(playerItem); updated = true; } } } Inventory.offset = receivedInventory.offset; Inventory.logic = receivedInventory.logic; } if (updated) { updatedData.reason = PlayerDataUpdateReasons.ServerUpdate; SpilUnityImplementationBase.firePlayerDataUpdated (JsonHelper.getJSONFromObject (updatedData)); } }
public void WalletOperation(string action, int currencyId, int amount, string reason) { if(currencyId <= 0 || reason == null){ Debug.Log ("Error updating wallet!"); return; } PlayerCurrencyData currency = null; for (int i = 0; i < Wallet.currencies.Count; i++) { if (Wallet.currencies [i].id == currencyId) { currency = Wallet.currencies [i]; } } if (currency == null) { Debug.Log ("Currency does not exist!"); return; } int currentBalance = currency.currentBalance; if (action.Equals ("subtract")) { amount = -amount; } int updatedBalance = currentBalance + amount; if (updatedBalance < 0) { Debug.Log ("Not enough balance for currency!"); return; } int updatedDelta = amount + currency.delta; if (updatedDelta == 0) { updatedDelta = amount; } currency.delta = updatedDelta; currency.currentBalance = updatedBalance; if (Wallet.logic.Equals ("CLIENT")) { UpdateCurrency (currency); PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData (); updatedData.currencies.Add (currency); updatedData.reason = reason; SpilUnityImplementationBase.firePlayerDataUpdated (JsonHelper.getJSONFromObject (updatedData)); SendUpdatePlayerDataEvent (reason); } else if (Wallet.logic.Equals ("SERVER")) { } }
public void ConsumeBundle(int bundleId, string reason) { PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData(); SpilBundleData bundle = GetBundleFromObjects(bundleId); if(bundle == null || reason == null){ Debug.Log("Error adding bundle to player inventory!"); return; } SpilShopPromotionData promotion = GetPromotionFromObjects(bundleId); bool isPromotionValid = false; if(promotion != null){ isPromotionValid = IsPromotionValid(promotion); } List<SpilBundlePriceData> bundlePrices; if(isPromotionValid){ bundlePrices = promotion.prices; } else { bundlePrices = bundle.prices; } for(int i = 0; i < bundlePrices.Count; i++){ PlayerCurrencyData currency = GetCurrencyFromWallet(bundlePrices[i].currencyId); if(currency == null){ Debug.Log("Currency does not exist!"); return; } int currentBalance = currency.currentBalance; int updatedBalance = currentBalance - bundlePrices[i].value; if(updatedBalance < 0){ Debug.Log("Not enough balance for currency!"); return; } int updatedDelta = - bundlePrices[i].value + currency.delta; if(updatedDelta == 0){ updatedDelta = - bundlePrices[i].value; } currency.delta = updatedDelta; currency.currentBalance = updatedBalance; UpdateCurrency(currency); updatedData.currencies.Add(currency); } for(int i = 0; i < bundle.items.Count; i++){ SpilItemData gameItem = GetItemFromObjects(bundle.items[i].id); if(gameItem != null){ PlayerItemData item = new PlayerItemData(); item.id = gameItem.id; item.name = gameItem.name; item.type = gameItem.type; PlayerItemData inventoryItem = GetItemFromInventory(bundle.items[i].id); int inventoryItemAmount; if(inventoryItem != null){ inventoryItemAmount = inventoryItem.amount; int promoAmount = 1; if(isPromotionValid){ promoAmount = promotion.amount; } inventoryItemAmount = inventoryItemAmount + bundle.items[i].amount * promoAmount; inventoryItem.delta = bundle.items[i].amount; inventoryItem.amount = inventoryItemAmount; UpdateItem(inventoryItem); updatedData.items.Add(inventoryItem); } else { inventoryItemAmount = bundle.items[i].amount; if(isPromotionValid){ inventoryItemAmount = inventoryItemAmount * promotion.amount; } item.delta = inventoryItemAmount; item.amount = inventoryItemAmount; Inventory.items.Add(item); updatedData.items.Add(inventoryItem); } } } updatedData.reason = reason; SpilUnityImplementationBase.firePlayerDataUpdated (JsonHelper.getJSONFromObject (updatedData)); SendUpdatePlayerDataEvent(bundle, reason); }
public void InventoryOperation(string action, int itemId, int amount, string reason) { SpilItemData gameItem = GetItemFromObjects(itemId); if(gameItem == null || itemId <= 0 || action == null || reason == null){ Debug.Log("Error updating item to player inventory!"); return; } PlayerItemData item = new PlayerItemData(); item.id = gameItem.id; item.name = gameItem.name; item.type = gameItem.type; 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){ Debug.Log("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")){ Debug.Log("Could not remove item as amount is too low!"); } } PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData(); updatedData.items.Add(item); updatedData.reason = reason; SpilUnityImplementationBase.firePlayerDataUpdated (JsonHelper.getJSONFromObject (updatedData)); SendUpdatePlayerDataEvent(item, reason); }
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; } } }
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 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); }
public void WalletOperation(string action, int currencyId, int amount, string reason, string reasonDetails, string location, string transactionId) { if (currencyId <= 0 || reason == null) { SpilLogging.Error("Error updating wallet!"); return; } PlayerCurrencyData currency = null; foreach (PlayerCurrencyData playerCurrency in Wallet.currencies) { if (playerCurrency.id == currencyId) { currency = playerCurrency; } } if (currency == null) { SpilLogging.Error("Currency does not exist!"); return; } int currentBalance = currency.currentBalance; if (action.Equals("subtract")) { amount = -amount; } int updatedBalance = currentBalance + amount; if (updatedBalance < 0) { SpilLogging.Error("Not enough balance for currency!"); return; } int updatedDelta = amount + currency.delta; if (updatedDelta == 0) { updatedDelta = amount; } currency.delta = updatedDelta; currency.currentBalance = updatedBalance; if (Wallet.logic.Equals("CLIENT")) { UpdateCurrency(currency); UserDataManager.UpdateUserDataVersions(); UserDataManager.UpdateUserDataMeta(); PlayerDataUpdatedData updatedData = new PlayerDataUpdatedData(); updatedData.currencies.Add(currency); updatedData.reason = reason; SpilUnityImplementationBase.firePlayerDataUpdated(JsonHelper.getJSONFromObject(updatedData)); SendUpdatePlayerDataEvent(null, reason, reasonDetails, location, transactionId); } else if (Wallet.logic.Equals("SERVER")) { } }
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(); } }