Skip to content

Commit

Permalink
Separation of modules and restructuring of models.
Browse files Browse the repository at this point in the history
  • Loading branch information
KepLer100500 authored Aug 17, 2023
2 parents 25ec5fc + 67fd7a4 commit 18b9bb3
Show file tree
Hide file tree
Showing 44 changed files with 353 additions and 115 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: Create application.properties for Dialog and Game services from GitHub.secrets
- name: Create application.properties for PointsCalculator service from GitHub.secrets
run: |
touch Dialog/src/main/resources/application.properties
echo "server.port=9092" >> Dialog/src/main/resources/application.properties
Expand All @@ -33,17 +33,17 @@ jobs:
echo "rabbitmq.queue.game=${{ secrets.RABBITMQ_QUEUE_GAME }}" >> Dialog/src/main/resources/application.properties
echo "rabbitmq.routingKey.game=${{ secrets.RABBITMQ_ROUTINGKEY_GAME }}" >> Dialog/src/main/resources/application.properties
touch Game/src/main/resources/application.properties
echo "server.port=9093" >> Game/src/main/resources/application.properties
echo "spring.jackson.deserialization.fail-on-unknown-properties=false" >> Game/src/main/resources/application.properties
echo "rabbitmq.hostname=${{ secrets.RABBITMQ_HOSTNAME }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.username=${{ secrets.RABBITMQ_USERNAME }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.password=${{ secrets.RABBITMQ_PASSWORD }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.virtualHost=${{ secrets.RABBITMQ_VIRTUALHOST }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.exchange=${{ secrets.RABBITMQ_EXCHANGE }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.queue.dialog=${{ secrets.RABBITMQ_QUEUE_DIALOG }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.routingKey.dialog=${{ secrets.RABBITMQ_ROUTINGKEY_DIALOG }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.queue.game=${{ secrets.RABBITMQ_QUEUE_GAME }}" >> Game/src/main/resources/application.properties
echo "rabbitmq.routingKey.game=${{ secrets.RABBITMQ_ROUTINGKEY_GAME }}" >> Game/src/main/resources/application.properties
touch PointsCalculator/src/main/resources/application.properties
echo "server.port=9093" >> PointsCalculator/src/main/resources/application.properties
echo "spring.jackson.deserialization.fail-on-unknown-properties=false" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.hostname=${{ secrets.RABBITMQ_HOSTNAME }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.username=${{ secrets.RABBITMQ_USERNAME }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.password=${{ secrets.RABBITMQ_PASSWORD }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.virtualHost=${{ secrets.RABBITMQ_VIRTUALHOST }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.exchange=${{ secrets.RABBITMQ_EXCHANGE }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.queue.dialog=${{ secrets.RABBITMQ_QUEUE_DIALOG }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.routingKey.dialog=${{ secrets.RABBITMQ_ROUTINGKEY_DIALOG }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.queue.game=${{ secrets.RABBITMQ_QUEUE_GAME }}" >> PointsCalculator/src/main/resources/application.properties
echo "rabbitmq.routingKey.game=${{ secrets.RABBITMQ_ROUTINGKEY_GAME }}" >> PointsCalculator/src/main/resources/application.properties
- name: Run tests
run: mvn clean test
10 changes: 10 additions & 0 deletions Dialog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@
<maven.compiler.target>11</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.5</version>
</plugin>
</plugins>
</build>

</project>
134 changes: 122 additions & 12 deletions Dialog/src/main/java/com/kepler/controller/AliceDialog.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.kepler.controller;

import com.kepler.model.ResultCalculations;
import com.kepler.model.Tokens;
import com.kepler.model.request.InputData;
import com.kepler.model.response.OutputData;
import com.kepler.model.response.Response;
import com.kepler.model.game.Command;
import com.kepler.model.calculator.ResultCalculations;
import com.kepler.model.calculator.Tokens;
import com.kepler.model.dialog.request.InputData;
import com.kepler.model.dialog.response.OutputData;
import com.kepler.model.dialog.response.Response;
import com.kepler.proxy.RabbitConsumer;
import com.kepler.proxy.RabbitProducer;
import com.kepler.service.Bot;
import com.kepler.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static com.kepler.model.game.Command.*;


@Slf4j
@RestController
Expand All @@ -24,21 +29,127 @@ public class AliceDialog {
@Autowired
private RabbitConsumer rabbitConsumer;

@Autowired
private UserService userService;

@Autowired
private Bot bot;

private OutputData outputData;
private Response response;
private Command command; // last command from dialog
private Integer turnCounter;
private Integer totalUserScopes;
private Integer totalBotScopes;

/**
* Get text form dialog with Alice, parse and calculate points and return sum points
* @param inputData
* @return OutputData outputData
*/
@PostMapping("/")
public OutputData entryPoint(@RequestBody InputData inputData) {
OutputData outputData = buildOutputData(inputData);
Integer summaryPoints = sendTextAndReceiveCalculatedPoints(inputData.getRequest().getNlu().getTokens());
Response response = buildResponse(summaryPoints);
log.info("Send to dialog: {}", summaryPoints);
outputData = buildOutputData(inputData);
response = buildResponse();

String text = "";

if(inputData.getSession().isNew_()) {
command = GREETING;
resetGameCounters();
}

switch (command) {
case GREETING : text = greetingUser(inputData); break;
case REGISTER : text = registerUser(inputData); break;
case NAME_VERIFICATION : text = confirmNickName(inputData); break;
case RENAME : text = renameUser(inputData); break;
case START_GAME : text = confirmStartGame(inputData); break;
case USER_TURN: text = userTurn(inputData); break;
default: text = "Неизвестная команда. Повторите пожалуйста запрос.";
}

response.setText(text);

outputData.setResponse(response);
return outputData;
}

private void resetGameCounters() {
turnCounter = 0;
totalUserScopes = 0;
totalBotScopes = 0;
}

private String userTurn(InputData inputData) {

Integer userPoints = sendTextAndReceiveCalculatedPoints(inputData.getRequest().getNlu().getTokens());
totalUserScopes += userPoints;
Integer botPoints = sendTextAndReceiveCalculatedPoints(bot.makeMove());
totalBotScopes += botPoints;
turnCounter++;

if(turnCounter >= 3) {
String text = "";
if(totalUserScopes > totalBotScopes) {
text = "Поздравляем, вы выиграли! Вы набрали " + totalUserScopes + " очков. Сыграем ещё?";
} else {
text = "Вы проиграли, ваши очки " + totalUserScopes + ". Сыграем ещё?";
}
command = START_GAME;
resetGameCounters();
return text;
}

return "Вы заработали " + userPoints + " очков... А я заработала " + botPoints + " очков. Ваш ход.";
}

private String confirmStartGame(InputData inputData) {
if(inputData.getRequest().getOriginal_utterance().toLowerCase().contains("да") ||
inputData.getRequest().getOriginal_utterance().toLowerCase().contains("ага")) {
command = USER_TURN;
return "Игра начата, ваш ход. Какие секторы вы поразили?";
} else {
command = GREETING;
return "Тогда, пожалуй, перезапущу навык, просто потому что я могу это сделать, почему бы и нет.";
}
}

private String renameUser(InputData inputData) {
userService.renameUser(inputData.getSession().getUser_id(), inputData.getRequest().getOriginal_utterance());
command = NAME_VERIFICATION;
return "Мне называть вас " + inputData.getRequest().getOriginal_utterance() + "?";
}

private String confirmNickName(InputData inputData) {
if(inputData.getRequest().getOriginal_utterance().toLowerCase().contains("да") ||
inputData.getRequest().getOriginal_utterance().toLowerCase().contains("ага")) {
command = START_GAME;
return "Хорошо, " + userService.getNickName(inputData.getSession().getUser_id()) + ". Начать игру?";
} else {
command = RENAME;
return "А как тогда вас называть?";
}
}

private String registerUser(InputData inputData) {
userService.registerUser(inputData.getSession().getUser_id(),
inputData.getRequest().getOriginal_utterance());
command = NAME_VERIFICATION;
return "Мне называть вас " + inputData.getRequest().getOriginal_utterance() + "?";
}

private String greetingUser(InputData inputData) {
if(userService.isNewUser(inputData.getSession().getUser_id())) { // check user in DB
command = REGISTER;
return "Здравствуйте. Кажется вы в первый раз запустили этот навык, представьтесь, как вас зовут?";
} else {
String username = userService.getNickName(inputData.getSession().getUser_id());
command = NAME_VERIFICATION;
return "Здравствуйте. Кажется вы уже запускали этот навык. В прошлый раз я звала вас " + username + ". Продолжить называть вас так же?";
}
}

/**
* Send array words through rabbitmq, calculate sum points and get it
* @param tokens
Expand Down Expand Up @@ -66,13 +177,12 @@ private OutputData buildOutputData(InputData inputData) {

/**
* Build text message for receive to dialog
* @param summaryPoints
* @param
* @return Response response
*/
private Response buildResponse(Integer summaryPoints) {
private Response buildResponse() {
return Response.builder()
.end_session(false)
.text("Вы набрали " + summaryPoints + " очков")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model;
package com.kepler.model.bot;

import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model;
package com.kepler.model.calculator;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model;
package com.kepler.model.calculator;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.request;
package com.kepler.model.dialog.request;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.kepler.model.response;
package com.kepler.model.dialog.response;

import com.kepler.model.request.Session;
import com.kepler.model.dialog.request.Session;
import lombok.Builder;
import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kepler.model.response;
package com.kepler.model.dialog.response;

import lombok.Builder;
import lombok.Data;
Expand Down
11 changes: 11 additions & 0 deletions Dialog/src/main/java/com/kepler/model/game/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kepler.model.game;

public enum Command {
GREETING,
REGISTER,
RENAME,
START_GAME,
USER_TURN,
MENU,
NAME_VERIFICATION;
}
25 changes: 25 additions & 0 deletions Dialog/src/main/java/com/kepler/model/game/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kepler.model.game;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "darts_users")
public class User {
@Id
private String id;
@Column(name = "nick_name")
private String nickName;
@Column(name = "max_scopes")
private Integer maxScopes;
}
Loading

0 comments on commit 18b9bb3

Please sign in to comment.