Esempio n. 1
0
        public async Task <bool> StartMockTraining(int trainingId)
        {
            // count total goals to achieve
            var totalGoals = await _goalsService.GetTotalGoalsCount(trainingId);

            // verify that there is no need to perform cleanup, else, unachieve all
            var current = await _goalsService.GetCurrentGoalByTrainingId(trainingId);

            if (current == null || current.Goal.Order > 1)
            {
                await _goalsService.UnAchieveAllTrainingGoals(trainingId);
            }
            // create the task
            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;
            var task        = new Task(() => MockTraining(trainingId, tokenSource), token, TaskCreationOptions.LongRunning);

            var mock = new TrainingMock()
            {
                EndTime = DateTime.Now + _config.Duration, Progress = 0, TotalGoals = totalGoals, AchievedGoals = 0
            };
            var didSucceeded = _mockedTrainings.TryAdd(trainingId, mock);

            // check if the the mock isn't already running
            if (didSucceeded)
            {
                task.Start();
            }
            else if (task != null)
            {
                task.Dispose();
            }

            return(didSucceeded);
        }
Esempio n. 2
0
        private bool ShouldAchiveNext(TrainingMock trainingMock)
        {
            // calculate the goal that should be marked as achieved now and return true/false
            var expected = ((float)trainingMock.Progress / 100) * trainingMock.TotalGoals;

            _logger.LogInformation($"progress: {trainingMock.Progress}; total goals: {trainingMock.TotalGoals} expected: {expected}");
            var result = expected >= trainingMock.AchievedGoals;

            _logger.LogInformation($"should achieve next: {result}");
            return(result);
        }