Exemplo n.º 1
0
 public void SetCurrentPuzzleInfo(CurrentPuzzleInfo puzzleInfo)
 {
     if (puzzleInfo == null)
         ClearCurrentPuzzleInfo();
     else
         HttpContext.Current.Session.Add(PUZZLE_INFO, puzzleInfo);
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public CurrentPuzzleInfo StartPuzzle(int puzzleId)
        {
            _puzzleCache.ClearCurrentPuzzleInfo();

            #region Ensure

            var user = _accountService.GetUserById(_authenticationService.CurrentUserId);
            if (user == null)
                throw new UnauthorizedAccessException("You must be logged in to play a puzzle");

            Puzzle puzzle = _puzzleService.GetPuzzleById(puzzleId);
            if (puzzle == null)
                throw new FileNotFoundException("The puzzle you are trying to play '{0}' does not exist.".ToFormat(puzzleId));

            Topic startTopic = _topicService.GetTopicByName(puzzle.StartTopic);
            if (startTopic == null)
                throw new FileNotFoundException("The starting topic '{0}' could not be found".ToFormat(puzzle.StartTopic));

            if (!puzzle.IsVerified && puzzle.User.Id != user.Id)
               throw new UnauthorizedAccessException("You are trying to access a puzzle that has not been verified and you aren't the creator.");

            #endregion

            var puzzleInfo = new CurrentPuzzleInfo
                                 {
                                     PuzzleId = puzzle.Id,
                                     UserId = user.Id,
                                     EndTopic = _topicService.GetTopicByName(puzzle.EndTopic),
                                     StartTopic = startTopic,
                                     CurrentTopic = null,
                                     PuzzleLevel = puzzle.Level
                                 };

            _puzzleCache.SetCurrentPuzzleInfo(puzzleInfo);

            return puzzleInfo;
        }
Exemplo n.º 4
0
 public CurrentPuzzleInfoViewModel(CurrentPuzzleInfo puzzleInfo)
 {
     _puzzleInfo = puzzleInfo;
     _isBrowsing = puzzleInfo == null;
 }