public void GetNextState_HelpTransition_ReturnTopicSelectionState() { var unknownUserState = new UnknownUserState(); var helpTransition = new HelpTransition(); var(state, command) = stateMachine.GetNextState(unknownUserState, helpTransition); state.Should().BeOfType <TopicSelectionState>(); command.Should().BeOfType <SelectTopicCommand>(); }
public static void SetNextState(this IStateMachine stateMachine) { if (stateMachine == null) { throw new ArgumentNullException(nameof(stateMachine)); } var activeStateId = stateMachine.GetState(); var nextStateId = stateMachine.GetNextState(activeStateId); stateMachine.SetState(nextStateId); }
public ICommand ProcessMessage(Update update) { long userId = -1; switch (update.Type) { case UpdateType.CallbackQuery: userId = update.CallbackQuery.Message.Chat.Id; break; case UpdateType.Message: userId = update.Message.Chat.Id; break; } var userEntity = userRepository.FindByTelegramId(userId) ?? userRepository.Insert(new UserEntity(new UnknownUserState(), userId, Guid.NewGuid(), 0)); var state = userEntity.CurrentState; var transition = parser.Parse(state, update, quizService, logger); logger.LogInformation($"Parsed transition {transition}"); logger.LogInformation($"Parsed state {state}"); if (transition is CorrectTransition correct) { logger.LogInformation($"State content: {correct.Content}"); } var(currentState, currentCommand) = stateMachine.GetNextState(state, transition); logger.LogInformation($"New state {currentState}"); userRepository.Update(new UserEntity(currentState, userId, userEntity.Id, userEntity.MessageId, userEntity.CurrentTask)); return(currentCommand); }