Esempio n. 1
0
        public ChangeCaptainOutput ChangeCaptain(ChangeCaptainInput input)
        {
            TeamRepository.Includes.Add(r => r.PlayerCareers);

            Team teamEntity = TeamRepository.Get(input.TeamId);

            if (teamEntity == null)
            {
                throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Team\"");
            }

            if (!TeamPolicy.CanChangeCaptainForEntity(teamEntity))
            {
                throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionUpdateDenied, "\"Team\"");
            }

            PlayerCareer newCap = teamEntity.PlayerCareers.SingleOrDefault(r => r.Id == input.NewCaptainCareerId);

            if (newCap != null)
            {
                var oldCap = teamEntity.Captain;

                if (oldCap.Id != newCap.Id)
                {
                    newCap.IsCaptain = true;
                    PlayerCareerRepository.Update(newCap);

                    oldCap.IsCaptain = false;
                    PlayerCareerRepository.Update(oldCap);
                }
            }

            return(new ChangeCaptainOutput());
        }
Esempio n. 2
0
        public TryToPassConditionOutput TryToPassCondition(TryToPassConditionInput input)
        {
            bool result = false;

            #region Retrieving current player career

            UserRepository.Includes.Add(r => r.PlayerCareers);
            UserRepository.Includes.Add(r => r.PlayerCareers.Select(e => e.Team.PlayerCareers));

            PlayerCareer currCareer = UserRepository.Get(Session.UserId ?? 0)
                                      .PlayerCareers.Where(r => r.CareerDateEnd == null).SingleOrDefault();

            UserRepository.Includes.Clear();

            #endregion

            if (currCareer != null)
            {
                #region Retrieving current condition

                ConditionRepository.Includes.Add(r => r.GameTask.Game);
                ConditionRepository.Includes.Add(r => r.ConditionType);

                Condition currentCondition = ConditionRepository.Get(input.ConditionId);
                if (currentCondition == null || currentCondition.GameTask == null || currentCondition.GameTask.Game == null ||
                    !GamePolicy.CanRetrieveEntityLight(currentCondition.GameTask.Game))
                {
                    //throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionRetrieveDenied, "\"Game\"");
                    return(new TryToPassConditionOutput()
                    {
                        Result = result
                    });
                }

                ConditionRepository.Includes.Clear();

                #endregion

                IList <long> teamPlayerCareerIds        = currCareer.Team.PlayerCareers.Select(e => e.Id).ToList();
                bool         alreadyPassedThisCondition = TeamGameTaskStatisticRepository.GetAll()
                                                          .Where(r => r.TeamId == currCareer.TeamId && r.GameTaskId == currentCondition.GameTaskId).Count() != 0 ||
                                                          PlayerGameTaskStatisticRepository.GetAll()
                                                          .Where(r => teamPlayerCareerIds.Contains(r.PlayerCareerId) && r.GameTaskId == currentCondition.GameTaskId).Count() != 0;
                if (!alreadyPassedThisCondition)
                {
                    result = HandleAttempt(currentCondition, currCareer, input.Value);
                }
            }

            return(new TryToPassConditionOutput()
            {
                Result = result
            });
        }
Esempio n. 3
0
        private bool HandleAttemptForTimeCondition(Condition condition, PlayerCareer playerCareer)
        {
            bool     attemptResult   = false;
            DateTime attemptDateTime = (DateTime.Now).RoundDateTime();

#warning TODO: handle Condition_Time
            if (true)
            {
                attemptResult = true;

                SuccessfulPlayerAttempt newSuccessfullAttempt = new SuccessfulPlayerAttempt()
                {
                    AttemptDateTime = attemptDateTime,
                    ConditionId     = condition.Id,
                    InputedValue    = String.Empty,
                    PlayerCareerId  = playerCareer.Id
                };
                SuccessfullPlayerAttemptRepository.Insert(newSuccessfullAttempt);

                IList <long> userCompleterIds = new List <long>();

                #region Creating Statistic entities

                List <DateTime> previousTaskEndDTs = TeamGameTaskStatisticRepository.GetAll()
                                                     .Where(r => r.TeamId == playerCareer.TeamId)
                                                     .Select(r => r.GameTaskEndDateTime)
                                                     .Distinct().ToList();
                DateTime gameTaskStartTime = previousTaskEndDTs.Count > 0 ? previousTaskEndDTs.Max() : condition.GameTask.Game.StartDate;

                IList <PlayerGameTaskStatistic> newPlayerGameTaskStatistics = new List <PlayerGameTaskStatistic>();
                foreach (var item in playerCareer.Team.PlayerCareers)
                {
                    newPlayerGameTaskStatistics.Add(new PlayerGameTaskStatistic(condition.GameTaskId,
                                                                                item.Id, gameTaskStartTime, attemptDateTime, (condition.Points ?? 0)));
                    userCompleterIds.Add(item.UserId);
                }
                PlayerGameTaskStatisticRepository.AddRange(newPlayerGameTaskStatistics);

                TeamGameTaskStatistic newTeamGameTaskStatistic = new TeamGameTaskStatistic(condition.GameTaskId,
                                                                                           playerCareer.TeamId, gameTaskStartTime, attemptDateTime, (condition.Points ?? 0));
                TeamGameTaskStatisticRepository.Insert(newTeamGameTaskStatistic);

                #endregion

                UowManager.Current.Completed += (sender, e) =>
                {
                    StatisticsChangesNotifier.RaiseOnGameTaskCompleted(new GameTaskCompletedMessage(condition.GameTaskId, userCompleterIds));
                };
            }
            return(attemptResult);
        }
