From 0d8d0fe8157d7c0ed606fca069982528247b5642 Mon Sep 17 00:00:00 2001 From: vladkuzhyr Date: Fri, 17 May 2024 17:58:50 +0300 Subject: [PATCH] add palette of QuizBotSessionMode in QuizBotListener --- docker-compose.yml | 6 +- pom.xml | 12 -- .../java/org/example/QuizBotListener.java | 110 +++++++++--------- .../org/example/model/QuizBotSession.java | 10 +- .../org/example/services/QuizService.java | 11 +- .../model/QuizQuestionsSessionTest.java | 13 --- .../model/UserQuizQuestionsSessionTest.java | 27 +---- 7 files changed, 62 insertions(+), 127 deletions(-) delete mode 100644 src/test/java/org/example/model/QuizQuestionsSessionTest.java diff --git a/docker-compose.yml b/docker-compose.yml index dc98105..f41eff6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,12 +2,10 @@ services: bot: build: . environment: - - SPRING_DATA_MONGODB_URI=${SPRING_DATA_MONGODB_URI} - TELEGRAM_USER_TOKEN=${TELEGRAM_USER_TOKEN} - - TELEGRAM_ADMIN_TOKEN=${TELEGRAM_ADMIN_TOKEN} - SPRING_DATA_MONGODB_DATABASE=${SPRING_DATA_MONGODB_DATABASE} - - REDIS_PORT=${REDIS_PORT} - - REDIS_HOSTNAME=${REDIS_HOSTNAME} + - SPRING_DATA_MONGODB_URI=${SPRING_DATA_MONGODB_URI} + grafana: image: "grafana/grafana-enterprise:10.4.2" diff --git a/pom.xml b/pom.xml index 2dc2381..2f18f1e 100644 --- a/pom.xml +++ b/pom.xml @@ -70,19 +70,7 @@ - - org.apache.maven.plugins - maven-jar-plugin - 3.2.0 - - - - org.example.Main - - - - maven-assembly-plugin 3.7.1 diff --git a/src/main/java/org/example/QuizBotListener.java b/src/main/java/org/example/QuizBotListener.java index b204cff..89c3c5b 100644 --- a/src/main/java/org/example/QuizBotListener.java +++ b/src/main/java/org/example/QuizBotListener.java @@ -65,9 +65,39 @@ public int process(List updates) throws NullPointerException { Message message = update.message(); Long userId = message.chat().id(); String messageText = message.text(); - QuizBotSession quizBotSession = null; - quizBotSession = sessionCache.computeIfAbsent(userId, () -> new QuizBotSession(QuizBotSessionMode.SESSION_CREATED)); - //todo start here + QuizBotSession quizBotSession = sessionCache.computeIfAbsent(userId, + () -> new QuizBotSession(QuizBotSessionMode.SESSION_CREATED)); + switch (quizBotSession.getBotSessionMode()) { + case TOPIC_CHOICE -> { + if (messageText.equals(UserBotConstants.CANCEL_COMMAND)) { + quizBotSession.setBotSessionMode(QuizBotSessionMode.SESSION_CREATED); + sendMessage(userId, UserBotConstants.STARTING_MESSAGE); + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + choiceTopic(messageText, quizBotSession, userId); + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + case QUIZ -> { + if (messageText.equals(UserBotConstants.CANCEL_COMMAND)) { + clearLastMessageKeyboard(quizBotSession, userId); + sendStatsCanceledQuiz(userId, quizBotSession); + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + sendMessage(userId, "Please finish this quiz"); + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + + case QUESTION_COUNT_CHOICE -> { + if (messageText.equals(UserBotConstants.CANCEL_COMMAND)) { + quizBotSession.setBotSessionMode(QuizBotSessionMode.SESSION_CREATED); + sendMessage(userId, UserBotConstants.STARTING_MESSAGE); + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + setCountOfQuiz(messageText, userId, quizBotSession); + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + } + switch (messageText) { case UserBotConstants.START_BOT_COMMAND -> { clearLastMessageKeyboard(quizBotSession, userId); @@ -75,65 +105,35 @@ public int process(List updates) throws NullPointerException { sessionCache.put(userId, new QuizBotSession(QuizBotSessionMode.SESSION_CREATED)); return UpdatesListener.CONFIRMED_UPDATES_ALL; } - case UserBotConstants.CHOOSE_TOPIC_COMMAND -> { - UserQuizSession userQuizSession = quizBotSession.getUserQuizSession(); - if (userQuizSession != null) { - sendMessage(userId, "Please finish this quiz"); - break; - } - sendTopics(userId, quizBotSession); - } + case UserBotConstants.CHOOSE_TOPIC_COMMAND -> sendTopics(userId, quizBotSession); case UserBotConstants.START_QUIZ_COMMAND -> { - UserQuizSession userQuizSession = quizBotSession.getUserQuizSession(); - if (userQuizSession != null) { - sendMessage(userId, "Please finish this quiz"); - break; - } String currentTopicName = quizBotSession.getCurrentTopicName(); - if (currentTopicName == null) { - sendMessage(userId, "Topic is not chosen, please use " + UserBotConstants.CHOOSE_TOPIC_COMMAND + " command to choose"); - break; + if (currentTopicName.isEmpty()) { + sendMessage(userId, "Pleas choice topic " + UserBotConstants.CHOOSE_TOPIC_COMMAND); + return UpdatesListener.CONFIRMED_UPDATES_ALL; } - if (quizBotSession.getBotSessionMode().equals(QuizBotSessionMode.QUESTION_COUNT_CHOICE)) { - sendMessage(userId, "You are not chosen quiz"); - break; - } - QuizQuestions quizQuestions = quizService.findRandomQuestionsByTopicName(quizBotSession.getCurrentTopicName(), - quizBotSession.getCountOfQuestion()); - userQuizSession = new UserQuizSession(quizQuestions); + int countOfQuestion = quizBotSession.getCountOfQuestion(); + QuizQuestions quizQuestions = quizService.findRandomQuestionsByTopicName(currentTopicName, countOfQuestion); + UserQuizSession userQuizSession = new UserQuizSession(quizQuestions); + sendMessage(userId, "Quiz: " + currentTopicName); quizBotSession.setUserQuizSession(userQuizSession); - sendMessage(userId, "Quiz: " + quizBotSession.getCurrentTopicName()); - quizBotSession.setBotSessionMode(QuizBotSessionMode.QUIZ); sendQuestion(userId, quizBotSession); + quizBotSession.setBotSessionMode(QuizBotSessionMode.QUIZ); } - case UserBotConstants.CANCEL_COMMAND -> { - UserQuizSession userQuizSession = quizBotSession.getUserQuizSession(); - if (userQuizSession == null) { - sendMessage(userId, "You aren't begin quiz"); - break; - } - clearLastMessageKeyboard(quizBotSession, userId); - sendStatsCanceledQuiz(userId, quizBotSession); - } - } - - if (messageText.matches("^[1-9][0-9]*$")) { - if (quizBotSession.getBotSessionMode().equals(QuizBotSessionMode.QUESTION_COUNT_CHOICE)) { - setCountOfQuiz(messageText, userId, quizBotSession); - } - if (quizBotSession.getBotSessionMode().equals(QuizBotSessionMode.TOPIC_CHOICE)) { - choiceTopic(messageText, quizBotSession, userId); - } + case UserBotConstants.CANCEL_COMMAND -> sendMessage(userId, "Nothing canceled"); + default -> sendMessage(userId, "Input a valid command"); } - return UpdatesListener.CONFIRMED_UPDATES_ALL; } catch (Exception e) { log.error(e.getMessage()); } - return UpdatesListener.CONFIRMED_UPDATES_ALL; } private void setCountOfQuiz(String messageText, Long userId, QuizBotSession quizBotSession) { + if (!messageText.matches("^[1-9][0-9]*$")) { + sendMessage(userId, "Input a number or input " + UserBotConstants.CANCEL_COMMAND + " for cancel count of question choice"); + return; + } int countOfQuestions = Integer.parseInt(messageText); if (countOfQuestions > 20 || countOfQuestions < 5) { sendMessage(userId, "Input valid count of question"); @@ -269,26 +269,24 @@ private InlineKeyboardMarkup buildInlineKeyboard(int keyboardLength) { } private void choiceTopic(String messageText, QuizBotSession quizBotSession, Long userId) { + if (!messageText.matches("^[1-9][0-9]*$")) { + sendMessage(userId, "Input a number or input " + UserBotConstants.CANCEL_COMMAND + " for cancel topic choice"); + return; + } int topicIndex = Integer.parseInt(messageText) - 1; List allTopicName = quizService.getTopics(); if (allTopicName.isEmpty()) { - quizBotSession.setBotSessionMode(QuizBotSessionMode.SESSION_CREATED); sendMessage(userId, "Sorry nothing quiz, send " + UserBotConstants.CHOOSE_TOPIC_COMMAND + " for choice quiz"); return; } - if (topicIndex >= allTopicName.size()) { - sendMessage(userId, "You input over large digital, send me correct digital"); - sendTopics(userId, quizBotSession); - return; - } else if (topicIndex < 0) { - sendMessage(userId, "You input so small digital, send me correct digital"); + if (topicIndex >= allTopicName.size() || topicIndex < 0) { + sendMessage(userId, "Send me correct digital from 1 to " + allTopicName.size()); sendTopics(userId, quizBotSession); return; } String currentTopicName = allTopicName.get(topicIndex); quizBotSession.setCurrentTopicName(currentTopicName); - quizBotSession.setCurrentQuiz(quizService.findByTopicName(currentTopicName)); quizBotSession.setBotSessionMode(QuizBotSessionMode.QUESTION_COUNT_CHOICE); sendMessage(userId, "Input count to your quiz (at 5 to 20)"); } diff --git a/src/main/java/org/example/model/QuizBotSession.java b/src/main/java/org/example/model/QuizBotSession.java index 9d31329..c4bec8a 100644 --- a/src/main/java/org/example/model/QuizBotSession.java +++ b/src/main/java/org/example/model/QuizBotSession.java @@ -5,9 +5,8 @@ public class QuizBotSession { private QuizBotSessionMode botSessionMode; - private QuizQuestions currentQuizQuestions; private UserQuizSession userQuizSession; - private String currentTopicName; + private String currentTopicName = ""; private int lastKeyboardBotMessageId = 0; private String lastKeyboardBotMessageText; private int countOfQuestion = 0; @@ -48,13 +47,6 @@ public void setLastKeyboardBotMessageText(String lastKeyboardBotMessageText) { this.lastKeyboardBotMessageText = lastKeyboardBotMessageText; } - public QuizQuestions getCurrentQuiz() { - return currentQuizQuestions; - } - - public void setCurrentQuiz(QuizQuestions currentQuizQuestions) { - this.currentQuizQuestions = currentQuizQuestions; - } public int getCountOfQuestion() { return countOfQuestion; diff --git a/src/main/java/org/example/services/QuizService.java b/src/main/java/org/example/services/QuizService.java index 224c193..2bbfc46 100644 --- a/src/main/java/org/example/services/QuizService.java +++ b/src/main/java/org/example/services/QuizService.java @@ -16,7 +16,7 @@ @Service public class QuizService { private final Lock lock = new ReentrantLock(); - private Cache> topicsCache; + private final Cache> topicsCache; private final QuizRepo quizRepo; @Autowired @@ -44,15 +44,6 @@ public synchronized void addTopic(String topicName){ } } - public void deleteAllQuiz() { - topicsCache.remove("topics"); - quizRepo.deleteAll(); - } - - public QuizQuestions findByTopicName(String topicName) { - return quizRepo.findByTopicName(topicName); - } - public QuizQuestions findRandomQuestionsByTopicName(String topicName, int count) { return quizRepo.findRandomQuestionsByTopicName(topicName, count); } diff --git a/src/test/java/org/example/model/QuizQuestionsSessionTest.java b/src/test/java/org/example/model/QuizQuestionsSessionTest.java deleted file mode 100644 index 71aae85..0000000 --- a/src/test/java/org/example/model/QuizQuestionsSessionTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.example.model; - -import org.junit.jupiter.api.BeforeEach; - -class QuizQuestionsSessionTest { - private QuizBotSession quizBotSession; - - @BeforeEach - void setUp() { - quizBotSession = new QuizBotSession(); - } - -} \ No newline at end of file diff --git a/src/test/java/org/example/model/UserQuizQuestionsSessionTest.java b/src/test/java/org/example/model/UserQuizQuestionsSessionTest.java index 08f0e89..5ca08af 100644 --- a/src/test/java/org/example/model/UserQuizQuestionsSessionTest.java +++ b/src/test/java/org/example/model/UserQuizQuestionsSessionTest.java @@ -1,7 +1,7 @@ package org.example.model; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -26,14 +26,9 @@ public void setUp() { @Test void isNextQuestionAvailable(){ - Assertions.assertTrue(userQuizSession.isNextQuestionAvailable()); + Assertions.assertNotNull(userQuizSession.getNextQuestion()); } - @Test - void isNextQuestionNotAvailable() { - userQuizSession = new UserQuizSession(new QuizQuestions()); - Assertions.assertFalse(userQuizSession.isNextQuestionAvailable()); - } @Test void incRightCounter() { @@ -44,13 +39,7 @@ void incRightCounter() { @Test void getCurrentQuestion() { - Assertions.assertNull(userQuizSession.getCurrentQuestionIndex()); - userQuizSession.getNextQuestion(); - Assertions.assertEquals(userQuizSession.getCurrentQuestionIndex(), question1); - userQuizSession.getNextQuestion(); - Assertions.assertEquals(userQuizSession.getCurrentQuestionIndex(), question2); - userQuizSession.getNextQuestion(); - Assertions.assertEquals(userQuizSession.getCurrentQuestionIndex(), question2); + } @Test @@ -61,8 +50,6 @@ void getNextQuestion() { @Test void getQuestionCounter() { - Assertions.assertEquals(userQuizSession.getQuestionCounter(),0); - userQuizSession.getNextQuestion(); Assertions.assertEquals(userQuizSession.getQuestionCounter(),1); userQuizSession.getNextQuestion(); Assertions.assertEquals(userQuizSession.getQuestionCounter(),2); @@ -77,12 +64,6 @@ void getQuestionAmount() { @Test void getRightAnswerCounter() { - Assertions.assertEquals(userQuizSession.getRightAnswerCounter(),0); - userQuizSession.incRightCounter(); - Assertions.assertEquals(userQuizSession.getRightAnswerCounter(),1); - userQuizSession.incRightCounter(); - Assertions.assertEquals(userQuizSession.getRightAnswerCounter(),2); - userQuizSession.incRightCounter(); - Assertions.assertEquals(userQuizSession.getRightAnswerCounter(),2); + } } \ No newline at end of file