Esempio n. 1
0
        public async Task <ICommandResult> End()
        {
            var context = await GetContextAsync().ConfigureAwait(false);

            var run = await _mediator.Send(new GetCurrentRunQuery(context.Room.Id, context.User.Id)).ConfigureAwait(false);

            if (run == null)
            {
                return(Response(ErrorStrings.EndRun_RunNotStarted(), ResponseType.User));
            }

            var orders = await _mediator.Send(new GetRunOrdersQuery(run.Id, context.User.Id)).ConfigureAwait(false);

            var command = new EndRunCommand(
                runId: run.Id,
                roomId: context.Room.Id,
                userId: context.User.Id,
                orders: orders
                );

            command.AddCallbackState(context.Command.ToCallbackData());

            await _mediator.Send(command).ConfigureAwait(false);

            return(Response(null, ResponseType.Channel));
        }
Esempio n. 2
0
        //End run
        public async Task <Unit> Handle(EndRunCommand request, CancellationToken cancellationToken)
        {
            var run = await _runRepository.GetAsync(request.RunId).ConfigureAwait(false);

            var runnerUserId = await GetRunner(request).ConfigureAwait(false);

            //update run
            run.Ended = true;
            await _runRepository.UpdateAsync(run).ConfigureAwait(false);

            //store result
            var runResult = new RunResult
            {
                RunId        = request.RunId,
                RunnerUserId = runnerUserId,
                EndedTime    = _clock.UtcNow()
            };

            await _runRepository.CreateResultAsync(runResult).ConfigureAwait(false);

            //publish event
            var evt = new RunEndedEvent
            {
                Orders       = request.Orders,
                RoomId       = request.RoomId,
                RunnerUserId = runResult.RunnerUserId,
                RunId        = runResult.RunId,
                EndedTime    = runResult.EndedTime,
                State        = request.State
            };

            await _eventPublisher.Publish(evt).ConfigureAwait(false);

            return(Unit.Value);
        }
Esempio n. 3
0
        private async Task ProcessAsync(EndRunCommand request, CancellationToken cancellationToken)
        {
            //delete lock
            var deleted = await _lockService.DeleteLockAsync(request.RoomId).ConfigureAwait(false);

            if (!deleted)
            {
                throw new RunEndException("There is no active run in this room", RunEndException.RunEndExceptionReason.NoActiveRun);
            }
        }
Esempio n. 4
0
        private async Task <long> GetRunner(EndRunCommand command)
        {
            //todo: dont tie illmake in with this handler directly...kinda gross
            var illMakeResults = await _illMakeRepository.GetAllByRunAsync(command.RunId).ConfigureAwait(false);

            if (illMakeResults.Any())
            {
                return(illMakeResults.OrderByDescending(o => o.CreatedDate).First().UserId);
            }

            //random runner
            return(await _randomizer.GetRunnerUserId(command.Orders).ConfigureAwait(false));
        }