diff --git a/bin/Serveur.class b/bin/Serveur.class index 87534d2..559a1cd 100644 Binary files a/bin/Serveur.class and b/bin/Serveur.class differ diff --git a/bin/game/Joueur.class b/bin/game/Joueur.class index 3d7ca39..5d1fb9a 100644 Binary files a/bin/game/Joueur.class and b/bin/game/Joueur.class differ diff --git a/bin/game/Match.class b/bin/game/Match.class index 98ab6b5..9c98df3 100644 Binary files a/bin/game/Match.class and b/bin/game/Match.class differ diff --git a/bin/game/PlayerTesteur.class b/bin/game/PlayerTesteur.class index 4dd36d1..3a671e8 100644 Binary files a/bin/game/PlayerTesteur.class and b/bin/game/PlayerTesteur.class differ diff --git a/bin/game/cards/CardRegistery.class b/bin/game/cards/CardRegistery.class index e4e6b6a..694f6ba 100644 Binary files a/bin/game/cards/CardRegistery.class and b/bin/game/cards/CardRegistery.class differ diff --git a/src/Serveur.java b/src/Serveur.java index 1648a99..ae1f63f 100644 --- a/src/Serveur.java +++ b/src/Serveur.java @@ -4,6 +4,7 @@ import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; +import game.ComsJoueur; import game.Match; import game.PlayerTesteur; @@ -16,7 +17,7 @@ public class Serveur { ServerSocket tCPserver = null; try { tCPserver = new ServerSocket(port, 50, InetAddress.getByName(host)); - tCPserver.setSoTimeout(250);// 250 de ping max + //tCPserver.setSoTimeout(); } catch (UnknownHostException e1) {// cette exception ne s'affichera jammais vu que l'on utilise localhost } catch (IOException e1) { System.err.println( @@ -24,47 +25,53 @@ public class Serveur { return; } - // l'udp est un protocole plus rapide que le tcp si on a des probleme de latence - // voila une solution - // mais il est moin fiable - // DatagramSocket uDPServeur = new DatagramSocket(port); - // uDPServeur.setSoTimeout(500);//si au bout d'une demi-seconde on a pas de - // réponse alors le client est hs - System.out.println("server ready"); Socket joueur1EnAttente = null; + ComsJoueur joueur1Com = null; Socket joueur2EnAttente = null; + ComsJoueur joueur2Com = null; + while (true) { if (joueur1EnAttente == null) { try { joueur1EnAttente = tCPserver.accept(); + joueur1Com = new ComsJoueur(joueur1EnAttente); System.out.println("joueur1 trouvé"); } catch (IOException e) { joueur1EnAttente = null; + joueur1Com=null; } } if (joueur2EnAttente == null) { try { joueur2EnAttente = tCPserver.accept(); + joueur2Com = new ComsJoueur(joueur2EnAttente); System.out.println("joueur2 trouvé"); } catch (IOException e) { joueur2EnAttente = null; + joueur2Com=null; } } - if (joueur1EnAttente != null && joueur2EnAttente != null) { + if (joueur1EnAttente != null && joueur1Com != null && joueur2EnAttente != null && joueur2Com != null) { try { - if (PlayerTesteur.playerTest(joueur1EnAttente)) { - if (PlayerTesteur.playerTest(joueur2EnAttente)) { - new Match(joueur1EnAttente, joueur2EnAttente); + if (PlayerTesteur.playerTest(joueur1Com)) { + if (PlayerTesteur.playerTest(joueur2Com)) { + new Match(joueur1Com,joueur2Com); System.out.println("match lancé"); } else { // le joueur est cassé donc on le retire de la file d'attente + System.out.println("j2 invalid"); + joueur2EnAttente.close(); joueur2EnAttente = null; + joueur2Com=null; } } else { // le joueur est cassé donc on le retire de la file d'attente + System.out.println("j1 invalid"); + joueur1EnAttente.close(); joueur1EnAttente = null; + joueur1Com=null; } } catch (IOException e) { diff --git a/src/game/Joueur.java b/src/game/Joueur.java index e5e0a85..5a5dd44 100644 --- a/src/game/Joueur.java +++ b/src/game/Joueur.java @@ -1,7 +1,6 @@ package game; import java.io.IOException; -import java.net.Socket; import game.cards.Card; import game.cards.CardRegistery; @@ -9,8 +8,6 @@ import game.deck.Deck; import game.deck.DeckRegistery; public class Joueur { - - private Socket connection; private ComsJoueur coms; private Deck deck; private Hand hand; @@ -19,9 +16,8 @@ public class Joueur { private int mana; - public Joueur(Socket connectionJoueur) throws IOException { - connection=connectionJoueur; - coms = new ComsJoueur(connection); + public Joueur(ComsJoueur connectionJoueur) throws IOException { + coms=connectionJoueur; pV =20; } @@ -75,13 +71,17 @@ public class Joueur { //debut de l'écoude des action client while(phaseActive) { - String command = (String) coms.recieve(); + String command; + command = (String) coms.recieve(); + + System.out.println("GOT COMMAND :"+command); if(command != null) { switch (command) { case Command.PING: coms.send(Command.PONG); + System.out.println("pong"); break; case Command.PASS_TURN: @@ -91,6 +91,9 @@ public class Joueur { case Command.PUT_CARD: putCard(adversaire,board); break; + case "time out": + System.exit(0); + break; default: coms.send(Command.POPUP); @@ -162,8 +165,8 @@ public class Joueur { } - public Socket getConnection() { - return connection; + public ComsJoueur getComs() { + return coms; } public void setPV(int pV) { diff --git a/src/game/Match.java b/src/game/Match.java index 4ebabfc..901873c 100644 --- a/src/game/Match.java +++ b/src/game/Match.java @@ -1,23 +1,22 @@ package game; import java.io.IOException; -import java.net.Socket; public class Match extends Thread { Joueur joueur1, joueur2; int tour = 1; private Board board; - public Match(Socket j1, Socket j2) throws IOException { + public Match(ComsJoueur joueur1Com, ComsJoueur joueur2Com) throws IOException { super(); this.board=new Board(); // random j2/j1 if (Math.random() > 0.5F) { - joueur1 = new Joueur(j1); - joueur2 = new Joueur(j2); + joueur1 = new Joueur(joueur1Com); + joueur2 = new Joueur(joueur2Com); } else { - joueur1 = new Joueur(j2); - joueur2 = new Joueur(j1); + joueur1 = new Joueur(joueur2Com); + joueur2 = new Joueur(joueur1Com); } joueur1.requestDataEarlyGameData(); @@ -47,7 +46,7 @@ public class Match extends Thread { } private void endGameAfterIssue() { - if(PlayerTesteur.playerTest(joueur1.getConnection())) + if(PlayerTesteur.playerTest(joueur1.getComs())) { try { joueur1.win(); @@ -55,7 +54,7 @@ public class Match extends Thread { e.printStackTrace(); } } - if(PlayerTesteur.playerTest(joueur2.getConnection())) + if(PlayerTesteur.playerTest(joueur2.getComs())) { try { joueur2.win(); diff --git a/src/game/PlayerTesteur.java b/src/game/PlayerTesteur.java index 4c1f0b8..49a181c 100644 --- a/src/game/PlayerTesteur.java +++ b/src/game/PlayerTesteur.java @@ -1,20 +1,12 @@ package game; import java.io.IOException; -import java.net.Socket; public class PlayerTesteur { - public static boolean playerTest(Socket joueur) + public static boolean playerTest(ComsJoueur com) { - ComsJoueur com; - try { - com = new ComsJoueur(joueur); - } catch (IOException e) { - return false; - } - try { com.send(Command.PING); } catch (IOException e) { @@ -30,12 +22,6 @@ public class PlayerTesteur { System.out.println("le client a été trop long pour répondre ou une erreur reseau c'est porduite"); return false; } - - try { - com.close(); - } catch (IOException e) { - e.printStackTrace(); - } - return false; + return true; } } diff --git a/src/game/cards/CardRegistery.java b/src/game/cards/CardRegistery.java index d14c34a..e1a4ea5 100644 --- a/src/game/cards/CardRegistery.java +++ b/src/game/cards/CardRegistery.java @@ -97,7 +97,7 @@ import game.cards.Renaissance.Rennaissance_Vlad; public class CardRegistery { public static List> registry; - public CardRegistery() + private static void initCardRegistery() { registry = new ArrayList<>(); @@ -205,17 +205,25 @@ public class CardRegistery { } public static int get(Class class1) { + checkregistry(); int a = registry.indexOf(class1); return a != -1 ? a : 0; } public static Class get(int index) { + checkregistry(); if(registry.size() <= index || index < 0) return null; return registry.get(index); } public static int get(Card card) { + checkregistry(); return get(card.getClass()); } + + public static void checkregistry() + { + if(registry == null)initCardRegistery(); + } } \ No newline at end of file