private void Deduct(DbAccount acc, int currency, int total, List <int> objectType) { switch ((Currency)currency) { case Currency.GOLD: { acc.Credits -= total; } break; case Currency.FAME: { acc.Fame -= total; acc.TotalFame -= total; } break; case Currency.GUILD_FAME: { acc.GuildFame -= total; } break; case Currency.FORTUNE_TOKENS: { acc.FortuneTokens -= total; } break; case Currency.EMPIRES_COIN: { acc.EmpiresCoin -= total; } break; case Currency.INVALID: default: return; } var items = acc.Gifts.ToList(); foreach (int item in objectType) { items.Add(item); } acc.Gifts = items.ToArray(); acc.FlushAsync(); acc.Reload(); }
public async Task <PkgPurchaseResult> Purchase(Database db, DbAccount acc) { var tran = db.Conn.CreateTransaction(); // handle quantity and max purchase checks ConditionResult quantityResult = null; ConditionResult maxPurchaseResult = null; if (Quantity > -1) { quantityResult = tran.AddCondition(Condition.HashNotEqual(_key, "amountPurchased", Quantity)); } tran.HashIncrementAsync(_key, "amountPurchased"); if (MaxPurchase > -1) { maxPurchaseResult = tran.AddCondition(Condition.HashNotEqual(_key, acc.AccountId, MaxPurchase)); } tran.HashIncrementAsync(_key, acc.AccountId); // deduct cost switch (Cost.Currency) { case 0: if (acc.Credits < Cost.Amount) { return(PkgPurchaseResult.NotEnoughGold); } db.UpdateCurrency(acc, -Cost.Amount, CurrencyType.Gold, tran); break; case 1: if (acc.Fame < Cost.Amount) { return(PkgPurchaseResult.NotEnoughFame); } db.UpdateCurrency(acc, -Cost.Amount, CurrencyType.Fame, tran); break; default: return(PkgPurchaseResult.InvalidCurrency); } // save gifts var items = new List <ushort>(); foreach (var pkgItem in Contents.Items ?? new PackageItem[0]) { items.AddRange(Enumerable.Repeat(pkgItem.Item, pkgItem.Count)); } db.AddGifts(acc, items, tran); // initiate transaction var t1 = tran.ExecuteAsync(); // upon success add character slots / vault chests var t2 = t1.ContinueWith(t => { var success = !t.IsCanceled && t.Result; if (!success) { if (quantityResult != null && !quantityResult.WasSatisfied) { return(PkgPurchaseResult.QuantityZero); } if (maxPurchaseResult != null && !maxPurchaseResult.WasSatisfied) { return(PkgPurchaseResult.MaxedPurchase); } return(PkgPurchaseResult.TransactionFailed); } if (Contents.CharacterSlots > 0) { acc.MaxCharSlot += Contents.CharacterSlots; } if (Contents.VaultChests > 0) { acc.VaultCount += Contents.VaultChests; } acc.FlushAsync(); return(PkgPurchaseResult.Success); }); // await tasks try { await Task.WhenAll(t1, t2); } catch (Exception e) { Log.Error(e); return(PkgPurchaseResult.Error); } return(t2.Result); }