Skip to content

Commit

Permalink
add palette of QuizBotSessionMode in QuizBotListener
Browse files Browse the repository at this point in the history
  • Loading branch information
XD-cods committed May 17, 2024
1 parent 9941a02 commit 0d8d0fe
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 127 deletions.
6 changes: 2 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 0 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,7 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>org.example.Main</mainClass>
</manifest>
</archive>

</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
Expand Down
110 changes: 54 additions & 56 deletions src/main/java/org/example/QuizBotListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,75 +65,75 @@ public int process(List<Update> 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);
sessionCache.remove(userId);
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");
Expand Down Expand Up @@ -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<String> 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)");
}
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/org/example/model/QuizBotSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/org/example/services/QuizService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Service
public class QuizService {
private final Lock lock = new ReentrantLock();
private Cache<String, List<String>> topicsCache;
private final Cache<String, List<String>> topicsCache;
private final QuizRepo quizRepo;

@Autowired
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 0 additions & 13 deletions src/test/java/org/example/model/QuizQuestionsSessionTest.java

This file was deleted.

27 changes: 4 additions & 23 deletions src/test/java/org/example/model/UserQuizQuestionsSessionTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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);

}
}

0 comments on commit 0d8d0fe

Please sign in to comment.