public static async Task SetStarsAccordingToTries( MissionRequest request, IMissionRequestRepository missionRequestRepository) { Expression <Func <MissionRequest, bool> > filter = a => a.MissionId == request.MissionId && a.UserId == request.UserId; var previousRequiests = await missionRequestRepository.GetMissionRequests(new QueryOptions <MissionRequest> { Filter = filter }); var tries = previousRequiests.Count + 1; if (tries <= request.Mission.TriesFor3Stars) { request.StarsCount = 3; } else if (tries <= request.Mission.TriesFor2Stars) { request.StarsCount = 2; } else if (tries <= request.Mission.TriesFor1Star) { request.StarsCount = 1; } else { throw new Exception("Can't pass mission with more tries than allowed"); } }
/// <summary> /// Gets the attached mission sets. /// </summary> /// <param name="userId">The user id.</param> /// <returns>Task{IEnumerable{UserMissionSet}}.</returns> public async Task <IEnumerable <UserMissionSet> > GetActiveMissionSets(string userId) { var user = await _userRepository.GetUser(userId); if (!user.ActiveMissionSetIds.AnyValues()) { return(null); } var activeSets = (await _missionSetRepository.GetMissionSets( new QueryOptions <MissionSet> { Filter = ExpressionHelper.Expr((MissionSet x) => x.Id) .In(user.ActiveMissionSetIds), Expand = new List <string> { "Missions" } })); var setsMissions = activeSets.SelectMany(attachedSet => attachedSet.Missions).Select(m => m.Mission).ToIList(); var missionRequests = (await _missionRequestRepository.GetMissionRequests( new QueryOptions <MissionRequest> { Filter = mr => mr.UserId == userId })).Where( missionRequest => setsMissions.Select(m => m.Id).Contains(missionRequest.MissionId)).ToIList(); var activeUserSets = new List <UserMissionSet>(); foreach (var missionSet in activeSets) { var userSet = new UserMissionSet { Id = missionSet.Id, Name = missionSet.Name }; var missions = new List <UserMission>(); foreach (var mission in missionSet.Missions.OrderBy(m => m.Order).Select(m => m.Mission)) { var convertedMission = ConvertToUserMission(mission, user); missions.Add(convertedMission); } userSet.Missions = missions; activeUserSets.Add(userSet); } SetDisplayStatuses(activeUserSets, user, missionRequests, setsMissions); return(activeUserSets.OrderBy(set => user.MissionSetIds.First(setId => setId.MissionSetId == set.Id).Order)); }
public static async Task <MissionCompletionResult> ProcessIncorrectTry( MissionRequest request, IMissionRepository missionRepository, IMissionRequestRepository missionRequestRepository) { Expression <Func <MissionRequest, bool> > filter = a => a.MissionId == request.MissionId && a.UserId == request.UserId; var previousRequiests = await missionRequestRepository.GetMissionRequests(new QueryOptions <MissionRequest> { Filter = filter }); var tryCount = previousRequiests.Count + 1; if (tryCount >= request.Mission.TriesFor1Star) { request.DeclineReason = "Увы, достигнуто максимальное количество попыток."; await RewardsCalculator.UpdateUserAfterMissionDecline(request, missionRepository); await missionRequestRepository.AddMissionRequest(request); return(new MissionCompletionResult { MissionCompletionStatus = MissionCompletionStatus.Fail, Description = request.DeclineReason }); } var rejectResult = new MissionCompletionResult { MissionCompletionStatus = MissionCompletionStatus.IntermediateFail, TryCount = tryCount }; var failedRequesResult = await missionRequestRepository.AddMissionRequest(request); return(failedRequesResult.Status != OperationResultStatus.Success ? MissionCompletionResult.FromOperationResult(failedRequesResult) : rejectResult); }
/// <summary> /// The get requests. /// </summary> /// <param name="options">The options.</param> /// <returns>The <see cref="Task" />.</returns> public Task <List <MissionRequest> > GetRequests(QueryOptions <MissionRequest> options = null) { return(_missionRequestRepository.GetMissionRequests(options)); }