public async Task PurchaseShopItem(int itemId, User activeUser) { using (var trans = TransactionScopeFactory.Create()) { (ShopItem shopItem, InventoryItem item)purchaseItem = (await ShopRepo.RetrieveShopItemByItemId(itemId)).FirstOrDefault(); if (purchaseItem.shopItem?.Price > 0 && activeUser.Cash >= purchaseItem.shopItem.Price) { var userChanges = new List <(int UserId, int CashDelta)> { (activeUser.Id, (-1) * purchaseItem.shopItem.Price.Value) }; if (purchaseItem.item.OwnerUserId != null) { userChanges.Add((purchaseItem.item.OwnerUserId.Value, purchaseItem.shopItem.Price.Value));//owner can be null if NPC shop } await UserDomain.ChangeUsersCash(userChanges); await InventoryDomain.TransferItemOwner(activeUser.Id, purchaseItem.item.Id, purchaseItem.item.OwnerUserId); if (!await ShopRepo.RemoveShopItem(itemId)) { throw new CritterException("Couldn't purchase item!", $"Failed to remove {itemId} from its shop", System.Net.HttpStatusCode.Conflict); } } trans.Complete(); } }
internal async Task <bool> ChangeItemOwner(int itemId, int?newOwnerId, int?currentOwnerId) { try { await InventoryDomain.TransferItemOwner(newOwnerId, itemId, currentOwnerId); } catch (Exception ex) { Log.Logger.Error(ex, "Failed to transfer item ownership via NPC action."); return(false); } return(true); }