private User AdjustInventory(UserDataViewModel user, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            if (!ChannelSession.Settings.Inventories.ContainsKey(inventoryID))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (inventoryUpdate == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            UserInventoryViewModel inventory = ChannelSession.Settings.Inventories[inventoryID];

            if (string.IsNullOrEmpty(inventoryUpdate.Name) || !inventory.Items.ContainsKey(inventoryUpdate.Name))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            if (inventoryUpdate.Amount < 0)
            {
                int quantityToRemove = inventoryUpdate.Amount * -1;
                if (!user.HasInventoryAmount(inventory, inventoryUpdate.Name, quantityToRemove))
                {
                    // If the request is to remove currency, but user doesn't have enough, fail
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }

                user.SubtractInventoryAmount(inventory, inventoryUpdate.Name, quantityToRemove);
            }
            else if (inventoryUpdate.Amount > 0)
            {
                user.AddInventoryAmount(inventory, inventoryUpdate.Name, inventoryUpdate.Amount);
            }

            return(UserFromUserDataViewModel(user));
        }
Exemplo n.º 2
0
        private User AdjustInventory(UserDataViewModel user, Guid inventoryID, [FromBody] AdjustInventory inventoryUpdate)
        {
            if (!ChannelSession.Settings.Inventories.ContainsKey(inventoryID))
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = $"Unable to find inventory: {inventoryID.ToString()}."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Inventory ID not found"
                };
                throw new HttpResponseException(resp);
            }

            if (inventoryUpdate == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = "Unable to parse inventory adjustment from POST body."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Invalid POST Body"
                };
                throw new HttpResponseException(resp);
            }

            UserInventoryViewModel inventory = ChannelSession.Settings.Inventories[inventoryID];

            if (string.IsNullOrEmpty(inventoryUpdate.Name) || !inventory.Items.ContainsKey(inventoryUpdate.Name))
            {
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new ObjectContent <Error>(new Error {
                        Message = "Unable to find requested inventory item."
                    }, new JsonMediaTypeFormatter(), "application/json"),
                    ReasonPhrase = "Invalid Inventory Item"
                };
                throw new HttpResponseException(resp);
            }

            if (inventoryUpdate.Amount < 0)
            {
                int quantityToRemove = inventoryUpdate.Amount * -1;
                if (!user.HasInventoryAmount(inventory, inventoryUpdate.Name, quantityToRemove))
                {
                    // If the request is to remove inventory, but user doesn't have enough, fail
                    var resp = new HttpResponseMessage(HttpStatusCode.Forbidden)
                    {
                        Content = new ObjectContent <Error>(new Error {
                            Message = "User does not have enough inventory to remove"
                        }, new JsonMediaTypeFormatter(), "application/json"),
                        ReasonPhrase = "Not Enough Inventory"
                    };
                    throw new HttpResponseException(resp);
                }

                user.SubtractInventoryAmount(inventory, inventoryUpdate.Name, quantityToRemove);
            }
            else if (inventoryUpdate.Amount > 0)
            {
                user.AddInventoryAmount(inventory, inventoryUpdate.Name, inventoryUpdate.Amount);
            }

            return(UserFromUserDataViewModel(user));
        }