From a840bdb14bb721be2214d4ab06cef8e6d62212ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= Date: Tue, 23 Apr 2024 15:00:23 +0200 Subject: [PATCH] feat(DevWeb): Add New game page --- .../java/uppa/project/bean/NewGameBean.java | 90 +++++++++++++++++++ .../project/web/servlet/NewGameServlet.java | 61 +++++++++++++ .../main/webapp/WEB-INF/pages/new-game.jsp | 23 +++++ 3 files changed, 174 insertions(+) create mode 100644 S2/DevWeb/Projet/src/main/java/uppa/project/bean/NewGameBean.java create mode 100644 S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/NewGameServlet.java create mode 100644 S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/new-game.jsp diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/NewGameBean.java b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/NewGameBean.java new file mode 100644 index 0000000..28427ac --- /dev/null +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/NewGameBean.java @@ -0,0 +1,90 @@ +package uppa.project.bean; + +import jakarta.persistence.EntityManager; +import java.io.Serializable; +import uppa.project.database.dao.DAO; +import uppa.project.database.dao.DAOException; +import uppa.project.database.dao.EntityManagerProvider; +import uppa.project.database.dao.jpa.Game_JPA_DAO_Factory; +import uppa.project.database.pojo.Game; +import uppa.project.json.HttpResponse; +import uppa.project.json.HttpResponseCode; + +public class NewGameBean implements Serializable { + + private static final long serialVersionUID = 1L; + + private String difficulty; + private String nbRounds; + private String timer; + private String nbValues; + private String nbColors; + + private Game game; + + private HttpResponse error; + + public NewGameBean() { + } + + public boolean validate() { + EntityManager entityManager = EntityManagerProvider.getInstance(); + + try { + Game game = new Game( + Game.Difficulty.valueOf(difficulty), + Integer.parseInt(timer), + Integer.parseInt(nbRounds), + Integer.parseInt(nbColors), + Integer.parseInt(nbValues) + ); + + // Sauvegarder la game en base de données + DAO gameDAO = new Game_JPA_DAO_Factory().getDAOGame(); + + entityManager.getTransaction().begin(); + this.game = gameDAO.create(game); + entityManager.getTransaction().commit(); + return true; + } catch (NumberFormatException e) { + error = new HttpResponse(HttpResponseCode.BAD_REQUEST, "Les valeurs entrées ne sont pas valides"); + } catch (DAOException e) { + entityManager.getTransaction().rollback(); + error = new HttpResponse(HttpResponseCode.INTERNAL_SERVER_ERROR, "Erreur lors de la création de la partie"); + } + return false; + } + + public NewGameBean setDifficulty(String difficulty) { + this.difficulty = difficulty; + return this; + } + + public NewGameBean setNbRounds(String nbRounds) { + this.nbRounds = nbRounds; + return this; + } + + public NewGameBean setTimer(String timer) { + this.timer = timer; + return this; + } + + public NewGameBean setNbValues(String nbValues) { + this.nbValues = nbValues; + return this; + } + + public NewGameBean setNbColors(String nbColors) { + this.nbColors = nbColors; + return this; + } + + public Game getGame() { + return game; + } + + public HttpResponse getError() { + return error; + } +} diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/NewGameServlet.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/NewGameServlet.java new file mode 100644 index 0000000..b07086e --- /dev/null +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/NewGameServlet.java @@ -0,0 +1,61 @@ +/* + * MainMenuServlet.java, 20/03/2024 + * UPPA M1 TI 2023-2024 + * Pas de copyright, aucun droits + */ + +package uppa.project.web.servlet; + +import com.google.gson.Gson; +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 java.io.IOException; +import java.io.PrintWriter; +import uppa.project.bean.NewGameBean; +import uppa.project.database.pojo.Game; +import uppa.project.json.HttpResponse; +import uppa.project.json.HttpResponseCode; + +@WebServlet(name = "newGameServlet", value = "/new") +public class NewGameServlet extends HttpServlet { + + public void init() { + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + request.getRequestDispatcher("/WEB-INF/pages/new-game.jsp").forward(request, response); + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + Gson gson = new Gson(); + + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + PrintWriter out = response.getWriter(); + + NewGameBean newGameBean = new NewGameBean() + .setDifficulty(request.getParameter("difficulty")) + .setNbRounds(request.getParameter("nbRounds")) + .setTimer(request.getParameter("timer")) + .setNbValues(request.getParameter("nbValues")) + .setNbColors(request.getParameter("nbColors")) + ; + + HttpResponse httpResponse; + if (newGameBean.validate()) { + Game game = newGameBean.getGame(); + httpResponse = new HttpResponse(HttpResponseCode.OK, game.getId().toString()); + } else { + httpResponse = newGameBean.getError(); + } + + out.println(gson.toJson(httpResponse)); + out.flush(); + } + + public void destroy() { + } +} diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/new-game.jsp b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/new-game.jsp new file mode 100644 index 0000000..4b96d15 --- /dev/null +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/new-game.jsp @@ -0,0 +1,23 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@taglib prefix="component" tagdir="/WEB-INF/tags/components" %> +<%@taglib prefix="layout" tagdir="/WEB-INF/tags/layouts" %> +<%@taglib prefix="form" tagdir="/WEB-INF/tags/forms" %> + + + +
+
+
+

Nouvelle partie

+ + + + + +
+
+
+
+