-
Notifications
You must be signed in to change notification settings - Fork 0
ProjectModule3SpaceQuest #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Roman-M-git
wants to merge
1
commit into
master
Choose a base branch
from
reviewModule3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,106 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.javarush</groupId> | ||
| <artifactId>SpaceQuestModule3</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>SpaceQuestModule3</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>3.8.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | ||
| http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>com.javarush</groupId> | ||
| <artifactId>HelloQuest</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <packaging>war</packaging> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>11</maven.compiler.source> | ||
| <maven.compiler.target>11</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <junit.version>5.9.2</junit.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
|
|
||
| <!-- Servlet API --> | ||
| <dependency> | ||
| <groupId>javax.servlet</groupId> | ||
| <artifactId>javax.servlet-api</artifactId> | ||
| <version>4.0.1</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <!-- JSTL - для тегов JSP --> | ||
| <dependency> | ||
| <groupId>javax.servlet</groupId> | ||
| <artifactId>jstl</artifactId> | ||
| <version>1.2</version> | ||
| </dependency> | ||
|
|
||
| <!-- JUnit 5 --> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>${junit.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- Mockito базовый --> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <version>5.6.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- Mockito + интеграция с JUnit 5 (@Mock и @InjectMocks) --> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-junit-jupiter</artifactId> | ||
| <version>5.6.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
|
|
||
| </dependencies> | ||
|
|
||
| <build> | ||
|
|
||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-war-plugin</artifactId> | ||
| <version>3.3.2</version> | ||
| </plugin> | ||
|
|
||
| <!-- Компилятор Java --> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.8.1</version> | ||
| <configuration> | ||
| <source>11</source> | ||
| <target>11</target> | ||
| <encoding>UTF-8</encoding> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <!-- Tomcat --> | ||
| <plugin> | ||
| <groupId>org.apache.tomcat.maven</groupId> | ||
| <artifactId>tomcat7-maven-plugin</artifactId> | ||
| <version>2.2</version> | ||
| <configuration> | ||
| <port>8080</port> | ||
| <path>/HelloQuest</path> | ||
| </configuration> | ||
| </plugin> | ||
|
|
||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.0.0-M7</version> | ||
| </plugin> | ||
|
|
||
| </plugins> | ||
|
|
||
| </build> | ||
|
|
||
| </project> |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package com.javarush.quest; | ||
|
|
||
| import com.javarush.quest.model.QuestStep; | ||
| import com.javarush.quest.service.QuestService; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpSession; | ||
| import java.io.IOException; | ||
|
|
||
| @WebServlet("/game") | ||
| public class GameServlet extends HttpServlet { | ||
|
|
||
| private final QuestService questService = new QuestService(); | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
| String stepId = req.getParameter("step"); | ||
| if (stepId == null || stepId.isEmpty()) { | ||
| stepId = "start"; | ||
| } | ||
|
|
||
| QuestStep step = questService.getStep(stepId); | ||
| if (step == null) { | ||
| resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Шаг не найден!"); | ||
| } | ||
|
|
||
| req.setAttribute("step", step); | ||
| req.getRequestDispatcher("/game.jsp").forward(req, resp); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| @Override | ||
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
| HttpSession session = req.getSession(); | ||
|
|
||
| // Получаем параметры | ||
| String answer = req.getParameter("answer"); | ||
| String currentStepId = req.getParameter("currentStep"); | ||
|
|
||
| // Валидация | ||
| if (answer == null || currentStepId == null) { | ||
| resp.sendRedirect(req.getContextPath() + "/game?step=start"); | ||
| return; | ||
| } | ||
|
|
||
| // Получить текущий шаг | ||
| QuestStep currentStep = questService.getStep(currentStepId); | ||
| if (currentStep == null) { | ||
| resp.sendRedirect(req.getContextPath() + "/game?step=start"); | ||
| return; | ||
| } | ||
|
|
||
| // Cлед шаг | ||
| String nextStepId; | ||
| if ("1".equals(answer)) { | ||
| nextStepId = currentStep.getNextStepId1(); | ||
| } else { | ||
| nextStepId = currentStep.getNextStepId2(); | ||
| } | ||
|
|
||
| // Обновляем статистику | ||
| if (questService.isFinalStep(nextStepId)) { | ||
| Integer gamesPlayed = (Integer) session.getAttribute("gamesPlayed"); | ||
| if (gamesPlayed == null) { | ||
| gamesPlayed = 0; | ||
| } | ||
| session.setAttribute("gamesPlayed", gamesPlayed + 1); | ||
| } | ||
|
|
||
| // на след шаг | ||
| resp.sendRedirect(req.getContextPath() + "/game?step=" + nextStepId); | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.javarush.quest; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
|
|
||
| @WebServlet("/hello") | ||
| public class HelloServlet extends HttpServlet { | ||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
| req.getRequestDispatcher("/index.jsp").forward(req, resp); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.javarush.quest; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.servlet.http.HttpSession; | ||
| import java.io.IOException; | ||
|
|
||
| @WebServlet("/start") | ||
| public class StartServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
| request.getRequestDispatcher("/index.jsp").forward(request, response); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | ||
| String playerName = request.getParameter("playerName"); | ||
| if (playerName == null || playerName.trim().isEmpty()) { | ||
| playerName = "Искатель приключений"; | ||
| } | ||
| HttpSession session = request.getSession(); | ||
| session.setAttribute("playerName", playerName); | ||
| session.setAttribute("gamesPlayed", 0); | ||
|
|
||
| response.sendRedirect(request.getContextPath() + "/game?step=start"); | ||
| } | ||
|
|
||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/javarush/quest/filter/EncodingFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.javarush.quest.filter; | ||
|
|
||
| import javax.servlet.*; | ||
| import javax.servlet.annotation.WebFilter; | ||
| import java.io.IOException; | ||
|
|
||
| @WebFilter("/*") | ||
| public class EncodingFilter implements Filter { | ||
| @Override | ||
| public void init(FilterConfig filterConfig) throws ServletException { | ||
| // Инициализация не требуется | ||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
| servletRequest.setCharacterEncoding("UTF-8"); | ||
| servletResponse.setCharacterEncoding("UTF-8"); | ||
| servletResponse.setContentType("text/html;charset=UTF-8"); | ||
| filterChain.doFilter(servletRequest, servletResponse); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| // Очистка не треб. | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Класс совмещает логику обработки HTTP-запросов, валидацию данных, управление сессией, бизнес-логику определения следующего шага и обновление статистики. Следует выделить отдельный сервисный класс GameLogicService, который будет отвечать за обработку ответов игрока и определение следующего шага