示例#1
0
 public static Props Props(
     IActorRef competitorsSource
     , ICompetitionActorPropsFactory competitorsPropsFactory)
 {
     return(Akka.Actor.Props.Create(() => new CompetitionSlot(competitorsSource, competitorsPropsFactory)));
 }
示例#2
0
        // ReSharper disable once MemberCanBePrivate.Global
        // public constructor required for akka
        public CompetitionSlot(
            IActorRef competitorSource
            , ICompetitionActorPropsFactory competitionActorPropsFactory)
        {
            StartWith(CompetitionSlotState.Idle, IdleData.FromReason(IdleReason.JustStarted));

            When(CompetitionSlotState.Idle, state =>
            {
                if (state.FsmEvent is RunCompetition runCompetition)
                {
                    return(GoToLobby(competitorSource));
                }

                return(null);
            });

            When(CompetitionSlotState.Lobby, state =>
            {
                if (state.FsmEvent is TypedAskResult <RoomCreated> agentsOrderCompleted)
                {
                    var competitorActorProps = competitionActorPropsFactory.Build(agentsOrderCompleted.Answer.ActorPlayers);
                    var competitorActor      = Context.ActorOf(competitorActorProps);

                    return(GoTo(CompetitionSlotState.Game)
                           .Using(
                               new ActiveGameData(
                                   competitorActor
                                   , agentsOrderCompleted.Answer.ActorPlayers)));
                }

                if (state.FsmEvent is NotEnoughPlayers)
                {
                    return(GoTo(CompetitionSlotState.Idle)
                           .Using(IdleData.FromReason(IdleReason.NotEnoughPlayers)));
                }

                return(null);
            });

            When(CompetitionSlotState.Game, state =>
            {
                if (state.FsmEvent is LobbyGameTerminated competitionResult)
                {
                    return(GoToLobby(competitorSource));
                }

                return(null);
            });


            OnTransition((initialState, nextState) =>
            {
                _logger.Info("Changed state from {0} to {1}", initialState, nextState);
                switch (initialState)
                {
                case CompetitionSlotState.Lobby when StateData is QueryData queryStateData:
                    Context.Stop(queryStateData.Query);
                    break;

                case CompetitionSlotState.Game when StateData is ActiveGameData activeGameData:
                    Context.Stop(activeGameData.Game);
                    break;

                case CompetitionSlotState.Idle:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(initialState), initialState, null);
                }

                switch (nextState)
                {
                case CompetitionSlotState.Game when NextStateData is ActiveGameData nextStateGameData:
                    Context.WatchWith(nextStateGameData.Game, new LobbyGameTerminated(nextStateGameData.Game));
                    nextStateGameData.Game.Tell(new Competition.RunCompetitionMessage());
                    break;

                case CompetitionSlotState.Idle when NextStateData is IdleData idleData:
                    Context.System.Scheduler.ScheduleTellOnce(
                        idleData.RestartAfter,
                        Self,
                        RunCompetition.Instance,
                        Self);
                    break;

                case CompetitionSlotState.Lobby:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(nextState), nextState, null);
                }
            });


            Initialize();
        }
 public CompetitionRegistration(ICompetitionInfo configuration, ICompetitionActorPropsFactory factory)
 {
     Info    = configuration;
     Factory = factory;
 }