// ReSharper disable once MemberCanBePrivate.Global // public constructor required for akka public Competitor( ICompetitorFactory playerAgentActorFactory , IActorRef playerLobby , IUser creator , IScript script , IActorRef resultRepository , CompetitorState initialState) { _playerAgentActorFactory = playerAgentActorFactory; _playerLobby = playerLobby; _creator = creator; _script = script; _resultRepository = resultRepository; _statusSubscriptions = new HashSet <IActorRef>(); _methodCallsFailureCounter = new Counter(MaxMethodCallsFailures); _state = initialState; if (_script.Exception != null) { _failureStateInfo = new AgentFailureState(new DeserializedException(_script.Exception)); Faulted(); } else { Active(); } }
private void Active() { ReactingToStatusMessages(); Receive <RequestCreateAgent>(msg => { _logger.Debug($"Spawning new agent"); var props = _playerAgentActorFactory.Build(_script); var agent = Context.ActorOf(props); var message = new GeneratedAgent(agent, _script.Id); Sender.Tell(message); }); Receive <AgentFailureState>(msg => { UpdateFailure(msg); DisconnectFromLobby(); UpdateState(CompetitorState.Faulted); Become(Faulted); throw new CompetitorFaultedException( Self , "agent failure" , _failureStateInfo.Exception , _script.Id); }); Receive <CompetitorScriptUpdated>(msg => { _script = msg.NewScript; _methodCallsFailureCounter.Reset(); UpdateFailure(AgentFailureState.Ok()); UpdateState(CompetitorState.Inconclusive); }); Receive <CompetitorResult>(msg => { UpdateState(CompetitorState.Active); var result = new Data.Entities.Messages.ScriptCompetitionResult(_script.Id, msg); _resultRepository.Tell(result); Sender.Tell(PoisonPill.Instance); }); ReceiveAny(msg => throw new InvalidOperationException($"Unknown message type received. Received object: {msg}")); }
private void Faulted() { ReactingToStatusMessages(); Receive <CompetitorScriptUpdated>(msg => { _script = msg.NewScript; _methodCallsFailureCounter.Reset(); UpdateFailure(AgentFailureState.Ok()); UpdateState(CompetitorState.Inconclusive); ConnectToLobby(); Become(Active); }); Receive <RequestCreateAgent>(msg => { var ex = new CompetitorFaultedException( Self , "Unable to create agent- competitor is faulted critically" , _failureStateInfo.Exception , _script.Id); Sender.Tell(ex); }); Receive <CompetitorResult>(msg => { //just in case Sender.Tell(PoisonPill.Instance); }); ReceiveAny(msg => throw new InvalidOperationException($"Unknown message type received. Received object: {msg}")); }
private void UpdateFailure(AgentFailureState newState) { _failureStateInfo = newState; BroadcastToSubscribers(_failureStateInfo); }