コード例 #1
0
        public void MustBeInLineup()
        {
            var state = _boutRunner.GetBoutState(Guid.Empty);

            state.Phase = Entities.BoutPhase.Jam;
            Assert.Throws <InvalidBoutPhaseException>(() => { _handler.Handle(_command); });
        }
コード例 #2
0
        public AddSkaterToJamCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            var state = _boutRunner.GetBoutState(Guid.Empty);

            state.Phase = BoutPhase.Lineup;

            _handler = new AddSkaterToJamCommandHandler(_boutData, _boutRunner);
        }
コード例 #3
0
        public SetTimeoutTypeCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            var state = _boutRunner.GetBoutState(Guid.Empty);

            state.Phase             = BoutPhase.Timeout;
            state.GameClock.Running = false;
            _handler = new SetTimeoutTypeCommandHandler(_boutRunner);
        }
コード例 #4
0
        public RemoveSkaterFromJamCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            var state = _boutRunner.GetBoutState(Guid.Empty);

            state.Phase = BoutPhase.Lineup;
            var team = state.Jams[0].Team("left");

            team.Roster.Add(new JamParticipant {
                Number = 8, Position = Position.Blocker
            });
            _handler = new RemoveSkaterFromJamCommandHandler(_boutRunner);
        }
コード例 #5
0
        public override ICommandResponse Handle(CreatePassCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);

            var jam = state.Jams.SingleOrDefault(x => x.JamNumber == command.Jam && x.Period == command.Period);

            if (jam == null)
            {
                throw new JamNotFoundException(command.Period, command.Jam);
            }

            var passes = jam.Team(command.Team).Passes;

            //If command.Number is default, we're creating the next pass.
            //If not, we are creating a certain pass, probably zero for "Eat the Baby"
            var passNumber = command.Number == -1
                ? (passes.Any()
                    ? passes.Max(x => x.Number) + 1
                    : 1)
                : command.Number;

            passes.Add(new Pass {
                Number = passNumber
            });

            return(new UpdateJamResponse(command.BoutId, jam));
        }
コード例 #6
0
        public override ICommandResponse Handle(AddSkaterToJamCommand command)
        {
            var bout  = _boutData.Load(command.BoutId);
            var state = _boutRunner.GetBoutState(command.BoutId);
            var jam   = state.Jams.SingleOrDefault(x => x.JamNumber == command.Jam && x.Period == command.Period);

            if (jam == null)
            {
                throw new JamNotFoundException(command.Period, command.Jam);
            }

            var team   = jam.Team(command.Team);
            var roster = command.Team == "left" ? bout.Left.Roster : bout.Right.Roster;
            var lineup = team.Roster;

            if (roster.All(x => x.Number != command.Number))
            {
                throw new InvalidSkaterNumberException(command.Team, command.Number);
            }

            if (lineup.Any(x => x.Number == command.Number || lineup.Count == 6))
            {
                return(new CommandResponse());
            }

            lineup.Add(new JamParticipant {
                Number = command.Number, Position = Position.Blocker
            });

            var response = new UpdateBoutStateResponse(state);

            return(response);
        }
コード例 #7
0
        public override ICommandResponse Handle(EndPeriodCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);

            if (state.Phase != Entities.BoutPhase.Lineup)
            {
                throw new InvalidBoutPhaseException(state.Phase);
            }
            var bout = _boutData.Load(command.BoutId);

            if (state.GameClock.Elapsed.TotalSeconds < bout.RuleSet.PeriodDurationSeconds)
            {
                return(new CommandResponse());
            }

            if (state.Period < bout.RuleSet.NumberOfPeriods)
            {
                state.Phase = BoutPhase.Halftime;
                state.Period++;
                state.JamNumber = 1;
                state.Jams.Add(new Jam(state.Period, state.JamNumber));
            }
            else
            {
                state.Phase = Entities.BoutPhase.UnofficialFinal;
            }

            var response = new UpdateBoutStateResponse(state);

            return(response);
        }
