From eaeb22051532e8d4a4f04ad51a126f12c145ff01 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Thu, 21 Jul 2022 00:50:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=BE=D0=BB=D1=8C=D1=88=D0=BE=D0=B5=20?= =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot-context/pom.xml | 2 +- .../godfather/context/domain/BoxAnswer.java | 24 +++++++- .../godfather/context/domain/ContextKey.java | 23 ++++++++ .../context/domain/unit/AnswerCheck.java | 8 +++ .../context/domain/unit/AnswerSave.java | 15 ++++- .../context/domain/unit/AnswerText.java | 36 +++++------- .../context/domain/unit/AnswerTimer.java | 8 +++ .../context/domain/unit/AnswerValidity.java | 12 +++- .../context/domain/unit/MainUnit.java | 4 +- .../context/domain/unit/cmd/ReplaceCmd.java | 10 +++- .../context/domain/unit/cmd/RollBackCmd.java | 10 +++- .../context/domain/unit/cmd/TeleportCmd.java | 8 +++ .../impl/local/StorylineMapRepository.java | 8 ++- .../preser/AnswerSaveMapPreservable.java | 2 +- .../context/service/StorylineContext.java | 23 ++++++++ .../context/service/save/Pusher.java | 2 +- .../service/usercode/MessageFunction.java | 10 ---- bot-core/pom.xml | 2 +- .../core/service/StorylineContextMapImpl.java | 55 +++++++++++++++++++ .../core/service/action/AnswerSaveAction.java | 18 ++++-- .../service/save/push/UserSanderPusher.java | 2 +- pom.xml | 8 +-- 22 files changed, 234 insertions(+), 56 deletions(-) create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/domain/ContextKey.java create mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineContext.java delete mode 100644 bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java create mode 100644 bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineContextMapImpl.java diff --git a/bot-context/pom.xml b/bot-context/pom.xml index b1e62d2..0cc1745 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.14 + 0.0.17 bot-context diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java index 426c5c5..5392893 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/BoxAnswer.java @@ -30,18 +30,38 @@ public class BoxAnswer { replace = builder.replace; } + public static BoxAnswer boxAnswer(boolean replace, String message) { + return BoxAnswer.builder().replace(replace).message(message).build(); + } + + public static BoxAnswer boxAnswer(boolean replace, String messageText, KeyBoard keyBoard) { + return BoxAnswer.builder().replace(replace).message(messageText).keyBoard(keyBoard).build(); + } + public static BoxAnswer boxAnswer(String message) { - return BoxAnswer.builder().message(message).build(); + return boxAnswer(false, message); } public static BoxAnswer boxAnswer(String messageText, KeyBoard keyBoard) { - return BoxAnswer.builder().message(messageText).keyBoard(keyBoard).build(); + return boxAnswer(false, messageText, keyBoard); + } + + public static BoxAnswer replaceBoxAnswer(String message) { + return boxAnswer(true, message); + } + + public static BoxAnswer replaceBoxAnswer(String messageText, KeyBoard keyBoard) { + return boxAnswer(true, messageText, keyBoard); } public static Builder builder() { return new Builder(); } + public static Builder replaceBuilder() { + return new Builder().replace(true); + } + public String getMessage() { return message; } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/ContextKey.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/ContextKey.java new file mode 100644 index 0000000..e6ac5cf --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/ContextKey.java @@ -0,0 +1,23 @@ +package dev.struchkov.godfather.context.domain; + +public interface ContextKey { + + String getValue(); + + Class getType(); + + static ContextKey of(String value, Class type) { + return new ContextKey<>() { + @Override + public String getValue() { + return value; + } + + @Override + public Class getType() { + return type; + } + }; + } + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java index 761eed0..7ea377a 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerCheck.java @@ -9,6 +9,7 @@ import dev.struchkov.godfather.context.service.usercode.CheckData; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -43,6 +44,7 @@ public class AnswerCheck extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -77,6 +79,7 @@ public class AnswerCheck extends MainUnit { private String name; private final Set keyWords = new HashSet<>(); private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private Pattern pattern; private Integer matchThreshold; private Integer priority; @@ -130,6 +133,11 @@ public class AnswerCheck extends MainUnit { return this; } + private Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java index 9155f49..83b776b 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerSave.java @@ -12,9 +12,11 @@ import dev.struchkov.godfather.context.service.save.Pusher; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static dev.struchkov.haiti.utils.Checker.checkNull; import static dev.struchkov.haiti.utils.Inspector.isNotNull; /** @@ -56,6 +58,7 @@ public class AnswerSave extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -112,6 +115,7 @@ public class AnswerSave extends MainUnit { private String name; private final Set keyWords = new HashSet<>(); private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private Pattern pattern; private Integer matchThreshold; private Integer priority; @@ -168,6 +172,11 @@ public class AnswerSave extends MainUnit { return this; } + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; @@ -230,8 +239,10 @@ public class AnswerSave extends MainUnit { public AnswerSave build() { isNotNull(preservable, "Не указан репозиторий для сохранения формы пользователя"); - isNotNull(preservableData, "Не указаны данные для сохранения"); - isNotNull(key, "Не указан ключ для сохранения"); + if (checkNull(pusher)) { + isNotNull(preservableData, "Не указаны данные для сохранения"); + isNotNull(key, "Не указан ключ для сохранения"); + } return new AnswerSave<>(this); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java index d491c10..2501d27 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerText.java @@ -8,13 +8,12 @@ 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 java.util.Collection; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -47,6 +46,7 @@ public class AnswerText extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -65,6 +65,10 @@ public class AnswerText extends MainUnit { return AnswerText.builder().boxAnswer(BoxAnswer.boxAnswer(message)).build(); } + public static AnswerText of(BoxAnswer boxAnswer) { + return AnswerText.builder().boxAnswer(boxAnswer).build(); + } + public static Builder builder() { return new Builder<>(); } @@ -84,6 +88,7 @@ public class AnswerText extends MainUnit { public static final class Builder { private final Set keyWords = new HashSet<>(); private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private String name; private ProcessingData boxAnswer; private Insert insert; @@ -109,22 +114,6 @@ public class AnswerText extends MainUnit { return this; } - public Builder message(MessageFunction function) { - this.boxAnswer = message -> { - final BoxAnswer.Builder builder = BoxAnswer.builder(); - function.build(message, builder); - return builder.build(); - }; - return this; - } - - public Builder boxAnswer(Consumer boxAnswer) { - final BoxAnswer.Builder boxAnswerBuilder = BoxAnswer.builder(); - boxAnswer.accept(boxAnswerBuilder); - this.boxAnswer = message -> boxAnswerBuilder.build(); - return this; - } - public Builder boxAnswer(BoxAnswer boxAnswer) { this.boxAnswer = message -> boxAnswer; return this; @@ -165,8 +154,13 @@ public class AnswerText extends MainUnit { return this; } - public Builder phrases(Collection val) { - phrases.addAll(val); + public Builder phrases(String... val) { + phrases.addAll(Arrays.asList(val)); + return this; + } + + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; return this; } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java index ef51233..ae9ed31 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerTimer.java @@ -9,6 +9,7 @@ import dev.struchkov.godfather.context.service.usercode.CheckData; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -47,6 +48,7 @@ public class AnswerTimer extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -90,6 +92,7 @@ public class AnswerTimer extends MainUnit { private CheckData checkLoop; private final Set keyWords = new HashSet<>(); private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private Pattern pattern; private Integer matchThreshold; private Integer priority; @@ -160,6 +163,11 @@ public class AnswerTimer extends MainUnit { return this; } + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java index c57bf6d..dfd3d27 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/AnswerValidity.java @@ -2,14 +2,15 @@ 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.service.Accessibility; -import dev.struchkov.godfather.context.service.ClarificationQuestion; import dev.struchkov.godfather.context.repository.preser.AnswerSaveMapPreservable; import dev.struchkov.godfather.context.repository.preser.Preservable; +import dev.struchkov.godfather.context.service.Accessibility; +import dev.struchkov.godfather.context.service.ClarificationQuestion; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -44,6 +45,7 @@ public class AnswerValidity extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -86,6 +88,7 @@ public class AnswerValidity extends MainUnit { public static final class Builder { private final Set keyWords = new HashSet<>(); private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private String name; private MainUnit unitYes; private MainUnit unitNo; @@ -161,6 +164,11 @@ public class AnswerValidity extends MainUnit { return this; } + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java index 8ed3ad3..91e7cbc 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/MainUnit.java @@ -8,6 +8,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.UUID; +import java.util.function.Predicate; import java.util.regex.Pattern; /** @@ -48,6 +49,7 @@ public abstract class MainUnit extends Unit { String name, Set keyWords, Set phrases, + Predicate triggerCheck, Pattern pattern, Integer matchThreshold, Integer priority, @@ -57,7 +59,7 @@ public abstract class MainUnit extends Unit { Accessibility accessibility, String type ) { - super(keyWords, phrases, pattern, matchThreshold, priority, nextUnits); + super(keyWords, phrases, triggerCheck, pattern, matchThreshold, priority, nextUnits); this.name = name; this.activeType = Optional.ofNullable(activeType).orElse(UnitActiveType.DEFAULT); this.accessibility = accessibility; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java index f9aed0a..1259290 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/ReplaceCmd.java @@ -8,6 +8,7 @@ import dev.struchkov.godfather.context.domain.unit.UnitActiveType; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -20,6 +21,7 @@ public class ReplaceCmd extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -43,7 +45,8 @@ public class ReplaceCmd extends MainUnit { public static final class Builder { private final Set keyWords = new HashSet<>(); private String name; - private Set phrases = new HashSet<>(); + private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private Pattern pattern; private Integer matchThreshold; private Integer priority; @@ -94,6 +97,11 @@ public class ReplaceCmd extends MainUnit { return this; } + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java index 4748172..6d740b6 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/unit/cmd/RollBackCmd.java @@ -9,6 +9,7 @@ import dev.struchkov.godfather.context.exception.UnitConfigException; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -32,6 +33,7 @@ public class RollBackCmd extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -80,7 +82,8 @@ public class RollBackCmd extends MainUnit { public static final class Builder { private final Set keyWords = new HashSet<>(); private String name; - private Set phrases = new HashSet<>(); + private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private Pattern pattern; private Integer matchThreshold; private Integer priority; @@ -121,6 +124,11 @@ public class RollBackCmd extends MainUnit { return this; } + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder phrases(Collection val) { phrases.addAll(val); return 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 index 5b9a879..fd0fe3a 100644 --- 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 @@ -8,6 +8,7 @@ import dev.struchkov.godfather.context.domain.unit.UnitActiveType; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -26,6 +27,7 @@ public class TeleportCmd extends MainUnit { builder.name, builder.keyWords, builder.phrases, + builder.triggerCheck, builder.pattern, builder.matchThreshold, builder.priority, @@ -49,6 +51,7 @@ public class TeleportCmd extends MainUnit { public static final class Builder { private final Set keyWords = new HashSet<>(); private final Set phrases = new HashSet<>(); + private Predicate triggerCheck; private String name; private Pattern pattern; private Integer matchThreshold; @@ -99,6 +102,11 @@ public class TeleportCmd extends MainUnit { return this; } + public Builder triggerCheck(Predicate trigger) { + triggerCheck = trigger; + return this; + } + public Builder matchThreshold(Integer val) { matchThreshold = val; return this; diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java index 8044296..f3355a6 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/impl/local/StorylineMapRepository.java @@ -5,18 +5,22 @@ import dev.struchkov.godfather.context.repository.StorylineRepository; import org.jetbrains.annotations.NotNull; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.Stack; public class StorylineMapRepository implements StorylineRepository { private final Map> map = new HashMap<>(); + private final Map> historyUnitName = new HashMap<>(); @Override public void save(@NotNull StorylineHistory history) { final Long personId = history.getPersonId(); map.computeIfAbsent(personId, k -> new Stack<>()).push(history); + historyUnitName.computeIfAbsent(personId, k -> new HashSet<>()).add(history.getUnitName()); } @Override @@ -29,6 +33,7 @@ public class StorylineMapRepository implements StorylineRepository { StorylineHistory storylineHistory = null; for (int i = 0; i < countUnitsToBack; i++) { storylineHistory = stack.pop(); + historyUnitName.get(personId).remove(storylineHistory.getUnitName()); } return Optional.ofNullable(storylineHistory); } @@ -40,8 +45,9 @@ public class StorylineMapRepository implements StorylineRepository { if (map.containsKey(personId)) { final Stack stack = map.get(personId); StorylineHistory storylineHistory; - while (!stack.isEmpty()) { + while (!stack.isEmpty() && historyUnitName.get(personId).contains(unitName)) { storylineHistory = stack.pop(); + historyUnitName.get(personId).remove(storylineHistory.getUnitName()); if (unitName.equals(storylineHistory.getUnitName())) { return Optional.of(storylineHistory); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java index 086d6fb..a17effd 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/repository/preser/AnswerSaveMapPreservable.java @@ -31,7 +31,7 @@ public class AnswerSaveMapPreservable implements AnswerSavePreservable { @Override public void push(Long personId, Pusher pusher) { - Optional.ofNullable(pusher).ifPresent(sPusher -> sPusher.push(getAllSaveElement(personId))); + Optional.ofNullable(pusher).ifPresent(sPusher -> sPusher.push(personId, getAllSaveElement(personId))); } } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineContext.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineContext.java new file mode 100644 index 0000000..13c2623 --- /dev/null +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/StorylineContext.java @@ -0,0 +1,23 @@ +package dev.struchkov.godfather.context.service; + +import dev.struchkov.godfather.context.domain.ContextKey; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.Optional; + +public interface StorylineContext { + + void save(@NotNull Long telegramId, @NotNull ContextKey key, Object objectForSave); + + Optional getByKey(@NotNull Long telegramId, @NotNull ContextKey key); + + T getByKeyOrThrow(@NotNull Long telegramId, @NotNull ContextKey key); + + Map getAllSaveElement(@NotNull Long telegramId); + + void removeAll(@NotNull Long telegramId); + + void removeByKey(@NotNull Long telegramId, @NotNull ContextKey key); + +} diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java index c4ff5b2..fcc9781 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/service/save/Pusher.java @@ -10,6 +10,6 @@ import java.util.Map; @FunctionalInterface public interface Pusher { - void push(Map saveElement); + void push(Long personId, Map saveElement); } diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java b/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java deleted file mode 100644 index 2508b6a..0000000 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/service/usercode/MessageFunction.java +++ /dev/null @@ -1,10 +0,0 @@ -package dev.struchkov.godfather.context.service.usercode; - -import dev.struchkov.godfather.context.domain.BoxAnswer; -import dev.struchkov.godfather.context.domain.content.Message; - -public interface MessageFunction { - - void build(M message, BoxAnswer.Builder builder); - -} diff --git a/bot-core/pom.xml b/bot-core/pom.xml index e1d5fda..60ff90a 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.14 + 0.0.17 bot-core diff --git a/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineContextMapImpl.java b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineContextMapImpl.java new file mode 100644 index 0000000..b28c807 --- /dev/null +++ b/bot-core/src/main/java/dev/struchkov/godfather/core/service/StorylineContextMapImpl.java @@ -0,0 +1,55 @@ +package dev.struchkov.godfather.core.service; + +import dev.struchkov.godfather.context.domain.ContextKey; +import dev.struchkov.godfather.context.service.StorylineContext; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException; +import static dev.struchkov.haiti.utils.Inspector.isNotNull; + +public class StorylineContextMapImpl implements StorylineContext { + + private final Map> map = new HashMap<>(); + + public void save(@NotNull Long personId, @NotNull ContextKey key, Object objectForSave) { + isNotNull(personId, key); + map.computeIfAbsent(personId, k -> new HashMap<>()).put(key.getValue(), objectForSave); + } + + public Optional getByKey(@NotNull Long personId, @NotNull ContextKey key) { + isNotNull(personId, key); + if (map.containsKey(personId)) { + final Map storage = map.get(personId); + final T object = (T) storage.get(key.getValue()); + if (object != null && object.getClass().equals(key.getType())) { + return Optional.of(object); + } + } + return Optional.empty(); + } + + @Override + public T getByKeyOrThrow(@NotNull Long personId, @NotNull ContextKey key) { + return getByKey(personId, key).orElseThrow(notFoundException("Не найдено значение ключа {0}, для пользователя {1}", key.getValue(), personId)); + } + + public Map getAllSaveElement(@NotNull Long personId) { + isNotNull(personId); + return map.get(personId); + } + + public void removeAll(@NotNull Long personId) { + isNotNull(personId); + map.remove(personId); + } + + public void removeByKey(@NotNull Long personId, @NotNull ContextKey key) { + isNotNull(personId, key); + map.computeIfAbsent(personId, k -> new HashMap<>()).remove(key.getValue()); + } + +} 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 059f09b..21d5aed 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 @@ -7,6 +7,9 @@ import dev.struchkov.godfather.context.domain.unit.MainUnit; import dev.struchkov.godfather.context.repository.preser.AnswerSavePreservable; import dev.struchkov.godfather.context.service.save.CheckSave; import dev.struchkov.godfather.context.service.save.PreservableData; +import dev.struchkov.godfather.context.service.save.Pusher; + +import static dev.struchkov.haiti.utils.Checker.checkNotNull; /** * Обработчик Unit-а {@link AnswerSave}. @@ -24,22 +27,25 @@ public class AnswerSaveAction implements ActionUnit, Message> { final Long personId = message.getPersonId(); final CheckSave checkSave = answerSave.getCheckSave(); - if (checkSave != null) { + if (checkNotNull(checkSave)) { MainUnit unit = checkSave.check(message); - if (unit != null) { + if (checkNotNull(unit)) { return UnitRequest.of(unit, message); } } - PreservableData preservableData = answerSave.getPreservableData(); - if (preservableData != null) { + final PreservableData preservableData = answerSave.getPreservableData(); + if (checkNotNull(preservableData)) { D data = preservableData.getData(message); - if (data != null) { + if (checkNotNull(data)) { preservable.save(personId, answerSave.getKey(), data); } } - preservable.push(personId, answerSave.getPusher()); + final Pusher pusher = answerSave.getPusher(); + if (checkNotNull(pusher)) { + preservable.push(personId, pusher); + } return UnitRequest.of(answerSave, message); } } 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 9a79034..472c053 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 @@ -19,7 +19,7 @@ public class UserSanderPusher implements Pusher { } @Override - public void push(Map saveElement) { + public void push(Long personId, Map saveElement) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("========= ").append(nameForm).append(" =========\n"); saveElement.forEach((key, value) -> stringBuilder.append(key).append(": ").append(value).append("\n")); diff --git a/pom.xml b/pom.xml index c400431..d56854e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.14 + 0.0.17 pom @@ -32,13 +32,13 @@ UTF-8 UTF-8 - 0.0.14 + 0.0.17 ${godfather.ver} ${godfather.ver} - 3.2.0 - 1.0.2 + 3.3.0 + 1.0.3 2.2 2.0.1.Final