Beispiel #1
0
        public async Task <bool> UnlockIsValid(IAchievement achievement, long userId, int newRank)
        {
            using (var scope = MikiApp.Instance.Services.CreateScope())
            {
                var context           = scope.ServiceProvider.GetService <MikiDbContext>();
                var achievementObject = await DatabaseHelpers.GetAchievementAsync(context, userId, achievement.ParentName);

                // If no achievement has been found and want to unlock first
                if (achievementObject == null && newRank == 0)
                {
                    achievementObject = context.Achievements.Add(new Achievement()
                    {
                        UserId     = userId,
                        Name       = achievement.ParentName,
                        Rank       = 0,
                        UnlockedAt = DateTime.UtcNow
                    }).Entity;

                    await DatabaseHelpers.UpdateCacheAchievementAsync(userId, achievement.Name, achievementObject);

                    await context.SaveChangesAsync();

                    return(true);
                }
                // If achievement we want to unlock is the next achievement
                if (achievementObject != null)
                {
                    if (achievementObject.Rank == newRank - 1)
                    {
                        achievementObject.Rank++;
                    }
                    else
                    {
                        return(false);
                    }

                    await DatabaseHelpers.UpdateCacheAchievementAsync(userId, achievement.ParentName, achievementObject);

                    await context.SaveChangesAsync();

                    return(true);
                }
            }
            return(false);
        }
Beispiel #2
0
        internal async Task <bool> UnlockIsValid(long userId, int newRank)
        {
            using (var context = new MikiContext())
            {
                var achievement = await DatabaseHelpers.GetAchievementAsync(context, userId, ParentName);

                // If no achievement has been found and want to unlock first
                if (achievement == null && newRank == 0)
                {
                    achievement = context.Achievements.Add(new Achievement()
                    {
                        Id   = userId,
                        Name = ParentName,
                        Rank = 0
                    }).Entity;

                    await DatabaseHelpers.UpdateCacheAchievementAsync(userId, Name, achievement);

                    await context.SaveChangesAsync();

                    return(true);
                }
                // If achievement we want to unlock is the next achievement
                if (achievement != null)
                {
                    if (achievement.Rank == newRank - 1)
                    {
                        achievement.Rank++;
                    }
                    else
                    {
                        return(false);
                    }

                    await DatabaseHelpers.UpdateCacheAchievementAsync(userId, ParentName, achievement);

                    await context.SaveChangesAsync();

                    return(true);
                }
            }
            return(false);
        }