Exemple #1
0
        public async Task <Maybe <uint> > QuickSellWaifu(ulong userId, int waifuId, uint amount, WaifuRarity?rarity = null)
        {
            return(await _soraTransactor.DoInTransactionAndGetAsync <Maybe <uint> >(async context =>
            {
                var user = await context.Users.FindAsync(userId).ConfigureAwait(false);
                if (user == null)
                {
                    return Maybe.FromErr <uint>("You dont have any Waifus. Get some by opening Waifu Boxes!");
                }
                var userWaifu = user.UserWaifus.FirstOrDefault(x => x.WaifuId == waifuId);
                if (userWaifu == null)
                {
                    return Maybe.FromErr <uint>("You don't have that Waifu.");
                }
                if (userWaifu.Count < amount)
                {
                    return Maybe.FromErr <uint>("You don't have enough of that Waifu. Try selling less!");
                }

                rarity ??= userWaifu.Waifu.Rarity;

                // We got the waifu and enough so sell it
                uint coinAmount = WaifuUtils.GetWaifuQuickSellCost(rarity.Value) * amount;
                if (userWaifu.Count == amount)
                {
                    user.UserWaifus.Remove(userWaifu);
                    // Make sure to remove it from favorite waifu if it is.
                    if (user.FavoriteWaifuId.HasValue && user.FavoriteWaifuId.Value == userWaifu.WaifuId)
                    {
                        user.FavoriteWaifuId = null;
                    }
                }
                else
                {
                    userWaifu.Count -= amount;
                }

                user.Coins += coinAmount;
                await context.SaveChangesAsync().ConfigureAwait(false);
                return Maybe.FromVal(coinAmount);
            }).ConfigureAwait(false));
        }
Exemple #2
0
        // TODO improve this code. this is rather slow and stupid ngl
        public async Task <Maybe <(uint waifusSold, uint coinAmount)> > SellDupes(ulong userId)
        {
            return(await _soraTransactor.DoInTransactionAndGetAsync(async context =>
            {
                var dupes = await context.UserWaifus
                            .Where(x => x.UserId == userId && x.Count > 1)
                            .ToListAsync()
                            .ConfigureAwait(false);

                if (dupes == null || dupes.Count == 0)
                {
                    return Maybe.FromErr <(uint, uint)>("You don't have any dupes to sell! Open some Waifu Boxes");
                }

                // Get the waifus that are not of special rarity
                var waifus = dupes.Select(d => d.Waifu).Where(w => !WaifuUtils.IsSpecialOrUltiWaifu(w.Rarity)).ToList();
                dupes = dupes.Where(d => waifus.Any(x => x.Id == d.WaifuId)).ToList();
                if (dupes.Count == 0)
                {
                    return Maybe.FromErr <(uint, uint)>("You don't have any dupes to sell! Open some Waifu Boxes. Ultimate or Special Waifus are not sold with this method!");
                }
                // Remove the dupes and accumulate the coins
                uint totalCoins = 0;
                uint totalSold = 0;
                foreach (var dupe in dupes)
                {
                    uint sold = dupe.Count - 1;
                    totalSold += sold;
                    dupe.Count = 1;
                    totalCoins += sold *WaifuUtils.GetWaifuQuickSellCost(dupe.Waifu.Rarity);
                }

                var user = await context.Users.FindAsync(userId).ConfigureAwait(false);
                user.Coins += totalCoins;
                await context.SaveChangesAsync().ConfigureAwait(false);
                return Maybe.FromVal((totalSold, totalCoins));
            }).ConfigureAwait(false));
        }