From 996194264e526c6a62c92844c34a50a8c3a3a7ce Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 10 Jul 2022 00:25:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BD=D0=BE=D0=B6=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=B5=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D1=83=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D1=88=D0=B5=D0=BD=D0=B8=D1=8F:=20*=20=D0=A3=D0=B4=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=BB=20=D0=B2=D1=81=D0=B5=20=D1=82=D0=B8=D0=BF?= =?UTF-8?q?=D1=8B=20=D0=B2=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9,=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D1=82=D0=B8=D0=BF=D1=8B?= =?UTF-8?q?=20=D0=B2=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D0=B9=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D1=83=D1=8E=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?=D0=B2=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D0=B0=D1=85=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=B0=D0=B6=D0=B4?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BE=D0=BD=D0=BA=D1=80=D0=B5=D1=82?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BC=D0=B5=D1=81=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=B4=D0=B6=D0=B5=D1=80=D0=B0.=20*=20=D0=A2=D0=B5=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D1=8C=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D1=8F=D0=B5?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D1=89=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=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=BF=D0=BE=20=D1=81=D1=86=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8E.=20=D0=A1=D0=BE=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D1=8F=D0=B5=D1=82=D1=81=D1=8F=20=D1=81=D0=BE=D0=BE=D0=B1?= =?UTF-8?q?=D1=89=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=20=D0=BD=D0=B0=D0=B7?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=8E=D0=BD=D0=B8=D1=82=D0=B0?= =?UTF-8?q?.=20=D0=AD=D1=82=D0=BE=20=D0=BF=D0=BE=D0=B7=D0=B2=D0=BE=D0=BB?= =?UTF-8?q?=D1=8F=D0=B5=D1=82=20=D0=BE=D1=82=D0=BA=D0=B0=D1=82=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D0=BD=D0=B0=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?.=20*=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B4=D0=B2=D0=B0=20=D0=BD=D0=BE=D0=B2=D1=8B=D1=85=20?= =?UTF-8?q?=D1=8E=D0=BD=D0=B8=D1=82=D0=B0.=20RollBackCmd=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B7=D0=B2=D0=BE=D0=BB=D1=8F=D0=B5=D1=82=20=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=D1=82=D0=B8=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D0=BD=D0=B0=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=20=D0=B2=20=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B8=D0=B7?= =?UTF-8?q?=D0=B2=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=BA=D0=BE=D0=BB?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=BE=20=D1=8E=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D0=B2.=20TeleportCmd=20=D0=BF=D0=BE=D0=B7=D0=B2?= =?UTF-8?q?=D0=BE=D0=BB=D1=8F=D0=B5=D1=82=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B8=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F=20=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B8=D0=B7=D0=B2=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D1=81=D1=86=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=D1=80=D0=B8=D1=8F.=20*=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB?= =?UTF-8?q?=20AnswerProcessing,=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B3=D0=BE=20=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20AnswerText.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot-context/pom.xml | 6 +- .../context}/domain/Clarification.java | 4 +- .../context/domain/StorylineHistory.java | 35 +++++ .../godfather/context/domain}/TypeUnit.java | 11 +- .../godfather/context/domain/UnitRequest.java | 34 ++++ .../context/domain/content/Mail.java | 14 +- .../domain/content/attachment/Attachment.java | 14 +- .../content/attachment/AttachmentType.java | 14 -- .../content/attachment/AudioMessage.java | 25 --- .../domain/content/attachment/Geo.java | 69 --------- .../content/attachment/GeoCoordinate.java | 41 ----- .../domain/content/attachment/Link.java | 19 --- .../context}/domain/unit/AnswerCheck.java | 26 +++- .../context}/domain/unit/AnswerSave.java | 34 ++-- .../context}/domain/unit/AnswerText.java | 29 +++- .../context}/domain/unit/AnswerTimer.java | 31 +++- .../context}/domain/unit/AnswerValidity.java | 34 ++-- .../context}/domain/unit/LazyUnit.java | 8 +- .../context}/domain/unit/MainUnit.java | 7 +- .../context}/domain/unit/UnitActiveType.java | 2 +- .../context/domain/unit/cmd/RollBackCmd.java | 133 ++++++++++++++++ .../context/domain/unit/cmd/TeleportCmd.java | 121 +++++++++++++++ .../repository/StorylineRepository.java | 16 ++ .../impl/local/StorylineMapRepository.java | 47 ++++++ .../impl/local/UnitPointLocalRepository.java | 2 +- .../context}/service/Accessibility.java | 2 +- .../service/ClarificationQuestion.java | 4 +- .../context/service/StorylineService.java | 35 +++++ .../context}/service/save/CheckSave.java | 4 +- .../service/save/LocalPreservable.java | 4 +- .../context}/service/save/Preservable.java | 4 +- .../service/save}/PreservableData.java | 2 +- .../context/service/save}/Pusher.java | 2 +- bot-core/pom.xml | 7 +- .../godfather/core/GeneralAutoResponder.java | 86 +++++------ .../core/{StoryLine.java => Storyline.java} | 6 +- .../godfather/core/StorylineMaker.java | 8 +- .../godfather/core/domain/Timer.java | 2 +- .../core/domain/unit/AnswerProcessing.java | 146 ------------------ .../core/service/StorylineMailService.java | 100 ++++++++++++ .../core/service/action/ActionUnit.java | 7 +- .../service/action/AnswerCheckAction.java | 20 ++- .../action/AnswerProcessingAction.java | 36 ----- .../core/service/action/AnswerSaveAction.java | 30 ++-- .../core/service/action/AnswerTextAction.java | 19 ++- .../service/action/AnswerTimerAction.java | 30 ++-- .../service/action/AnswerValidityAction.java | 35 +++-- .../service/action/cmd/BackCmdAction.java | 38 +++++ .../service/action/cmd/TeleportCmdAction.java | 30 ++++ .../save/data/PreservableDataSimple.java | 1 + .../service/save/push/UserSanderPusher.java | 1 + .../core/service/timer/TimerActionTask.java | 9 +- .../godfather/core/utils/QuestionUtils.java | 4 +- pom.xml | 6 +- 54 files changed, 890 insertions(+), 564 deletions(-) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/Clarification.java (77%) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/StorylineHistory.java rename {bot-core/src/main/java/dev/struchkov/godfather/core/utils => bot-context/src/main/java/dev/struchkov/godfather/context/domain}/TypeUnit.java (62%) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AttachmentType.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AudioMessage.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Geo.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/GeoCoordinate.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Link.java rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/AnswerCheck.java (84%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/AnswerSave.java (85%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/AnswerText.java (86%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/AnswerTimer.java (82%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/AnswerValidity.java (81%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/LazyUnit.java (77%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/MainUnit.java (93%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/domain/unit/UnitActiveType.java (78%) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/repository/StorylineRepository.java create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/service/Accessibility.java (73%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/service/ClarificationQuestion.java (64%) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/service/save/CheckSave.java (70%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/service/save/LocalPreservable.java (87%) rename {bot-core/src/main/java/dev/struchkov/godfather/core => bot-context/src/main/java/dev/struchkov/godfather/context}/service/save/Preservable.java (90%) rename {bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data => bot-context/src/main/java/dev/struchkov/godfather/context/service/save}/PreservableData.java (75%) rename {bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push => bot-context/src/main/java/dev/struchkov/godfather/context/service/save}/Pusher.java (79%) rename bot-core/src/main/java/dev/struchkov/godfather/core/{StoryLine.java => Storyline.java} (91%) delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerProcessing.java create mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java delete mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerProcessingAction.java create 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/TeleportCmdAction.java diff --git a/bot-context/pom.xml b/bot-context/pom.xml index 7b312c6..a3e0a1f 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.8 + 0.0.9 bot-context @@ -15,6 +15,10 @@ Доменные сущности, интерфейсы, для библиотеки Godfather + + dev.struchkov + autoresponder + dev.struchkov.haiti haiti-utils diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/Clarification.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/Clarification.java similarity index 77% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/Clarification.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/Clarification.java index 626d440..9af7946 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/Clarification.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/Clarification.java @@ -1,6 +1,4 @@ -package dev.struchkov.godfather.core.domain; - -import dev.struchkov.godfather.context.domain.BoxAnswer; +package dev.struchkov.godfather.context.domain; public class Clarification { diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/StorylineHistory.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/StorylineHistory.java new file mode 100644 index 0000000..54494df --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/StorylineHistory.java @@ -0,0 +1,35 @@ +package dev.struchkov.godfather.context.domain; + +import dev.struchkov.godfather.context.domain.content.Message; + +public class StorylineHistory extends BasicEntity { + + private Long personId; + private String unitName; + private Message message; + + public Long getPersonId() { + return personId; + } + + public void setPersonId(Long personId) { + this.personId = personId; + } + + public String getUnitName() { + return unitName; + } + + public void setUnitName(String unitName) { + this.unitName = unitName; + } + + public Message getMessage() { + return message; + } + + public void setMessage(Message message) { + this.message = message; + } + +} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/utils/TypeUnit.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/TypeUnit.java similarity index 62% rename from bot-core/src/main/java/dev/struchkov/godfather/core/utils/TypeUnit.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/TypeUnit.java index 6296c40..96bcbcb 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/utils/TypeUnit.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/TypeUnit.java @@ -1,4 +1,6 @@ -package dev.struchkov.godfather.core.utils; +package dev.struchkov.godfather.context.domain; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; /** * Тип Unit-а. Обределяет способ обработки. @@ -9,14 +11,15 @@ public class TypeUnit { public static final String TEXT = "TEXT"; public static final String SAVE = "SAVE"; - public static final String PROCESSING = "PROCESSING"; public static final String TIMER = "TIMER"; public static final String CHECK = "CHECK"; public static final String VALIDITY = "VALIDITY"; - public static final String ACCOUNT = "ACCOUNT"; + public static final String BACK_CMD = "BACK_CMD"; + + public static final String TELEPORT_CMD = "TELEPORT_CMD"; private TypeUnit() { - throw new IllegalStateException("Утилитарный класс"); + utilityClass(); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java new file mode 100644 index 0000000..2773e4a --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/UnitRequest.java @@ -0,0 +1,34 @@ +package dev.struchkov.godfather.context.domain; + +import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.domain.unit.MainUnit; + +/** + * Сущность инкапсулирует в себе данные, необходимые для обработки сценария. + * + * @param Тип юнита + * @param Тип сообщения + */ +public class UnitRequest { + + private final U unit; + private final M message; + + private UnitRequest(U unit, M message) { + this.unit = unit; + this.message = message; + } + + public static UnitRequest of(U mainUnit, M message) { + return new UnitRequest<>(mainUnit, message); + } + + public U getUnit() { + return unit; + } + + public M getMessage() { + return message; + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Mail.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Mail.java index 1eafa1d..66f9286 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Mail.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/Mail.java @@ -7,6 +7,8 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.OneToMany; import javax.persistence.Table; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -33,9 +35,9 @@ public class Mail extends Message { /** * Вложения к сообщению. */ - @OneToMany(fetch = FetchType.EAGER) @Column(name = "attachment") - private List attachments; + @OneToMany(fetch = FetchType.LAZY) + private List attachments = new ArrayList<>(); /** * Пересланные сообщения. @@ -68,8 +70,12 @@ public class Mail extends Message { return attachments; } - public void setAttachments(List attachments) { - this.attachments = attachments; + public void addAttachment(Attachment attachment) { + this.attachments.add(attachment); + } + + public void addAttachments(Collection attachments) { + this.attachments.addAll(attachments); } public List getForwardMail() { diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Attachment.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Attachment.java index 7605b66..453c9cb 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Attachment.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Attachment.java @@ -2,10 +2,7 @@ package dev.struchkov.godfather.context.domain.content.attachment; import dev.struchkov.godfather.context.domain.BasicEntity; -import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; /** * Абстрактная сущность, для всех вложений к сообщениям от пользователей. @@ -15,15 +12,6 @@ import javax.persistence.Enumerated; @Entity public abstract class Attachment extends BasicEntity { - /** - * Тип сущности. - */ - @Column(name = "type") - @Enumerated(value = EnumType.STRING) - protected AttachmentType type; - - public AttachmentType getType() { - return type; - } + public abstract String getType(); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AttachmentType.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AttachmentType.java deleted file mode 100644 index 696f5f4..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AttachmentType.java +++ /dev/null @@ -1,14 +0,0 @@ -package dev.struchkov.godfather.context.domain.content.attachment; - -/** - * Тип вложения {@link Attachment} к сообщению. - * - * @author upagge [08/07/2019] - */ -public enum AttachmentType { - - AUDIO_MESSAGE, - GEO, - LINK - -} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AudioMessage.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AudioMessage.java deleted file mode 100644 index d833434..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/AudioMessage.java +++ /dev/null @@ -1,25 +0,0 @@ -package dev.struchkov.godfather.context.domain.content.attachment; - -import java.net.URL; - -/** - * Вложение типа "Аудиосообщение". - * - * @author upagge [08/07/2019] - */ -public class AudioMessage extends Attachment { - - /** - * Ссылка на аудиозапись в формате odd. - */ - private URL linkOdd; - - public AudioMessage() { - type = AttachmentType.AUDIO_MESSAGE; - } - - public AudioMessage(URL linkOdd) { - this.linkOdd = linkOdd; - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Geo.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Geo.java deleted file mode 100644 index 7f04300..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Geo.java +++ /dev/null @@ -1,69 +0,0 @@ -package dev.struchkov.godfather.context.domain.content.attachment; - -/** - * Вложение типа "Карта". - * - * @author upagge [08/07/2019] - */ -public class Geo extends Attachment { - - /** - * Географические координаты. - */ - private GeoCoordinate geoCoordinate; - - /** - * Название страны. - */ - private String country; - - /** - * Название города. - */ - private String city; - - private Geo() { - type = AttachmentType.GEO; - } - - public static Builder builder() { - return new Geo().new Builder(); - } - - public GeoCoordinate getGeoCoordinate() { - return geoCoordinate; - } - - public String getCountry() { - return country; - } - - public String getCity() { - return city; - } - - public class Builder { - private Builder() { - } - - public Builder coordinate(Float lat, Float aLong) { - Geo.this.geoCoordinate = new GeoCoordinate(lat, aLong); - return this; - } - - public Builder country(String countryName) { - Geo.this.country = countryName; - return this; - } - - public Builder city(String cityName) { - Geo.this.city = cityName; - return this; - } - - public Geo build() { - return Geo.this; - } - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/GeoCoordinate.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/GeoCoordinate.java deleted file mode 100644 index d0ac11d..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/GeoCoordinate.java +++ /dev/null @@ -1,41 +0,0 @@ -package dev.struchkov.godfather.context.domain.content.attachment; - -/** - * Сущность для хранения географических координат. - * - * @author upagge [08/07/2019] - */ -public class GeoCoordinate { - - /** - * Широта. - */ - private Float latitude; - - /** - * Долгота. - */ - private Float longitude; - - public GeoCoordinate(Float latitude, Float longitude) { - this.latitude = latitude; - this.longitude = longitude; - } - - public Float getLatitude() { - return latitude; - } - - public void setLatitude(Float latitude) { - this.latitude = latitude; - } - - public Float getLongitude() { - return longitude; - } - - public void setLongitude(Float longitude) { - this.longitude = longitude; - } - -} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Link.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Link.java deleted file mode 100644 index 3dd78ff..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/content/attachment/Link.java +++ /dev/null @@ -1,19 +0,0 @@ -package dev.struchkov.godfather.context.domain.content.attachment; - -public class Link extends Attachment { - - private String url; - - public Link() { - this.type = AttachmentType.LINK; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - -} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerCheck.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java similarity index 84% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerCheck.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java index 2ad53b4..dc75f64 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerCheck.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java @@ -1,13 +1,15 @@ -package dev.struchkov.godfather.core.domain.unit; +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.service.Accessibility; import dev.struchkov.godfather.context.service.usercode.CheckData; -import dev.struchkov.godfather.core.service.Accessibility; -import dev.struchkov.godfather.core.utils.TypeUnit; import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static dev.struchkov.godfather.context.exception.UnitConfigException.unitConfigException; import static dev.struchkov.haiti.utils.Inspector.isAnyNotNull; @@ -71,7 +73,7 @@ public class AnswerCheck extends MainUnit { public static final class Builder { private String name; - private Set keyWords = new HashSet<>(); + private Set keyWords = new HashSet<>(); private String phrase; private Pattern pattern; private Integer matchThreshold; @@ -90,13 +92,23 @@ public class AnswerCheck extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords = val; + public Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } public Builder keyWord(String val) { - keyWords.add(val); + keyWords.add(KeyWord.of(val)); return this; } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerSave.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java similarity index 85% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerSave.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java index fac35c3..1c7f9e9 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerSave.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java @@ -1,17 +1,19 @@ -package dev.struchkov.godfather.core.domain.unit; +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.core.service.Accessibility; -import dev.struchkov.godfather.core.service.save.CheckSave; -import dev.struchkov.godfather.core.service.save.Preservable; -import dev.struchkov.godfather.core.service.save.data.PreservableData; -import dev.struchkov.godfather.core.service.save.push.Pusher; -import dev.struchkov.godfather.core.utils.TypeUnit; +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; import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static dev.struchkov.haiti.utils.Inspector.isNotNull; @@ -107,7 +109,7 @@ public class AnswerSave extends MainUnit { public static final class Builder { private String name; - private Set keyWords = new HashSet<>(); + private Set keyWords = new HashSet<>(); private String phrase; private Pattern pattern; private Integer matchThreshold; @@ -129,13 +131,23 @@ public class AnswerSave extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords = val; + public Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } public Builder keyWord(String val) { - keyWords.add(val); + keyWords.add(KeyWord.of(val)); return this; } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java similarity index 86% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java index beb7dca..a7018df 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerText.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java @@ -1,19 +1,21 @@ -package dev.struchkov.godfather.core.domain.unit; +package dev.struchkov.godfather.context.domain.unit; +import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.BoxAnswer; +import dev.struchkov.godfather.context.domain.TypeUnit; import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.exception.UnitConfigException; +import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.context.service.usercode.Insert; import dev.struchkov.godfather.context.service.usercode.MessageFunction; import dev.struchkov.godfather.context.service.usercode.ProcessingData; -import dev.struchkov.godfather.core.service.Accessibility; -import dev.struchkov.godfather.core.utils.TypeUnit; import java.util.HashSet; import java.util.Set; import java.util.function.Consumer; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static dev.struchkov.haiti.utils.Inspector.isNotNull; @@ -82,7 +84,7 @@ public class AnswerText extends MainUnit { private ProcessingData boxAnswer; private Insert insert; private Sending sending; - private Set keyWords = new HashSet<>(); + private Set keyWords = new HashSet<>(); private String phrase; private Pattern pattern; private Integer matchThreshold; @@ -135,8 +137,23 @@ public class AnswerText extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords = val; + public Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + return this; + } + + public Builder keyWord(String val) { + keyWords.add(KeyWord.of(val)); return this; } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerTimer.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java similarity index 82% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerTimer.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java index 2742e52..71e01fb 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerTimer.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java @@ -1,12 +1,16 @@ -package dev.struchkov.godfather.core.domain.unit; +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.domain.keyboard.KeyBoard; +import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.usercode.CheckData; -import dev.struchkov.godfather.core.service.Accessibility; -import dev.struchkov.godfather.core.utils.TypeUnit; +import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static dev.struchkov.godfather.context.exception.UnitConfigException.unitConfigException; import static dev.struchkov.haiti.utils.Inspector.isNotNull; @@ -83,7 +87,7 @@ public class AnswerTimer extends MainUnit { private Integer timeDelaySec; private Integer timeDeathSec; private CheckData checkLoop; - private Set keyWords; + private Set keyWords = new HashSet<>(); private String phrase; private Pattern pattern; private Integer matchThreshold; @@ -119,8 +123,23 @@ public class AnswerTimer extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords = val; + public Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + return this; + } + + public Builder keyWord(String val) { + keyWords.add(KeyWord.of(val)); return this; } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerValidity.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java similarity index 81% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerValidity.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java index 0b78c8a..eaa86d4 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerValidity.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java @@ -1,14 +1,16 @@ -package dev.struchkov.godfather.core.domain.unit; +package dev.struchkov.godfather.context.domain.unit; -import dev.struchkov.godfather.core.service.Accessibility; -import dev.struchkov.godfather.core.service.ClarificationQuestion; -import dev.struchkov.godfather.core.service.save.LocalPreservable; -import dev.struchkov.godfather.core.service.save.Preservable; -import dev.struchkov.godfather.core.utils.TypeUnit; +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 java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * Обработка данных со страницы пользователя. @@ -85,7 +87,7 @@ public class AnswerValidity extends MainUnit { private MainUnit unitNo; private MainUnit unitNull; private ClarificationQuestion clarificationQuestion; - private Set keyWords; + private Set keyWords = new HashSet<>(); private String phrase; private Pattern pattern; private Integer matchThreshold; @@ -121,13 +123,23 @@ public class AnswerValidity extends MainUnit { return this; } - public Builder keyWords(Set val) { - keyWords = val; + public Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); return this; } public Builder keyWord(String val) { - keyWords.add(val); + keyWords.add(KeyWord.of(val)); return this; } @@ -162,7 +174,7 @@ public class AnswerValidity extends MainUnit { } public Builder clearKeyWords() { - nextUnits.clear(); + keyWords.clear(); return this; } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/LazyUnit.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/LazyUnit.java similarity index 77% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/LazyUnit.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/LazyUnit.java index 33d46e1..62a255b 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/LazyUnit.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/LazyUnit.java @@ -1,7 +1,9 @@ -package dev.struchkov.godfather.core.domain.unit; +package dev.struchkov.godfather.context.domain.unit; +import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.context.domain.UnitDefinition; -import dev.struchkov.godfather.core.service.Accessibility; +import dev.struchkov.godfather.context.domain.keyboard.KeyBoard; +import dev.struchkov.godfather.context.service.Accessibility; import java.util.Set; import java.util.regex.Pattern; @@ -12,7 +14,7 @@ public class LazyUnit extends MainUnit { private LazyUnit( String name, - Set keyWords, + Set keyWords, String phrase, Pattern pattern, Integer matchThreshold, diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/MainUnit.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java similarity index 93% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/MainUnit.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java index 248212a..46a9f5a 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/MainUnit.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java @@ -1,7 +1,8 @@ -package dev.struchkov.godfather.core.domain.unit; +package dev.struchkov.godfather.context.domain.unit; +import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.autoresponder.entity.Unit; -import dev.struchkov.godfather.core.service.Accessibility; +import dev.struchkov.godfather.context.service.Accessibility; import java.util.Objects; import java.util.Optional; @@ -43,7 +44,7 @@ public abstract class MainUnit extends Unit { protected MainUnit( String name, - Set keyWords, + Set keyWords, String phrase, Pattern pattern, Integer matchThreshold, diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/UnitActiveType.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/UnitActiveType.java similarity index 78% rename from bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/UnitActiveType.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/UnitActiveType.java index 0297508..569317f 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/UnitActiveType.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/UnitActiveType.java @@ -1,4 +1,4 @@ -package dev.struchkov.godfather.core.domain.unit; +package dev.struchkov.godfather.context.domain.unit; /** * Тип активации Unit-а. Определяет порядок обработки Unit. 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 new file mode 100644 index 0000000..d2011b9 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java @@ -0,0 +1,133 @@ +package dev.struchkov.godfather.context.domain.unit.cmd; + +import dev.struchkov.autoresponder.entity.KeyWord; +import dev.struchkov.godfather.context.domain.TypeUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.UnitActiveType; +import dev.struchkov.godfather.context.exception.UnitConfigException; + +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Юнит, который позволяет откатить пользователя на предыдущие юниты в сценарии с сохранением ранее введенной информации в сообщениях. + */ +public class RollBackCmd extends MainUnit { + + /** + * Количество юнитов, на которые можно откатиться назад. + */ + private final int countBack; + + private RollBackCmd(Builder builder) { + super( + builder.name, + builder.keyWords, + builder.phrase, + builder.pattern, + builder.matchThreshold, + builder.priority, + new HashSet<>(), + builder.activeType, + null, + TypeUnit.BACK_CMD + ); + this.countBack = builder.countBack; + } + + public int getCountBack() { + return countBack; + } + + public static RollBackCmd.Builder builder() { + return new RollBackCmd.Builder(); + } + + public static RollBackCmd rollBack(int countToBack) { + return RollBackCmd.builder().countBack(countToBack).build(); + } + + public static RollBackCmd singleRollBack() { + return RollBackCmd.builder().countBack(1).build(); + } + + public static final class Builder { + 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 Builder() { + } + + public RollBackCmd.Builder name(String name) { + this.name = name; + return this; + } + + public RollBackCmd.Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public RollBackCmd.Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public RollBackCmd.Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + return this; + } + + public RollBackCmd.Builder keyWord(String val) { + keyWords.add(KeyWord.of(val)); + return this; + } + + public RollBackCmd.Builder phrase(String val) { + phrase = val; + return this; + } + + public RollBackCmd.Builder pattern(Pattern val) { + pattern = val; + return this; + } + + public RollBackCmd.Builder matchThreshold(Integer val) { + matchThreshold = val; + return this; + } + + public RollBackCmd.Builder priority(Integer val) { + priority = val; + return this; + } + + public RollBackCmd.Builder activeType(UnitActiveType val) { + activeType = val; + return this; + } + + public RollBackCmd.Builder countBack(int val) { + countBack = val + 1; + return this; + } + + public RollBackCmd build() { + if (countBack < 2) { + throw new UnitConfigException("Ошибка конфигурирования юнита {0}: Количество юнитов для отката не должно быть меньше 1.", name); + } + return new RollBackCmd(this); + } + + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java new file mode 100644 index 0000000..545bf23 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/TeleportCmd.java @@ -0,0 +1,121 @@ +package dev.struchkov.godfather.context.domain.unit.cmd; + +import dev.struchkov.autoresponder.entity.KeyWord; +import dev.struchkov.godfather.context.domain.TypeUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.UnitActiveType; + +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Позволяет перенести пользователя в произвольное место в сценарии. + */ +public class TeleportCmd extends MainUnit { + + /** + * Название юнита, в которое необходимо осуществить перенос. + */ + private String unitNameToTeleport; + + private TeleportCmd(Builder builder) { + super( + builder.name, + builder.keyWords, + builder.phrase, + builder.pattern, + builder.matchThreshold, + builder.priority, + new HashSet<>(), + builder.activeType, + null, + TypeUnit.TELEPORT_CMD + ); + this.unitNameToTeleport = builder.unitNameToTeleport; + } + + public String getUnitNameToTeleport() { + return unitNameToTeleport; + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + 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 String unitNameToTeleport; + + private Builder() { + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder keyWords(Set val) { + keyWords.addAll(val); + return this; + } + + public Builder keyWord(KeyWord val) { + keyWords.add(val); + return this; + } + + public Builder stringKeyWords(Set val) { + keyWords.addAll(val.stream().map(KeyWord::of).collect(Collectors.toSet())); + return this; + } + + public Builder keyWord(String val) { + keyWords.add(KeyWord.of(val)); + return this; + } + + public Builder phrase(String val) { + phrase = val; + return this; + } + + public Builder pattern(Pattern val) { + pattern = val; + return this; + } + + public Builder matchThreshold(Integer val) { + matchThreshold = val; + return this; + } + + public Builder priority(Integer val) { + priority = val; + return this; + } + + public Builder activeType(UnitActiveType val) { + activeType = val; + return this; + } + + public Builder unitNameToTeleport(String val) { + unitNameToTeleport = val; + return this; + } + + public TeleportCmd build() { + return new TeleportCmd(this); + } + + } + +} 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 new file mode 100644 index 0000000..5263336 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/StorylineRepository.java @@ -0,0 +1,16 @@ +package dev.struchkov.godfather.context.repository; + +import dev.struchkov.godfather.context.domain.StorylineHistory; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + +public interface StorylineRepository { + + void save(@NotNull StorylineHistory storylineHistory); + + Optional findByCountLast(long personId, int countUnitsToBack); + + 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 new file mode 100644 index 0000000..8b09ae5 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java @@ -0,0 +1,47 @@ +package dev.struchkov.godfather.context.repository.impl.local; + +import dev.struchkov.godfather.context.domain.StorylineHistory; +import dev.struchkov.godfather.context.repository.StorylineRepository; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Stack; + +public class StorylineMapRepository implements StorylineRepository { + + private final Map> map = new HashMap<>(); + + @Override + public void save(@NotNull StorylineHistory history) { + final Long personId = history.getPersonId(); + map.computeIfAbsent(personId, k -> new Stack<>()).push(history); + } + + @Override + public Optional findByCountLast(long personId, int countUnitsToBack) { + if (map.containsKey(personId)) { + final Stack stack = map.get(personId); + if (stack.size() < countUnitsToBack) { + countUnitsToBack = stack.size(); + } + StorylineHistory storylineHistory = null; + for (int i = 0; i < countUnitsToBack; i++) { + storylineHistory = stack.pop(); + } + return Optional.ofNullable(storylineHistory); + } + return Optional.empty(); + } + + @Override + public void cleanHistoryByPersonId(@NotNull Long personId) { + if (map.containsKey(personId)) { + map.get(personId).clear(); + } else { + map.put(personId, new Stack<>()); + } + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/UnitPointLocalRepository.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/UnitPointLocalRepository.java index 88e9b0b..5a5d17a 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/UnitPointLocalRepository.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/UnitPointLocalRepository.java @@ -10,7 +10,7 @@ import java.util.Optional; public class UnitPointLocalRepository implements UnitPointerRepository { - public static final Map map = new HashMap<>(); + public final Map map = new HashMap<>(); @Override public UnitPointer save(@NotNull UnitPointer unitPointer) { diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/Accessibility.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/Accessibility.java similarity index 73% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/Accessibility.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/Accessibility.java index d801e6f..f76fc5c 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/Accessibility.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/Accessibility.java @@ -1,4 +1,4 @@ -package dev.struchkov.godfather.core.service; +package dev.struchkov.godfather.context.service; import dev.struchkov.godfather.context.domain.content.Message; diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/ClarificationQuestion.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/ClarificationQuestion.java similarity index 64% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/ClarificationQuestion.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/ClarificationQuestion.java index 6ab7122..18bc266 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/ClarificationQuestion.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/ClarificationQuestion.java @@ -1,7 +1,7 @@ -package dev.struchkov.godfather.core.service; +package dev.struchkov.godfather.context.service; +import dev.struchkov.godfather.context.domain.Clarification; import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.core.domain.Clarification; @FunctionalInterface public interface ClarificationQuestion { 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 new file mode 100644 index 0000000..5d016ef --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineService.java @@ -0,0 +1,35 @@ +package dev.struchkov.godfather.context.service; + +import dev.struchkov.godfather.context.domain.StorylineHistory; +import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.domain.unit.MainUnit; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; +import java.util.Set; + +public interface StorylineService { + + void save(@NotNull StorylineHistory storylineHistory); + + Optional getUnitNameByPersonId(@NotNull Long personId); + + Set getNextUnitByPersonId(@NotNull Long personId); + + void save(Long personId, String name, T message); + + Optional replaceUserToBack(long personId, int countUnitsToBack); + + Optional getDefaultUnit(); + + /** + * Ленивая (поздняя) связка юнитов между собой. Осуществляется уже после создания сценария. С помощью данного подхода можно реализовать циклические зависимости юнитов. Либо можно использовать {@link dev.struchkov.godfather.context.domain.unit.cmd.TeleportCmd} + * + * @param firstName + * @param secondName + */ + void lazyLink(String firstName, String secondName); + + Optional getUnitByName(String unitName); + +} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/CheckSave.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java similarity index 70% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/save/CheckSave.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java index c074d86..57a4b60 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/CheckSave.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/CheckSave.java @@ -1,7 +1,7 @@ -package dev.struchkov.godfather.core.service.save; +package dev.struchkov.godfather.context.service.save; import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; /** * TODO: Добавить описание интерфейса. diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/LocalPreservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java similarity index 87% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/save/LocalPreservable.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java index 23d2b45..99ab0f6 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/LocalPreservable.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/LocalPreservable.java @@ -1,4 +1,6 @@ -package dev.struchkov.godfather.core.service.save; +package dev.struchkov.godfather.context.service.save; + +import dev.struchkov.godfather.context.service.save.Preservable; import java.util.HashMap; import java.util.Map; diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/Preservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Preservable.java similarity index 90% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/save/Preservable.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Preservable.java index 2df8240..a5ed2d5 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/Preservable.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Preservable.java @@ -1,6 +1,6 @@ -package dev.struchkov.godfather.core.service.save; +package dev.struchkov.godfather.context.service.save; -import dev.struchkov.godfather.core.service.save.push.Pusher; +import dev.struchkov.godfather.context.service.save.Pusher; import java.util.Map; import java.util.Optional; diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableData.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java similarity index 75% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableData.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java index 8978dce..6c1ed99 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableData.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/PreservableData.java @@ -1,4 +1,4 @@ -package dev.struchkov.godfather.core.service.save.data; +package dev.struchkov.godfather.context.service.save; import dev.struchkov.godfather.context.domain.content.Message; diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/Pusher.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java similarity index 79% rename from bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/Pusher.java rename to bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java index 2002ef5..c4ff5b2 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/Pusher.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java @@ -1,4 +1,4 @@ -package dev.struchkov.godfather.core.service.save.push; +package dev.struchkov.godfather.context.service.save; import java.util.Map; diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 124c8e8..4ced09c 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.8 + 0.0.9 bot-core @@ -19,11 +19,6 @@ dev.struchkov.godfather bot-context - - - dev.struchkov - autoresponder - \ No newline at end of file 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 8115559..21ab4a6 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 @@ -1,27 +1,25 @@ package dev.struchkov.godfather.core; import dev.struchkov.autoresponder.Responder; -import dev.struchkov.autoresponder.entity.Unit; -import dev.struchkov.godfather.context.domain.UnitPointer; +import dev.struchkov.godfather.context.domain.TypeUnit; +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.UnitActiveType; import dev.struchkov.godfather.context.exception.ConfigAppException; +import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.Modifiable; import dev.struchkov.godfather.context.service.PersonSettingService; -import dev.struchkov.godfather.context.service.UnitPointerService; +import dev.struchkov.godfather.context.service.StorylineService; import dev.struchkov.godfather.context.service.sender.Sending; -import dev.struchkov.godfather.core.domain.unit.MainUnit; -import dev.struchkov.godfather.core.domain.unit.UnitActiveType; -import dev.struchkov.godfather.core.service.Accessibility; import dev.struchkov.godfather.core.service.ErrorHandler; import dev.struchkov.godfather.core.service.action.ActionUnit; import dev.struchkov.godfather.core.service.action.AnswerCheckAction; -import dev.struchkov.godfather.core.service.action.AnswerProcessingAction; import dev.struchkov.godfather.core.service.action.AnswerSaveAction; import dev.struchkov.godfather.core.service.action.AnswerTextAction; import dev.struchkov.godfather.core.service.action.AnswerTimerAction; import dev.struchkov.godfather.core.service.action.AnswerValidityAction; import dev.struchkov.godfather.core.service.timer.TimerService; -import dev.struchkov.godfather.core.utils.TypeUnit; import dev.struchkov.haiti.context.exception.NotFoundException; import java.util.HashMap; @@ -33,10 +31,10 @@ import java.util.stream.Collectors; public class GeneralAutoResponder { - private ErrorHandler errorHandler; private final PersonSettingService personSettingService; - private final UnitPointerService unitPointerService; - private final StoryLine storyLine; + + private ErrorHandler errorHandler; + private final StorylineService storyLineService; protected Map> actionUnitMap = new HashMap<>(); protected List> modifiable; @@ -44,18 +42,15 @@ public class GeneralAutoResponder { protected GeneralAutoResponder( Sending sending, PersonSettingService personSettingService, - UnitPointerService unitPointerService, - List unitConfigurations + StorylineService storyLineService ) { this.personSettingService = personSettingService; - this.unitPointerService = unitPointerService; - this.storyLine = new StorylineMaker(unitConfigurations).createStoryLine(); + this.storyLineService = storyLineService; init(sending); } private void init(Sending sending) { actionUnitMap.put(TypeUnit.CHECK, new AnswerCheckAction()); - actionUnitMap.put(TypeUnit.PROCESSING, new AnswerProcessingAction(sending)); actionUnitMap.put(TypeUnit.TEXT, new AnswerTextAction(sending)); actionUnitMap.put(TypeUnit.VALIDITY, new AnswerValidityAction()); } @@ -64,7 +59,7 @@ public class GeneralAutoResponder { this.modifiable = modifiable; } - public void initActionUnit(String typeUnit, ActionUnit actionUnit) { + public void initActionUnit(String typeUnit, ActionUnit actionUnit) { if (!actionUnitMap.containsKey(typeUnit)) { actionUnitMap.put(typeUnit, actionUnit); } else { @@ -113,16 +108,12 @@ public class GeneralAutoResponder { if (modifiable != null) { modifiable.forEach(m -> m.change(message)); } - final Set units = unitPointerService.getUnitNameByPersonId(message.getPersonId()) - .flatMap(storyLine::getUnit) - .map(Unit::getNextUnits) - .filter(mainUnits -> !mainUnits.isEmpty()) - .orElse(storyLine.getStartingUnits()); + final Set units = storyLineService.getNextUnitByPersonId(message.getPersonId()); final Optional optAnswer = Responder.nextUnit(message.getText(), units); if (optAnswer.isPresent()) { final MainUnit answer = optAnswer.get(); if (checkPermission(answer.getAccessibility(), message)) { - answer(message, answer); + answer(UnitRequest.of(answer, message)); } } } @@ -131,50 +122,51 @@ public class GeneralAutoResponder { return accessibility.isEmpty() || accessibility.get().check(message); } - public void answer(T message, MainUnit unitAnswer) { + public void answer(UnitRequest unitRequest) { try { - unitAnswer = getAction(message, unitAnswer); - unitAnswer = activeUnitAfter(unitAnswer, message); + unitRequest = getAction(unitRequest); + unitRequest = activeUnitAfter(unitRequest); - final Optional optDefaultUnit = storyLine.getDefaultUnit(); - if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unitAnswer)) { - unitPointerService.save(new UnitPointer(message.getPersonId(), unitAnswer.getName())); + final Optional optDefaultUnit = storyLineService.getDefaultUnit(); + final MainUnit unit = unitRequest.getUnit(); + final T message = unitRequest.getMessage(); + if (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit)) { + storyLineService.save(message.getPersonId(), unit.getName(), message); } } catch (Exception e) { if (errorHandler != null) { - errorHandler.handle(message, e); + errorHandler.handle(unitRequest.getMessage(), e); } else { throw e; } } } - private MainUnit activeUnitAfter(MainUnit mainUnit, T content) { - if (mainUnit.getNextUnits() != null) { - Optional first = mainUnit.getNextUnits().stream() + private UnitRequest activeUnitAfter(UnitRequest unitRequest) { + final Set nextUnits = unitRequest.getUnit().getNextUnits(); + if (nextUnits != null) { + Optional first = nextUnits.stream() .filter(unit -> UnitActiveType.AFTER.equals(unit.getActiveType())) .findFirst(); if (first.isPresent()) { - getAction(content, first.get()); - return activeUnitAfter(first.get(), content); + getAction(UnitRequest.of(first.get(), unitRequest.getMessage())); + return activeUnitAfter(UnitRequest.of(first.get(), unitRequest.getMessage())); } } - return mainUnit; + return unitRequest; } - private MainUnit getAction(T event, MainUnit unitAnswer) { - if (actionUnitMap.containsKey(unitAnswer.getType())) { - ActionUnit actionUnit = actionUnitMap.get(unitAnswer.getType()); - MainUnit mainUnit = actionUnit.action(unitAnswer, event); - return !unitAnswer.equals(mainUnit) ? getAction(event, mainUnit) : mainUnit; + private UnitRequest getAction(UnitRequest unitRequest) { + final MainUnit unit = unitRequest.getUnit(); + final String typeUnit = unit.getType(); + if (actionUnitMap.containsKey(typeUnit)) { + ActionUnit actionUnit = actionUnitMap.get(typeUnit); + UnitRequest newUnitRequest = actionUnit.action(unitRequest); + final MainUnit newUnit = newUnitRequest.getUnit(); + return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest; } else { - throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unitAnswer.getType()); + throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); } } - //TODO [22.06.2022]: Временное решение для ленивой инициализации - public void link(String firstName, String secondName) { - storyLine.link(firstName, secondName); - } - } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/StoryLine.java b/bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java similarity index 91% rename from bot-core/src/main/java/dev/struchkov/godfather/core/StoryLine.java rename to bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java index 39cc4ec..8c48962 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/StoryLine.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/Storyline.java @@ -1,6 +1,6 @@ package dev.struchkov.godfather.core; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.haiti.utils.Inspector; import org.jetbrains.annotations.NotNull; @@ -10,13 +10,13 @@ import java.util.Map; import java.util.Optional; import java.util.Set; -public class StoryLine { +public class Storyline { private MainUnit defaultUnit; private final Set startingUnits = new HashSet<>(); private final Map units = new HashMap<>(); - public StoryLine(Set startingUnits, Map units) { + public Storyline(Set startingUnits, Map units) { this.startingUnits.addAll(startingUnits); this.units.putAll(units); } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java b/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java index efb8827..d35afb8 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/StorylineMaker.java @@ -3,8 +3,8 @@ package dev.struchkov.godfather.core; import dev.struchkov.godfather.context.domain.UnitDefinition; import dev.struchkov.godfather.context.domain.annotation.Unit; import dev.struchkov.godfather.context.exception.UnitConfigException; -import dev.struchkov.godfather.core.domain.unit.LazyUnit; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.LazyUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.haiti.utils.Inspector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,13 +46,13 @@ public class StorylineMaker { return unitMap; } - public StoryLine createStoryLine() { + public Storyline createStoryLine() { generateUnitDefinitions(); try { createUnitMap(); createLazy(); final Set mainUnit = getMainUnit(); - return new StoryLine(mainUnit, unitMap); + return new Storyline(mainUnit, unitMap); } catch (IllegalAccessException | InvocationTargetException e) { log.error(e.getMessage(), e); } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/Timer.java b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/Timer.java index ca4ccce..bd08ada 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/Timer.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/Timer.java @@ -1,7 +1,7 @@ package dev.struchkov.godfather.core.domain; import dev.struchkov.godfather.context.service.usercode.CheckData; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import java.time.LocalDateTime; diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerProcessing.java b/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerProcessing.java deleted file mode 100644 index 4febdd2..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/domain/unit/AnswerProcessing.java +++ /dev/null @@ -1,146 +0,0 @@ -package dev.struchkov.godfather.core.domain.unit; - -import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.context.service.sender.Sending; -import dev.struchkov.godfather.context.service.usercode.ProcessingData; -import dev.struchkov.godfather.core.service.Accessibility; -import dev.struchkov.godfather.core.utils.TypeUnit; - -import java.util.HashSet; -import java.util.Set; -import java.util.regex.Pattern; - -/** - * Обработчик для кастомных реализаций. - * - * @author upagge [08/07/2019] - */ -public class AnswerProcessing extends MainUnit { - - /** - * Кастомная обработка. - */ - private final ProcessingData processingData; - - /** - * Объект для сквозной отправки ответа. - */ - private final Sending sending; - - private AnswerProcessing(Builder builder) { - super( - builder.name, - builder.keyWords, - builder.phrase, - builder.pattern, - builder.matchThreshold, - builder.priority, - builder.nextUnits, - builder.activeType, - builder.accessibility, - TypeUnit.PROCESSING - ); - processingData = builder.processingData; - sending = builder.sending; - } - - public static Builder builder() { - return new Builder<>(); - } - - public ProcessingData getProcessingData() { - return processingData; - } - - public Sending getSending() { - return sending; - } - - public static final class Builder { - private String name; - private Set keyWords = new HashSet<>(); - private String phrase; - private Pattern pattern; - private Integer matchThreshold; - private Integer priority; - private Set nextUnits = new HashSet<>(); - private ProcessingData processingData; - private Sending sending; - private UnitActiveType activeType; - private Accessibility accessibility; - - private Builder() { - } - - public Builder name(String name) { - this.name = name; - return this; - } - - public Builder processingData(ProcessingData val) { - processingData = val; - return this; - } - - public Builder sending(Sending val) { - sending = val; - return this; - } - - public Builder keyWords(Set val) { - keyWords = val; - return this; - } - - public Builder keyWord(String val) { - keyWords.add(val); - return this; - } - - public Builder phrase(String val) { - phrase = val; - return this; - } - - public Builder pattern(Pattern val) { - pattern = val; - return this; - } - - public Builder matchThreshold(Integer val) { - matchThreshold = val; - return this; - } - - public Builder priority(Integer val) { - priority = val; - return this; - } - - public Builder nextUnits(Set val) { - nextUnits = val; - return this; - } - - public Builder nextUnit(MainUnit val) { - nextUnits.add(val); - return this; - } - - public Builder accessibility(Accessibility val) { - accessibility = val; - return this; - } - - public Builder activeType(UnitActiveType val) { - activeType = val; - return this; - } - - public AnswerProcessing build() { - return new AnswerProcessing<>(this); - } - - } - -} 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 new file mode 100644 index 0000000..ab1571b --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineMailService.java @@ -0,0 +1,100 @@ +package dev.struchkov.godfather.core.service; + +import dev.struchkov.autoresponder.entity.Unit; +import dev.struchkov.godfather.context.domain.StorylineHistory; +import dev.struchkov.godfather.context.domain.UnitPointer; +import dev.struchkov.godfather.context.domain.content.Mail; +import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.repository.StorylineRepository; +import dev.struchkov.godfather.context.service.StorylineService; +import dev.struchkov.godfather.context.service.UnitPointerService; +import dev.struchkov.godfather.core.Storyline; +import dev.struchkov.godfather.core.StorylineMaker; +import dev.struchkov.haiti.utils.Inspector; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static dev.struchkov.haiti.utils.Inspector.isNotNull; + +/** + * Отвечает за работу со сценарием в личных сообщениях с пользователем. + */ +public class StorylineMailService implements StorylineService { + + private final UnitPointerService unitPointerService; + private final StorylineRepository storylineRepository; + private final Storyline storyLine; + + public StorylineMailService( + UnitPointerService unitPointerService, + StorylineRepository storylineRepository, + List unitConfigurations + ) { + this.storyLine = new StorylineMaker(unitConfigurations).createStoryLine(); + this.unitPointerService = unitPointerService; + this.storylineRepository = storylineRepository; + } + + @Override + public void save(@NotNull StorylineHistory storylineHistory) { + isNotNull(storylineHistory); + storylineHistory.setId(null); + storylineRepository.save(storylineHistory); + } + + @Override + public Optional getUnitNameByPersonId(@NotNull Long personId) { + Inspector.isNotNull(personId); + return unitPointerService.getUnitNameByPersonId(personId) + .flatMap(storyLine::getUnit); + } + + @Override + public Set getNextUnitByPersonId(@NotNull Long personId) { + final Optional> optMainUnits = getUnitNameByPersonId(personId) + .map(Unit::getNextUnits) + .filter(mainUnits -> !mainUnits.isEmpty()); + if (optMainUnits.isEmpty()) { + storylineRepository.cleanHistoryByPersonId(personId); + } + return optMainUnits + .orElse(storyLine.getStartingUnits()); + } + + @Override + public void save(Long personId, String unitName, Mail mail) { + Inspector.isNotNull(personId, unitName, mail); + unitPointerService.save(new UnitPointer(personId, unitName)); + + final StorylineHistory storylineHistory = new StorylineHistory(); + storylineHistory.setPersonId(personId); + storylineHistory.setUnitName(unitName); + storylineHistory.setMessage(mail); + storylineRepository.save(storylineHistory); + } + + @Override + public Optional replaceUserToBack(long personId, int countUnitsToBack) { + return storylineRepository.findByCountLast(personId, countUnitsToBack); + } + + @Override + public Optional getDefaultUnit() { + return storyLine.getDefaultUnit(); + } + + //TODO [22.06.2022]: Временное решение для ленивой инициализации + @Override + public void lazyLink(String firstName, String secondName) { + storyLine.link(firstName, secondName); + } + + @Override + public Optional getUnitByName(String unitName) { + return storyLine.getUnit(unitName); + } + +} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java index cc783f2..da726de 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/ActionUnit.java @@ -1,7 +1,8 @@ package dev.struchkov.godfather.core.service.action; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.MainUnit; /** * Интерфейс для обработки Unit-ов. @@ -14,10 +15,8 @@ public interface ActionUnit { /** * Метод обработки Unit-а. * - * @param unit Unit, который необходимо обработать - * @param content Запрос пользователя * @return Новый Unit, который может нуждаться в обработке */ - MainUnit action(M unit, C content); + UnitRequest action(UnitRequest unitRequest); } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java index 6757452..8e65ae1 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerCheckAction.java @@ -1,11 +1,13 @@ package dev.struchkov.godfather.core.service.action; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.core.domain.unit.AnswerCheck; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.AnswerCheck; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Objects; import java.util.Optional; /** @@ -18,16 +20,18 @@ public class AnswerCheckAction implements ActionUnit { private static final Logger log = LoggerFactory.getLogger(AnswerCheckAction.class); @Override - public MainUnit action(AnswerCheck answerCheck, Message mail) { + public UnitRequest action(UnitRequest unitRequest) { + final AnswerCheck unit = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); + MainUnit unitAnswer; - if (answerCheck.getCheck().checked(mail)) { + if (unit.getCheck().checked(message)) { log.info("Проверка пройдена"); - unitAnswer = answerCheck.getUnitTrue(); + unitAnswer = unit.getUnitTrue(); } else { log.info("Проверка не пройдена"); - unitAnswer = answerCheck.getUnitFalse(); + unitAnswer = unit.getUnitFalse(); } - return Optional.ofNullable(unitAnswer).orElse(answerCheck); + return UnitRequest.of(Objects.requireNonNullElse(unitAnswer, unit), message); } - } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerProcessingAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerProcessingAction.java deleted file mode 100644 index 08b09e2..0000000 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerProcessingAction.java +++ /dev/null @@ -1,36 +0,0 @@ -package dev.struchkov.godfather.core.service.action; - -import dev.struchkov.godfather.context.domain.BoxAnswer; -import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.context.service.sender.Sending; -import dev.struchkov.godfather.context.utils.Sender; -import dev.struchkov.godfather.core.domain.unit.AnswerProcessing; -import dev.struchkov.godfather.core.domain.unit.MainUnit; - -/** - * Обработчик Unit-а {@link AnswerProcessing}. - * - * @author upagge [11/07/2019] - */ -public class AnswerProcessingAction implements ActionUnit, Message> { - - private final Sending sending; - - public AnswerProcessingAction(Sending sending) { - this.sending = sending; - } - - @Override - public MainUnit action(AnswerProcessing answerProcessing, Message message) { - final BoxAnswer boxAnswer = answerProcessing.getProcessingData().processing(message); - - Sending answerProcessingSending = answerProcessing.getSending(); - if (answerProcessingSending != null) { - Sender.sends(message, boxAnswer, answerProcessingSending); - } else { - Sender.sends(message, boxAnswer, this.sending); - } - return answerProcessing; - } - -} 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 4f8aaa1..4a75d19 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 @@ -1,11 +1,12 @@ package dev.struchkov.godfather.core.service.action; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.core.domain.unit.AnswerSave; -import dev.struchkov.godfather.core.domain.unit.MainUnit; -import dev.struchkov.godfather.core.service.save.CheckSave; -import dev.struchkov.godfather.core.service.save.Preservable; -import dev.struchkov.godfather.core.service.save.data.PreservableData; +import dev.struchkov.godfather.context.domain.unit.AnswerSave; +import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.service.save.CheckSave; +import dev.struchkov.godfather.context.service.save.Preservable; +import dev.struchkov.godfather.context.service.save.PreservableData; /** * Обработчик Unit-а {@link AnswerSave}. @@ -15,27 +16,30 @@ import dev.struchkov.godfather.core.service.save.data.PreservableData; public class AnswerSaveAction implements ActionUnit, Message> { @Override - public MainUnit action(AnswerSave answerSave, Message mail) { - Preservable preservable = answerSave.getPreservable(); - Long personId = mail.getPersonId(); + public UnitRequest action(UnitRequest, Message> unitRequest) { + final AnswerSave answerSave = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); - CheckSave checkSave = answerSave.getCheckSave(); + final Preservable preservable = answerSave.getPreservable(); + final Long personId = message.getPersonId(); + + final CheckSave checkSave = answerSave.getCheckSave(); if (checkSave != null) { - MainUnit unit = checkSave.check(mail); + MainUnit unit = checkSave.check(message); if (unit != null) { - return unit; + return UnitRequest.of(unit, message); } } PreservableData preservableData = answerSave.getPreservableData(); if (preservableData != null) { - D data = preservableData.getData(mail); + D data = preservableData.getData(message); if (data != null) { preservable.save(personId, answerSave.getKey(), data); } } preservable.push(personId, answerSave.getPusher()); - return answerSave; + return UnitRequest.of(answerSave, message); } } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java index a94d01b..30eac31 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTextAction.java @@ -1,12 +1,13 @@ package dev.struchkov.godfather.core.service.action; import dev.struchkov.godfather.context.domain.BoxAnswer; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.domain.unit.AnswerText; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.context.utils.InsertWords; import dev.struchkov.godfather.context.utils.Sender; -import dev.struchkov.godfather.core.domain.unit.AnswerText; -import dev.struchkov.godfather.core.domain.unit.MainUnit; import java.util.List; @@ -24,18 +25,21 @@ public class AnswerTextAction implements ActionUnit, Message } @Override - public MainUnit action(AnswerText answerText, Message message) { - final BoxAnswer boxAnswer = answerText.getBoxAnswer().processing(message); - replaceMarkers(answerText, message, boxAnswer); + public UnitRequest action(UnitRequest, Message> unitRequest) { + final AnswerText unit = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); - final Sending answerTextSending = answerText.getSending(); + final BoxAnswer boxAnswer = unit.getBoxAnswer().processing(message); + replaceMarkers(unit, message, boxAnswer); + + final Sending answerTextSending = unit.getSending(); if (answerTextSending != null) { Sender.sends(message, boxAnswer, answerTextSending); } else { Sender.sends(message, boxAnswer, this.sending); } - return answerText; + return UnitRequest.of(unit, message); } private void replaceMarkers(AnswerText answerText, Message message, BoxAnswer boxAnswer) { @@ -46,5 +50,4 @@ public class AnswerTextAction implements ActionUnit, Message } } - } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java index f37fa7a..a80967a 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerTimerAction.java @@ -1,11 +1,12 @@ package dev.struchkov.godfather.core.service.action; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.exception.TimerSettingException; import dev.struchkov.godfather.core.GeneralAutoResponder; import dev.struchkov.godfather.core.domain.Timer; -import dev.struchkov.godfather.core.domain.unit.AnswerTimer; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.AnswerTimer; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.core.service.timer.TimerActionTask; import dev.struchkov.godfather.core.service.timer.TimerService; @@ -40,29 +41,32 @@ public class AnswerTimerAction implements ActionUnit { } @Override - public MainUnit action(AnswerTimer answerTimer, Message message) { - LocalDateTime timeActive = LocalDateTime + public UnitRequest action(UnitRequest unitRequest) { + final AnswerTimer unit = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); + + final LocalDateTime timeActive = LocalDateTime .now(Clock.tickSeconds(ZoneId.systemDefault())) .plusSeconds(Optional - .ofNullable(answerTimer.getTimeDelaySec()) + .ofNullable(unit.getTimeDelaySec()) .orElseThrow(() -> new TimerSettingException("Не установлена временная задержка таймера"))); - Timer.Builder timer = Timer.builder() + final Timer.Builder timer = Timer.builder() .personId(message.getPersonId()) - .unitAnswer(answerTimer.getUnitAnswer()) + .unitAnswer(unit.getUnitAnswer()) .timeActive(timeActive) - .periodSec(answerTimer.getTimeDelaySec()) - .checkLoop(answerTimer.getCheckLoop()); + .periodSec(unit.getTimeDelaySec()) + .checkLoop(unit.getCheckLoop()); - if (answerTimer.getTimeDeathSec() != null) { + if (unit.getTimeDeathSec() != null) { timer.timeDeath(LocalDateTime .now(Clock.tickSeconds(ZoneId.systemDefault())) - .plusSeconds(answerTimer.getTimeDeathSec())); - } else if (answerTimer.getCheckLoop() == null) { + .plusSeconds(unit.getTimeDeathSec())); + } else if (unit.getCheckLoop() == null) { timer.timeDeath(timeActive); } timerService.add(timer.build()); - return answerTimer; + return UnitRequest.of(unit, message); } } diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java index 01b7e99..3f4f5c4 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/AnswerValidityAction.java @@ -1,10 +1,11 @@ package dev.struchkov.godfather.core.service.action; +import dev.struchkov.godfather.context.domain.Clarification; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; -import dev.struchkov.godfather.core.domain.Clarification; -import dev.struchkov.godfather.core.domain.unit.AnswerText; -import dev.struchkov.godfather.core.domain.unit.AnswerValidity; -import dev.struchkov.godfather.core.domain.unit.MainUnit; +import dev.struchkov.godfather.context.domain.unit.AnswerText; +import dev.struchkov.godfather.context.domain.unit.AnswerValidity; +import dev.struchkov.godfather.context.domain.unit.MainUnit; import java.util.Set; @@ -20,31 +21,37 @@ public class AnswerValidityAction implements ActionUnit public static final Set WORDS_YES_NO = Set.of("да", "ага", "нет", "неа"); @Override - public MainUnit action(AnswerValidity unit, Message content) { + public UnitRequest action(UnitRequest unitRequest) { + final AnswerValidity unit = unitRequest.getUnit(); + final Message content = unitRequest.getMessage(); + String message = content.getText(); Long personId = content.getPersonId(); if (WORDS_YES.contains(message.toLowerCase())) { unit.getTempSave().getByKey(personId, "temp").ifPresent(content::setText); - return unit.getUnitYes(); + return UnitRequest.of(unit.getUnitYes(), content); } else if (WORDS_NO.contains(message.toLowerCase())) { unit.getTempSave().getByKey(personId, "temp").ifPresent(content::setText); - return unit.getUnitNo(); + return UnitRequest.of(unit.getUnitNo(), content); } else { Clarification clarification = unit.getClarificationQuestion().getClarification(content); final String value = clarification.getValue(); if (value == null) { - return unit.getUnitNull(); + return UnitRequest.of(unit.getUnitNull(), content); } else { unit.getTempSave().save(personId, "temp", value); AnswerValidity newValidity = unit.builder() - .clearKeyWords().keyWords(WORDS_YES_NO) - .build(); - return AnswerText.builder() - .message(mes -> clarification.getQuestion()) - .nextUnit(newValidity) + .clearKeyWords() + .stringKeyWords(WORDS_YES_NO) .build(); + return UnitRequest.of( + AnswerText.builder() + .message(mes -> clarification.getQuestion()) + .nextUnit(newValidity) + .build(), + content + ); } } } - } 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 new file mode 100644 index 0000000..2f25189 --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/BackCmdAction.java @@ -0,0 +1,38 @@ +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/TeleportCmdAction.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/TeleportCmdAction.java new file mode 100644 index 0000000..16b1745 --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/action/cmd/TeleportCmdAction.java @@ -0,0 +1,30 @@ +package dev.struchkov.godfather.core.service.action.cmd; + +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.TeleportCmd; +import dev.struchkov.godfather.context.service.StorylineService; +import dev.struchkov.godfather.core.service.action.ActionUnit; + +import java.util.Optional; + +public class TeleportCmdAction implements ActionUnit { + + private final StorylineService storyLineService; + + public TeleportCmdAction(StorylineService storyLineService) { + this.storyLineService = storyLineService; + } + + @Override + public UnitRequest action(UnitRequest unitRequest) { + final TeleportCmd unit = unitRequest.getUnit(); + final T message = unitRequest.getMessage(); + final Optional optNextUnit = storyLineService.getUnitByName(unit.getUnitNameToTeleport()); + return optNextUnit + .map(mainUnit -> UnitRequest.of(mainUnit, message)) + .orElseGet(() -> UnitRequest.of(unit, message)); + } + +} diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableDataSimple.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableDataSimple.java index e11b026..b857e8e 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableDataSimple.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/data/PreservableDataSimple.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.core.service.save.data; import dev.struchkov.godfather.context.domain.content.Message; +import dev.struchkov.godfather.context.service.save.PreservableData; import dev.struchkov.haiti.utils.Pair; /** diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/UserSanderPusher.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/UserSanderPusher.java index 6e45953..9a79034 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/UserSanderPusher.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/save/push/UserSanderPusher.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.core.service.save.push; import dev.struchkov.godfather.context.domain.BoxAnswer; +import dev.struchkov.godfather.context.service.save.Pusher; import dev.struchkov.godfather.context.service.sender.Sending; import java.util.Map; diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/timer/TimerActionTask.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/timer/TimerActionTask.java index a8760ad..c3ac230 100644 --- a/bot-core/src/main/java/dev/struchkov/godfather/core/service/timer/TimerActionTask.java +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/timer/TimerActionTask.java @@ -1,5 +1,6 @@ package dev.struchkov.godfather.core.service.timer; +import dev.struchkov.godfather.context.domain.UnitRequest; import dev.struchkov.godfather.context.domain.content.Message; import dev.struchkov.godfather.context.service.usercode.CheckData; import dev.struchkov.godfather.context.utils.MessageUtils; @@ -50,17 +51,17 @@ public class TimerActionTask extends TimerTask { if (!timeDeath(nowDate, timer.getTimeDeath())) { if (checkLoop != null) { if (checkLoop.checked(emptyMessage)) { - generalAutoresponder.answer(emptyMessage, timer.getUnitAnswer()); + generalAutoresponder.answer(UnitRequest.of(timer.getUnitAnswer(), emptyMessage)); timerService.remove(timer.getId()); } else { reinstallation(timer); } } else { - generalAutoresponder.answer(emptyMessage, timer.getUnitAnswer()); + generalAutoresponder.answer(UnitRequest.of(timer.getUnitAnswer(), emptyMessage)); reinstallation(timer); } } else { - generalAutoresponder.answer(emptyMessage, timer.getUnitAnswer()); + generalAutoresponder.answer(UnitRequest.of(timer.getUnitAnswer(), emptyMessage)); death(timer, emptyMessage); } } @@ -74,7 +75,7 @@ public class TimerActionTask extends TimerTask { private void death(Timer timer, Message emptyMessage) { if (timer.getUnitDeath() != null) { - generalAutoresponder.answer(emptyMessage, timer.getUnitDeath()); + generalAutoresponder.answer(UnitRequest.of(timer.getUnitDeath(), emptyMessage)); } timerService.remove(timer.getId()); } 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 93ad6e9..a52746b 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,8 +9,8 @@ //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.core.service.save.Preservable; -//import dev.struchkov.godfather.core.service.save.push.Pusher; +//import dev.struchkov.godfather.context.service.save.Preservable; +//import dev.struchkov.godfather.context.service.save.Pusher; // //import java.util.List; //import java.util.Optional; diff --git a/pom.xml b/pom.xml index 34ab1fb..ff753b1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.8 + 0.0.9 pom @@ -32,12 +32,12 @@ UTF-8 UTF-8 - 0.0.8 + 0.0.9 ${godfather.ver} ${godfather.ver} - 3.0.0 + 3.1.0 1.0.2 2.2