Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Oleksandr-JR-Example/Dockerfile → Denys/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN mvn clean package -DskipTests

FROM tomcat:9.0-jdk17

# @TODO Replace Oleksandr-JR-Example.war with your app name
COPY --from=build /app/target/Oleksandr-JR-Example.war /usr/local/tomcat/webapps/ROOT.war

COPY --from=build /app/target/Denys.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["catalina.sh", "run"]
File renamed without changes.
24 changes: 21 additions & 3 deletions Oleksandr-JR-Example/pom.xml → Denys/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ua.com.javarush.gnew</groupId>
<artifactId>Oleksandr-JR-Example</artifactId>
<artifactId>Denys</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Oleksandr-JR-Example Maven Webapp</name>
<name>Denys Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
Expand Down Expand Up @@ -50,6 +50,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down Expand Up @@ -87,9 +94,20 @@
<version>${commons-lang3.version}</version>
</dependency>

<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>2.0.0</version>
</dependency>

</dependencies>
<build>
<finalName>Oleksandr-JR-Example</finalName>
<finalName>Denys</finalName>
<plugins>

<plugin>
Expand Down
64 changes: 64 additions & 0 deletions Denys/src/main/java/controller/QuizServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package controller;

import model.Question;
import repository.QuestionRepository;

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(name = "QuizServlet", value = "/quiz")
public class QuizServlet extends HttpServlet {
private static final QuestionRepository questionRepository = QuestionRepository.getInstance();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();

if ("true".equals(req.getParameter("restart"))) {
session.removeAttribute("currentIndex");

Integer games = (Integer) session.getAttribute("gamesPlayed");
if (games == null) games = 0;
session.setAttribute("gamesPlayed", games + 1);
}

Integer currentIndex = (Integer) session.getAttribute("currentIndex");
if (currentIndex == null) {
currentIndex = 0;
session.setAttribute("currentIndex", currentIndex);
}

Question question = questionRepository.getAll().get(currentIndex);
req.setAttribute("question", question);
req.setAttribute("qIndex", currentIndex);

getServletContext().getRequestDispatcher("/quiz.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();

int qIndex = Integer.parseInt(req.getParameter("qIndex"));
int answerIndex = Integer.parseInt(req.getParameter("answerIndex"));

Question question = questionRepository.getAll().get(qIndex);

if (question.getNextQuestions() == null || question.getNextQuestions().isEmpty()) {
getServletContext().getRequestDispatcher("/result.jsp").forward(req, resp);
return;
}

int nextIndex = question.getNextQuestions().get(answerIndex);
session.setAttribute("currentIndex", nextIndex);

resp.sendRedirect("quiz");
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
@Builder
public class Question {
private String text;

private ArrayList<String> answers;

private int correctAnswer;
}
private ArrayList<Integer> nextQuestions;
private Integer correctAnswer;
}
86 changes: 86 additions & 0 deletions Denys/src/main/java/repository/QuestionRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package repository;

import model.Question;

import java.util.ArrayList;
import java.util.List;

public class QuestionRepository {
private static QuestionRepository INSTANCE;

private final ArrayList<Question> questions = new ArrayList<>();

private QuestionRepository() {
questions.add(Question.builder()
.text("Вам пропонують почати дослідження всесвіту. Прийняти пропозицію?")
.answers(new ArrayList<>(List.of("Прийняти", "Відхилити")))
.nextQuestions(new ArrayList<>(List.of(1, 2)))
.build());

questions.add(Question.builder()
.text("Штовхнути темну матерію?")
.answers(new ArrayList<>(List.of("Так", "Ні")))
.nextQuestions(new ArrayList<>(List.of(3, 5)))
.build());

questions.add(Question.builder()
.text("У результаті вашого відказу, дослідження було перенесене на великий термін. Поразка")
.answers(new ArrayList<>())
.nextQuestions(new ArrayList<>())
.build());

questions.add(Question.builder()
.text("Випарувалася невелика чорна діра, та залишився новий елемент під назвою — частка Бога! Що робимо далі?")
.answers(new ArrayList<>(List.of("Збільшити навантаження!", "Дослідити частку")))
.nextQuestions(new ArrayList<>(List.of(4, 9)))
.build());

questions.add(Question.builder()
.text("Чорна діра вбила все живе! Поразка.")
.answers(new ArrayList<>())
.nextQuestions(new ArrayList<>())
.build());

questions.add(Question.builder()
.text("Що будете робити?")
.answers(new ArrayList<>(List.of("Розвинути проект світлої матерії", "До дому.")))
.nextQuestions(new ArrayList<>(List.of(6, 2)))
.build());

questions.add(Question.builder()
.text("Скільки матерії додати до коллайдеру?")
.answers(new ArrayList<>(List.of("6 ммоль", "100 ммоль")))
.nextQuestions(new ArrayList<>(List.of(7, 8)))
.build());

questions.add(Question.builder()
.text("Людство позбулося енергетичної кризи. Перемога!")
.answers(new ArrayList<>())
.nextQuestions(new ArrayList<>())
.build());

questions.add(Question.builder()
.text("Нейтронна бомба вбила все живе! Поразка!")
.answers(new ArrayList<>())
.nextQuestions(new ArrayList<>())
.build());

questions.add(Question.builder()
.text("Людство позбулося енергетичної кризи. Перемога!")
.answers(new ArrayList<>())
.nextQuestions(new ArrayList<>())
.build());
}

public static QuestionRepository getInstance() {
if (INSTANCE == null) {
INSTANCE = new QuestionRepository();
}

return INSTANCE;
}

public ArrayList<Question> getAll() {
return questions;
}
}
49 changes: 49 additions & 0 deletions Denys/src/main/webapp/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
body {
margin: 0;
font-family: 'Segoe UI', sans-serif;
background: radial-gradient(circle, #0f2027, #203a43, #2c5364);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}

h1, h2 {
font-size: 3em;
margin-bottom: 10px;
text-align: center;
text-shadow: 0 0 10px #00ffff;
}

p {
font-size: 1.5em;
text-align: center;
margin-bottom: 30px;
}

a.button, button {
background: #00ffff;
color: #000;
padding: 12px 25px;
border-radius: 10px;
font-weight: bold;
font-size: 1.2em;
border: none;
cursor: pointer;
transition: 0.3s;
}

a.button:hover, button:hover {
background: #00cccc;
box-shadow: 0 0 10px #00ffff;
}

form {
display: flex;
flex-direction: column;
align-items: center;
gap: 15px;
margin-top: 30px;
}
14 changes: 14 additions & 0 deletions Denys/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<title>Коллайдер</title>
<link rel="stylesheet" href="<%= request.getContextPath() %>/css/style.css">
</head>
<body>
<h1>Коллайдер</h1>
<p>Вітаємо в інтерактивному квесті!<br>Почни своє проходження прямо зараз!</p>
<a href="quiz" class="button">Почати гру</a>
</body>
</html>
47 changes: 47 additions & 0 deletions Denys/src/main/webapp/quiz.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="model.Question" %>
<%@ page import="java.util.*" %>
<%
Question question = (Question) request.getAttribute("question");
int qIndex = (Integer) request.getAttribute("qIndex");
Integer gamesPlayed = (Integer) session.getAttribute("gamesPlayed");
if (gamesPlayed == null) gamesPlayed = 0;

String text = question.getText().toLowerCase();
boolean isEnding = text.contains("поразка") || text.contains("перемога");
%>
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<title>Квест</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>

<h2><%= question.getText() %></h2>

<p>Кількість пройдених ігор: <strong><%= gamesPlayed %></strong></p>

<% if (isEnding) { %>
<a href="quiz?restart=true" class="button">Почати заново</a>
<% } else { %>
<form method="post" action="quiz">
<input type="hidden" name="qIndex" value="<%= qIndex %>"/>
<%
List<String> answers = question.getAnswers();
for (int i = 0; i < answers.size(); i++) {
%>
<button type="submit" name="answerIndex" value="<%= i %>" class="button">
<%= answers.get(i) %>
</button>
<%
}
%>
</form>
<% } %>

</body>
</html>


19 changes: 19 additions & 0 deletions Denys/src/main/webapp/result.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<title>Результат</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>

<h1>Гра завершена!</h1>

<p>Дякуємо за гру!</p>
<p>Ви вже зіграли <strong><%= session.getAttribute("gamesPlayed") != null ? session.getAttribute("gamesPlayed") : 1 %></strong> раз(ів).</p>

<a href="quiz?restart=true" class="button">Почати заново</a>

</body>
</html>
Loading