/// <summary> /// Remove a number of an item from this user. /// </summary> /// <param name="user">User from which to remove this/these item(s)</param> /// <param name="playerInventoryItemId">PlayerInventoryItem "slot" from which to remove items</param> /// <param name="amount">The amount of this item to remove. Will throw an exception if this is greater than the amount the User currently has</param> public async Task RemoveItemFromUser(User user, Guid playerInventoryItemId, int amount) { PlayerInventoryItem existingInventoryItem = GetInventoryItemById(user, playerInventoryItemId); // Validate user has an item with this id if (existingInventoryItem == null) { throw new System.ArgumentException($"Cannot remove item from user. User '{user.Id}' has no inventory item with id '{playerInventoryItemId}'"); } // Validate user has enough of this item to remove else if (existingInventoryItem.Count < amount) { throw new InvalidOperationException($"Cannot remove {amount} of item from user. User '{user.Id}' only has {existingInventoryItem.Count} of item '{playerInventoryItemId}'"); } // Decrease amount existingInventoryItem.Count -= amount; if (existingInventoryItem.Count == 0) { // Remove from user entirely user.Inventory.Remove(existingInventoryItem); } // Save changes to DB this.db.Users.Update(user); await this.db.SaveChangesAsync(); }
/// <summary> /// Give the user an item. /// </summary> /// <param name="user">User for which to give this item</param> /// <param name="itemId">Unique ID of the item to give to this user. A PlayerInventoryItem "slot" will be created if one doesn't exist already</param> public async Task AddItemToUser(User user, Guid itemId, int amount) { // Look up item to add (validation) Item item = this.itemService.GetItemById(itemId); if (item == null) { throw new ArgumentException($"Cannot add item to user. No item exists with id '{itemId}'", nameof(itemId)); } // See if player already holds this item PlayerInventoryItem existingInventoryItem = GetInventoryItemByItemId(user, itemId); if (existingInventoryItem != null) { // Update inventory item existingInventoryItem.Count += amount; } else { // Create new inventory item PlayerInventoryItem newInventoryItem = new PlayerInventoryItem { Count = amount, UserId = user.Id, ItemId = item.Id, }; user.Inventory.Add(newInventoryItem); } // Save changes to DB this.db.Users.Update(user); await this.db.SaveChangesAsync(); }
public async Task UserDonateItem(User user, Guid playerInventoryItemId) { // @TODO less trips to the database // Fetch and validate playerInventoryItemId PlayerInventoryItem playerInventoryItem = this.userService.GetInventoryItemById(user, playerInventoryItemId); if (playerInventoryItem == null) { throw new ArgumentException($"Cannot donate item to taking tree. Player has no item with id '{playerInventoryItemId}'", nameof(playerInventoryItemId)); } // Remove item from user await this.userService.RemoveItemFromUser(user, playerInventoryItemId, 1); // Add item to taking tree await this.db.TakingTreeInventoryItems.AddAsync(new TakingTreeInventoryItem { ItemId = playerInventoryItem.Item.Id, DonatedById = user.Id, }); await this.db.SaveChangesAsync(); }