From 970695e4b885852f3d7a1b23f0e736b7bb13be23 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 29 Mar 2023 01:15:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20=D0=B4?= =?UTF-8?q?=D0=B5=D1=84=D0=BE=D0=BB=D1=82=D0=BD=D1=83=D1=8E=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=B0=D1=82=D1=83=D1=80=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/context/utils/SimpleKeyBoards.java | 112 ------------------ .../quarkus/core/GeneralAutoResponder.java | 22 +++- .../core/action/AnswerCheckAction.java | 3 +- .../simple/core/GeneralAutoResponder.java | 22 +++- .../keyboard/simple/SimpleKeyBoard.java | 74 ------------ .../keyboard/simple/SimpleKeyBoardLine.java | 45 ++----- 6 files changed, 42 insertions(+), 236 deletions(-) delete mode 100644 bot-context/bot-context-main/src/main/java/dev/struchkov/godfather/main/context/utils/SimpleKeyBoards.java delete mode 100644 bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoard.java diff --git a/bot-context/bot-context-main/src/main/java/dev/struchkov/godfather/main/context/utils/SimpleKeyBoards.java b/bot-context/bot-context-main/src/main/java/dev/struchkov/godfather/main/context/utils/SimpleKeyBoards.java deleted file mode 100644 index 9b0555f..0000000 --- a/bot-context/bot-context-main/src/main/java/dev/struchkov/godfather/main/context/utils/SimpleKeyBoards.java +++ /dev/null @@ -1,112 +0,0 @@ -package dev.struchkov.godfather.main.context.utils; - -import dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton; -import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoard; -import dev.struchkov.godfather.main.domain.keyboard.simple.SimpleKeyBoardLine; - -import java.util.Arrays; -import java.util.List; - -/** - * Используется для быстрого создания клавиаутр {@link SimpleKeyBoard}. - * - * @author upagge [08/07/2019] - */ -public class SimpleKeyBoards { - - public static final SimpleButton YES_BUTTON = SimpleButton.simpleButton("Да", "{\"button\": \"yes\"}"); - public static final SimpleButton NO_BUTTON = SimpleButton.simpleButton("Нет", "{\"button\": \"no\"}"); - - private SimpleKeyBoards() { - throw new IllegalStateException(); - } - - /** - * Возвращает клавиатуру формата 1х2, с кнопками "Да | Нет" - * - * @return {@link SimpleKeyBoard} - */ - public static SimpleKeyBoard keyBoardYesNo() { - return SimpleKeyBoard.builder().line( - SimpleKeyBoardLine.builder().button(YES_BUTTON).button(NO_BUTTON).build() - ).build(); - } - - /** - * Возвращает клавиатуру формата 1хN, где N - это количество элементов в переданном списке - * - * @param labelButtons Список названий для кнопок - * @return {@link SimpleKeyBoard} - */ - public static SimpleKeyBoard verticalMenuString(List labelButtons) { - final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.builder(); - for (String labelButton : labelButtons) { - final SimpleButton simpleButton = SimpleButton.simpleButton(labelButton, "{\"button\": \"" + labelButton + "\"}"); - keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build()); - } - return keyBoard.build(); - } - - /** - * Возвращает клавиатуру формата 1хN, где N - это количество элементов в переданном списке - * - * @param labelButton Список названий для кнопок - * @return {@link SimpleKeyBoard} - */ - public static SimpleKeyBoard verticalMenuString(String... labelButton) { - return verticalMenuString(Arrays.asList(labelButton)); - } - - /** - * Возвращает клавиатуру формата 2х(N/2), где N - это количество элементов в переданном списке - * - * @param labelButton Список названий для кнопок - * @return {@link SimpleKeyBoard} - */ - public static SimpleKeyBoard verticalDuoMenuString(String... labelButton) { - return verticalDuoMenuString(Arrays.asList(labelButton)); - } - - /** - * Возвращает клавиатуру формата 2х(N/2), где N - это количество элементов в переданном списке - * - * @param labelButton Список названий для кнопок - * @return {@link SimpleKeyBoard} - */ - public static SimpleKeyBoard verticalDuoMenuString(List labelButton) { - final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.builder(); - boolean flag = true; - SimpleKeyBoardLine.Builder keyBoardLine = SimpleKeyBoardLine.builder(); - for (int i = 0; i <= labelButton.size() - 1; i++) { - String label = labelButton.get(i); - keyBoardLine.button(SimpleButton.simpleButton(label)); - if (flag) { - if (i == labelButton.size() - 1) { - keyBoard.line(keyBoardLine.build()); - } else { - flag = false; - } - } else { - keyBoard.line(keyBoardLine.build()); - keyBoardLine = SimpleKeyBoardLine.builder(); - flag = true; - } - } - return keyBoard.build(); - } - - /** - * Возвращает клавиатуру формата 1xN сформированную из списка кнопок, где N - количество кнопок в списке - * - * @param simpleButtons Список кнопок - * @return {@link SimpleKeyBoard} - */ - public static SimpleKeyBoard verticalMenuButton(List simpleButtons) { - final SimpleKeyBoard.Builder keyBoard = SimpleKeyBoard.builder(); - for (SimpleButton simpleButton : simpleButtons) { - keyBoard.line(SimpleKeyBoardLine.builder().button(simpleButton).build()); - } - return keyBoard.build(); - } - -} diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java index 3a7270e..9547f17 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java @@ -13,6 +13,7 @@ import dev.struchkov.haiti.context.exception.NotFoundException; import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,7 +31,7 @@ public class GeneralAutoResponder { protected final PersonSettingService personSettingService; protected final StorylineService storyLineService; - protected Map> actionUnitMap = new HashMap<>(); + protected Map> actionUnitMap = new HashMap<>(); protected List> modifiable; protected ErrorHandler errorHandler; @@ -43,7 +44,8 @@ public class GeneralAutoResponder { } public void registrationActionUnit(ActionUnit actionUnit) { - actionUnitMap.computeIfAbsent(actionUnit.getUnitType(), k -> new HashMap<>()).putIfAbsent(actionUnit.getMessageType(), actionUnit); + actionUnitMap.computeIfAbsent(actionUnit.getUnitType(), k -> new ArrayList<>()); + actionUnitMap.get(actionUnit.getUnitType()).add(actionUnit); } public void initModifiable(List> modifiable) { @@ -154,9 +156,10 @@ public class GeneralAutoResponder { final M message = unitRequest.getMessage(); final String typeUnit = unit.getType(); if (actionUnitMap.containsKey(typeUnit)) { - Map actionMap = actionUnitMap.get(typeUnit); - if (actionMap.containsKey(message.getClass())) { - final ActionUnit actionUnit = actionMap.get(message.getClass()); + List actionUnits = actionUnitMap.get(typeUnit); + final Class messageClass = message.getClass(); + final ActionUnit actionUnit = getActionUnit(actionUnits, messageClass); + if (checkNotNull(actionUnit)) { return actionUnit.action(unitRequest) .flatMap( newUnitRequest -> { @@ -181,4 +184,13 @@ public class GeneralAutoResponder { throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); } + private ActionUnit getActionUnit(List actionUnits, Class messageClass) { + for (ActionUnit actionUnit : actionUnits) { + if (actionUnit.getMessageType().isAssignableFrom(messageClass)) { + return actionUnit; + } + } + return null; + } + } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java index 9d75eeb..6fbadda 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java @@ -4,7 +4,6 @@ import dev.struchkov.godfather.main.domain.content.Message; import dev.struchkov.godfather.quarkus.context.service.Sending; import dev.struchkov.godfather.quarkus.domain.BoxAnswer; import dev.struchkov.godfather.quarkus.domain.unit.AnswerCheck; -import dev.struchkov.godfather.quarkus.domain.unit.AnswerText; import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; import dev.struchkov.godfather.quarkus.domain.unit.UnitRequest; import io.smallrye.mutiny.Uni; @@ -62,7 +61,7 @@ public class AnswerCheckAction implements ActionUnit, Messa @Override public String getUnitType() { - return AnswerText.TYPE; + return AnswerCheck.TYPE; } @Override diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java index d217747..fa0174d 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java @@ -33,7 +33,7 @@ public class GeneralAutoResponder { protected final PersonSettingService personSettingService; protected final StorylineService storyLineService; - protected Map> actionUnitMap = new HashMap<>(); + protected Map> actionUnitMap = new HashMap<>(); protected List> modifiable; protected ErrorHandler errorHandler; protected ExecutorService executorService; @@ -160,15 +160,16 @@ public class GeneralAutoResponder { return unitRequest; } - private UnitRequest, M> getAction(UnitRequest, M> unitRequest) { + private UnitRequest getAction(UnitRequest unitRequest) { final MainUnit unit = unitRequest.getUnit(); final M message = unitRequest.getMessage(); final String typeUnit = unit.getType(); if (actionUnitMap.containsKey(typeUnit)) { - Map actionMap = actionUnitMap.get(typeUnit); - if (actionMap.containsKey(message.getClass())) { - final ActionUnit actionUnit = actionMap.get(message.getClass()); - UnitRequest, M> newUnitRequest = actionUnit.action(unitRequest); + List actionUnits = actionUnitMap.get(typeUnit); + final Class messageClass = message.getClass(); + final ActionUnit actionUnit = getActionUnit(actionUnits, messageClass); + if (checkNotNull(actionUnit)) { + UnitRequest newUnitRequest = actionUnit.action(unitRequest); final Optional> optDefaultUnit = storyLineService.getDefaultUnit(); if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { storyLineService.save(message.getFromPersonId(), unit.getName(), message); @@ -180,4 +181,13 @@ public class GeneralAutoResponder { throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); } + private ActionUnit getActionUnit(List actionUnits, Class messageClass) { + for (ActionUnit actionUnit : actionUnits) { + if (actionUnit.getMessageType().isAssignableFrom(messageClass)) { + return actionUnit; + } + } + return null; + } + } diff --git a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoard.java b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoard.java deleted file mode 100644 index 430002d..0000000 --- a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoard.java +++ /dev/null @@ -1,74 +0,0 @@ -package dev.struchkov.godfather.main.domain.keyboard.simple; - -import dev.struchkov.godfather.main.domain.keyboard.KeyBoard; -import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton; -import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine; - -import java.util.ArrayList; -import java.util.List; - -/** - * Сущность клавиатуры, для создания меню с вариантами выбора. - * - * @author upagge [08/07/2019] - */ -public class SimpleKeyBoard implements KeyBoard { - - public static final String TYPE = "SIMPLE"; - - /** - * Строки меню. - */ - protected List lines = new ArrayList<>(); - - public SimpleKeyBoard(List lines) { - this.lines = lines; - } - - private SimpleKeyBoard(Builder builder) { - lines = builder.lines; - } - - public static SimpleKeyBoard simpleKeyboard(KeyBoardLine line) { - return new SimpleKeyBoard(List.of(line)); - } - - public static Builder builder() { - return new Builder(); - } - - public SimpleKeyBoard simpleKeyboard(KeyBoardButton keyBoardButton) { - return simpleKeyboard(SimpleKeyBoardLine.simpleLine(keyBoardButton)); - } - - @Override - public String getType() { - return TYPE; - } - - @Override - public List getLines() { - return lines; - } - - public static final class Builder { - private List lines = new ArrayList<>(); - - private Builder() { - } - - public Builder lines(List val) { - lines = val; - return this; - } - - public Builder line(KeyBoardLine val) { - lines.add(val); - return this; - } - - public SimpleKeyBoard build() { - return new SimpleKeyBoard(this); - } - } -} diff --git a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoardLine.java b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoardLine.java index 89f8773..b7eeedd 100644 --- a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoardLine.java +++ b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/keyboard/simple/SimpleKeyBoardLine.java @@ -2,66 +2,37 @@ package dev.struchkov.godfather.main.domain.keyboard.simple; import dev.struchkov.godfather.main.domain.keyboard.KeyBoardButton; import dev.struchkov.godfather.main.domain.keyboard.KeyBoardLine; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.Singular; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** - * Строка в меню клавиатуры {@link SimpleKeyBoard}. + * Строка в меню клавиатуры {@link dev.struchkov.godfather.main.domain.keyboard.KeyBoard}. * * @author upagge [08/07/2019] */ @Getter @Setter +@Builder @NoArgsConstructor +@AllArgsConstructor(access = AccessLevel.PRIVATE) public class SimpleKeyBoardLine implements KeyBoardLine { /** * Кнопки в строке. */ + @Singular protected List buttons; - public SimpleKeyBoardLine(List buttons) { - this.buttons = buttons; - } - - private SimpleKeyBoardLine(Builder builder) { - buttons = builder.buttons; - } - public static SimpleKeyBoardLine simpleLine(KeyBoardButton... keyBoardButton) { return new SimpleKeyBoardLine(Arrays.stream(keyBoardButton).toList()); } - public static Builder builder() { - return new Builder(); - } - - public static final class Builder { - - private List buttons = new ArrayList<>(); - - private Builder() { - } - - public Builder buttons(List val) { - buttons = val; - return this; - } - - public Builder button(KeyBoardButton val) { - buttons.add(val); - return this; - } - - public SimpleKeyBoardLine build() { - return new SimpleKeyBoardLine(this); - } - - } - }