Большое улучшение функциональности

This commit is contained in:
Struchkov Mark 2022-07-21 00:50:14 +03:00
parent 87c565f4d2
commit eaeb220515
22 changed files with 234 additions and 56 deletions

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.14</version> <version>0.0.17</version>
</parent> </parent>
<artifactId>bot-context</artifactId> <artifactId>bot-context</artifactId>

View File

@ -30,18 +30,38 @@ public class BoxAnswer {
replace = builder.replace; 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) { public static BoxAnswer boxAnswer(String message) {
return BoxAnswer.builder().message(message).build(); return boxAnswer(false, message);
} }
public static BoxAnswer boxAnswer(String messageText, KeyBoard keyBoard) { 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() { public static Builder builder() {
return new Builder(); return new Builder();
} }
public static Builder replaceBuilder() {
return new Builder().replace(true);
}
public String getMessage() { public String getMessage() {
return message; return message;
} }

View File

@ -0,0 +1,23 @@
package dev.struchkov.godfather.context.domain;
public interface ContextKey<T> {
String getValue();
Class<T> getType();
static <T> ContextKey<T> of(String value, Class<T> type) {
return new ContextKey<>() {
@Override
public String getValue() {
return value;
}
@Override
public Class<T> getType() {
return type;
}
};
}
}

View File

@ -9,6 +9,7 @@ import dev.struchkov.godfather.context.service.usercode.CheckData;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -43,6 +44,7 @@ public class AnswerCheck extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -77,6 +79,7 @@ public class AnswerCheck extends MainUnit {
private String name; private String name;
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
private Integer priority; private Integer priority;
@ -130,6 +133,11 @@ public class AnswerCheck extends MainUnit {
return this; return this;
} }
private Builder triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder matchThreshold(Integer val) { public Builder matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -12,9 +12,11 @@ import dev.struchkov.godfather.context.service.save.Pusher;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static dev.struchkov.haiti.utils.Checker.checkNull;
import static dev.struchkov.haiti.utils.Inspector.isNotNull; import static dev.struchkov.haiti.utils.Inspector.isNotNull;
/** /**
@ -56,6 +58,7 @@ public class AnswerSave<D> extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -112,6 +115,7 @@ public class AnswerSave<D> extends MainUnit {
private String name; private String name;
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
private Integer priority; private Integer priority;
@ -168,6 +172,11 @@ public class AnswerSave<D> extends MainUnit {
return this; return this;
} }
public Builder<D> triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder<D> matchThreshold(Integer val) { public Builder<D> matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;
@ -230,8 +239,10 @@ public class AnswerSave<D> extends MainUnit {
public AnswerSave<D> build() { public AnswerSave<D> build() {
isNotNull(preservable, "Не указан репозиторий для сохранения формы пользователя"); isNotNull(preservable, "Не указан репозиторий для сохранения формы пользователя");
isNotNull(preservableData, "Не указаны данные для сохранения"); if (checkNull(pusher)) {
isNotNull(key, "Не указан ключ для сохранения"); isNotNull(preservableData, "Не указаны данные для сохранения");
isNotNull(key, "Не указан ключ для сохранения");
}
return new AnswerSave<>(this); return new AnswerSave<>(this);
} }

View File

@ -8,13 +8,12 @@ import dev.struchkov.godfather.context.exception.UnitConfigException;
import dev.struchkov.godfather.context.service.Accessibility; import dev.struchkov.godfather.context.service.Accessibility;
import dev.struchkov.godfather.context.service.sender.Sending; import dev.struchkov.godfather.context.service.sender.Sending;
import dev.struchkov.godfather.context.service.usercode.Insert; 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.context.service.usercode.ProcessingData;
import java.util.Collection; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -47,6 +46,7 @@ public class AnswerText<M extends Message> extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -65,6 +65,10 @@ public class AnswerText<M extends Message> extends MainUnit {
return AnswerText.<M>builder().boxAnswer(BoxAnswer.boxAnswer(message)).build(); return AnswerText.<M>builder().boxAnswer(BoxAnswer.boxAnswer(message)).build();
} }
public static <M extends Message> AnswerText<M> of(BoxAnswer boxAnswer) {
return AnswerText.<M>builder().boxAnswer(boxAnswer).build();
}
public static <M extends Message> Builder<M> builder() { public static <M extends Message> Builder<M> builder() {
return new Builder<>(); return new Builder<>();
} }
@ -84,6 +88,7 @@ public class AnswerText<M extends Message> extends MainUnit {
public static final class Builder<M extends Message> { public static final class Builder<M extends Message> {
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private String name; private String name;
private ProcessingData<M> boxAnswer; private ProcessingData<M> boxAnswer;
private Insert insert; private Insert insert;
@ -109,22 +114,6 @@ public class AnswerText<M extends Message> extends MainUnit {
return this; return this;
} }
public Builder<M> message(MessageFunction<M> function) {
this.boxAnswer = message -> {
final BoxAnswer.Builder builder = BoxAnswer.builder();
function.build(message, builder);
return builder.build();
};
return this;
}
public Builder<M> boxAnswer(Consumer<BoxAnswer.Builder> boxAnswer) {
final BoxAnswer.Builder boxAnswerBuilder = BoxAnswer.builder();
boxAnswer.accept(boxAnswerBuilder);
this.boxAnswer = message -> boxAnswerBuilder.build();
return this;
}
public Builder<M> boxAnswer(BoxAnswer boxAnswer) { public Builder<M> boxAnswer(BoxAnswer boxAnswer) {
this.boxAnswer = message -> boxAnswer; this.boxAnswer = message -> boxAnswer;
return this; return this;
@ -165,8 +154,13 @@ public class AnswerText<M extends Message> extends MainUnit {
return this; return this;
} }
public Builder<M> phrases(Collection<String> val) { public Builder<M> phrases(String... val) {
phrases.addAll(val); phrases.addAll(Arrays.asList(val));
return this;
}
public Builder<M> triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this; return this;
} }

View File

@ -9,6 +9,7 @@ import dev.struchkov.godfather.context.service.usercode.CheckData;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -47,6 +48,7 @@ public class AnswerTimer<M extends Message> extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -90,6 +92,7 @@ public class AnswerTimer<M extends Message> extends MainUnit {
private CheckData<M> checkLoop; private CheckData<M> checkLoop;
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
private Integer priority; private Integer priority;
@ -160,6 +163,11 @@ public class AnswerTimer<M extends Message> extends MainUnit {
return this; return this;
} }
public Builder<M> triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder<M> matchThreshold(Integer val) { public Builder<M> matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -2,14 +2,15 @@ package dev.struchkov.godfather.context.domain.unit;
import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.autoresponder.entity.KeyWord;
import dev.struchkov.godfather.context.domain.TypeUnit; 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.AnswerSaveMapPreservable;
import dev.struchkov.godfather.context.repository.preser.Preservable; 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.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -44,6 +45,7 @@ public class AnswerValidity extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -86,6 +88,7 @@ public class AnswerValidity extends MainUnit {
public static final class Builder { public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private String name; private String name;
private MainUnit unitYes; private MainUnit unitYes;
private MainUnit unitNo; private MainUnit unitNo;
@ -161,6 +164,11 @@ public class AnswerValidity extends MainUnit {
return this; return this;
} }
public Builder triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder matchThreshold(Integer val) { public Builder matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -8,6 +8,7 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -48,6 +49,7 @@ public abstract class MainUnit extends Unit<MainUnit> {
String name, String name,
Set<KeyWord> keyWords, Set<KeyWord> keyWords,
Set<String> phrases, Set<String> phrases,
Predicate<String> triggerCheck,
Pattern pattern, Pattern pattern,
Integer matchThreshold, Integer matchThreshold,
Integer priority, Integer priority,
@ -57,7 +59,7 @@ public abstract class MainUnit extends Unit<MainUnit> {
Accessibility accessibility, Accessibility accessibility,
String type String type
) { ) {
super(keyWords, phrases, pattern, matchThreshold, priority, nextUnits); super(keyWords, phrases, triggerCheck, pattern, matchThreshold, priority, nextUnits);
this.name = name; this.name = name;
this.activeType = Optional.ofNullable(activeType).orElse(UnitActiveType.DEFAULT); this.activeType = Optional.ofNullable(activeType).orElse(UnitActiveType.DEFAULT);
this.accessibility = accessibility; this.accessibility = accessibility;

View File

@ -8,6 +8,7 @@ import dev.struchkov.godfather.context.domain.unit.UnitActiveType;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -20,6 +21,7 @@ public class ReplaceCmd extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -43,7 +45,8 @@ public class ReplaceCmd extends MainUnit {
public static final class Builder { public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private String name; private String name;
private Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
private Integer priority; private Integer priority;
@ -94,6 +97,11 @@ public class ReplaceCmd extends MainUnit {
return this; return this;
} }
public Builder triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder matchThreshold(Integer val) { public Builder matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -9,6 +9,7 @@ import dev.struchkov.godfather.context.exception.UnitConfigException;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -32,6 +33,7 @@ public class RollBackCmd extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -80,7 +82,8 @@ public class RollBackCmd extends MainUnit {
public static final class Builder { public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private String name; private String name;
private Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
private Integer priority; private Integer priority;
@ -121,6 +124,11 @@ public class RollBackCmd extends MainUnit {
return this; return this;
} }
public Builder triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder phrases(Collection<String> val) { public Builder phrases(Collection<String> val) {
phrases.addAll(val); phrases.addAll(val);
return this; return this;

View File

@ -8,6 +8,7 @@ import dev.struchkov.godfather.context.domain.unit.UnitActiveType;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -26,6 +27,7 @@ public class TeleportCmd extends MainUnit {
builder.name, builder.name,
builder.keyWords, builder.keyWords,
builder.phrases, builder.phrases,
builder.triggerCheck,
builder.pattern, builder.pattern,
builder.matchThreshold, builder.matchThreshold,
builder.priority, builder.priority,
@ -49,6 +51,7 @@ public class TeleportCmd extends MainUnit {
public static final class Builder { public static final class Builder {
private final Set<KeyWord> keyWords = new HashSet<>(); private final Set<KeyWord> keyWords = new HashSet<>();
private final Set<String> phrases = new HashSet<>(); private final Set<String> phrases = new HashSet<>();
private Predicate<String> triggerCheck;
private String name; private String name;
private Pattern pattern; private Pattern pattern;
private Integer matchThreshold; private Integer matchThreshold;
@ -99,6 +102,11 @@ public class TeleportCmd extends MainUnit {
return this; return this;
} }
public Builder triggerCheck(Predicate<String> trigger) {
triggerCheck = trigger;
return this;
}
public Builder matchThreshold(Integer val) { public Builder matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -5,18 +5,22 @@ import dev.struchkov.godfather.context.repository.StorylineRepository;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.Stack; import java.util.Stack;
public class StorylineMapRepository implements StorylineRepository { public class StorylineMapRepository implements StorylineRepository {
private final Map<Long, Stack<StorylineHistory>> map = new HashMap<>(); private final Map<Long, Stack<StorylineHistory>> map = new HashMap<>();
private final Map<Long, Set<String>> historyUnitName = new HashMap<>();
@Override @Override
public void save(@NotNull StorylineHistory history) { public void save(@NotNull StorylineHistory history) {
final Long personId = history.getPersonId(); final Long personId = history.getPersonId();
map.computeIfAbsent(personId, k -> new Stack<>()).push(history); map.computeIfAbsent(personId, k -> new Stack<>()).push(history);
historyUnitName.computeIfAbsent(personId, k -> new HashSet<>()).add(history.getUnitName());
} }
@Override @Override
@ -29,6 +33,7 @@ public class StorylineMapRepository implements StorylineRepository {
StorylineHistory storylineHistory = null; StorylineHistory storylineHistory = null;
for (int i = 0; i < countUnitsToBack; i++) { for (int i = 0; i < countUnitsToBack; i++) {
storylineHistory = stack.pop(); storylineHistory = stack.pop();
historyUnitName.get(personId).remove(storylineHistory.getUnitName());
} }
return Optional.ofNullable(storylineHistory); return Optional.ofNullable(storylineHistory);
} }
@ -40,8 +45,9 @@ public class StorylineMapRepository implements StorylineRepository {
if (map.containsKey(personId)) { if (map.containsKey(personId)) {
final Stack<StorylineHistory> stack = map.get(personId); final Stack<StorylineHistory> stack = map.get(personId);
StorylineHistory storylineHistory; StorylineHistory storylineHistory;
while (!stack.isEmpty()) { while (!stack.isEmpty() && historyUnitName.get(personId).contains(unitName)) {
storylineHistory = stack.pop(); storylineHistory = stack.pop();
historyUnitName.get(personId).remove(storylineHistory.getUnitName());
if (unitName.equals(storylineHistory.getUnitName())) { if (unitName.equals(storylineHistory.getUnitName())) {
return Optional.of(storylineHistory); return Optional.of(storylineHistory);
} }

View File

@ -31,7 +31,7 @@ public class AnswerSaveMapPreservable<S> implements AnswerSavePreservable<S> {
@Override @Override
public void push(Long personId, Pusher<S> pusher) { public void push(Long personId, Pusher<S> pusher) {
Optional.ofNullable(pusher).ifPresent(sPusher -> sPusher.push(getAllSaveElement(personId))); Optional.ofNullable(pusher).ifPresent(sPusher -> sPusher.push(personId, getAllSaveElement(personId)));
} }
} }

View File

@ -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);
<T> Optional<T> getByKey(@NotNull Long telegramId, @NotNull ContextKey<T> key);
<T> T getByKeyOrThrow(@NotNull Long telegramId, @NotNull ContextKey<T> key);
Map<String, Object> getAllSaveElement(@NotNull Long telegramId);
void removeAll(@NotNull Long telegramId);
void removeByKey(@NotNull Long telegramId, @NotNull ContextKey<?> key);
}

View File

@ -10,6 +10,6 @@ import java.util.Map;
@FunctionalInterface @FunctionalInterface
public interface Pusher<D> { public interface Pusher<D> {
void push(Map<String, D> saveElement); void push(Long personId, Map<String, D> saveElement);
} }

View File

@ -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<M extends Message> {
void build(M message, BoxAnswer.Builder builder);
}

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.14</version> <version>0.0.17</version>
</parent> </parent>
<artifactId>bot-core</artifactId> <artifactId>bot-core</artifactId>

View File

@ -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<Long, Map<String, Object>> 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 <T> Optional<T> getByKey(@NotNull Long personId, @NotNull ContextKey<T> key) {
isNotNull(personId, key);
if (map.containsKey(personId)) {
final Map<String, Object> 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> T getByKeyOrThrow(@NotNull Long personId, @NotNull ContextKey<T> key) {
return getByKey(personId, key).orElseThrow(notFoundException("Не найдено значение ключа {0}, для пользователя {1}", key.getValue(), personId));
}
public Map<String, Object> 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());
}
}

View File

@ -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.repository.preser.AnswerSavePreservable;
import dev.struchkov.godfather.context.service.save.CheckSave; import dev.struchkov.godfather.context.service.save.CheckSave;
import dev.struchkov.godfather.context.service.save.PreservableData; 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}. * Обработчик Unit-а {@link AnswerSave}.
@ -24,22 +27,25 @@ public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<D>, Message> {
final Long personId = message.getPersonId(); final Long personId = message.getPersonId();
final CheckSave<? super Message> checkSave = answerSave.getCheckSave(); final CheckSave<? super Message> checkSave = answerSave.getCheckSave();
if (checkSave != null) { if (checkNotNull(checkSave)) {
MainUnit unit = checkSave.check(message); MainUnit unit = checkSave.check(message);
if (unit != null) { if (checkNotNull(unit)) {
return UnitRequest.of(unit, message); return UnitRequest.of(unit, message);
} }
} }
PreservableData<D, ? super Message> preservableData = answerSave.getPreservableData(); final PreservableData<D, ? super Message> preservableData = answerSave.getPreservableData();
if (preservableData != null) { if (checkNotNull(preservableData)) {
D data = preservableData.getData(message); D data = preservableData.getData(message);
if (data != null) { if (checkNotNull(data)) {
preservable.save(personId, answerSave.getKey(), data); preservable.save(personId, answerSave.getKey(), data);
} }
} }
preservable.push(personId, answerSave.getPusher()); final Pusher<D> pusher = answerSave.getPusher();
if (checkNotNull(pusher)) {
preservable.push(personId, pusher);
}
return UnitRequest.of(answerSave, message); return UnitRequest.of(answerSave, message);
} }
} }

View File

@ -19,7 +19,7 @@ public class UserSanderPusher implements Pusher<String> {
} }
@Override @Override
public void push(Map<String, String> saveElement) { public void push(Long personId, Map<String, String> saveElement) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("========= ").append(nameForm).append(" =========\n"); stringBuilder.append("========= ").append(nameForm).append(" =========\n");
saveElement.forEach((key, value) -> stringBuilder.append(key).append(": ").append(value).append("\n")); saveElement.forEach((key, value) -> stringBuilder.append(key).append(": ").append(value).append("\n"));

View File

@ -6,7 +6,7 @@
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>godfather-bot</artifactId> <artifactId>godfather-bot</artifactId>
<version>0.0.14</version> <version>0.0.17</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -32,13 +32,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.ver>0.0.14</godfather.ver> <godfather.ver>0.0.17</godfather.ver>
<godfather.context.ver>${godfather.ver}</godfather.context.ver> <godfather.context.ver>${godfather.ver}</godfather.context.ver>
<godfather.core.ver>${godfather.ver}</godfather.core.ver> <godfather.core.ver>${godfather.ver}</godfather.core.ver>
<autoresponder.ver>3.2.0</autoresponder.ver> <autoresponder.ver>3.3.0</autoresponder.ver>
<haiti.utils>1.0.2</haiti.utils> <haiti.utils>1.0.3</haiti.utils>
<javax.persistence.api.ver>2.2</javax.persistence.api.ver> <javax.persistence.api.ver>2.2</javax.persistence.api.ver>
<validation.api.ver>2.0.1.Final</validation.api.ver> <validation.api.ver>2.0.1.Final</validation.api.ver>