Exemplo n.º 1
0
        public void NewCompletedPuzzle(CompletedPuzzleModel cp)
        {
            using (IDbConnection connection = new SQLiteConnection(ConfigurationString))
            {
                connection.Execute("INSERT OR REPLACE INTO COMPLETEDPUZZLES (UserId, PuzzleCode, DateCompleted) " +
                                   "VALUES (@UserId, @PuzzleCode, @DateCompleted)", cp);

                connection.Execute("UPDATE USER " +
                                   "SET Solved=(SELECT COUNT(*) FROM COMPLETEDPUZZLES WHERE UserId=@UserId) " +
                                   "WHERE Id=@UserId", cp);
            }
        }
Exemplo n.º 2
0
        public async Task ProcessCorrectAnswer(IGuildUser user, string code)
        {
            string userId = user.Id.ToString();

            var puzzle = _data.GetPuzzle(code);
            var dbUser = _data.GetUserById(userId) ?? new UserModel(user.Id.ToString(), user.Username);

            if (dbUser.HideSolved)
            {
                await SetChannelViewPermissionAsync(user, code, true);
            }

            int oldScore = dbUser.Score;

            dbUser.Score += puzzle.Points;
            dbUser.Solved++;
            _data.AddOrUpdateUser(dbUser);

            await UpdateRoleAsync(user, dbUser.Score, oldScore);

            if (puzzle.Points > Constants.PUZZLE_MINIMUM_POINTS)
            {
                var usersWhoSolvedPuzzle = _data.GetUsersWhoCompletedPuzzle(code);

                int oldPuzzlePoints = CalculatePuzzlePoints(usersWhoSolvedPuzzle.Count);
                int newPuzzlePoints = CalculatePuzzlePoints(usersWhoSolvedPuzzle.Count + 2);

                int pointDifference = oldPuzzlePoints - puzzle.Points;

                foreach (var userWhoSolvedPuzzle in usersWhoSolvedPuzzle)
                {
                    userWhoSolvedPuzzle.Score -= pointDifference;
                    _data.AddOrUpdateUser(userWhoSolvedPuzzle);
                }

                var p = new PuzzleModel
                {
                    Code   = puzzle.Code,
                    Points = newPuzzlePoints
                };

                _data.AddOrUpdatePuzzle(p);
            }

            var cpm = new CompletedPuzzleModel
            {
                UserId        = userId,
                PuzzleCode    = code,
                DateCompleted = DateTime.UtcNow
            };

            _data.NewCompletedPuzzle(cpm);
        }