From 29145fe093e1e8bb6ed9a99523edddf7c02015c2 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 13 Jul 2022 20:39:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8F=20=D0=BD=D0=B0=D0=B7=D0=B0=D0=B4=20=D0=B2=20?= =?UTF-8?q?=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD=D0=B8=D1=8E=20=D1=8E?= =?UTF-8?q?=D0=BD=D0=B8=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot-context/pom.xml | 2 +- .../context/domain/unit/cmd/RollBackCmd.java | 50 ++++++++++++------- .../context/exception/RollBackException.java | 18 +++++++ .../repository/StorylineRepository.java | 2 + .../impl/local/StorylineMapRepository.java | 15 ++++++ .../context/service/StorylineService.java | 5 +- bot-core/pom.xml | 2 +- .../godfather/core/GeneralAutoResponder.java | 4 +- .../core/service/StorylineMailService.java | 5 ++ .../service/action/cmd/BackCmdAction.java | 38 -------------- .../service/action/cmd/RollBackCmdAction.java | 37 ++++++++++++++ pom.xml | 4 +- 12 files changed, 117 insertions(+), 65 deletions(-) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/exception/RollBackException.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/BackCmdAction.java create mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java diff --git a/bot-context/pom.xml b/bot-context/pom.xml index a3e0a1f..34d02fa 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.9 + 0.0.10 bot-context diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java index d2011b9..0cf8be4 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java @@ -21,6 +21,11 @@ public class RollBackCmd extends MainUnit { */ private final int countBack; + /** + * Имя юнита, на который нужно вернуть пользователя. + */ + private final String rollbackUnitName; + private RollBackCmd(Builder builder) { super( builder.name, @@ -35,10 +40,7 @@ public class RollBackCmd extends MainUnit { TypeUnit.BACK_CMD ); this.countBack = builder.countBack; - } - - public int getCountBack() { - return countBack; + this.rollbackUnitName = builder.rollbackUnitName; } public static RollBackCmd.Builder builder() { @@ -53,76 +55,90 @@ public class RollBackCmd extends MainUnit { return RollBackCmd.builder().countBack(1).build(); } + public int getCountBack() { + return countBack; + } + + public String getRollbackUnitName() { + return rollbackUnitName; + } + public static final class Builder { + private final Set keyWords = new HashSet<>(); private String name; - private Set keyWords = new HashSet<>(); private String phrase; private Pattern pattern; private Integer matchThreshold; private Integer priority; private UnitActiveType activeType = UnitActiveType.DEFAULT; private int countBack; + private String rollbackUnitName; private Builder() { } - public RollBackCmd.Builder name(String name) { + public Builder name(String name) { this.name = name; return this; } - public RollBackCmd.Builder keyWords(Set val) { + public Builder keyWords(Set val) { keyWords.addAll(val); return this; } - public RollBackCmd.Builder keyWord(KeyWord val) { + public Builder keyWord(KeyWord val) { keyWords.add(val); return this; } - public RollBackCmd.Builder stringKeyWords(Set val) { + public Builder stringKeyWords(Set val) { keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } - public RollBackCmd.Builder keyWord(String val) { + public Builder keyWord(String val) { keyWords.add(KeyWord.of(val)); return this; } - public RollBackCmd.Builder phrase(String val) { + public Builder phrase(String val) { phrase = val; return this; } - public RollBackCmd.Builder pattern(Pattern val) { + public Builder pattern(Pattern val) { pattern = val; return this; } - public RollBackCmd.Builder matchThreshold(Integer val) { + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; } - public RollBackCmd.Builder priority(Integer val) { + public Builder priority(Integer val) { priority = val; return this; } - public RollBackCmd.Builder activeType(UnitActiveType val) { + public Builder activeType(UnitActiveType val) { activeType = val; return this; } - public RollBackCmd.Builder countBack(int val) { + public Builder countBack(int val) { countBack = val + 1; return this; } + public Builder rollbackUnitName(String val) { + rollbackUnitName = val; + return this; + } + public RollBackCmd build() { - if (countBack < 2) { + if (rollbackUnitName == null && countBack < 2) { throw new UnitConfigException("Ошибка конфигурирования юнита {0}: Количество юнитов для отката не должно быть меньше 1.", name); } return new RollBackCmd(this); diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/exception/RollBackException.java b/bot-context/src/main/java/dev/struchkov/godfather/context/exception/RollBackException.java new file mode 100644 index 0000000..f6d29f2 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/exception/RollBackException.java @@ -0,0 +1,18 @@ +package dev.struchkov.godfather.context.exception; + +import java.util.function.Supplier; + +/** + * Ошибки связанные с юнитом RollBackCmd + */ +public class RollBackException extends AppBotException { + + public RollBackException(String message) { + super(message); + } + + public static Supplier rollBackException(String message) { + return () -> new RollBackException(message); + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/StorylineRepository.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/StorylineRepository.java index 5263336..be1459c 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/StorylineRepository.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/StorylineRepository.java @@ -11,6 +11,8 @@ public interface StorylineRepository { Optional findByCountLast(long personId, int countUnitsToBack); + Optional findByCountLast(long personId, String unitName); + void cleanHistoryByPersonId(@NotNull Long personId); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java index 8b09ae5..dbb7c1f 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java @@ -35,6 +35,21 @@ public class StorylineMapRepository implements StorylineRepository { return Optional.empty(); } + @Override + public Optional findByCountLast(long personId, String unitName) { + if (map.containsKey(personId)) { + final Stack stack = map.get(personId); + StorylineHistory storylineHistory = null; + while (!stack.isEmpty()) { + storylineHistory = stack.pop(); + if (unitName.equals(storylineHistory.getUnitName())) { + return Optional.of(storylineHistory); + } + } + } + return Optional.empty(); + } + @Override public void cleanHistoryByPersonId(@NotNull Long personId) { if (map.containsKey(personId)) { diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java index 5d016ef..69e96f9 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java @@ -20,13 +20,12 @@ public interface StorylineService { Optional replaceUserToBack(long personId, int countUnitsToBack); + Optional replaceUserToBack(long personId, String unitName); + Optional getDefaultUnit(); /** * Ленивая (поздняя) связка юнитов между собой. Осуществляется уже после создания сценария. С помощью данного подхода можно реализовать циклические зависимости юнитов. Либо можно использовать {@link dev.struchkov.godfather.context.domain.unit.cmd.TeleportCmd} - * - * @param firstName - * @param secondName */ void lazyLink(String firstName, String secondName); diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 4ced09c..4e919e4 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.9 + 0.0.10 bot-core diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java index 21ab4a6..3127432 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/GeneralAutoResponder.java @@ -32,12 +32,10 @@ import java.util.stream.Collectors; public class GeneralAutoResponder { private final PersonSettingService personSettingService; - - private ErrorHandler errorHandler; private final StorylineService storyLineService; - protected Map> actionUnitMap = new HashMap<>(); protected List> modifiable; + private ErrorHandler errorHandler; protected GeneralAutoResponder( Sending sending, diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java index ab1571b..6a6002e 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java @@ -81,6 +81,11 @@ public class StorylineMailService implements StorylineService { return storylineRepository.findByCountLast(personId, countUnitsToBack); } + @Override + public Optional replaceUserToBack(long personId, String unitName) { + return storylineRepository.findByCountLast(personId, unitName); + } + @Override public Optional getDefaultUnit() { return storyLine.getDefaultUnit(); diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/BackCmdAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/BackCmdAction.java deleted file mode 100644 index 2f25189..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/BackCmdAction.java +++ /dev/null @@ -1,38 +0,0 @@ -package dev.struchkov.godfather.core.service.action.cmd; - -import dev.struchkov.godfather.context.domain.StorylineHistory; -import dev.struchkov.godfather.context.domain.UnitRequest; -import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.context.domain.unit.cmd.RollBackCmd; -import dev.struchkov.godfather.context.domain.unit.MainUnit; -import dev.struchkov.godfather.context.service.StorylineService; -import dev.struchkov.godfather.core.service.action.ActionUnit; - -import java.util.Optional; - -public class BackCmdAction implements ActionUnit { - - private final StorylineService storyLineService; - - public BackCmdAction(StorylineService storyLineService) { - this.storyLineService = storyLineService; - } - - @Override - public UnitRequest action(UnitRequest unitRequest) { - final RollBackCmd unit = unitRequest.getUnit(); - final T message = unitRequest.getMessage(); - - final int countToBack = unit.getCountBack(); - - final Optional optHistory = storyLineService.replaceUserToBack(message.getPersonId(), countToBack); - if (optHistory.isPresent()) { - final StorylineHistory history = optHistory.get(); - final String unitName = history.getUnitName(); - final MainUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); - final T oldMessage = (T) history.getMessage(); - return UnitRequest.of(nextUnit, oldMessage); - } - return UnitRequest.of(unit, message); - } -} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java new file mode 100644 index 0000000..5d0b8c8 --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/RollBackCmdAction.java @@ -0,0 +1,37 @@ +package dev.struchkov.godfather.core.service.action.cmd; + +import dev.struchkov.godfather.context.domain.StorylineHistory; +import dev.struchkov.godfather.context.domain.UnitRequest; +import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.cmd.RollBackCmd; +import dev.struchkov.godfather.context.service.StorylineService; +import dev.struchkov.godfather.core.service.action.ActionUnit; + +import static dev.struchkov.godfather.context.exception.RollBackException.rollBackException; + +public class RollBackCmdAction implements ActionUnit { + + private final StorylineService storyLineService; + + public RollBackCmdAction(StorylineService storyLineService) { + this.storyLineService = storyLineService; + } + + @Override + public UnitRequest action(UnitRequest unitRequest) { + final RollBackCmd unit = unitRequest.getUnit(); + final T message = unitRequest.getMessage(); + + final int countToBack = unit.getCountBack(); + final String rollbackUnitName = unit.getRollbackUnitName(); + + StorylineHistory history = rollbackUnitName != null + ? storyLineService.replaceUserToBack(message.getPersonId(), rollbackUnitName).orElseThrow(rollBackException("Юнит для возвращения не был найден")) + : storyLineService.replaceUserToBack(message.getPersonId(), countToBack).orElseThrow(rollBackException("Юнит для возвращения не был найден")); + final String unitName = history.getUnitName(); + final MainUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); + final T oldMessage = (T) history.getMessage(); + return UnitRequest.of(nextUnit, oldMessage); + } +} diff --git a/pom.xml b/pom.xml index ff753b1..bf9c914 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.9 + 0.0.10 pom @@ -32,7 +32,7 @@ UTF-8 UTF-8 - 0.0.9 + 0.0.10 ${godfather.ver} ${godfather.ver}