fix: devWeb - fix score management

This commit is contained in:
kmitresse
2024-06-14 11:37:19 +02:00
parent 01c1793c48
commit e3397efcf4
3 changed files with 84 additions and 34 deletions
@@ -53,12 +53,19 @@ public class Player implements Serializable {
@Column(name = "click_count") @Column(name = "click_count")
private int clickCount; private int clickCount;
@Column(name = "right_click_count") @Column(name = "right_click_count")
private int rightClickCount; private int rightClickCount;
@Transient
private int tmpRightClickCount;
@Transient @Transient
private int partialRightClickCount; private int partialRightClickCount;
@Transient
private int tmpPartialRightClickCount;
@Column(name = "rapid_click_count") @Column(name = "rapid_click_count")
private int rapidClickCount; private int rapidClickCount;
@@ -90,6 +97,8 @@ public class Player implements Serializable {
this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor()); this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor());
this.deck.shuffle(); this.deck.shuffle();
this.partialRightClickCount = 0; this.partialRightClickCount = 0;
this.tmpRightClickCount = 0;
this.tmpPartialRightClickCount = 0;
} }
/** /**
@@ -109,6 +118,8 @@ public class Player implements Serializable {
this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor()); this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor());
this.deck.shuffle(); this.deck.shuffle();
this.partialRightClickCount = 0; this.partialRightClickCount = 0;
this.tmpRightClickCount = 0;
this.tmpPartialRightClickCount = 0;
} }
/** /**
@@ -132,6 +143,8 @@ public class Player implements Serializable {
this.rightClickCount = rightClickCount; this.rightClickCount = rightClickCount;
this.rapidClickCount = rapidClickCount; this.rapidClickCount = rapidClickCount;
this.partialRightClickCount = 0; this.partialRightClickCount = 0;
this.tmpRightClickCount = 0;
this.tmpPartialRightClickCount = 0;
this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor()); this.deck = new Deck(game.getNbColors(), game.getNbValuesPerColor());
this.deck.shuffle(); this.deck.shuffle();
@@ -265,6 +278,22 @@ public class Player implements Serializable {
rightClickCount++; rightClickCount++;
} }
/**
* @return le nombre de clics corrects du joueur
*/
public int getTmpRightClickCount() {
return tmpRightClickCount;
}
/**
* Incrémente de 1 le nombre de clics corrects
*/
public void incrementTmpRightClickCount() {
tmpRightClickCount++;
}
/** /**
* @return le pourcentage de clics corrects du joueur sur la partie courante * @return le pourcentage de clics corrects du joueur sur la partie courante
*/ */
@@ -294,6 +323,20 @@ public class Player implements Serializable {
partialRightClickCount++; partialRightClickCount++;
} }
/**
* @return le nombre de clics corrects du joueur
*/
public int getTmpPartialRightClickCount() {
return tmpPartialRightClickCount;
}
/**
* Incrémente de 1 le nombre de clics corrects
*/
public void incrementTmpPartialRightClickCount() {
tmpPartialRightClickCount++;
}
/** /**
* Modifie le nombre de clics rapides * Modifie le nombre de clics rapides
* *
@@ -357,4 +400,7 @@ public class Player implements Serializable {
} }
public void setPartialRightClickCount(int tmpPartialRightClickCount) {
this.partialRightClickCount = tmpPartialRightClickCount;
}
} }
@@ -100,6 +100,12 @@ public class GameWS {
timers.put(game, timer); timers.put(game, timer);
} }
ArrayList<Card> playersCurrentCards = new ArrayList<>(games.get(game).size());
for (Player player : games.get(game)) {
playersCurrentCards.add(player.getDeck().getCards().get((player.getPartialRightClickCount()+player.getRightClickCount()) % player.getDeck().getCards().size()));
}
if (message.getType().equals("click")) { if (message.getType().equals("click")) {
ClickChoice choice = gson.fromJson(message.getData(), ClickChoice.class); ClickChoice choice = gson.fromJson(message.getData(), ClickChoice.class);
@@ -188,10 +194,11 @@ public class GameWS {
case TIMER_END -> {} case TIMER_END -> {}
} }
} else { } else {
int nbSameCard = countSameCard(gameCard, games.get(game), game.getCurrentRound()); int nbSameCard = countSameCard(gameCard, playersCurrentCards);
int nbSameColor = countSameColor(gameCard, games.get(game), game.getCurrentRound()); int nbSameColor = countSameColor(gameCard, playersCurrentCards);
int nbSameValue = countSameValue(gameCard, games.get(game), game.getCurrentRound()); int nbSameValue = countSameValue(gameCard, playersCurrentCards);
int nbNone = countNone(gameCard, games.get(game), game.getCurrentRound()); int nbNone = countNone(gameCard, playersCurrentCards);
switch (choice) { switch (choice) {
case COLOR_VALUE -> { case COLOR_VALUE -> {
if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) {
@@ -199,7 +206,7 @@ public class GameWS {
player.incrementRapidClickCount(); player.incrementRapidClickCount();
playerScore++; playerScore++;
} }
player.incrementRightClickCount(); player.incrementTmpRightClickCount();
player.setScore(playerScore + 2); player.setScore(playerScore + 2);
} else { } else {
player.setScore(playerScore - 1); player.setScore(playerScore - 1);
@@ -207,14 +214,14 @@ public class GameWS {
} }
case COLOR -> { case COLOR -> {
if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) {
player.incrementPartialRightClickCount(); player.incrementTmpPartialRightClickCount();
player.setScore(playerScore + 1); player.setScore(playerScore + 1);
} else if ((nbSameColor > nbSameCard) && (nbSameColor >= nbSameValue) && (nbSameColor >= nbNone)) { } else if ((nbSameColor > nbSameCard) && (nbSameColor >= nbSameValue) && (nbSameColor >= nbNone)) {
if (isRapid) { if (isRapid) {
player.incrementRapidClickCount(); player.incrementRapidClickCount();
playerScore++; playerScore++;
} }
player.incrementRightClickCount(); player.incrementTmpRightClickCount();
player.setScore(playerScore + 2); player.setScore(playerScore + 2);
} else { } else {
player.setScore(playerScore - 1); player.setScore(playerScore - 1);
@@ -222,14 +229,14 @@ public class GameWS {
} }
case VALUE -> { case VALUE -> {
if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) { if ((nbSameCard >= nbSameColor) && (nbSameCard >= nbSameValue) && (nbSameCard >= nbNone)) {
player.incrementPartialRightClickCount(); player.incrementTmpPartialRightClickCount();
player.setScore(playerScore + 1); player.setScore(playerScore + 1);
} else if ((nbSameValue > nbSameCard) && (nbSameValue > nbSameColor) && (nbSameValue >= nbNone)) { } else if ((nbSameValue > nbSameCard) && (nbSameValue > nbSameColor) && (nbSameValue >= nbNone)) {
if (isRapid) { if (isRapid) {
player.incrementRapidClickCount(); player.incrementRapidClickCount();
playerScore++; playerScore++;
} }
player.incrementRightClickCount(); player.incrementTmpRightClickCount();
player.setScore(playerScore + 2); player.setScore(playerScore + 2);
} else { } else {
player.setScore(playerScore - 1); player.setScore(playerScore - 1);
@@ -241,7 +248,7 @@ public class GameWS {
player.incrementRapidClickCount(); player.incrementRapidClickCount();
playerScore++; playerScore++;
} }
player.incrementRightClickCount(); player.incrementTmpRightClickCount();
player.setScore(playerScore + 2); player.setScore(playerScore + 2);
} else { } else {
player.setScore(playerScore - 1); player.setScore(playerScore - 1);
@@ -249,6 +256,7 @@ public class GameWS {
} }
case TIMER_END -> {} case TIMER_END -> {}
} }
} }
// Diffuser le score du joueur // Diffuser le score du joueur
@@ -260,7 +268,11 @@ public class GameWS {
timers.get(game).cancel(); timers.get(game).cancel();
// Réinitialiser les clics courrants // Réinitialiser les clics courrants
for (Player p : games.get(game)) p.setCurrentClick(null); for (Player p : games.get(game)) {
p.setRightClickCount(p.getTmpRightClickCount());
p.setPartialRightClickCount(p.getTmpPartialRightClickCount());
p.setCurrentClick(null);
}
List<Player> players = games.get(game); List<Player> players = games.get(game);
players.sort((p1, p2) -> { players.sort((p1, p2) -> {
@@ -308,15 +320,14 @@ public class GameWS {
* Retourne le nombre de joueurs avec une carte identique à celle du plateau * Retourne le nombre de joueurs avec une carte identique à celle du plateau
* *
* @param gameCard carte du plateau * @param gameCard carte du plateau
* @param players liste des joueurs * @param currentCards liste des cartes des joueurs
* @param currentRound manche courante
* @return nombre de cartes identiques à celle du plateau * @return nombre de cartes identiques à celle du plateau
*/ */
private int countSameCard(Card gameCard, List<Player> players, int currentRound) { private int countSameCard(Card gameCard, List<Card> currentCards) {
int counter = 0; int counter = 0;
for (Player player : players) { for (Card card : currentCards) {
Card card = player.getDeck().getCards().get(currentRound);
if (gameCard.equals(card)) { if (gameCard.equals(card)) {
counter++; counter++;
} }
} }
@@ -326,15 +337,13 @@ public class GameWS {
/** /**
* Retourne le nombre de joueurs avec une carte avec seulement la couleur correspondante à celle du plateau * Retourne le nombre de joueurs avec une carte avec seulement la couleur correspondante à celle du plateau
* *
* @param gameCard * @param gameCard carte du plateau
* @param players * @param currentCards liste des cartes des joueurs
* @param currentRound
* @return nombre de couleurs identiques à celle du plateau * @return nombre de couleurs identiques à celle du plateau
*/ */
private int countSameColor(Card gameCard, List<Player> players, int currentRound) { private int countSameColor(Card gameCard, List<Card> currentCards) {
int counter = 0; int counter = 0;
for (Player player : players) { for (Card card : currentCards) {
Card card = player.getDeck().getCards().get(currentRound);
if (gameCard.getColor().equals(card.getColor()) && !gameCard.getValue().equals(card.getValue())) { if (gameCard.getColor().equals(card.getColor()) && !gameCard.getValue().equals(card.getValue())) {
counter++; counter++;
} }
@@ -346,14 +355,12 @@ public class GameWS {
* Retourne le nombre de joueurs avec une carte avec seulement la valeur correspondante à celle du plateau * Retourne le nombre de joueurs avec une carte avec seulement la valeur correspondante à celle du plateau
* *
* @param gameCard carte du plateau * @param gameCard carte du plateau
* @param players liste des joueurs * @param currentCards liste des cartes des joueurs
* @param currentRound manche courante
* @return nombre de valeurs identiques à celle du plateau * @return nombre de valeurs identiques à celle du plateau
*/ */
private int countSameValue(Card gameCard, List<Player> players, int currentRound) { private int countSameValue(Card gameCard, List<Card> currentCards) {
int counter = 0; int counter = 0;
for (Player player : players) { for (Card card : currentCards) {
Card card = player.getDeck().getCards().get(currentRound);
if (gameCard.getValue().equals(card.getValue()) && !gameCard.getColor().equals(card.getColor())) { if (gameCard.getValue().equals(card.getValue()) && !gameCard.getColor().equals(card.getColor())) {
counter++; counter++;
} }
@@ -364,15 +371,13 @@ public class GameWS {
/** /**
* Retourne le nombre de joueurs avec une carte totalement différente de celle du plateau * Retourne le nombre de joueurs avec une carte totalement différente de celle du plateau
* *
* @param gameCard * @param gameCard carte du plateau
* @param players * @param currentCards liste des cartes des joueurs
* @param currentRound
* @return nombre de cartes totalement différentes de celle du plateau * @return nombre de cartes totalement différentes de celle du plateau
*/ */
private int countNone(Card gameCard, List<Player> players, int currentRound) { private int countNone(Card gameCard, List<Card> currentCards) {
int counter = 0; int counter = 0;
for (Player player : players) { for (Card card : currentCards) {
Card card = player.getDeck().getCards().get(currentRound);
if (!gameCard.getColor().equals(card.getColor()) && !gameCard.getValue().equals(card.getValue())) { if (!gameCard.getColor().equals(card.getColor()) && !gameCard.getValue().equals(card.getValue())) {
counter++; counter++;
} }
@@ -201,7 +201,6 @@ wsgame.onMessage("nextRound", (game) => {
game.players game.players
.filter(p => p.user.id.toString() !== userSessionId.value) .filter(p => p.user.id.toString() !== userSessionId.value)
.forEach(p => { .forEach(p => {
console.log('adversaire:', p)
const playerHand = new PlayerHand(p); const playerHand = new PlayerHand(p);
otherCards.innerHTML += playerHand.render({ otherCards.innerHTML += playerHand.render({
textPosition: PlayerHand.TextPosition.TOP, textPosition: PlayerHand.TextPosition.TOP,