Esempio n. 4
0
 private bool HandleAttempt(Condition condition, PlayerCareer playerCareer, string inputedValue)
 {
     if (condition.ConditionType.Name == CityQuestConsts.ConditionJustInputCode)
     {
         return(HandleAttemptForInputCodeCondition(condition, playerCareer, inputedValue));
     }
     else if (condition.ConditionType.Name == CityQuestConsts.ConditionTime)
     {
         return(HandleAttemptForTimeCondition(condition, playerCareer));
     }
     else
     {
         return(false);
     }
 }
Esempio n. 5
0
        private void CreateNewPlayerCareer(long userId, long newTeamId, DateTime currentDT)
        {
            PlayerCareerRepository.Includes.Add(r => r.Team.PlayerCareers);

            var carrersForUpdating = PlayerCareerRepository.GetAll()
                                     .Where(r => r.UserId == userId && r.CareerDateEnd == null).ToList();

            foreach (var item in carrersForUpdating)
            {
                if (item.IsCaptain == true)
                {
#warning Using to prevent deactivating default team
                    if (item.Team.IsDefault == true)
                    {
                        throw new Abp.UI.UserFriendlyException("Unallowed action!",
                                                               "Captain of default team can not leave it. This action will be available after transfering captaincy to another player.");
                    }

                    var newCaptain = item.Team.CurrentPlayers.FirstOrDefault(r => !r.IsCaptain);
                    if (newCaptain == null)
                    {
                        //deactivate team
                        item.Team.IsActive = false;
                        TeamRepository.Update(item.Team);
                    }
                    else
                    {
                        newCaptain.IsCaptain = true;
                    }
                }
                item.CareerDateEnd = currentDT;
                item.IsCaptain     = false;
                PlayerCareerRepository.Update(item);
            }

            PlayerCareer newPlayerCareer = new PlayerCareer()
            {
                CareerDateEnd   = null,
                CareerDateStart = currentDT,
                IsCaptain       = false,
                TeamId          = newTeamId,
                UserId          = userId,
                IsActive        = true,
            };
            PlayerCareerRepository.Insert(newPlayerCareer);

            PlayerCareerRepository.Includes.Clear();
        }
Esempio n. 6
0
        public long?GetPlayerTeamId(DateTime dateTime)
        {
            PlayerCareer playerCareer = GetPlayerCareer(dateTime);

            return(playerCareer != null ? (long?)playerCareer.TeamId : null);
        }
Esempio n. 7
0
        public CreateOutput <UserDto, long> Create(CreateInput <UserDto, long> input)
        {
            User existingUser = UserRepository.FirstOrDefault(r => r.UserName == input.Entity.UserName);

            if (existingUser != null)
            {
                throw new UserFriendlyException("Wrong username!", String.Format(
                                                    "There is already a user with username: \"{0}\"! Please, select another user name.", input.Entity.UserName));
            }
            User newUserEntity = new User()
            {
                EmailAddress     = input.Entity.EmailAddress,
                IsEmailConfirmed = true,
                Name             = input.Entity.Name,
                Password         = input.Entity.Password,
                PhoneNumber      = input.Entity.PhoneNumber,
                Roles            = new List <UserRole>(),
                Surname          = input.Entity.Surname,
                UserName         = input.Entity.UserName
            };

            newUserEntity.Password = new PasswordHasher().HashPassword(newUserEntity.Password);

            #region Creating UserRole

            Role defaultRole = RoleRepository.FirstOrDefault(r => r.IsDefault);
            if (defaultRole == null)
            {
                throw new UserFriendlyException("Error during the action!",
                                                "Can not get default role! Please, contact your system administrator or system support.");
            }

            newUserEntity.Roles.Clear();
            newUserEntity.Roles.Add(new UserRole()
            {
                Role   = defaultRole,
                RoleId = defaultRole.Id,
                User   = newUserEntity
            });

            #endregion

            #region Creating PlayerCareer

            try
            {
                long defaultTeamId = TeamRepository.Single(r => r.IsDefault == true).Id;

                PlayerCareer newPlayerCareer = new PlayerCareer()
                {
                    CareerDateStart = DateTime.Now,
                    IsActive        = true,
                    IsCaptain       = false,
                    TeamId          = defaultTeamId,
                    User            = newUserEntity,
                };

                newUserEntity.PlayerCareers.Add(newPlayerCareer);
            }
            catch
            {
                throw new UserFriendlyException("User creating fault!", "User creating fault! Please contact system administrator or support.");
            }

            #endregion

            UserDto newUserDto = (UserRepository.Insert(newUserEntity)).MapTo <UserDto>();

            return(new CreateOutput <UserDto, long>()
            {
                CreatedEntity = newUserDto
            });
        }