|
@@ -1,20 +1,17 @@
|
|
|
package in.ocsf.these.days.app.service;/* kpmy 19.02.2017 */
|
|
|
|
|
|
import com.pengrad.telegrambot.model.Update;
|
|
|
-import in.ocsf.these.days.app.SM;
|
|
|
+import in.ocsf.these.days.app.messaging.ChatHelper;
|
|
|
import in.ocsf.these.days.app.messaging.UpdateHelper;
|
|
|
-import in.ocsf.these.days.app.object.ChatEvent;
|
|
|
-import in.ocsf.these.days.app.object.ChatState;
|
|
|
import in.ocsf.these.days.app.object.User;
|
|
|
import in.ocsf.these.days.app.repo.CardRepositrory;
|
|
|
import in.ocsf.these.days.app.repo.MessageRepository;
|
|
|
import in.ocsf.these.days.app.repo.UserRepository;
|
|
|
+import in.ocsf.these.days.app.state.UserEvent;
|
|
|
+import in.ocsf.these.days.app.state.UserState;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.statemachine.StateMachine;
|
|
|
-import org.springframework.statemachine.config.StateMachineFactory;
|
|
|
-import org.springframework.statemachine.persist.DefaultStateMachinePersister;
|
|
|
-import org.springframework.statemachine.persist.StateMachinePersister;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
@@ -34,7 +31,10 @@ public class UpdateService {
|
|
|
private CardRepositrory cardRepo;
|
|
|
|
|
|
@Autowired
|
|
|
- private StateMachineFactory<ChatState, ChatEvent> stateMachineFactory;
|
|
|
+ private StateService stateService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ChatHelper chat;
|
|
|
|
|
|
private void handleUpdate(UpdateHelper upd) throws Exception {
|
|
|
if (!upd.isEdit()) {
|
|
@@ -44,11 +44,12 @@ public class UpdateService {
|
|
|
switch (cmd.get(0)) {
|
|
|
case "/start":
|
|
|
User user = User.fromUser(upd.getUser());
|
|
|
- StateMachine<ChatState, ChatEvent> state = stateMachineFactory.getStateMachine(Long.toHexString(user.getId()));
|
|
|
- state.start();
|
|
|
- StateMachinePersister<ChatState, ChatEvent, Long> persist = new DefaultStateMachinePersister<>(new SM.InMemoryStateMachinePersist());
|
|
|
- persist.persist(state, user.getId());
|
|
|
- log.info(Long.toHexString(user.getId()));
|
|
|
+ StateMachine<UserState, UserEvent> state = stateService.getStateFor(user);
|
|
|
+ if (state.getState().getId().equals(UserState.unknown)) {
|
|
|
+ stateService.setStateFor(user, state);
|
|
|
+ chat.sendSimpleTextMessage(upd.getChatId(), "дратути...");
|
|
|
+ userRepo.save(user);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
} else {
|