feat(DevWeb): Add New game page

This commit is contained in:
Lucàs
2024-04-23 15:00:23 +02:00
parent 46764fee99
commit a840bdb14b
3 changed files with 174 additions and 0 deletions
@@ -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<Game> 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;
}
}
@@ -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() {
}
}
@@ -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" %>
<layout:form>
<component:hero>
<div class="columns is-centered">
<div class="column is-5-tablet is-5-desktop is-5-widescreen">
<div class="box">
<h1 class="title has-text-centered">Nouvelle partie</h1>
<form:new-game>
<jsp:attribute name="back_button">
<div class="column">
<a class="button is-fullwidth" href="${pageContext.request.contextPath}/lobby">Revenir au menu</a>
</div>
</jsp:attribute>
</form:new-game>
</div>
</div>
</div>
</component:hero>
</layout:form>