mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-14 01:21:49 +00:00
feat: dev-web - servlet part
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package uppa.project;
|
||||
|
||||
public class Global {
|
||||
public final class Global {
|
||||
|
||||
public static final String PERSISTENCE_UNIT_NAME = "prod";
|
||||
public static final String PERSISTENCE_UNIT_NAME_TEST = "test";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package uppa.project;
|
||||
|
||||
import java.util.Calendar;
|
||||
import uppa.project.dao.DAO;
|
||||
import uppa.project.dao.DAOException;
|
||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
@@ -7,8 +8,8 @@ import uppa.project.pojo.User;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
public static void main(String[] args) throws DAOException {
|
||||
try {
|
||||
Game_JPA_DAO_Factory jpaDaoFactory = new Game_JPA_DAO_Factory();
|
||||
DAO<User> daoJpaUser = jpaDaoFactory.getDAOUser();
|
||||
// DAO<Game> daoJpaGame = jpaDaoFactory.getDAOGame();
|
||||
@@ -21,17 +22,17 @@ public class Main {
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
// // Ajout d'User :
|
||||
// Calendar cal1 = Calendar.getInstance();
|
||||
// cal1.set(1996, Calendar.FEBRUARY, 20);
|
||||
// User user1 = new User("Kevin", "Mitresse", cal1.getTime(), User.Gender.MALE);
|
||||
//
|
||||
// Calendar cal2 = Calendar.getInstance();
|
||||
// cal2.set(2002, Calendar.JUNE, 28);
|
||||
// User user2 = new User("Lucàs", "Vabre", cal2.getTime(), User.Gender.MALE);
|
||||
//
|
||||
// daoJpaUser.create(user1);
|
||||
// daoJpaUser.create(user2);
|
||||
// Ajout d'User :
|
||||
Calendar cal1 = Calendar.getInstance();
|
||||
cal1.set(1996, Calendar.FEBRUARY, 20);
|
||||
User user1 = new User("Kevin","kmitresse@gmail.com", "Mitresse", cal1.getTime(), User.Gender.MALE);
|
||||
|
||||
Calendar cal2 = Calendar.getInstance();
|
||||
cal2.set(2002, Calendar.JUNE, 28);
|
||||
User user2 = new User("Lucàs", "lucas@gmail.com" ,"Vabre", cal2.getTime(), User.Gender.MALE);
|
||||
|
||||
daoJpaUser.create(user1);
|
||||
daoJpaUser.create(user2);
|
||||
|
||||
// System.out.println("test récupération user");
|
||||
// User[] users2 = daoJpaUser.findByField("username", "Kevin");
|
||||
|
||||
@@ -8,9 +8,6 @@ package uppa.project.pojo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Représentation d'un paquet de cartes
|
||||
@@ -24,7 +21,7 @@ public class Deck {
|
||||
* Ensemble de cartes du paquet
|
||||
* @see Card
|
||||
*/
|
||||
private Set<Card> cards;
|
||||
private ArrayList<Card> cards;
|
||||
|
||||
/**
|
||||
* Constructeur par défaut
|
||||
@@ -38,9 +35,13 @@ public class Deck {
|
||||
cards = initializeDeck(nbColors, nbValues);
|
||||
}
|
||||
|
||||
public Set<Card> getCards() {
|
||||
/**
|
||||
* @return l'ensemble de cartes du paquet
|
||||
*/
|
||||
public ArrayList<Card> getCards() {
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Créé un paquet de cartes mélangé avec un nombre de couleurs et de valeurs donné
|
||||
*
|
||||
@@ -48,9 +49,10 @@ public class Deck {
|
||||
* @param nbValues nombre de valeurs (doit être compris entre 1 et le nombre de valeurs de {@link Card.Value})
|
||||
* @return un ensemble de cartes mélangées
|
||||
*/
|
||||
private static Set<Card> initializeDeck(int nbColors, int nbValues) {
|
||||
Set<Card> cards = createSetOfCard(nbColors, nbValues);
|
||||
return shuffleSetOfCard(cards);
|
||||
private static ArrayList<Card> initializeDeck(int nbColors, int nbValues) {
|
||||
ArrayList<Card> cards = createSetOfCard(nbColors, nbValues);
|
||||
shuffleSetOfCard(cards);
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,8 +65,8 @@ public class Deck {
|
||||
* @throws IllegalArgumentException si le nombre de couleurs ou de valeurs est incorrect
|
||||
* @return un ensemble de cartes
|
||||
*/
|
||||
private static Set<Card> createSetOfCard(int nbColors, int nbValues) throws IllegalArgumentException {
|
||||
Set<Card> cards = new HashSet<>();
|
||||
private static ArrayList<Card> createSetOfCard(int nbColors, int nbValues) throws IllegalArgumentException {
|
||||
ArrayList<Card> cards = new ArrayList<>(nbColors*nbValues);
|
||||
|
||||
if (nbColors < 1 || nbColors > Card.Color.values().length) {
|
||||
throw new IllegalArgumentException("Le nombre de couleurs doit être compris entre 1 et " + Card.Color.values().length);
|
||||
@@ -87,10 +89,8 @@ public class Deck {
|
||||
* @param cards ensemble de cartes à mélanger
|
||||
* @return un ensemble de cartes mélangées
|
||||
*/
|
||||
private static HashSet<Card> shuffleSetOfCard(Set<Card> cards) {
|
||||
List<Card> cardList = new ArrayList<>(cards);
|
||||
Collections.shuffle(cardList);
|
||||
return new HashSet<>(cardList);
|
||||
private static void shuffleSetOfCard(ArrayList<Card> cards) {
|
||||
Collections.shuffle(cards);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ import jakarta.persistence.TemporalType;
|
||||
import jakarta.persistence.Transient;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Représentation d'une partie de jeu
|
||||
@@ -60,7 +60,7 @@ public class Game implements Serializable {
|
||||
private int nbValuesPerColor;
|
||||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private Set<Player> players;
|
||||
private ArrayList<Player> players;
|
||||
|
||||
@Transient
|
||||
private Deck deck;
|
||||
@@ -98,7 +98,7 @@ public class Game implements Serializable {
|
||||
* @param nbValuesPerColor le nombre de valeurs par couleur
|
||||
* @param players les joueurs de la partie
|
||||
*/
|
||||
public Game(BigDecimal id, Date createdAt, Difficulty difficulty, int nbRounds, int nbColors, int nbValuesPerColor, Set<Player> players) {
|
||||
public Game(BigDecimal id, Date createdAt, Difficulty difficulty, int nbRounds, int nbColors, int nbValuesPerColor, ArrayList<Player> players) {
|
||||
this.id = id;
|
||||
this.createdAt = createdAt;
|
||||
this.difficulty = difficulty;
|
||||
@@ -187,7 +187,7 @@ public class Game implements Serializable {
|
||||
/**
|
||||
* @return les joueurs de la partie
|
||||
*/
|
||||
public Set<Player> getPlayers() {
|
||||
public ArrayList<Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ public class Game implements Serializable {
|
||||
*
|
||||
* @param players les nouveaux joueurs
|
||||
*/
|
||||
public void setPlayers(Set<Player> players) {
|
||||
public void setPlayers(ArrayList<Player> players) {
|
||||
this.players = players;
|
||||
}
|
||||
|
||||
@@ -216,7 +216,7 @@ public class Game implements Serializable {
|
||||
this.players.add(player);
|
||||
}
|
||||
|
||||
public Set<Card> getDeck() {
|
||||
public ArrayList<Card> getDeck() {
|
||||
return deck.getCards();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,8 @@ import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Transient;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Représentation d'un joueur
|
||||
@@ -259,7 +259,7 @@ public class Player implements Serializable {
|
||||
return (double) rapidClickCount * 100 / clickCount;
|
||||
}
|
||||
|
||||
public Set<Card> getDeck() {
|
||||
public ArrayList<Card> getDeck() {
|
||||
return deck.getCards();
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -23,9 +23,9 @@ import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Représentation d'un utilisateur
|
||||
@@ -60,10 +60,10 @@ public class User implements Serializable {
|
||||
private Gender gender;
|
||||
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private Set<Player> playedGame;
|
||||
private ArrayList<Player> playedGames;
|
||||
|
||||
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
private Set<RecoveryPasswordToken> recoveryPasswordTokens;
|
||||
private ArrayList<RecoveryPasswordToken> recoveryPasswordTokens;
|
||||
|
||||
/**
|
||||
* Constructeur par défaut
|
||||
@@ -98,13 +98,14 @@ public class User implements Serializable {
|
||||
* @param birth la date de naissance
|
||||
* @param gender le genre
|
||||
*/
|
||||
public User(BigDecimal id, String username, String email, String password, Date birth, Gender gender) {
|
||||
public User(BigDecimal id, String username, String email, String password, Date birth, Gender gender, ArrayList<Player> playedGames) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.birth = birth;
|
||||
this.gender = gender;
|
||||
this.playedGames = playedGames;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,13 +246,22 @@ public class User implements Serializable {
|
||||
return hashedPassword != null && hashedPassword.equals(this.password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère la liste des parties jouées par l'utilisateur
|
||||
*
|
||||
* @return la liste des parties jouées
|
||||
*/
|
||||
public ArrayList<Player> getPlayedGames() {
|
||||
return playedGames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère le nombre de parties jouées
|
||||
*
|
||||
* @return le nombre de parties jouées
|
||||
*/
|
||||
public int getNbPlayedGame() {
|
||||
return playedGame.size();
|
||||
return playedGames.size();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,7 +271,7 @@ public class User implements Serializable {
|
||||
*/
|
||||
public int getNbWin(){
|
||||
int nbWin = 0;
|
||||
for (Player p : playedGame) {
|
||||
for (Player p : playedGames) {
|
||||
if (p.isWinner()) nbWin++;
|
||||
}
|
||||
return nbWin;
|
||||
@@ -283,7 +293,7 @@ public class User implements Serializable {
|
||||
*/
|
||||
public int getNbClicks(){
|
||||
int nbClicks = 0;
|
||||
for (Player p : playedGame) {
|
||||
for (Player p : playedGames) {
|
||||
nbClicks += p.getClickCount();
|
||||
}
|
||||
return nbClicks;
|
||||
@@ -296,7 +306,7 @@ public class User implements Serializable {
|
||||
*/
|
||||
public int getNbRightClicks(){
|
||||
int nbRightClicks = 0;
|
||||
for (Player p : playedGame) {
|
||||
for (Player p : playedGames) {
|
||||
nbRightClicks += p.getRightClickCount();
|
||||
}
|
||||
return nbRightClicks;
|
||||
@@ -318,7 +328,7 @@ public class User implements Serializable {
|
||||
*/
|
||||
public int getNbRapidClicks(){
|
||||
int nbRapidClicks = 0;
|
||||
for (Player p : playedGame) {
|
||||
for (Player p : playedGames) {
|
||||
nbRapidClicks += p.getRapidClickCount();
|
||||
}
|
||||
return nbRapidClicks;
|
||||
|
||||
@@ -19,7 +19,7 @@ import uppa.project.Global;
|
||||
* @see jakarta.persistence.EntityManager
|
||||
*/
|
||||
public final class EntityManagerProvider {
|
||||
private static final String PERSISTENCE_UNIT_NAME = Global.PERSISTENCE_UNIT_NAME;
|
||||
private static String PERSISTENCE_UNIT_NAME = Global.PERSISTENCE_UNIT_NAME;
|
||||
|
||||
private static EntityManager instance;
|
||||
private static EntityManagerFactory factory;
|
||||
@@ -35,6 +35,9 @@ public final class EntityManagerProvider {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void setPersitenceUnitName(String name) {
|
||||
PERSISTENCE_UNIT_NAME = name;
|
||||
}
|
||||
public static void close() {
|
||||
if (instance.isOpen()) instance.close();
|
||||
if (factory.isOpen()) factory.close();
|
||||
|
||||
@@ -12,6 +12,11 @@ import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import uppa.project.pojo.Game;
|
||||
import uppa.project.pojo.Player;
|
||||
import uppa.project.pojo.User;
|
||||
|
||||
@WebServlet(name = "mainMenuServlet", value = "/main-menu")
|
||||
public class MainMenuServlet extends HttpServlet {
|
||||
@@ -20,15 +25,39 @@ public class MainMenuServlet extends HttpServlet {
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
if (request.getSession().getAttribute("user") == null) {
|
||||
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
|
||||
User user = (User) request.getSession().getAttribute("user");
|
||||
if (user == null) {
|
||||
response.sendRedirect(request.getContextPath() + "/login");
|
||||
return;
|
||||
}
|
||||
|
||||
manageNewGame(request, response, user);
|
||||
manageStatistiques(request, response, user);
|
||||
response.sendRedirect(request.getContextPath() + "/main-menu");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
|
||||
private void manageMainMenu(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
}
|
||||
|
||||
private void manageNewGame(HttpServletRequest request, HttpServletResponse response, User sessionUser) throws IOException, ServletException {
|
||||
List<User> connectedUsers = new ArrayList<User>();
|
||||
/*TODO: récuperer la liste des joueurs connectés
|
||||
penser à retirer l'utilisateur principal de la liste*/
|
||||
connectedUsers.remove(sessionUser);
|
||||
request.setAttribute("connectedUsers", connectedUsers);
|
||||
request.getRequestDispatcher("/WEB-INF/views/new-game.jsp").forward(request, response);
|
||||
}
|
||||
|
||||
private void manageStatistiques(HttpServletRequest request, HttpServletResponse response, User sessionUser) throws IOException, ServletException {
|
||||
List<Game> games = new ArrayList<Game>();
|
||||
for(Player player : sessionUser.getPlayedGames()) {
|
||||
Game game = player.getGame();
|
||||
game.sortPlayersByScore();
|
||||
games.add(game);
|
||||
}
|
||||
request.setAttribute("games", games);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,16 +26,6 @@ public class NewGameServlet extends HttpServlet {
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
if (request.getSession().getAttribute("user") == null) {
|
||||
response.sendRedirect(request.getContextPath() + "/login");
|
||||
return;
|
||||
}
|
||||
User[] users = new User[0];
|
||||
/*TODO: récuperer la liste des joueurs connectés
|
||||
penser à retirer l'utilisateur principal de la liste*/
|
||||
|
||||
request.setAttribute("connectedUsers", users);
|
||||
request.getRequestDispatcher("/WEB-INF/views/new-game.jsp").forward(request, response);
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
|
||||
Reference in New Issue
Block a user