Beispiel #1
0
#pragma warning disable 1998
        public async Task Dispatch(ICommand command)
#pragma warning restore 1998
        {
            var callback = _callbackFactory.Get();
            var key      = command.GetType().Name;

            if (!Handlers.ContainsKey(key))
            {
                throw new UnknownCommandTypeException(command);
            }

            try
            {
                var response = Handlers[key].Handle(command);
                callback.Callback(response);
            }
            catch (Exception ex)
            {
                var response = new CommandResponse();
                response.AddEvent(MessageBaseEvent.Error(ex.Message), command.Originator);
                //TODO: Better error handling
                callback.Callback(response);
            }
        }
Beispiel #2
0
        public override ICommandResponse Handle(StartJamCommand command)
        {
            var response = new CommandResponse();
            var bout     = _boutDataService.Load(command.BoutId);
            var state    = _boutRunnerService.GetBoutState(command.BoutId);

            //State must be in lineup
            if (state.Phase != BoutPhase.Lineup)
            {
                throw new InvalidBoutPhaseException(state.Phase);
            }

            //There must be time left on the game clock
            var elapsed = state.GameClock.Elapsed.TotalSeconds;

            if (elapsed > bout.RuleSet.PeriodDurationSeconds)
            {
                response.AddEvent(
                    MessageBaseEvent.Error($"Call to start jam came too late.  Elapsed: {state.GameClock.Elapsed.TotalSeconds}"),
                    command.Originator);
                return(response);
            }

            state.Phase    = BoutPhase.Jam;
            state.JamStart = DateTime.Now;
            state.GameClock.Start();

            state.PenaltyBox.ForEach(x =>
            {
                x.StopWatch.Start();
                response.AddEvent(new ChairUpdatedEvent(state.BoutId, x), Audiences.Bout(state.BoutId));
            });

            response.AddEvent(new BoutStateUpdatedEvent(state), Audiences.Bout(state.BoutId));
            return(response);
        }