Example #1
0
        public override ICommandResponse Handle(AssignRoleToNodeCommand command)
        {
            var response = new CommandResponse();

            //If in role, no nothing.
            if (!_nodeService.IsInRole(command.NodeId, command.Role))
            {
                _nodeService.AddRole(command.NodeId, command.Role);
                response.AddEvent(new NodeRolesUpdatedEvent(command.NodeId, _nodeService.GetRoles(command.NodeId)), _nodeService.GetConnection(command.NodeId));
                response.AddEvent(new NodeRolesUpdatedEvent(command.NodeId, _nodeService.GetRoles(command.NodeId)), command.Originator);
            }
            return(response);
        }
        public override ICommandResponse Handle(RemoveRoleFromNodeCommand command)
        {
            var response = new CommandResponse();

            if (!_nodeService.IsInRole(command.NodeId, command.Role))
            {
                return(response);
            }
            _nodeService.RemoveRole(command.NodeId, command.Role);
            response.AddEvent(new NodeRolesUpdatedEvent(command.NodeId, _nodeService.GetRoles(command.NodeId)), _nodeService.GetConnection(command.NodeId));
            response.AddEvent(new NodeRolesUpdatedEvent(command.NodeId, _nodeService.GetRoles(command.NodeId)), command.Originator);
            return(response);
        }
        public override ICommandResponse Handle(ButtHitSeatCommand command)
        {
            var response = new CommandResponse();
            var state    = _boutRunner.GetBoutState(command.BoutId);

            if (state.PenaltyBox.Any(x => x.Id == command.Chair.Id))
            {
                throw new ButtAlreadyInChairException(command.Chair.Id);
            }

            if (state.Phase == Entities.BoutPhase.Jam)
            {
                command.Chair.StopWatch.Start();
            }

            if (command.Chair.IsJammer)
            {
                var jammer = state.GetCurrentJammer(command.Chair.Team);
                if (jammer.HasValue)
                {
                    command.Chair.Number = jammer.Value;

                    command.Chair.SecondsOwed = state.Penalties.Any(x => x.Number == command.Chair.Number)
                        ? state.Penalties.Where(x => x.Number == command.Chair.Number).Sum(x => x.SecondsOwed)
                        : command.Chair.SecondsOwed;
                }

                var otherTeam        = command.Chair.Team == "left" ? "right" : "left";
                var otherJammerInBox = state.PenaltyBox.SingleOrDefault(x => x.Team == otherTeam && x.IsJammer);
                if (otherJammerInBox != null)
                {
                    var timeRemaining = Math.Max(otherJammerInBox.SecondsOwed - (int)Math.Round(otherJammerInBox.StopWatch.Elapsed.TotalSeconds), 0);
                    if (timeRemaining < command.Chair.SecondsOwed)
                    {
                        otherJammerInBox.SecondsOwed -= timeRemaining;
                        command.Chair.SecondsOwed    -= timeRemaining;
                    }
                    else
                    {
                        otherJammerInBox.SecondsOwed -= command.Chair.SecondsOwed;
                        command.Chair.SecondsOwed     = 0;
                    }
                    response.AddEvent(new ChairUpdatedEvent(command.BoutId, otherJammerInBox), Audiences.Bout(command.BoutId));
                }
            }

            state.PenaltyBox.Add(command.Chair);
            response.AddEvent(new ChairUpdatedEvent(command.BoutId, command.Chair), Audiences.Bout(command.BoutId));
            return(response);
        }
        public override ICommandResponse Handle(ConnectNodeCommand command)
        {
            var response   = new CommandResponse();
            var connection = _nodeService.ConnectNode(command.Originator, command.ConnectionId);

            response.AddEvent(new NodeConnectedEvent(connection), Audiences.All);
            if (connection.BoutId != Guid.Empty)
            {
                var bout      = _boutData.Load(connection.BoutId);
                var boutState = _boutRunnerService.GetBoutState(connection.BoutId);
                response.AddEvent(new InitializeBoutEvent(bout, boutState), command.ConnectionId);
            }
            return(response);
        }
        public override ICommandResponse Handle(AddNodeToBoutCommand command)
        {
            var response = new CommandResponse();

            if (!_boutRunnerService.IsRunning(command.BoutId))
            {
                throw new BoutNotFoundException(command.BoutId);
            }

            var state = _boutRunnerService.GetBoutState(command.BoutId);
            var bout  = _boutData.Load(command.BoutId);


            _nodeService.AddToBout(command.NodeId, command.BoutId);
            response.AddEvent(new InitializeBoutEvent(bout, state), _nodeService.GetConnection(command.NodeId));
            response.AddEvent(new NodeJoinedBoutEvent(command.NodeId, command.BoutId), command.Originator);
            return(response);
        }
        public override ICommandResponse Handle(CancelSitCommand command)
        {
            var state    = _boutRunner.GetBoutState(command.BoutId);
            var sit      = state.PenaltyBox.SingleOrDefault(x => x.Id == command.ChairId);
            var response = new CommandResponse();

            if (sit != null)
            {
                state.PenaltyBox.Remove(sit);
                response.AddEvent(new ChairRemovedEvent(command.BoutId, command.ChairId), Audiences.Bout(command.BoutId));
            }
            return(response);
        }
        public override ICommandResponse Handle(CreatePenaltyCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);
            var data  = _boutData.Load(command.BoutId);

            var penalty = new Penalty(command.Team, command.Period, command.Jam, state.GameClock.Elapsed, data.RuleSet.PenaltyDurationSeconds);

            state.Penalties.Add(penalty);

            var response = new CommandResponse();

            response.AddEvent(new PenaltyUpdatedEvent(command.BoutId, penalty), Audiences.Bout(command.BoutId));
            return(response);
        }
        public override ICommandResponse Handle(ReleaseSkaterCommand command)
        {
            var response = new CommandResponse();
            var state    = _boutRunner.GetBoutState(command.BoutId);
            var sit      = state.PenaltyBox.SingleOrDefault(x => x.Id == command.ChairId);

            if (sit == null)
            {
                return(response);
            }

            var timeServed = sit.SecondsOwed;
            var penalties  = state.Penalties.Where(x => x.Team == sit.Team && x.Number == sit.Number).ToList();

            while (timeServed > 0 && penalties.Any())
            {
                var penalty = penalties.First();
                if (timeServed > penalty.SecondsOwed)
                {
                    timeServed         -= penalty.SecondsOwed;
                    penalty.SecondsOwed = 0;
                    penalties.Remove(penalty);
                }
                else
                {
                    penalty.SecondsOwed -= timeServed;
                    timeServed           = 0;
                }

                response.AddEvent(new PenaltyUpdatedEvent(command.BoutId, penalty), Audiences.Bout(command.BoutId));
            }

            state.PenaltyBox.Remove(sit);
            response.AddEvent(new ChairRemovedEvent(command.BoutId, command.ChairId),
                              Audiences.Bout(command.BoutId));
            return(response);
        }
