-
Notifications
You must be signed in to change notification settings - Fork 27
Lazarev_FInal_24.01.2026 #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.javarush.khmelov.lesson13.controller; | ||
|
|
||
| import com.javarush.khmelov.lesson13.model.GameResult; | ||
| import com.javarush.khmelov.lesson13.model.Player; | ||
| import com.javarush.khmelov.lesson13.model.Scene; | ||
| import com.javarush.khmelov.lesson13.service.GameEngine; | ||
| import com.javarush.khmelov.lesson13.service.PlayerService; | ||
| import com.javarush.khmelov.lesson13.service.QuestRegistry; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.annotation.WebServlet; | ||
| import jakarta.servlet.http.HttpServlet; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import jakarta.servlet.http.HttpSession; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @WebServlet("/game") | ||
| public class GameServlet extends HttpServlet { | ||
|
|
||
| private final PlayerService playerService = PlayerService.getInstance(); | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
|
|
||
| HttpSession session = req.getSession(); | ||
|
|
||
| String login = (String) session.getAttribute("currentPlayerLogin"); | ||
| String questId = (String) session.getAttribute("questId"); | ||
|
|
||
| if (login == null || questId == null) { | ||
| resp.sendRedirect(req.getContextPath() + "/login"); | ||
| return; | ||
| } | ||
|
|
||
| Player player = playerService.findByLogin(login); | ||
| if (player == null) { | ||
| resp.sendRedirect(req.getContextPath() + "/login"); | ||
| return; | ||
| } | ||
| req.setAttribute("player", player); | ||
|
|
||
| String questPath = QuestRegistry.getPath(questId); | ||
| GameEngine engine = new GameEngine(questPath); | ||
|
|
||
| String sceneId = (String) session.getAttribute("sceneId"); | ||
| if (sceneId == null) { | ||
| sceneId = engine.getStartSceneId(); | ||
| session.setAttribute("sceneId", sceneId); | ||
| } | ||
|
|
||
| String choiceId = req.getParameter("choice"); | ||
| if (choiceId != null) { | ||
| GameResult result = engine.makeChoice(sceneId, choiceId); | ||
|
|
||
| if (result.isGameOver()) { | ||
| if (result.isWin()) { | ||
| player.recordWin(); | ||
| } else { | ||
| player.recordLoss(); | ||
| } | ||
| } | ||
|
|
||
| sceneId = result.getNextSceneId(); | ||
| session.setAttribute("sceneId", sceneId); | ||
|
|
||
| req.setAttribute("gameOver", result.isGameOver()); | ||
| req.setAttribute("win", result.isWin()); | ||
| } | ||
|
|
||
| Scene scene = engine.getScene(sceneId); | ||
| req.setAttribute("scene", scene); | ||
|
|
||
| req.getRequestDispatcher("/WEB-INF/game.jsp").forward(req, resp); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.javarush.khmelov.lesson13.controller; | ||
|
|
||
| import com.javarush.khmelov.lesson13.model.Player; | ||
| import com.javarush.khmelov.lesson13.service.PlayerService; | ||
| import com.javarush.khmelov.lesson13.service.QuestRegistry; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.annotation.WebServlet; | ||
| import jakarta.servlet.http.HttpServlet; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import jakarta.servlet.http.HttpSession; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @WebServlet("/login") | ||
| public class LoginServlet extends HttpServlet { | ||
|
|
||
| private final PlayerService playerService = PlayerService.getInstance(); | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Параметры из запроса (req.getParameter) могут содержать вредоносный код или спецсимволы. Необходимо экранировать данные перед использованием или отображением. [WARNING] |
||
|
|
||
| req.setAttribute("players", playerService.getAll()); | ||
| req.setAttribute("quests", QuestRegistry.getQuestIds()); | ||
| req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req, resp); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) | ||
| throws ServletException, IOException { | ||
|
|
||
| String login = req.getParameter("login"); | ||
| String password = req.getParameter("password"); | ||
| String questId = req.getParameter("questId"); | ||
| String action = req.getParameter("action"); // login / register | ||
|
|
||
| try { | ||
| Player player; | ||
|
|
||
| if ("register".equals(action)) { | ||
| player = playerService.register(login, password); | ||
| } else { | ||
| player = playerService.login(login, password); | ||
| } | ||
|
|
||
| HttpSession session = req.getSession(true); | ||
| session.setAttribute("currentPlayerLogin", player.getLogin()); | ||
| session.setAttribute("questId", questId); | ||
| session.removeAttribute("sceneId"); | ||
|
|
||
| resp.sendRedirect(req.getContextPath() + "/game"); | ||
|
|
||
| } catch (IllegalArgumentException e) { | ||
| req.setAttribute("error", e.getMessage()); | ||
| req.setAttribute("players", playerService.getAll()); | ||
| req.setAttribute("quests", QuestRegistry.getQuestIds()); | ||
| req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req, resp); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,23 @@ | ||
| package com.javarush.khmelov.lesson13.controller; | ||
|
|
||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.annotation.WebServlet; | ||
| import jakarta.servlet.http.HttpServlet; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import jakarta.servlet.http.HttpSession; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @WebServlet("") | ||
| public class IndexServlet extends HttpServlet { | ||
| @WebServlet("/restart") | ||
| public class RestartServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
| req.getRequestDispatcher("WEB-INF/index.jsp") | ||
|
||
| .forward(req, resp); | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
| throws IOException { | ||
|
|
||
| HttpSession session = req.getSession(); | ||
| session.invalidate(); | ||
|
|
||
| resp.sendRedirect(req.getContextPath() + "/login"); | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.javarush.khmelov.lesson13.model; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| public class Choice { | ||
|
|
||
| @Getter | ||
| private final String id; | ||
| @Getter | ||
| private final String text; | ||
| @Getter | ||
| private final String nextSceneId; | ||
|
|
||
| public Choice(String id, String text, String nextSceneId) { | ||
| this.id = id; | ||
| this.text = text; | ||
| this.nextSceneId = nextSceneId; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.javarush.khmelov.lesson13.model; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| public class GameResult { | ||
|
|
||
| @Getter | ||
| private final String nextSceneId; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если класс используется только для передачи данных между слоями, рассмотрите возможность сделать его поля final (иммутабельность) для обеспечения надежности данных. [INFO] |
||
| @Getter | ||
| private final boolean gameOver; | ||
| @Getter | ||
| private final boolean win; | ||
|
|
||
| public GameResult(String nextSceneId, boolean gameOver, boolean win) { | ||
| this.nextSceneId = nextSceneId; | ||
| this.gameOver = gameOver; | ||
| this.win = win; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.javarush.khmelov.lesson13.model; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| public class Player { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Класс содержит логику изменения состояния (recordWin). Согласно принципам Clean Code, стоит убедиться, что модель не перегружена бизнес-логикой, которую можно вынести в сервис. [INFO] |
||
|
|
||
| @Getter | ||
| private final String login; | ||
| @Getter | ||
| private final String passwordHash; | ||
| @Getter | ||
| private int gamesPlayed; | ||
| @Getter | ||
| private int wins; | ||
| @Getter | ||
| private int losses; | ||
|
|
||
| public Player(String login, String passwordHash) { | ||
|
|
||
| this.login = login; | ||
| this.passwordHash = passwordHash; | ||
| } | ||
|
|
||
| public boolean checkPassword(String rawPassword) { | ||
| return passwordHash.equals(hash(rawPassword)); | ||
| } | ||
|
|
||
| public void recordWin() { | ||
| gamesPlayed++; | ||
| wins++; | ||
| } | ||
|
|
||
| public void recordLoss() { | ||
| gamesPlayed++; | ||
| losses++; | ||
| } | ||
|
|
||
| private static String hash(String input) { | ||
| return Integer.toHexString(input.hashCode()); | ||
| } | ||
| } | ||
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.
Прямое управление бизнес-логикой квеста в методе doGet сервлета нарушает принцип единственной ответственности (Single Responsibility). Логику стоит делегировать GameService. [ERROR]