private User AdjustCurrency(UserDataViewModel user, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate)
        {
            if (!ChannelSession.Settings.Currencies.ContainsKey(currencyID))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

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

            UserCurrencyViewModel currency = ChannelSession.Settings.Currencies[currencyID];

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

                user.SubtractCurrencyAmount(currency, quantityToRemove);
            }
            else if (currencyUpdate.Amount > 0)
            {
                user.AddCurrencyAmount(currency, currencyUpdate.Amount);
            }

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

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

            UserCurrencyViewModel currency = ChannelSession.Settings.Currencies[currencyID];

            if (currencyUpdate.Amount < 0)
            {
                int quantityToRemove = currencyUpdate.Amount * -1;
                if (!user.HasCurrencyAmount(currency, quantityToRemove))
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.Forbidden)
                    {
                        Content = new ObjectContent <Error>(new Error {
                            Message = "User does not have enough currency to remove"
                        }, new JsonMediaTypeFormatter(), "application/json"),
                        ReasonPhrase = "Not Enough Currency"
                    };
                    throw new HttpResponseException(resp);
                }

                user.SubtractCurrencyAmount(currency, quantityToRemove);
            }
            else if (currencyUpdate.Amount > 0)
            {
                user.AddCurrencyAmount(currency, currencyUpdate.Amount);
            }

            return(UserFromUserDataViewModel(user));
        }
        public bool TrySubtractAmount(UserDataViewModel userData, int amount)
        {
            if (this.DoesMeetCurrencyRequirement(amount))
            {
                UserCurrencyViewModel currency = this.GetCurrency();
                if (currency == null)
                {
                    return(false);
                }

                if (!userData.HasCurrencyAmount(currency, amount))
                {
                    return(false);
                }
                userData.SubtractCurrencyAmount(currency, amount);
                return(true);
            }
            return(false);
        }