From 6d9454fd03db4983900c82c4616e514c526a62fa Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Fri, 15 Jul 2022 08:45:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D1=83=D1=81=D1=82=D0=B0=D0=BD=D0=B0=D0=B2=D0=BB=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D1=8E=D0=BD=D0=B8=D1=82=D1=8B,=20?= =?UTF-8?q?=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D0=B3=D0=B4=D0=B0=20=D0=B1=D1=83=D0=B4=D1=83=D1=82=20=D1=83?= =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D0=B2=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B2=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B5?= =?UTF-8?q?.=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B2=D0=BE?= =?UTF-8?q?=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BD=D0=B0=D0=B2=D0=BB=D0=B8=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D1=8E=D0=BD=D0=B8=D1=82=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D0=BE=D0=B2,=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B3=D0=B4=D0=B0=20=D0=BD=D0=B8=D0=BA=D0=B0=D0=BA=D0=BE=D0=B9?= =?UTF-8?q?=20=D1=8E=D0=BD=D0=B8=D1=82=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D1=82?= =?UTF-8?q?=D0=B2=D0=B5=D1=82=D0=B0=20=D0=BD=D0=B5=20=D0=BD=D0=B0=D0=B9?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/domain/annotation/Unit.java | 4 +++- .../context/service/StorylineService.java | 2 ++ .../godfather/core/GeneralAutoResponder.java | 7 ++++++- .../struchkov/godfather/core/Storyline.java | 10 ++++----- .../godfather/core/StorylineMaker.java | 21 +++++++++++++++---- .../core/service/StorylineMailService.java | 14 ++++++++++--- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/annotation/Unit.java b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/annotation/Unit.java index 3a1143b..65b895c 100644 --- a/bot-context/src/main/java/dev/struchkov/godfather/context/domain/annotation/Unit.java +++ b/bot-context/src/main/java/dev/struchkov/godfather/context/domain/annotation/Unit.java @@ -13,6 +13,8 @@ public @interface Unit { String value() default ""; - boolean mainUnit() default false; + boolean main() default false; + + boolean global() default false; } 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 index 69e96f9..a5f7275 100644 --- 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 @@ -31,4 +31,6 @@ public interface StorylineService { Optional getUnitByName(String unitName); + void setDefaultUnit(String unitName); + } 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 3127432..9b74510 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 @@ -80,6 +80,10 @@ public class GeneralAutoResponder { this.errorHandler = errorHandler; } + public void setDefaultUnit(String unitName) { + storyLineService.setDefaultUnit(unitName); + } + public void processingNewMessage(T newMessage) { if (newMessage != null) { final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()).orElse(true); @@ -107,7 +111,8 @@ public class GeneralAutoResponder { modifiable.forEach(m -> m.change(message)); } final Set units = storyLineService.getNextUnitByPersonId(message.getPersonId()); - final Optional optAnswer = Responder.nextUnit(message.getText(), units); + final Optional optAnswer = Responder.nextUnit(message.getText(), units) + .or(storyLineService::getDefaultUnit); if (optAnswer.isPresent()) { final MainUnit answer = optAnswer.get(); if (checkPermission(answer.getAccessibility(), message)) { 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 index 8c48962..8b32ebf 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 @@ -12,8 +12,8 @@ import java.util.Set; public class Storyline { - private MainUnit defaultUnit; private final Set startingUnits = new HashSet<>(); + private final Set globalUnits = new HashSet<>(); private final Map units = new HashMap<>(); public Storyline(Set startingUnits, Map units) { @@ -21,12 +21,12 @@ public class Storyline { this.units.putAll(units); } - public void setDefaultUnit(MainUnit defaultUnit) { - this.defaultUnit = defaultUnit; + public void addGlobalUnits(Set globalUnits) { + this.globalUnits.addAll(globalUnits); } - public Optional getDefaultUnit() { - return Optional.ofNullable(defaultUnit); + public Set getGlobalUnits() { + return globalUnits; } /** 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 b0b59ef..4c3fbfb 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 @@ -2,9 +2,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.context.domain.unit.LazyUnit; import dev.struchkov.godfather.context.domain.unit.MainUnit; +import dev.struchkov.godfather.context.exception.UnitConfigException; import dev.struchkov.haiti.utils.Inspector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,7 +30,9 @@ public class StorylineMaker { private final Map unitDefinitions = new HashMap<>(); private final Map unitMap = new HashMap<>(); + private final Set mainUnits = new HashSet<>(); + private final Set globalUnits = new HashSet<>(); public StorylineMaker(List unitConfigurations) { this.configurations.addAll(unitConfigurations); @@ -50,7 +51,10 @@ public class StorylineMaker { try { createUnitMap(); final Set mainUnit = getMainUnit(); - return new Storyline(mainUnit, unitMap); + final Set globalUnit = getGlobalUnit(); + final Storyline storyline = new Storyline(mainUnit, unitMap); + storyline.addGlobalUnits(globalUnit); + return storyline; } catch (IllegalAccessException | InvocationTargetException e) { log.error(e.getMessage(), e); } @@ -64,6 +68,12 @@ public class StorylineMaker { .collect(Collectors.toSet()); } + private Set getGlobalUnit() { + return globalUnits.stream() + .map(unitMap::get) + .collect(Collectors.toSet()); + } + private void createUnitMap() throws IllegalAccessException, InvocationTargetException { for (UnitDefinition unitDefinition : unitDefinitions.values()) { if (!unitMap.containsKey(unitDefinition.getName())) { @@ -118,9 +128,12 @@ public class StorylineMaker { unitDefinition.setMethod(method); unitDefinition.setObjectConfig(config); - if (unitConfig.mainUnit()) { + if (unitConfig.main()) { mainUnits.add(unitName); } + if (unitConfig.global()) { + globalUnits.add(unitName); + } final Parameter[] nextUnits = method.getParameters(); if (nextUnits.length > 0) { 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 index 6a6002e..9bdd3bd 100644 --- 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 @@ -27,6 +27,7 @@ public class StorylineMailService implements StorylineService { private final UnitPointerService unitPointerService; private final StorylineRepository storylineRepository; private final Storyline storyLine; + private String defaultUnitName; public StorylineMailService( UnitPointerService unitPointerService, @@ -60,8 +61,9 @@ public class StorylineMailService implements StorylineService { if (optMainUnits.isEmpty()) { storylineRepository.cleanHistoryByPersonId(personId); } - return optMainUnits - .orElse(storyLine.getStartingUnits()); + final Set nextUnits = optMainUnits.orElse(storyLine.getStartingUnits()); + nextUnits.addAll(storyLine.getGlobalUnits()); + return nextUnits; } @Override @@ -88,7 +90,13 @@ public class StorylineMailService implements StorylineService { @Override public Optional getDefaultUnit() { - return storyLine.getDefaultUnit(); + if (defaultUnitName == null) return Optional.empty(); + return storyLine.getUnit(defaultUnitName); + } + + @Override + public void setDefaultUnit(String defaultUnit) { + defaultUnitName = defaultUnit; } //TODO [22.06.2022]: Временное решение для ленивой инициализации