示例#1
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);
        }
示例#2
0
        public async Task AddPuzzleDataAsync(PuzzleDataModel puzzle)
        {
            if (_data.GetPuzzle(puzzle.PuzzleCode) == null)
            {
                var newPuzzle = new PuzzleModel
                {
                    Code   = puzzle.PuzzleCode,
                    Points = CalculatePuzzlePoints(0)
                };
                _data.AddOrUpdatePuzzle(newPuzzle);

                await SendMessageToChannelAsync($"Added new puzzle: <#{puzzle.PuzzleCode}>",
                                                Constants.NOTIFICATIONS_CHANNEL);
            }

            _data.AddPuzzleData(puzzle);
        }