コード例 #8
0
        public override ICommandResponse Handle(StartPeriodCommand command)
        {
            //Bout must be running
            var bout = _boutDataService.Load(command.BoutId);

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

            //Game Must Be in pregame or halftime
            var state = _boutRunnerService.GetBoutState(command.BoutId);

            if (state.Phase != BoutPhase.Pregame && state.Phase != BoutPhase.Halftime)
            {
                return(new CommandResponse());
            }

            state.Phase = BoutPhase.Lineup;
            state.GameClock.Clear();

            var response = new UpdateBoutStateResponse(state);

            return(response);
        }
コード例 #9
0
 public void ExitPregameGoesToLineupPhase()
 {
     var command = new StartPeriodCommand(Guid.Empty, "connection");
     var handler = new StartPeriodCommandHandler(_boutRunner, _boutData);
     handler.Handle(command);
     var state = _boutRunner.GetBoutState(Guid.Empty);
     Assert.Equal(BoutPhase.Lineup, state.Phase);
 }
コード例 #10
0
        public StopJamCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state       = _boutRunner.GetBoutState(Guid.Empty);
            _state.Phase = BoutPhase.Jam;
            _handler     = new StopJamCommandHandler(_boutRunner, _boutData);
        }
コード例 #11
0
        public ButtHitSeatCommandHandlerTests()
        {
            _handler = new ButtHitSeatCommandHandler(_boutRunner);
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state   = _boutRunner.GetBoutState(Guid.Empty);
            _builder = new BoutStateBuilder(_state);
            _builder.SetPhase(BoutPhase.Jam);
        }
コード例 #12
0
        public UpdateJammerStatusCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state       = _boutRunner.GetBoutState(Guid.Empty);
            _state.Phase = BoutPhase.Jam;

            _handler = new UpdateJammerStatusCommandHandler(_boutRunner);
        }
コード例 #13
0
        public CreatePenaltyCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state       = _boutRunner.GetBoutState(Guid.Empty);
            _state.Phase = BoutPhase.Lineup;

            _handler = new CreatePenaltyCommandHandler(_boutRunner, _boutData);
        }
コード例 #14
0
        public CancelSitCommandHandlerTests()
        {
            _command = new CancelSitCommand(Guid.Empty, "o", _toUpdate.Id);
            _handler = new CancelSitCommandHandler(_boutRunner);
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state = _boutRunner.GetBoutState(Guid.Empty);
            _state.PenaltyBox.Add(_toUpdate);
            _state.Phase = BoutPhase.Jam;
        }
コード例 #15
0
        public ReleaseSkaterCommandHandlerTests()
        {
            _handler = new ReleaseSkaterCommandHandler(_boutRunner);
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state    = _boutRunner.GetBoutState(Guid.Empty);
            _builder  = new BoutStateBuilder(_state);
            _toUpdate = _builder.AddSkaterToBox(team: "left", number: 8);
            _command  = new ReleaseSkaterCommand(Guid.Empty, "o", _toUpdate.Id);
        }
コード例 #16
0
        public override ICommandResponse Handle(StopJamCommand command)
        {
            var bout  = _boutDataService.Load(command.BoutId);
            var state = _boutRunnerService.GetBoutState(command.BoutId);

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

            //Check to see if bout is over
            if (state.GameClock.Elapsed.Seconds < bout.RuleSet.PeriodDurationSeconds)
            {
                //Still good
                state.LineupStart = DateTime.Now;
                state.Phase       = BoutPhase.Lineup;
                state.CreateNextJam();

                //keep the jam clock going in case of undo
            }
            else
            {
                //Intermission
                if (state.Period < bout.RuleSet.NumberOfPeriods)
                {
                    state.JamNumber = 1;
                    state.Period++;
                    state.GameClock.Clear();
                    state.Phase = BoutPhase.Halftime;
                }
                else
                //Game Over
                {
                    state.Phase = BoutPhase.UnofficialFinal;
                }
            }


            state.JamStart = DateTime.Now;
            state.GameClock.Start();

            var response = new UpdateBoutStateResponse(state);

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

            return(response);
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        public UpdatePenaltyCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state    = _boutRunner.GetBoutState(Guid.Empty);
            _builder  = new BoutStateBuilder(_state);
            _penalty1 = _builder.AddPenalty();

            _handler         = new UpdatePenaltyCommandHandler(_boutRunner);
            _penalty2.Id     = _penalty1.Id;
            _penalty2.Number = 8;
            _command         = new UpdatePenaltyCommand(Guid.Empty, "originator", _penalty2);
        }
