mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-16 01:21:47 +00:00
feat: dev-web - User and Player tests
This commit is contained in:
@@ -250,8 +250,8 @@ public class Game implements Serializable {
|
||||
this.players.add(player);
|
||||
}
|
||||
|
||||
public ArrayList<Card> getDeck() {
|
||||
return deck.getCards();
|
||||
public Deck getDeck() {
|
||||
return deck;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -228,7 +228,7 @@ public class Player implements Serializable {
|
||||
* @return le pourcentage de clics corrects du joueur sur la partie courante
|
||||
*/
|
||||
public double getRatioRightClick() {
|
||||
return (double) rightClickCount * 100 / clickCount;
|
||||
return (double) Math.abs(rightClickCount * 10000 / clickCount) / 100;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,11 +258,11 @@ public class Player implements Serializable {
|
||||
* @return le pourcentage de clics rapides du joueur sur la partie courante
|
||||
*/
|
||||
public double getRatioRapidClick() {
|
||||
return (double) rapidClickCount * 100 / clickCount;
|
||||
return (double) Math.abs(rapidClickCount * 10000 / clickCount) / 100;
|
||||
}
|
||||
|
||||
public ArrayList<Card> getDeck() {
|
||||
return deck.getCards();
|
||||
public Deck getDeck() {
|
||||
return deck;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,6 +23,8 @@ import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
@@ -81,6 +83,9 @@ public class User implements Serializable {
|
||||
* @param gender le genre
|
||||
*/
|
||||
public User(String username, String email, String password, Date birth, Gender gender) {
|
||||
if (!isValidBirthDate(birth)){
|
||||
throw new IllegalArgumentException("La date de naissance n'est pas valide");
|
||||
}
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.password = hashPassword(password);
|
||||
@@ -99,6 +104,9 @@ public class User implements Serializable {
|
||||
* @param gender le genre
|
||||
*/
|
||||
public User(BigDecimal id, String username, String email, String password, Date birth, Gender gender, ArrayList<Player> playedGames) {
|
||||
if (!isValidBirthDate(birth)){
|
||||
throw new IllegalArgumentException("La date de naissance n'est pas valide");
|
||||
}
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
@@ -114,7 +122,7 @@ public class User implements Serializable {
|
||||
* @param password le mot de passe à hasher
|
||||
* @return le mot de passe hashé
|
||||
*/
|
||||
private static String hashPassword(String password) {
|
||||
public static String hashPassword(String password) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
|
||||
@@ -223,18 +231,6 @@ public class User implements Serializable {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcule l'âge de l'utilisateur
|
||||
*
|
||||
* @return l'âge de l'utilisateur en années
|
||||
*/
|
||||
public int getAge() {
|
||||
Date currentDate = new Date();
|
||||
long diff = currentDate.getTime() - birth.getTime();
|
||||
long diffDays = diff / (24 * 60 * 60 * 1000);
|
||||
return (int) (diffDays / 365);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat qui vérifie si le mot de passe fourni est correct
|
||||
*
|
||||
@@ -283,7 +279,7 @@ public class User implements Serializable {
|
||||
* @return le pourcentage de victoire
|
||||
*/
|
||||
public double getWinRate(){
|
||||
return (double) getNbWin() * 100 / getNbPlayedGame();
|
||||
return (double) Math.abs(getNbWin() * 10000 / getNbPlayedGame()) /100;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -318,7 +314,7 @@ public class User implements Serializable {
|
||||
* @return le pourcentage de clics réussi
|
||||
*/
|
||||
public double getRightClickPercentRate(){
|
||||
return (double) getNbRightClicks() * 100 / getNbClicks();
|
||||
return (double) Math.abs(getNbRightClicks() * 10000 / getNbClicks())/100;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,15 +336,21 @@ public class User implements Serializable {
|
||||
* @return le pourcentage de clics les plus rapides
|
||||
*/
|
||||
public double getRapidClickPercentRate(){
|
||||
return (double) getNbRapidClicks() * 100 / getNbClicks();
|
||||
return (double) Math.abs(getNbRapidClicks() * 10000 / getNbClicks())/100;
|
||||
}
|
||||
|
||||
public boolean isValidBirthDate(Date birthdate){
|
||||
Date currentDate = new Date();
|
||||
return birthdate.before(currentDate) || birthdate.equals(currentDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format(
|
||||
"User{id = '%s', username='%s', email='%s', birth=%s, gender=%s}",
|
||||
"User{id='%s', username=%s, email=%s, birth='%s', gender='%s'}",
|
||||
id != null ? id.toString() : "null",
|
||||
username,
|
||||
email,
|
||||
birth.toString(),
|
||||
gender.toString()
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user