From 87c565f4d209cc187d96c3e0d06a1506188ca677 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Tue, 19 Jul 2022 20:36:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B8=20=D1=84=D0=BE=D1=82=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot-context/pom.xml | 2 +- .../keyboard/simple/SimpleKeyBoardLine.java | 7 ++-- .../context/domain/unit/AnswerSave.java | 14 +++---- .../context/domain/unit/AnswerValidity.java | 6 +-- .../context/domain/unit/cmd/RollBackCmd.java | 12 ++++++ .../preser/AnswerSaveMapPreservable.java | 37 +++++++++++++++++++ .../preser/AnswerSavePreservable.java | 14 +++++++ .../preser}/Preservable.java | 13 +------ .../context/service/UnitContextFactory.java | 10 +++++ .../service/save/LocalPreservable.java | 37 ------------------- .../service/usercode/ProcessingData.java | 3 +- bot-core/pom.xml | 2 +- .../core/service/action/AnswerSaveAction.java | 4 +- .../godfather/core/utils/QuestionUtils.java | 2 +- pom.xml | 4 +- 15 files changed, 96 insertions(+), 71 deletions(-) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSavePreservable.java rename bot-context/src/main/java/dev/struchkov/godfather/context/{service/save => repository/preser}/Preservable.java (55%) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/service/UnitContextFactory.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java diff --git a/bot-context/pom.xml b/bot-context/pom.xml index fa90386..b1e62d2 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.12 + 0.0.14 bot-context diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java index e5c599f..30638ab 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/keyboard/simple/SimpleKeyBoardLine.java @@ -4,6 +4,7 @@ import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton; import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -16,7 +17,7 @@ public class SimpleKeyBoardLine implements KeyBoardLine { /** * Кнопки в строке. */ - protected List buttons = new ArrayList<>(); + protected List buttons; public SimpleKeyBoardLine(List buttons) { this.buttons = buttons; @@ -26,8 +27,8 @@ public class SimpleKeyBoardLine implements KeyBoardLine { buttons = builder.buttons; } - public static SimpleKeyBoardLine simpleLine(KeyBoardButton keyBoardButton) { - return new SimpleKeyBoardLine(List.of(keyBoardButton)); + public static SimpleKeyBoardLine simpleLine(KeyBoardButton... keyBoardButton) { + return new SimpleKeyBoardLine(Arrays.stream(keyBoardButton).toList()); } public static Builder builder() { diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java index 226dfd9..9155f49 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java @@ -3,9 +3,9 @@ package dev.struchkov.godfather.context.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.TypeUnit; import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.repository.preser.AnswerSavePreservable; import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.save.CheckSave; -import dev.struchkov.godfather.context.service.save.Preservable; import dev.struchkov.godfather.context.service.save.PreservableData; import dev.struchkov.godfather.context.service.save.Pusher; @@ -27,7 +27,7 @@ public class AnswerSave extends MainUnit { /** * Объект отвечающий за сохранение - репозиторий. */ - private final Preservable preservable; + private final AnswerSavePreservable preservable; /** * Ключ для данных. @@ -84,7 +84,7 @@ public class AnswerSave extends MainUnit { } } - public Preservable getPreservable() { + public AnswerSavePreservable getPreservable() { return preservable; } @@ -110,13 +110,13 @@ public class AnswerSave extends MainUnit { public static final class Builder { private String name; - private Set keyWords = new HashSet<>(); - private Set phrases = new HashSet<>(); + private final Set keyWords = new HashSet<>(); + private final Set phrases = new HashSet<>(); private Pattern pattern; private Integer matchThreshold; private Integer priority; private Set nextUnits = new HashSet<>(); - private Preservable preservable; + private AnswerSavePreservable preservable; private String key; private Pusher pusher; private PreservableData preservableData; @@ -188,7 +188,7 @@ public class AnswerSave extends MainUnit { return this; } - public Builder preservable(Preservable val) { + public Builder preservable(AnswerSavePreservable val) { this.preservable = val; return this; } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java index 23cf582..c57bf6d 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java @@ -4,8 +4,8 @@ import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.TypeUnit; import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.ClarificationQuestion; -import dev.struchkov.godfather.context.service.save.LocalPreservable; -import dev.struchkov.godfather.context.service.save.Preservable; +import dev.struchkov.godfather.context.repository.preser.AnswerSaveMapPreservable; +import dev.struchkov.godfather.context.repository.preser.Preservable; import java.util.Collection; import java.util.HashSet; @@ -35,7 +35,7 @@ public class AnswerValidity extends MainUnit { */ private final MainUnit unitNull; - private final Preservable tempSave = new LocalPreservable<>(); + private final Preservable tempSave = new AnswerSaveMapPreservable<>(); private final ClarificationQuestion clarificationQuestion; 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 257f7ac..4748172 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 @@ -57,6 +57,18 @@ public class RollBackCmd extends MainUnit { return RollBackCmd.builder().countBack(1).build(); } + public static RollBackCmd doubleRollBack() { + return RollBackCmd.builder().countBack(2).build(); + } + + public static RollBackCmd rollBack(String unitName) { + return RollBackCmd.builder().rollbackUnitName(unitName).build(); + } + + public static RollBackCmd rollBack(String phrase, String unitName) { + return RollBackCmd.builder().phrase(phrase).rollbackUnitName(unitName).build(); + } + public int getCountBack() { return countBack; } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java new file mode 100644 index 0000000..086d6fb --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java @@ -0,0 +1,37 @@ +package dev.struchkov.godfather.context.repository.preser; + +import dev.struchkov.godfather.context.service.save.Pusher; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class AnswerSaveMapPreservable implements AnswerSavePreservable { + + private final Map> saveMap = new HashMap<>(); + + @Override + public void save(Long personId, String key, S save) { + saveMap.computeIfAbsent(personId, k -> new HashMap<>()).put(key, save); + } + + @Override + public Optional getByKey(Long personId, String key) { + if (saveMap.containsKey(personId) + && saveMap.get(personId).containsKey(key)) { + return Optional.of(saveMap.get(personId).get(key)); + } + return Optional.empty(); + } + + @Override + public Map getAllSaveElement(Long personId) { + return saveMap.get(personId); + } + + @Override + public void push(Long personId, Pusher pusher) { + Optional.ofNullable(pusher).ifPresent(sPusher -> sPusher.push(getAllSaveElement(personId))); + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSavePreservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSavePreservable.java new file mode 100644 index 0000000..526fff1 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSavePreservable.java @@ -0,0 +1,14 @@ +package dev.struchkov.godfather.context.repository.preser; + +import dev.struchkov.godfather.context.service.save.Pusher; + +public interface AnswerSavePreservable extends Preservable { + + /** + * Финальное сохранение, можно реализовать как отправку данных куда-то + * + * @param personId Идентификатор пользователя + */ + void push(Long personId, Pusher pusher); + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Preservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/Preservable.java similarity index 55% rename from bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Preservable.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/Preservable.java index a5ed2d5..8910c80 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Preservable.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/Preservable.java @@ -1,6 +1,4 @@ -package dev.struchkov.godfather.context.service.save; - -import dev.struchkov.godfather.context.service.save.Pusher; +package dev.struchkov.godfather.context.repository.preser; import java.util.Map; import java.util.Optional; @@ -22,15 +20,6 @@ public interface Preservable { Optional getByKey(Long personId, String key); - /** - * Финальное сохранение, можно реализовать как отправку данных куда-то - * - * @param personId Идентификатор пользователя - */ - default void push(Long personId, Pusher pusher) { - Optional.ofNullable(pusher).ifPresent(sPusher -> sPusher.push(getAllSaveElement(personId))); - } - Map getAllSaveElement(Long personId); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/UnitContextFactory.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/UnitContextFactory.java new file mode 100644 index 0000000..d33aba7 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/UnitContextFactory.java @@ -0,0 +1,10 @@ +package dev.struchkov.godfather.context.service; + +import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.domain.unit.AnswerText; + +public interface UnitContextFactory { + + AnswerText.Builder createAnswerText(); + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java deleted file mode 100644 index 99ab0f6..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.struchkov.godfather.context.service.save; - -import dev.struchkov.godfather.context.service.save.Preservable; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class LocalPreservable implements Preservable { - - private final Map> saveMap = new HashMap<>(); - - @Override - public void save(Long personId, String key, S save) { - if (!saveMap.containsKey(personId)) { - saveMap.put(personId, new HashMap<>()); - } - saveMap.get(personId).put(key, save); - } - - @Override - public Optional getByKey(Long personId, String key) { - if (saveMap.containsKey(personId)) { - if (saveMap.get(personId).containsKey(key)) { - return Optional.of(saveMap.get(personId).get(key)); - } - } - return Optional.empty(); - } - - @Override - public Map getAllSaveElement(Long personId) { - return saveMap.get(personId); - } - - -} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java index fda5b84..ca46acf 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/ProcessingData.java @@ -1,10 +1,9 @@ package dev.struchkov.godfather.context.service.usercode; import dev.struchkov.godfather.context.domain.BoxAnswer; -import dev.struchkov.godfather.context.domain.content.Message; @FunctionalInterface -public interface ProcessingData { +public interface ProcessingData { BoxAnswer processing(C content); diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 32a4d90..e1d5fda 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.12 + 0.0.14 bot-core diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java index 4a75d19..059f09b 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerSaveAction.java @@ -4,8 +4,8 @@ import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.domain.unit.AnswerSave; import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.repository.preser.AnswerSavePreservable; import dev.struchkov.godfather.context.service.save.CheckSave; -import dev.struchkov.godfather.context.service.save.Preservable; import dev.struchkov.godfather.context.service.save.PreservableData; /** @@ -20,7 +20,7 @@ public class AnswerSaveAction implements ActionUnit, Message> { final AnswerSave answerSave = unitRequest.getUnit(); final Message message = unitRequest.getMessage(); - final Preservable preservable = answerSave.getPreservable(); + final AnswerSavePreservable preservable = answerSave.getPreservable(); final Long personId = message.getPersonId(); final CheckSave checkSave = answerSave.getCheckSave(); diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java b/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java index a52746b..a1e2558 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/utils/QuestionUtils.java @@ -9,7 +9,7 @@ //import dev.struchkov.godfather.core.domain.unit.AnswerText; //import dev.struchkov.godfather.core.domain.unit.MainUnit; //import dev.struchkov.godfather.core.domain.unit.UnitActiveType; -//import dev.struchkov.godfather.context.service.save.Preservable; +//import dev.struchkov.godfather.context.repository.preser.Preservable; //import dev.struchkov.godfather.context.service.save.Pusher; // //import java.util.List; diff --git a/pom.xml b/pom.xml index 87be887..c400431 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.12 + 0.0.14 pom @@ -32,7 +32,7 @@ UTF-8 UTF-8 - 0.0.12 + 0.0.14 ${godfather.ver} ${godfather.ver}