コード例 #19
0
        public UpdatePassCommandHandlerTests()
        {
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state       = _boutRunner.GetBoutState(Guid.Empty);
            _state.Phase = BoutPhase.Jam;
            var team = _state.Jams[0].Team("left");

            team.Passes.Add(new Pass {
                Number = 1
            });
            _handler = new UpdatePassCommandHandler(_boutRunner);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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);
        }
コード例 #23
0
        public UpdateChairCommandHandlerTests()
        {
            _handler = new UpdateChairCommandHandler(_boutRunner, _boutData);
            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            _state   = _boutRunner.GetBoutState(Guid.Empty);
            _builder = new BoutStateBuilder(_state);
            var inBox = _builder.AddSkaterToBox(number: -1);

            _command = new UpdateChairCommand(Guid.Empty, "originator",
                                              new Chair {
                Id = inBox.Id, Team = "left", IsJammer = false, Number = 8
            });
        }
コード例 #24
0
        public override ICommandResponse Handle(RemoveSkaterFromJamCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);
            var jam   = state.Jams.SingleOrDefault(x => x.JamNumber == command.Jam && x.Period == command.Period);

            if (jam == null)
            {
                throw new JamNotFoundException(command.Period, command.Jam);
            }

            var roster = jam.Team(command.Team).Roster;

            roster.RemoveAll(x => x.Number == command.Number);

            return(new UpdateBoutStateResponse(state));
        }
コード例 #25
0
        public override ICommandResponse Handle(StartTimeoutCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);

            if (state.Phase != BoutPhase.Lineup)
            {
                throw new InvalidBoutPhaseException(state.Phase);
            }
            state.TimeOutStart       = command.ServerTime;
            state.LoseOfficialReview = false;
            state.TimeoutType        = TimeoutType.Official;
            state.Phase = BoutPhase.Timeout;
            state.GameClock.Stop();
            var response = new UpdateBoutStateResponse(state);

            return(response);
        }
コード例 #26
0
        public StartTimeoutCommandHandlerTests()
        {
            _handler = new StartTimeoutCommandHandler(_boutRunner);

            var bout = _boutData.Load(Guid.Empty);

            _boutRunner.StartBout(bout);
            var state = _boutRunner.GetBoutState(Guid.Empty);

            state.Phase             = BoutPhase.Lineup;
            state.GameClock.Running = true;
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        public override ICommandResponse Handle(UpdateChairCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);
            var chair = state.PenaltyBox.Single(x => x.Id == command.Chair.Id);

            chair.SecondsOwed = command.Chair.SecondsOwed;
            if (chair.Number != command.Chair.Number)
            {
                chair.Number      = command.Chair.Number;
                chair.SecondsOwed = state.Penalties.Any(x => x.Number == chair.Number)
                    ? state.Penalties.Where(x => x.Number == chair.Number).Sum(x => x.SecondsOwed)
                    : chair.SecondsOwed;
            }
            chair.IsJammer    = command.Chair.IsJammer;
            chair.ChairNumber = command.Chair.ChairNumber;

            return(new UpdatePenaltySeatResponse(command.BoutId, chair));
        }
コード例 #29
0
        public override ICommandResponse Handle(StopTimeoutCommand command)
        {
            var state = _boutRunner.GetBoutState(command.BoutId);

            state.Phase       = BoutPhase.Lineup;
            state.LineupStart = DateTime.Now;

            switch (state.TimeoutType)
            {
            case TimeoutType.Official:
                break;

            case TimeoutType.LeftTeam:
                state.LeftTeamState.TimeOutsRemaining--;
                break;

            case TimeoutType.RightTeam:
                state.RightTeamState.TimeOutsRemaining--;
                break;

            case TimeoutType.LeftReview:
                if (state.LoseOfficialReview)
                {
                    state.LeftTeamState.OfficialReviews--;
                }
                break;

            case TimeoutType.RightReview:
                if (state.LoseOfficialReview)
                {
                    state.RightTeamState.OfficialReviews--;
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            state.TimeoutType = TimeoutType.Official;

            var response = new UpdateBoutStateResponse(state);

            return(response);
        }
コード例 #30
0
        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);
        }