ajout mana + patch defait/victoire + resilience en anti crash

This commit is contained in:
marc barbier
2021-01-04 16:10:09 +01:00
parent 13e2c1bab1
commit a8fde43596
9 changed files with 198 additions and 137 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-10
View File
@@ -28,14 +28,4 @@ public class Board {
return board[zone]; return board[zone];
} }
public void printDebugBoard() {
for(Card c : board)
{
if(c == null) System.out.print("X");
else System.out.print("O");
}
System.out.println();
}
} }
+3 -7
View File
@@ -5,7 +5,7 @@ package game;
*/ */
public final class Command { public final class Command {
private Command(){} // pas de contructor on ne veut pas instancier cette classe private Command(){} // pas de contructor on ne veut pas instancier cette classe
public static final String YOURTURN="yourturn"; // commande de debut de tours public static final String YOURTURN="yourturn"; // commande de debut de tours
public static final String DRAW = "draw"; //demande a pioché public static final String DRAW = "draw"; //demande a pioché
public static final String GET_DECK = "getDeck"; // demande le deck public static final String GET_DECK = "getDeck"; // demande le deck
@@ -26,10 +26,6 @@ public final class Command {
public static final String DESTROY_CARD = "destroyself"; //demande au clien de retirer une carte du terrain public static final String DESTROY_CARD = "destroyself"; //demande au clien de retirer une carte du terrain
public static final String DESTROY_ADV_CARD = "destroyadv"; //demande au client de retirer une carte adverse du terrain public static final String DESTROY_ADV_CARD = "destroyadv"; //demande au client de retirer une carte adverse du terrain
public static final String BATTLE = "battlephase"; public static final String BATTLE = "battlephase";
public static final String SET_ENEMY_HP = "setenemhp";
public static final String SET_HP = "sethp";
//inutilisé
public static final String SELECT_CARD = "selectCard";
public static final String DEFEND = "defend";
public static final String CAST_SPELL = "castSpell";
} }
+157 -89
View File
@@ -13,18 +13,20 @@ public class Joueur {
private Hand hand; private Hand hand;
private Board board; private Board board;
private int pV; private int pV;
private int currentMana=0; // la quantité maximum de mana elle augment de 1 par tours
private int currentMana = 0;
// la mana actuele du joueur
private int mana; private int mana;
public Joueur(ComsJoueur connectionJoueur) { public Joueur(ComsJoueur connectionJoueur) {
coms=connectionJoueur; coms = connectionJoueur;
pV =20; pV = 20;
this.board=new Board(); this.board = new Board();
} }
/** /**
* cette fonction permet d'obtenir le deck du joueur et de préparer la main * cette fonction permet d'obtenir le deck du joueur et de préparer la main
*
* @throws IOException * @throws IOException
*/ */
public void requestDataEarlyGameData() throws IOException { public void requestDataEarlyGameData() throws IOException {
@@ -35,16 +37,20 @@ public class Joueur {
hand = new Hand(deck); hand = new Hand(deck);
coms.send(DeckSerializer.serializeDeck(deck)); coms.send(DeckSerializer.serializeDeck(deck));
draw(5); draw(5);
coms.send(Command.SET_HP);
coms.send(pV);
coms.send(Command.SET_ENEMY_HP);
coms.send(pV);
} }
/** /**
* cette fonction permet de pioche une quantité donné * cette fonction permet de pioche une quantité donné
*
* @param amount la quantié * @param amount la quantié
* @throws IOException * @throws IOException
*/ */
public void draw(int amount) throws IOException public void draw(int amount) throws IOException {
{
coms.send(Command.DRAW); coms.send(Command.DRAW);
coms.send(amount); coms.send(amount);
hand.draw(amount); hand.draw(amount);
@@ -52,6 +58,7 @@ public class Joueur {
/** /**
* affiche sur l'ecran que c'est le tours de l'adversaire * affiche sur l'ecran que c'est le tours de l'adversaire
*
* @throws IOException * @throws IOException
*/ */
public void debutTourEnemie() throws IOException { public void debutTourEnemie() throws IOException {
@@ -60,6 +67,7 @@ public class Joueur {
/** /**
* affiche un menu de victoire * affiche un menu de victoire
*
* @throws IOException * @throws IOException
*/ */
public void win() throws IOException { public void win() throws IOException {
@@ -68,6 +76,7 @@ public class Joueur {
/** /**
* affiche un menu de defaite * affiche un menu de defaite
*
* @throws IOException * @throws IOException
*/ */
public void lose() throws IOException { public void lose() throws IOException {
@@ -75,37 +84,41 @@ public class Joueur {
} }
/** /**
* cette commande doit être utilisé a chaque début de tour elle permet d'augmenter la mana du joueur * cette commande doit être utilisé a chaque début de tour elle permet
* d'augmenter la mana du joueur
*
* @throws IOException * @throws IOException
*/ */
public void prepMana() throws IOException { public void prepMana() throws IOException {
//on prepare la mana // on prepare la mana
if(currentMana < 10)currentMana++; if (currentMana < 10)
mana=currentMana; currentMana++;
mana = currentMana;
coms.send(Command.SETMANA); coms.send(Command.SETMANA);
coms.send(mana); coms.send(mana);
} }
/** /**
* premiére phase de jeu avant d'attacker * premiére phase de jeu avant d'attacker
*
* @param adversaire * @param adversaire
* @param board * @param board
* @throws IOException * @throws IOException
*/ */
public void mainPhase1(Joueur adversaire) throws IOException { public void mainPhase1(Joueur adversaire) throws IOException {
boolean phaseActive=true; boolean phaseActive = true;
//on retire le time out sur l'adversaire car sa peut causer des désyncronisatoin et des crash // on retire le time out sur l'adversaire car sa peut causer des
// désyncronisatoin et des crash
adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE); adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE);
//debut de l'écoude des action client // debut de l'écoude des action client
while(phaseActive) int alive = 0;
{ while (phaseActive) {
coms.getSocket().setSoTimeout(500); coms.getSocket().setSoTimeout(500);
String command = coms.recieve(); String command = coms.recieve();
coms.getSocket().setSoTimeout(Integer.MAX_VALUE); coms.getSocket().setSoTimeout(Integer.MAX_VALUE);
if(command != null && !command.equals("time out")) if (command != null && !command.equals("time out")) {
{ alive = 0;
System.out.println("GOT COMMAND :"+command); System.out.println("GOT COMMAND :" + command);
switch (command) { switch (command) {
@@ -114,57 +127,69 @@ public class Joueur {
break; break;
case Command.PASS_TURN: case Command.PASS_TURN:
phaseActive=false; phaseActive = false;
break; break;
case Command.PUT_CARD: case Command.PUT_CARD:
System.out.println("entering put card"); System.out.println("entering put card");
putCard(adversaire,board); putCard(adversaire, board);
break; break;
default: default:
System.err.println("unknown command "+command); System.err.println("unknown command " + command);
} }
} else {
alive++;
// 3000xalive =300s = 5minutes
// si au bout de 5 minute le client ne repond pas on envoie une erreur
if (alive > 300000) {
throw new IOException("dead client");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
} }
} }
} }
//cette mothode est utilisé l'orse qu'un joueur utilise Command.PLACECARD // cette mothode est utilisé l'orse qu'un joueur utilise Command.PLACECARD
//elle verifie si le joueur peut la jouer et refuse ou confirme en fonction // elle verifie si le joueur peut la jouer et refuse ou confirme en fonction
private void putCard(Joueur adversaire, Board board) throws IOException { private void putCard(Joueur adversaire, Board board) throws IOException {
String info = coms.recieve(); String info = coms.recieve();
//si ce n'est pas un nombre // si ce n'est pas un nombre
if(!isInt(info)) { if (!isInt(info)) {
//alors on affiche l'inforamtion si la sortie d'erreur // alors on affiche l'inforamtion si la sortie d'erreur
System.err.println(info); System.err.println(info);
}else { } else {
//on recupére la carte // on recupére la carte
int cardId = Integer.parseInt(info); int cardId = Integer.parseInt(info);
Class<? extends Card> cardClass = CardRegistery.get(cardId); Class<? extends Card> cardClass = CardRegistery.get(cardId);
System.out.println(cardClass.getName()); System.out.println(cardClass.getName());
int zone = Integer.parseInt(coms.recieve()); int zone = Integer.parseInt(coms.recieve());
Card handCard = hand.contains(cardClass); Card handCard = hand.contains(cardClass);
//si la carte n'est pas trouve ou si elle coute trop cher // si la carte n'est pas trouve ou si elle coute trop cher
if(handCard == null || mana < handCard.getCost()){ if (handCard == null || mana < handCard.getCost()) {
//on refuse le placement // on refuse le placement
System.out.println("no mana or no card"); System.out.println("no mana or no card");
coms.send(Command.NOK); coms.send(Command.NOK);
} } else {
else { if (board.isZoneAvaliable(zone)) {
if(board.isZoneAvaliable(zone))
{
hand.remove(handCard); hand.remove(handCard);
board.setCard(zone, handCard); board.setCard(zone, handCard);
adversaire.enemyPlay(cardId,zone); adversaire.enemyPlay(cardId, zone);
coms.send(Command.OK); coms.send(Command.OK);
}else {
coms.send(Command.SETMANA);
mana -= handCard.getCost();
coms.send(mana);
} else {
System.out.println("the zone is not avaliable"); System.out.println("the zone is not avaliable");
coms.send(Command.NOK); coms.send(Command.NOK);
} }
@@ -179,11 +204,12 @@ public class Joueur {
/** /**
* cette fonctoin est appelé affin de placer une carte sur le plateur énemie * cette fonctoin est appelé affin de placer une carte sur le plateur énemie
*
* @param cardId * @param cardId
* @param zone * @param zone
* @throws IOException * @throws IOException
*/ */
private void enemyPlay(int cardId,int zone) throws IOException { private void enemyPlay(int cardId, int zone) throws IOException {
coms.send(Command.PUT_ENEMY_CARD); coms.send(Command.PUT_ENEMY_CARD);
coms.send(cardId); coms.send(cardId);
coms.send(zone); coms.send(zone);
@@ -204,43 +230,42 @@ public class Joueur {
this.pV = pV; this.pV = pV;
} }
public boolean isDead() public boolean isDead() {
{
return pV == 0; return pV == 0;
} }
public boolean isAlive() public boolean isAlive() {
{
return !isDead(); return !isDead();
} }
/** /**
* envoie le message donné qui aparaitera alors comme une toast * envoie le message donné qui aparaitera alors comme une toast
*
* @param message * @param message
* @throws IOException * @throws IOException
*/ */
public void sendMessage(String message) throws IOException public void sendMessage(String message) throws IOException {
{
coms.send(Command.POPUP); coms.send(Command.POPUP);
coms.send(message); coms.send(message);
} }
/** /**
* affiche le debut de tours * affiche le debut de tours
*
* @throws IOException * @throws IOException
*/ */
public void yourturn() throws IOException { public void yourturn() throws IOException {
//on affiche le debut de tour // on affiche le debut de tour
coms.send(Command.YOURTURN); coms.send(Command.YOURTURN);
} }
public boolean isHandFull() { public boolean isHandFull() {
return hand.isFull(); return hand.isFull();
} }
/** /**
* retire le nombre de carte donné du deck * retire le nombre de carte donné du deck
*
* @param nbcard nombre de carte a supprimé * @param nbcard nombre de carte a supprimé
* @throws IOException * @throws IOException
*/ */
@@ -250,23 +275,20 @@ public class Joueur {
deck.draw(1); deck.draw(1);
} }
public void battlePhase(Joueur adversaire) throws IOException public void battlePhase(Joueur adversaire) throws IOException {
{
coms.send(Command.BATTLE); coms.send(Command.BATTLE);
boolean phaseActive = true; boolean phaseActive = true;
adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE); adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE);
//debut de l'écoude des action client // debut de l'écoude des action client
while(phaseActive) while (phaseActive) {
{
coms.getSocket().setSoTimeout(500); coms.getSocket().setSoTimeout(500);
String command = coms.recieve(); String command = coms.recieve();
coms.getSocket().setSoTimeout(Integer.MAX_VALUE); coms.getSocket().setSoTimeout(Integer.MAX_VALUE);
if(command != null && !command.equals("time out")) if (command != null && !command.equals("time out")) {
{ System.out.println("GOT COMMAND :" + command);
System.out.println("GOT COMMAND :"+command);
switch (command) { switch (command) {
@@ -275,55 +297,101 @@ public class Joueur {
break; break;
case Command.PASS_TURN: case Command.PASS_TURN:
phaseActive=false; phaseActive = false;
break; break;
case Command.ATTACK: case Command.ATTACK:
//numero de la zone de la carte attaquante // numero de la zone de la carte attaquante
String carteAttaquante = coms.recieve(); String carteAttaquante = coms.recieve();
//numero de la zone de la carte ciblé // numero de la zone de la carte ciblé
String carteCible = coms.recieve(); String carteCible = coms.recieve();
if(!isInt(carteAttaquante) || !isInt(carteCible))throw new RuntimeException("non number zone recived in attack phase"); if (!isInt(carteAttaquante) || !isInt(carteCible))
throw new RuntimeException("non number zone recived in attack phase");
Card attackingCard = board.getCardInZone(Integer.parseInt(carteAttaquante)); Card attackingCard = board.getCardInZone(Integer.parseInt(carteAttaquante));
Card attackedCard = adversaire.getBoard().getCardInZone(Integer.parseInt(carteCible));
if(attackedCard ==null || attackingCard == null) if (attackingCard == null) {
{ throw new RuntimeException("invalid attack");
board.printDebugBoard(); } else {
throw new RuntimeException("error invalid carteCible:"+carteCible + " carteAttaquante : "+carteAttaquante); if (carteCible.equals("100")) {
} boolean isadvDead = handleAttackAgainstPlayer(attackingCard, adversaire);
if(isadvDead)
System.out.println("FIGHT " + "carteCible:"+carteCible + " carteAttaquante : "+carteAttaquante); {
phaseActive = false;
boolean isDestroyed = attackedCard.takeDamage(attackingCard.getAttack()); }
if(isDestroyed) } else {
{ handleAttackAgainstCard(attackingCard, carteCible, adversaire);
//on suprime la carte du terrain }
board.setCard(Integer.parseInt(carteCible), null);
//demande a l'adversaire de retirer la carte de son terrain
adversaire.getComs().send(Command.DESTROY_CARD);
adversaire.getComs().send(carteCible);
//retire la carte au terrain adverse
this.getComs().send(Command.DESTROY_ADV_CARD);
this.getComs().send(carteCible);
//appelle les effet de quand la carte est détruite
//NOTE : non implémenter dans les cartes individuelles par manque de temps
attackedCard.onCardDestroyed();
} }
break; break;
default: default:
System.err.println("unknown command "+command); System.err.println("unknown command " + command);
} }
} }
} }
} }
private boolean handleAttackAgainstPlayer(Card attackingCard, Joueur adversaire) throws IOException {
boolean result = adversaire.takeDamage(attackingCard.getAttack());
if(!result)
{
adversaire.updateHp(this);
return false;
}
return true;
}
private void updateHp(Joueur adversaire) throws IOException {
coms.send(Command.SET_HP);
coms.send(pV);
adversaire.coms.send(Command.SET_ENEMY_HP);
adversaire.coms.send(pV);
}
private void handleAttackAgainstCard(Card attackingCard, String carteCible, Joueur adversaire) throws IOException {
Card attackedCard = adversaire.getBoard().getCardInZone(Integer.parseInt(carteCible));
if (attackedCard == null) {
throw new RuntimeException(
"error invalid carteCible:" + carteCible + " carteAttaquante : " + attackingCard);
}
boolean isDestroyed = attackedCard.takeDamage(attackingCard.getAttack());
if (isDestroyed) {
// on suprime la carte du terrain
board.setCard(Integer.parseInt(carteCible), null);
// demande a l'adversaire de retirer la carte de son terrain
adversaire.getComs().send(Command.DESTROY_CARD);
adversaire.getComs().send(carteCible);
// retire la carte au terrain adverse
this.getComs().send(Command.DESTROY_ADV_CARD);
this.getComs().send(carteCible);
// appelle les effet de quand la carte est détruite
// NOTE : non implémenter dans les cartes individuelles par manque de temps
attackedCard.onCardDestroyed();
}
}
private Board getBoard() { private Board getBoard() {
return board; return board;
} }
/**
* inflige des degats au joueur
* @param amount
* @return true si les pv du joueur on attein zero
*/
private boolean takeDamage(int amount)
{
pV -= amount;
pV = pV > 0 ? pV : 0;
return pV == 0;
}
} }
+14 -7
View File
@@ -30,30 +30,36 @@ public class Match extends Thread {
// gameplay // gameplay
@Override @Override
public void run() { public void run() {
//cette variable permet de decider le gagnant en cas d'erreur reseau
boolean isJ1Turn=true;
while (joueur1.isAlive() && joueur2.isAlive()) { while (joueur1.isAlive() && joueur2.isAlive()) {
try { try {
tour(joueur1); tour(joueur1);
tour(joueur2); isJ1Turn=false;
if(joueur2.isAlive())tour(joueur2);
isJ1Turn=true;
} catch (IOException e) { } catch (IOException e) {
endGameAfterIssue(); endGameAfterIssue(isJ1Turn);
return; return;
} }
} }
try { try {
endMatch(); endMatch();
} catch (IOException e) { } catch (IOException e) {
endGameAfterIssue(); endGameAfterIssue(isJ1Turn);
} }
} }
/** /**
* si le jeu a un crash il faut que si un joueur ai perdu la connection l'autre gagne * si le jeu a un crash il faut que si un joueur ai perdu la connection l'autre gagne
* @param isJ1Turn
*/ */
private void endGameAfterIssue() { private void endGameAfterIssue(boolean isJ1Turn) {
if(PlayerTesteur.playerTest(joueur1.getComs())) if(PlayerTesteur.playerTest(joueur1.getComs()))
{ {
try { try {
joueur1.win(); if(isJ1Turn)joueur1.win();
else joueur1.lose();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -61,7 +67,8 @@ public class Match extends Thread {
if(PlayerTesteur.playerTest(joueur2.getComs())) if(PlayerTesteur.playerTest(joueur2.getComs()))
{ {
try { try {
joueur2.win(); if(!isJ1Turn)joueur2.win();
else joueur2.lose();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -114,7 +121,7 @@ public class Match extends Thread {
joueur.mainPhase1(enemy); //Joue autant de carte qu'il veut/peut joueur.mainPhase1(enemy); //Joue autant de carte qu'il veut/peut
joueur.battlePhase(enemy);//lance des attaques à son adversaire qui peut répliquer joueur.battlePhase(enemy);//lance des attaques à son adversaire qui peut répliquer
//cette phases ne serira surment pas car nous manquon de temps pour implrementer les fonctionnalité specifique de certaine cartes //cette phases ne serira surment pas car nous manquon de temps pour implrementer les fonctionnalité specifique de certaine cartes
joueur.endPhase();//fin du tour du @joueur if(enemy.isAlive())joueur.endPhase();//fin du tour du @joueur
} }
} }