Beispiel #1
0
        public async Task GiveUserItem(Profile profile, Profile botProfile, string itemName)
        {
            using var context = new RPGContext(_options);

            RobbingItems item = context.RobbingItems.First(x => x.Name.ToLower() == itemName.ToLower());

            if (profile.Gold < item.Cost)
            {
                throw new Exception("User cannot afford the item.");
            }

            if (profile.Level < item.LvlRequired)
            {
                throw new Exception("User is not the right level");
            }

            ItemsJson itemsJson = JsonConvert.DeserializeObject <ItemsJson>(profile.ItemJson);

            string newItemsJson;

            if (itemsJson.Robbing.Exists(x => x.Id == item.Id))
            {
                if (itemsJson.Robbing.SingleOrDefault(x => x.Id == item.Id).Count >= item.MaxAllowed)
                {
                    throw new Exception("user already owns max amount of items.");
                }


                itemsJson.Robbing.SingleOrDefault(x => x.Id == item.Id).Count++;

                newItemsJson = JsonConvert.SerializeObject(itemsJson);
            }
            else
            {
                Robbing newRobbing = new Robbing {
                    Id = item.Id, Count = 1
                };

                itemsJson.Robbing.Add(newRobbing);

                newItemsJson = JsonConvert.SerializeObject(itemsJson);
            }



            profile.ItemJson = newItemsJson;
            profile.Gold    -= item.Cost;

            context.Profiles.Update(profile);

            botProfile.Gold += item.Cost;

            context.Profiles.Update(botProfile);

            await context.SaveChangesAsync().ConfigureAwait(false);
        }
Beispiel #2
0
        public async Task GiveUserItem(Profile profile, Profile botProfile, int itemId)
        {
            using var context = new RPGContext(_options);

            RobbingItems item = context.RobbingItems.SingleOrDefault(x => x.Id == itemId);

            if (profile.Gold < item.Cost)
            {
                throw new InvalidOperationException("The user cannot afford to purchase this item.");
            }

            if (profile.Level < item.LvlRequired)
            {
                throw new InvalidOperationException("The user is not the correct level to purchase this item.");
            }

            ItemsJson itemsJson = JsonConvert.DeserializeObject <ItemsJson>(profile.ItemJson);

            if (itemsJson.Robbing.SingleOrDefault(x => x.Id == itemId).Count >= item.MaxAllowed)
            {
                throw new InvalidOperationException("The user already owns the maximum number of items allowed.");
            }

            string newItemsJson;

            if (itemsJson.Robbing.Exists(x => x.Id == itemId))
            {
                itemsJson.Robbing.SingleOrDefault(x => x.Id == itemId).Count++;

                newItemsJson = JsonConvert.SerializeObject(itemsJson);
            }
            else
            {
                Robbing newRobbing = new Robbing {
                    Id = itemId, Count = 1
                };

                itemsJson.Robbing.Add(newRobbing);

                newItemsJson = JsonConvert.SerializeObject(itemsJson);
            }

            profile.ItemJson = newItemsJson;
            profile.Gold    -= item.Cost;

            context.Profiles.Update(profile);

            botProfile.Gold += item.Cost;

            context.Profiles.Update(botProfile);

            await context.SaveChangesAsync().ConfigureAwait(false);
        }