public PuzzleViewModel(Puzzle puzzle, VoteType voteType, Solution latestSolution, Solution bestSolution, int userSolutionCount, IEnumerable<SolutionViewModel> puzzleLeaderBoard, bool isCreator, bool isLeading)
 {
     _puzzle = puzzle;
     _voteType = voteType;
     LatestSolution = latestSolution;
     BestSolution = bestSolution;
     UserSolutionCount = userSolutionCount;
     PuzzleLeaderBoard = puzzleLeaderBoard;
     IsCreator = isCreator;
     Themes = puzzle.Themes.Select(x => x.Name);
     IsLeading = isLeading;
 }
        public void CalculateUserReputationForSolution(Solution currentSolution)
        {
            using (_repository.OpenSession())
            {
                if (_repository.All<Puzzle>().ById(currentSolution.PuzzleId).User.Id == currentSolution.UserId)
                    return;

                var solutionUser = _repository.All<User>().ById(currentSolution.UserId);
                var puzzleUser = _repository.All<Puzzle>().ById(currentSolution.PuzzleId).User;
                var solutions = _repository.All<Solution>().ByPuzzleId(currentSolution.PuzzleId).ByUser(currentSolution.UserId);

                solutionUser.Reputation += CalculateSolutionUserReputation(currentSolution, solutions);

                if (solutions.Count() == 1)
                    puzzleUser.Reputation += Settings.PointsAwardedToCreatorWhenPuzzleIsPlayed;

                using (var tx = _repository.BeginTransaction())
                {
                    _repository.Save(solutionUser);
                    _repository.Save(puzzleUser);
                    tx.Commit();
                }
            }
        }
Esempio n. 3
0
        private void CreateSolution(CurrentPuzzleInfo puzzleInfo)
        {
            User user;
            Puzzle puzzle;
            Solution solution;
            var isValidating = false;
            bool isLeader;

            using (_repository.OpenSession())
            {
                user = _repository.All<User>().ById(puzzleInfo.UserId);

                using (var tx = _repository.BeginTransaction())
                {
                    puzzle = _repository.All<Puzzle>().ById(puzzleInfo.PuzzleId);
                    solution = new Solution
                                       {
                                           PuzzleId = puzzle.Id,
                                           UserId = puzzleInfo.UserId,
                                           PointsAwarded = _reputationService.CalculateSolutionReputation(puzzle.User.Id, puzzleInfo.UserId, puzzleInfo.Steps.Count + 1, puzzle.Level),
                                           StepCount = puzzleInfo.Steps.Count + 1 /* add 1 for the final step */,
                                           DateCreated = DateTime.Now,
                                           CurrentPuzzleLevel = puzzle.Level,
                                           CurrentSolutionCount = puzzle.SolutionCount
                                       };

                    _repository.Save(solution);

                    for (var i = 0; i < puzzleInfo.Steps.Count; i++)
                    {
                        var step = new Step
                                       {
                                           StepNumber = i,
                                           Topic = puzzleInfo.Steps[i].Name,
                                           SolutionId = solution.Id,
                                       };
                        _repository.Save(step);
                    }
                    var finalStep = new Step
                                        {
                                            StepNumber = puzzleInfo.Steps.Count,
                                            Topic = puzzle.EndTopic,
                                            SolutionId = solution.Id
                                        };

                    _repository.Save(finalStep);

                    if (!puzzle.IsVerified)
                    {
                        isValidating = true;
                        puzzle.IsVerified = true;
                    }

                    _repository.Save(puzzle);

                    _repository.Save(new ActionItem
                                         {
                                             Action = ActionType.SolvedPuzzle,
                                             DateCreated = DateTime.Now,
                                             PuzzleId = puzzle.Id,
                                             SolutionId = solution.Id,
                                             UserId = user.Id,
                                             AffectedUserId = puzzle.User.Id
                                         });

                    tx.Commit();
                }
                //Solution is commited at this point so if there is only 1 solution then we are the leader
                isLeader = _repository.All<Solution>().Where(x => x.PuzzleId == puzzle.Id && x.StepCount <= solution.StepCount).Count() == 1;
            }

            _puzzleCache.ClearCurrentPuzzleInfo();

            _puzzleService.UpdatePuzzleStats(puzzle.Id);

            _reputationService.CalculateUserReputationForSolution(solution);

            if (isValidating)
                _twitterService.SendPuzzleCreatedMessage(puzzle.Id);

            if (isLeader)
                _twitterService.SendNewPuzzleLeaderMessage(user.Id, solution.StepCount, puzzle.Id);
        }
        private static int CalculateSolutionUserReputation(Solution currentSolution, IEnumerable<Solution> solutions)
        {
            var previousBestSolution = solutions
                .Where(x => x.Id != currentSolution.Id)
                .OrderByDescending(x => x.PointsAwarded)
                .FirstOrDefault();

            if (previousBestSolution == null)
                return currentSolution.PointsAwarded;

            var repToAward = 0;
            if (previousBestSolution.PointsAwarded < currentSolution.PointsAwarded)
                repToAward = currentSolution.PointsAwarded - previousBestSolution.PointsAwarded;

            return repToAward;
        }
 public SolutionViewModel(Solution solution, User user)
 {
     _solution = solution;
     _user = user;
 }