diff --git a/S2/DevWeb/Projet/README.md b/S2/DevWeb/Projet/README.md index c53af3a..057ce85 100644 --- a/S2/DevWeb/Projet/README.md +++ b/S2/DevWeb/Projet/README.md @@ -50,6 +50,8 @@ Depuis IntelliJ, ouvrir l'onglet `file > project structure` et ajouter les artef ![Onglet project_structure.png](readmeTools/project_structure.png) Nous utilisons tomcat pour lancer notre projet. + Sur IntelliJ, vous pouvez ajouter une configuration Tomcat depuis l'onglet "edit run configurations", puis en cliquant sur le bouton "+" en haut à gauche de la fenêtre de lancement. + Editez la configuration comme suit (le port utilisé pour Tomcat conseillé est 8080, mais vous pouvez le changer si vous le souhaitez ou s'il est déjà utilisé par un autre service sur votre machine) : ![Configuration Tomcat.png](readmeTools/tomcat_configuration.png) diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Card.java b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Card.java index 05f67f4..83697c4 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Card.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Card.java @@ -74,4 +74,11 @@ public class Card { ", value=" + value + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Card card)) return false; + return getColor() == card.getColor() && getValue() == card.getValue(); + } } diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Game.java b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Game.java index d70f07a..56f6ed7 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Game.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Game.java @@ -289,7 +289,7 @@ public class Game implements Serializable { } /** - * Tri des joueurs de la partie par score + * Tri des joueurs de la partie par score puis par rapidité */ public void sortPlayersByScoreAndRapidity() { players.sort((p1, p2) -> { @@ -305,9 +305,9 @@ public class Game implements Serializable { * * @return le nom du gagnant */ - public String getWinner(){ - sortPlayersByScore(); - return players.get(0).getUser().getUsername(); + public Player getWinner(){ + this.sortPlayersByScoreAndRapidity(); + return players.get(0); } /** diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java index b90db50..f356417 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/database/pojo/Player.java @@ -257,7 +257,7 @@ public class Player implements Serializable { */ public double getRatioRightClick() { if (clickCount == 0 || rightClickCount == 0) return 0; - return (double) Math.abs(rightClickCount * 10000 / clickCount) / 100; + return (double) Math.abs(rightClickCount * 10000 / game.getNbRounds()) / 100; } /** @@ -288,7 +288,7 @@ public class Player implements Serializable { */ public double getRatioRapidClick() { if (clickCount == 0 || rapidClickCount == 0) return 0; - return (double) Math.abs(rapidClickCount * 10000 / clickCount) / 100; + return (double) Math.abs(rapidClickCount * 10000 / game.getNbRounds()) / 100; } public Deck getDeck() { diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/GameStatisticsServlet.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/GameStatisticsServlet.java index 27ef716..80effd8 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/GameStatisticsServlet.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/GameStatisticsServlet.java @@ -22,6 +22,9 @@ public class GameStatisticsServlet extends HttpServlet { try { DAO gameDAO = new Game_JPA_DAO_Factory().getDAOGame(); game = gameDAO.findById(Integer.parseInt(request.getParameter("id"))); + for(Player p : game.getPlayers()) { + System.out.println(p.toString()); + } request.removeAttribute("id"); game.sortPlayersByScoreAndRapidity(); request.setAttribute("game", game); diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/RulesServlet.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/RulesServlet.java new file mode 100644 index 0000000..8341469 --- /dev/null +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/RulesServlet.java @@ -0,0 +1,18 @@ +package uppa.project.web.servlet; + +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; + +@WebServlet(name = "rulesServlet", value = "/rules") +public class RulesServlet extends HttpServlet { + public void init() { + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + request.getRequestDispatcher("/WEB-INF/pages/rules.jsp").forward(request, response); + } +} diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java index 39c1318..a25213f 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/websocket/GameWS.java @@ -11,6 +11,7 @@ import jakarta.websocket.server.PathParam; import jakarta.websocket.server.ServerEndpoint; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import uppa.project.database.dao.DAO; import uppa.project.database.dao.DAOException; import uppa.project.database.dao.EntityManagerProvider; @@ -106,45 +107,90 @@ public class GameWS { if (gameClickCount == 1) player.incrementRapidClickCount(); // Check if the player has clicked on the right card - switch (choice) { - case COLOR_VALUE -> { - if (gameCard.getColor().equals(playerCard.getColor()) && gameCard.getValue() == playerCard.getValue()) { - player.incrementRightClickCount(); - player.setScore(playerScore + 2); - } else { - player.setScore(playerScore - 1); - } - } - case COLOR -> { - if (gameCard.getColor().equals(playerCard.getColor())) { - if (gameCard.getValue() != playerCard.getValue()) { + if (game.getDifficulty().equals(Game.Difficulty.EASY)) { + switch (choice) { + case COLOR_VALUE -> { + if (gameCard.equals(playerCard)) { player.incrementRightClickCount(); player.setScore(playerScore + 2); } else { - player.setScore(playerScore + 1); + player.setScore(playerScore - 1); } - } else { - player.setScore(playerScore - 1); } - } - case VALUE -> { - if(gameCard.getValue() == playerCard.getValue()) { - if (!gameCard.getColor().equals(playerCard.getColor())) { + case COLOR -> { + if (gameCard.getColor().equals(playerCard.getColor())) { + if (gameCard.getValue() != playerCard.getValue()) { + player.incrementRightClickCount(); + player.setScore(playerScore + 2); + } else { + player.setScore(playerScore + 1); + } + } else { + player.setScore(playerScore - 1); + } + } + case VALUE -> { + if (gameCard.getValue() == playerCard.getValue()) { + if (!gameCard.getColor().equals(playerCard.getColor())) { + player.incrementRightClickCount(); + player.setScore(playerScore + 2); + } else { + player.setScore(playerScore + 1); + } + } else { + player.setScore(playerScore - 1); + } + } + case NONE -> { + if (!gameCard.getColor().equals(playerCard.getColor()) && gameCard.getValue() != playerCard.getValue()) { player.incrementRightClickCount(); player.setScore(playerScore + 2); } else { - player.setScore(playerScore + 1); + player.setScore(playerScore - 1); } - } else { - player.setScore(playerScore - 1); } } - case NONE -> { - if (!gameCard.getColor().equals(playerCard.getColor()) && gameCard.getValue() != playerCard.getValue()) { - player.incrementRightClickCount(); - player.setScore(playerScore + 2); - } else { - player.setScore(playerScore - 1); + } else { + int nbSameCard = countSameCard(gameCard, game.getPlayers(), game.getCurrentRound()); + int nbSameColor = countSameColor(gameCard, game.getPlayers(), game.getCurrentRound()); + int nbSameValue = countSameValue(gameCard, game.getPlayers(), game.getCurrentRound()); + int nbNone = countNone(gameCard, game.getPlayers(), game.getCurrentRound()); + switch (choice) { + case COLOR_VALUE -> { + if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { + player.incrementRightClickCount(); + player.setScore(playerScore + 2); + } else { + player.setScore(playerScore - 1); + } + } + case COLOR -> { + if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { + player.setScore(playerScore + 1); + } else if ((nbSameColor > nbSameCard) && (nbSameColor >= nbSameValue) && (nbSameColor >= nbNone)) { + player.incrementRightClickCount(); + player.setScore(playerScore + 2); + } else { + player.setScore(playerScore - 1); + } + } + case VALUE -> { + if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { + player.setScore(playerScore + 1); + } else if ((nbSameValue > nbSameCard) && (nbSameValue > nbSameColor) && (nbSameValue >= nbNone)) { + player.incrementRightClickCount(); + player.setScore(playerScore + 2); + } else { + player.setScore(playerScore - 1); + } + } + case NONE -> { + if ((nbNone > nbSameCard) && (nbNone >= nbSameColor) && (nbNone > nbSameValue)){ + player.incrementRightClickCount(); + player.setScore(playerScore + 2); + } else { + player.setScore(playerScore - 1); + } } } } @@ -194,6 +240,78 @@ public class GameWS { } } + /** + * Retourne le nombre de joueurs avec une carte identique à celle du plateau + * @param gameCard carte du plateau + * @param players liste des joueurs + * @param currentRound manche courante + * @return nombre de cartes identiques à celle du plateau + */ + private int countSameCard(Card gameCard, List players, int currentRound) { + int counter = 0; + for (Player player : players) { + Card card = player.getDeck().getCards().get(currentRound); + if (gameCard.equals(card)) { + counter ++; + } + } + return counter; + } + + /** + * Retourne le nombre de joueurs avec une carte avec seulement la couleur correspondante à celle du plateau + * @param gameCard + * @param players + * @param currentRound + * @return nombre de couleurs identiques à celle du plateau + */ + private int countSameColor(Card gameCard, List players, int currentRound) { + int counter = 0; + for (Player player : players) { + Card card = player.getDeck().getCards().get(currentRound); + if (gameCard.getColor().equals(card.getColor()) && !gameCard.getValue().equals(card.getValue())) { + counter ++; + } + } + return counter; + } + + /** + * Retourne le nombre de joueurs avec une carte avec seulement la valeur correspondante à celle du plateau + * @param gameCard carte du plateau + * @param players liste des joueurs + * @param currentRound manche courante + * @return nombre de valeurs identiques à celle du plateau + */ + private int countSameValue(Card gameCard, List players, int currentRound) { + int counter = 0; + for (Player player : players) { + Card card = player.getDeck().getCards().get(currentRound); + if (gameCard.getValue().equals(card.getValue()) && !gameCard.getColor().equals(card.getColor())) { + counter ++; + } + } + return counter; + } + + /** + * Retourne le nombre de joueurs avec une carte totalement différente de celle du plateau + * @param gameCard + * @param players + * @param currentRound + * @return nombre de cartes totalement différentes de celle du plateau + */ + private int countNone(Card gameCard, List players, int currentRound) { + int counter = 0; + for (Player player : players) { + Card card = player.getDeck().getCards().get(currentRound); + if (!gameCard.getColor().equals(card.getColor()) && !gameCard.getValue().equals(card.getValue())) { + counter ++; + } + } + return counter; + } + private void broadcast(String message) { for (Player player : games.get(game)) { try { diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp index ff1df51..7e75178 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/game.jsp @@ -46,18 +46,19 @@ -<%-- TODO: Si le temps nous le permet, mettre en place un système de pagination --%> -

Parties jouées

@@ -44,11 +42,12 @@ <% for (int i = 0; i < user.getPlayedGames().size(); i++) { Player player = user.getPlayedGames().get(i); + System.out.println(player.toString()); %> - + <% } %>
<%= player.getGame().getCreatedAt().toLocaleString() %> <%= player.getScore() %><%= player.getGame().getWinner() %><%= player.getGame().getWinner().getUser().getUsername() %> Voir