public override void RegisterProduct(IProduct product) { if (product == null) { Debug.LogError("Null product being registered to Wallet"); return; } string type = product.GetProductType(); int id = product.GetProductId(); if (IsProductRegistered(product)) { Debug.LogError("Trying to register duplicate products for ID " + id); return; } productAmounts[id] = new ProductAmount(product, 0); if (!productsByType.ContainsKey(type)) { productsByType[type] = new List <IProduct>(); } productsByType[type].Add(product); registeredProducts.Add(product); }
public override void ClearWalletAmounts() { int len = productAmounts.Keys.Count; Dictionary <int, ProductAmount> .KeyCollection keys = productAmounts.Keys; int[] keysArray = new int[len]; keys.CopyTo(keysArray, 0); foreach (int key in keysArray) { productAmounts[key] = new ProductAmount(productAmounts[key].product, 0); } }
private bool DeltaOperation(int productId, int delta, bool canOverflow) { if (!productAmounts.ContainsKey(productId)) { Debug.LogError("Product not registered for id " + productId); return(false); } ProductAmount stored = productAmounts[productId]; bool canPerform = canOverflow || delta > 0 || delta > stored.amount; if (canPerform) { stored.amount = (uint)(Mathf.Max(stored.amount + delta, 0)); productAmounts[productId] = stored; } else if (!canOverflow) { Debug.LogError("Change to product " + productId + " overflowed negatively"); } return(canPerform); }