ajout de commentaire et suppretion de clases et code inutilisé

This commit is contained in:
marc barbier
2020-12-17 11:04:05 +01:00
parent f86a8e1658
commit ccf6c1a1b9
13 changed files with 209 additions and 90 deletions
+2 -1
View File
@@ -14,10 +14,10 @@ public class Serveur {
final String host = "localhost"; final String host = "localhost";
final int port = 10430; final int port = 10430;
//socket du serveur permet la connection de client
ServerSocket tCPserver = null; ServerSocket tCPserver = null;
try { try {
tCPserver = new ServerSocket(port, 50, InetAddress.getByName(host)); tCPserver = new ServerSocket(port, 50, InetAddress.getByName(host));
//tCPserver.setSoTimeout();
} catch (UnknownHostException e1) {// cette exception ne s'affichera jammais vu que l'on utilise localhost } catch (UnknownHostException e1) {// cette exception ne s'affichera jammais vu que l'on utilise localhost
} catch (IOException e1) { } catch (IOException e1) {
System.err.println( System.err.println(
@@ -27,6 +27,7 @@ public class Serveur {
System.out.println("server ready"); System.out.println("server ready");
//la file d'attente
Socket joueur1EnAttente = null; Socket joueur1EnAttente = null;
ComsJoueur joueur1Com = null; ComsJoueur joueur1Com = null;
Socket joueur2EnAttente = null; Socket joueur2EnAttente = null;
+4
View File
@@ -2,6 +2,10 @@ package game;
import game.cards.Card; import game.cards.Card;
/**
* cette classe est le terrain du jeu
* tout les carte placé sur le terrain sont stocké ici
*/
public class Board { public class Board {
private Card[] board; private Card[] board;
+23 -17
View File
@@ -1,27 +1,33 @@
package game; package game;
/**
* cette classe contien une liste de commande utilisé entre le client et le serveur
*/
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"; public static final String YOURTURN="yourturn"; // commande de debut de tours
public static final String DRAW = "draw"; public static final String DRAW = "draw"; //demande a pioché
public static final String GET_DECK = "getDeck"; public static final String GET_DECK = "getDeck"; // demande le deck
public static final String PUT_CARD = "putCard"; public static final String PUT_CARD = "putCard"; // place une carte
public static final String PUT_ENEMY_CARD = "enemycard"; public static final String PUT_ENEMY_CARD = "enemycard"; // place une carte sur le terrain adverse
public static final String PASS_TURN = "passTurn"; // passe son tours
public static final String POPUP = "pupup"; // affiche un messsage
public static final String ENEMYTURN = "enemyTurn"; // tours enemy
public static final String SETMANA = "setmana"; // defini la mana
public static final String WIN = "win"; // gagne
public static final String LOSE = "lose"; // perds
public static final String PING = "ping"; //envoie un ping en l'attente d'un pong
public static final String PONG = "pong";
public static final String NOK = "nok"; // refuse une actoin
public static final String OK = "ok"; // accepte une actoin
public static final String MEULE = "meule"; // fait pioché mais n'ajoute pas a la main la carte sera donc perdu
//inutilisé
public static final String SHUFFLE_DECK = "shuffleDeck"; public static final String SHUFFLE_DECK = "shuffleDeck";
public static final String SELECT_CARD = "selectCard"; public static final String SELECT_CARD = "selectCard";
public static final String ATTACK = "attack"; public static final String ATTACK = "attack";
public static final String DEFEND = "defend"; public static final String DEFEND = "defend";
public static final String CAST_SPELL = "castSpell"; public static final String CAST_SPELL = "castSpell";
public static final String PASS_TURN = "passTurn";
public static final String POPUP = "pupup";
public static final String ENEMYTURN = "enemyTurn";
public static final String SETMANA = "setmana";
public static final String WIN = "win";
public static final String LOSE = "lose";
public static final String PING = "ping";
public static final String PONG = "pong";
public static final String NOK = "nok";
public static final String OK = "ok";
public static final String MEULE = "meule";
} }
+32 -13
View File
@@ -16,13 +16,26 @@ public class ComsJoueur {
serverIn = new ObjectInputStream(connection.getInputStream()); serverIn = new ObjectInputStream(connection.getInputStream());
this.socket=connection; this.socket=connection;
} }
/**
* envoie un message sous forme de chaine de caractére
* le choix de la chaine de caractére permet déviter des crash on n'a pas euh de cours sur le reseau
* donc ce genre de communication est assez obscure pour nous
* @param message
* @throws IOException
*/
public void send(String message) throws IOException public void send(String message) throws IOException
{ {
serverOut.writeObject(message); serverOut.writeObject(message);
System.out.println("Sending "+message); System.out.println("Sending "+message);
} }
/**
* recoit une chaine de caractére
* voire la description de send() pour des info suplementaire
* @return
*/
public String recieve() public String recieve()
{ {
try { try {
@@ -34,26 +47,32 @@ public class ComsJoueur {
return "time out"; return "time out";
} }
} }
public Object waitAndRecieve() /**
{ * ferme la connection a la fin du match
String returnVal; * @throws IOException
while((returnVal =(String) recieve())!= null); */
return returnVal;
}
public void close() throws IOException public void close() throws IOException
{ {
serverOut.close(); serverOut.close();
serverIn.close(); serverIn.close();
} }
/**
* retourne le socket de connection
* @return
*/
public Socket getSocket() { public Socket getSocket() {
return socket; return socket;
} }
public void send(int cardId) throws IOException { /**
send(""+cardId); * envoie un entier en le convertissant en chaine
* @param message
* @throws IOException
*/
public void send(int message) throws IOException {
send(""+message);
} }
} }
+18 -2
View File
@@ -4,12 +4,23 @@ import game.cards.Card;
import game.deck.Deck; import game.deck.Deck;
public class DeckSerializer { public class DeckSerializer {
/**
* transforme le deck en chaine envoyable au client
* @param d
* @return
*/
public static String serializeDeck(Deck d) public static String serializeDeck(Deck d)
{ {
//la direction reste incertaine car on n'est pas sur de l'ordre des données recut
return forwardSerialisze(d); return forwardSerialisze(d);
} }
/**
* sérialise le deck a l'envers
* @param d
* @return
*/
private static String reverseSerialisze(Deck d) private static String reverseSerialisze(Deck d)
{ {
StringBuilder str = new StringBuilder(d.getCards().size()*2); StringBuilder str = new StringBuilder(d.getCards().size()*2);
@@ -20,7 +31,12 @@ public class DeckSerializer {
} }
return str.toString(); return str.toString();
} }
/**
* sérialise la deck a l'endroit
* @param d
* @return
*/
private static String forwardSerialisze(Deck d) private static String forwardSerialisze(Deck d)
{ {
StringBuilder str = new StringBuilder(d.getCards().size()*2); StringBuilder str = new StringBuilder(d.getCards().size()*2);
+51 -5
View File
@@ -16,11 +16,15 @@ public class Joueur {
private int mana; private int mana;
public Joueur(ComsJoueur connectionJoueur) throws IOException { public Joueur(ComsJoueur connectionJoueur) {
coms=connectionJoueur; coms=connectionJoueur;
pV =20; pV =20;
} }
/**
* cette fonction permet d'obtenir le deck du joueur et de préparer la main
* @throws IOException
*/
public void requestDataEarlyGameData() throws IOException { public void requestDataEarlyGameData() throws IOException {
coms.send(Command.GET_DECK); coms.send(Command.GET_DECK);
String deckname = coms.recieve(); String deckname = coms.recieve();
@@ -31,7 +35,12 @@ public class Joueur {
draw(5); draw(5);
} }
/**
* cette fonction permet de pioche une quantité donné
* @param amount la quantié
* @throws IOException
*/
public void draw(int amount) throws IOException public void draw(int amount) throws IOException
{ {
coms.send(Command.DRAW); coms.send(Command.DRAW);
@@ -39,14 +48,26 @@ public class Joueur {
hand.draw(amount); hand.draw(amount);
} }
/**
* affiche sur l'ecran que c'est le tours de l'adversaire
* @throws IOException
*/
public void debutTourEnemie() throws IOException { public void debutTourEnemie() throws IOException {
coms.send(Command.ENEMYTURN); coms.send(Command.ENEMYTURN);
} }
/**
* affiche un menu de victoire
* @throws IOException
*/
public void win() throws IOException { public void win() throws IOException {
coms.send(Command.WIN); coms.send(Command.WIN);
} }
/**
* affiche un menu de defaite
* @throws IOException
*/
public void lose() throws IOException { public void lose() throws IOException {
coms.send(Command.LOSE); coms.send(Command.LOSE);
} }
@@ -109,6 +130,8 @@ public class Joueur {
} }
} }
//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
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
@@ -148,15 +171,23 @@ public class Joueur {
} }
/**
* cette fonctoin est appelé affin de placer une carte sur le plateur énemie
* @param cardId
* @param zone
* @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);
} }
/**
* cette methode a pour but d'executé les evenement de fin de trous des cartes
*/
public void endPhase() { public void endPhase() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
public ComsJoueur getComs() { public ComsJoueur getComs() {
@@ -176,22 +207,37 @@ public class Joueur {
{ {
return !isDead(); return !isDead();
} }
/**
* envoie le message donné qui aparaitera alors comme une toast
* @param message
* @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
* @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
* @param nbcard nombre de carte a supprimé
* @throws IOException
*/
public void meule(int nbcard) throws IOException { public void meule(int nbcard) throws IOException {
coms.send(Command.MEULE); coms.send(Command.MEULE);
coms.send(nbcard); coms.send(nbcard);
+17 -3
View File
@@ -38,6 +38,7 @@ public class Match extends Thread {
tour(joueur2); tour(joueur2);
} catch (IOException e) { } catch (IOException e) {
endGameAfterIssue(); endGameAfterIssue();
return;
} }
} }
try { try {
@@ -47,6 +48,9 @@ public class Match extends Thread {
} }
} }
/**
* si le jeu a un crash il faut que si un joueur ai perdu la connection l'autre gagne
*/
private void endGameAfterIssue() { private void endGameAfterIssue() {
if(PlayerTesteur.playerTest(joueur1.getComs())) if(PlayerTesteur.playerTest(joueur1.getComs()))
{ {
@@ -64,8 +68,12 @@ public class Match extends Thread {
e.printStackTrace(); e.printStackTrace();
} }
} }
joueur1.setPV(0); try {
joueur2.setPV(0); joueur1.getComs().close();
} catch (IOException e) {}
try {
joueur2.getComs().close();
} catch (IOException e) {}
} }
//annonce du vainqueur et du perdant //annonce du vainqueur et du perdant
@@ -77,9 +85,15 @@ public class Match extends Thread {
joueur1.win(); joueur1.win();
joueur2.lose(); joueur2.lose();
} }
joueur1.getComs().close();
joueur2.getComs().close();
} }
//enchainement d'action que fait un joueur durant son tour /**
* enchainement d'acction effectué durant un tours
* @param joueur joueur effectuant les actions
* @throws IOException
*/
private void tour(Joueur joueur) throws IOException { private void tour(Joueur joueur) throws IOException {
//on affiche le message du debut de tour enemie //on affiche le message du debut de tour enemie
Joueur enemy = joueur.equals(joueur1) ? joueur2 : joueur1; Joueur enemy = joueur.equals(joueur1) ? joueur2 : joueur1;
+6 -2
View File
@@ -3,8 +3,12 @@ package game;
import java.io.IOException; import java.io.IOException;
public class PlayerTesteur { public class PlayerTesteur {
/**
* test si un joueur est planté ou non
* @param com un ComsJoeur correspondant au joueur
* @return false si le joeur est hs
*/
public static boolean playerTest(ComsJoueur com) public static boolean playerTest(ComsJoueur com)
{ {
try { try {
-35
View File
@@ -1,35 +0,0 @@
package game;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
public class SimpleUDP {
private byte[] buf;
private DatagramSocket uDPServeur;
private InetAddress address;
private static int port;
public SimpleUDP(DatagramSocket uDPServeur,Socket baseSocket)
{
this.uDPServeur=uDPServeur;
this.address=baseSocket.getInetAddress();
port = baseSocket.getPort();
}
public void sendPacket(String message) throws IOException
{
DatagramPacket packet
= new DatagramPacket(buf, buf.length, address, port);
uDPServeur.send(packet);
}
public String recivePacket() throws IOException
{
DatagramPacket packet = new DatagramPacket(buf, buf.length);
uDPServeur.receive(packet);
return new String(packet.getData(), 0, packet.getLength());
}
}
+9
View File
@@ -25,7 +25,16 @@ public abstract class Card{
return health; return health;
} }
/**
* l'attack par defaut des cartes
* @return
*/
public abstract int getDefaultAttack(); public abstract int getDefaultAttack();
/**
* les point de vie par default de la carte
* @return
*/
public abstract int getDefaultHealth(); public abstract int getDefaultHealth();
/** /**
+19 -9
View File
@@ -94,6 +94,9 @@ import game.cards.Renaissance.Rennaissance_Skanderberg;
import game.cards.Renaissance.Rennaissance_Succession_Bourgogne; import game.cards.Renaissance.Rennaissance_Succession_Bourgogne;
import game.cards.Renaissance.Rennaissance_Vlad; import game.cards.Renaissance.Rennaissance_Vlad;
/**
* cette carte est un registre contenant toutes les cartes cela permet de sérialisé les cartes entre le client et le servuer avec un minimum d'information
*/
public class CardRegistery { public class CardRegistery {
public static List<Class<? extends Card>> registry; public static List<Class<? extends Card>> registry;
@@ -204,25 +207,32 @@ public class CardRegistery {
registry.add(Moyen_Age_Pouvoir_Du_Franc.class); registry.add(Moyen_Age_Pouvoir_Du_Franc.class);
} }
/**
* retourne l'index de la carte donné en argument
* @param class1 la class de la carte
* @return index
*/
public static int get(Class<? extends Card> class1) { public static int get(Class<? extends Card> class1) {
checkregistry(); checkRegistry();
int a = registry.indexOf(class1); int a = registry.indexOf(class1);
return a != -1 ? a : 0; return a != -1 ? a : 0;
} }
/**
* retourne la class de la carte a l'index donné
* @param index index de la class
* @return class de la carte
*/
public static Class<? extends Card> get(int index) { public static Class<? extends Card> get(int index) {
checkregistry(); checkRegistry();
if(registry.size() <= index || index < 0) return null; if(registry.size() <= index || index < 0) return null;
return registry.get(index); return registry.get(index);
} }
/**
public static int get(Card card) { * verifie si le registre existe sinon le crée
checkregistry(); */
return get(card.getClass()); private static void checkRegistry()
}
public static void checkregistry()
{ {
if(registry == null)initCardRegistery(); if(registry == null)initCardRegistery();
} }
+14 -2
View File
@@ -11,23 +11,35 @@ public abstract class Deck {
public abstract Stack<Card> getCards(); public abstract Stack<Card> getCards();
/**
* mélange le deck
* /!\ il faut envoyer le deck au client une fois mélanger
*/
public void shuffle() public void shuffle()
{ {
Collections.shuffle(getCards()); Collections.shuffle(getCards());
} }
/**
* retire une carte de deck
* @return la carte retirer
*/
public Card draw() public Card draw()
{ {
return getCards().pop(); return getCards().pop();
} }
/**
* retire une liste de carte
* @param amount
* @return
*/
public List<Card> draw(int amount) public List<Card> draw(int amount)
{ {
List<Card> cards = new ArrayList<>(); List<Card> cards = new ArrayList<>();
for(int i = 0 ; i < amount ; i++) for(int i = 0 ; i < amount ; i++)
{ {
cards.add(getCards().pop()); cards.add(draw());
} }
return cards; return cards;
} }
+14 -1
View File
@@ -1,6 +1,19 @@
package game.deck; package game.deck;
/**
* registre de deck
* le client ne sais pas quelles cartes apartienne a quel deck
* cette information est conservé uniquement sur le serveur donc le client demande un deck
* et le serveur le sert
* d'ou l'interet d'un registre de deck
*/
public class DeckRegistery { public class DeckRegistery {
/**
* retourne un nouveau deck en fonction du nom donné
* @param name
* @return
*/
public static Deck get(String name) public static Deck get(String name)
{ {
switch (name.trim()) { switch (name.trim()) {
@@ -11,7 +24,7 @@ public class DeckRegistery {
case "renaissance": case "renaissance":
return new Renaissance(); return new Renaissance();
default: default:
System.out.println(name); System.out.println("deck non trouvé :"+name);
return new MoyeneAge(); return new MoyeneAge();
} }
} }