Example #9
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);
        }
        public override ICommandResponse Handle(RunBoutCommand command)
        {
            var response = new CommandResponse();
            var bout     = _boutDataService.Load(command.BoutId);

            if (_boutRunnerService.IsRunning(bout.BoutId))
            {
                throw new BoutAlreadyRunningException(bout.BoutId);
            }

            _boutRunnerService.StartBout(bout);
            response.AddEvent(new BoutRunningEvent(bout.BoutId, bout.Name), Audiences.All);

            return(response);
        }
        public override ICommandResponse Handle(UpdatePenaltyCommand command)
        {
            var state  = _boutRunner.GetBoutState(command.BoutId);
            var target = state.Penalties.SingleOrDefault(x => x.Id == command.Penalty.Id);

            if (target == null)
            {
                throw new NoSuchPenaltyException(command.Penalty.Id);
            }
            var source = command.Penalty;

            target.Team = source.Team;
            if (target.Number != source.Number)
            {
                // If the penalty is moving off of a skater in the box, remove time
                // If it's moving to a skater in the box, add time
                var skaterInBox = state.PenaltyBox.SingleOrDefault(x => x.Number == target.Number);
                if (skaterInBox != null)
                {
                    skaterInBox.SecondsOwed -= 30;
                }

                target.Number = source.Number;

                skaterInBox = state.PenaltyBox.SingleOrDefault(x => x.Number == target.Number);
                if (skaterInBox != null)
                {
                    skaterInBox.SecondsOwed += 30;
                }
            }

            target.SecondsOwed = source.SecondsOwed;
            target.PenaltyCode = source.PenaltyCode;
            target.Period      = source.Period;
            target.JamNumber   = source.JamNumber;
            target.GameClock   = source.GameClock;

            var response = new CommandResponse();

            response.AddEvent(new PenaltyUpdatedEvent(command.BoutId, target), Audiences.Bout(command.BoutId));
            return(response);
        }
        public override ICommandResponse Handle(SetTimeoutTypeCommand command)
        {
            var response = new CommandResponse();
            var state    = _boutRunner.GetBoutState(command.BoutId);

            if (state.TimeoutType == command.TimeoutType)
            {
                return(response);
            }

            if ((command.TimeoutType == TimeoutType.LeftTeam && state.LeftTeamState.TimeOutsRemaining == 0) ||
                (command.TimeoutType == TimeoutType.RightTeam && state.RightTeamState.TimeOutsRemaining == 0))
            {
                throw new NoTimeoutsRemainingException();
            }

            state.TimeoutType = command.TimeoutType;

            response.AddEvent(new BoutStateUpdatedEvent(state), Audiences.All);
            return(response);
        }
Example #13
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);
            }
        }