patch test

This commit is contained in:
marc barbier committed 2020-12-13 13:27:05 +01:00
1 parent 7f4362cf33
commit 6b9fade19d
10 files changed
+49 -46

No files matched your search

BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+19 -12
View File
@@ -4,6 +4,7 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import game.ComsJoueur;
import game.Match; import game.Match;
import game.PlayerTesteur; import game.PlayerTesteur;
@@ -16,7 +17,7 @@ public class Serveur {
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(250);// 250 de ping max //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(
@@ -24,47 +25,53 @@ public class Serveur {
return; 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"); System.out.println("server ready");
Socket joueur1EnAttente = null; Socket joueur1EnAttente = null;
ComsJoueur joueur1Com = null;
Socket joueur2EnAttente = null; Socket joueur2EnAttente = null;
ComsJoueur joueur2Com = null;
while (true) { while (true) {
if (joueur1EnAttente == null) { if (joueur1EnAttente == null) {
try { try {
joueur1EnAttente = tCPserver.accept(); joueur1EnAttente = tCPserver.accept();
joueur1Com = new ComsJoueur(joueur1EnAttente);
System.out.println("joueur1 trouvé"); System.out.println("joueur1 trouvé");
} catch (IOException e) { } catch (IOException e) {
joueur1EnAttente = null; joueur1EnAttente = null;
joueur1Com=null;
} }
} }
if (joueur2EnAttente == null) { if (joueur2EnAttente == null) {
try { try {
joueur2EnAttente = tCPserver.accept(); joueur2EnAttente = tCPserver.accept();
joueur2Com = new ComsJoueur(joueur2EnAttente);
System.out.println("joueur2 trouvé"); System.out.println("joueur2 trouvé");
} catch (IOException e) { } catch (IOException e) {
joueur2EnAttente = null; joueur2EnAttente = null;
joueur2Com=null;
} }
} }
if (joueur1EnAttente != null && joueur2EnAttente != null) { if (joueur1EnAttente != null && joueur1Com != null && joueur2EnAttente != null && joueur2Com != null) {
try { try {
if (PlayerTesteur.playerTest(joueur1EnAttente)) { if (PlayerTesteur.playerTest(joueur1Com)) {
if (PlayerTesteur.playerTest(joueur2EnAttente)) { if (PlayerTesteur.playerTest(joueur2Com)) {
new Match(joueur1EnAttente, joueur2EnAttente); new Match(joueur1Com,joueur2Com);
System.out.println("match lancé"); System.out.println("match lancé");
} else { } else {
// le joueur est cassé donc on le retire de la file d'attente // le joueur est cassé donc on le retire de la file d'attente
System.out.println("j2 invalid");
joueur2EnAttente.close();
joueur2EnAttente = null; joueur2EnAttente = null;
joueur2Com=null;
} }
} else { } else {
// le joueur est cassé donc on le retire de la file d'attente // le joueur est cassé donc on le retire de la file d'attente
System.out.println("j1 invalid");
joueur1EnAttente.close();
joueur1EnAttente = null; joueur1EnAttente = null;
joueur1Com=null;
} }
} catch (IOException e) { } catch (IOException e) {
+12 -9
View File
@@ -1,7 +1,6 @@
package game; package game;
import java.io.IOException; import java.io.IOException;
import java.net.Socket;
import game.cards.Card; import game.cards.Card;
import game.cards.CardRegistery; import game.cards.CardRegistery;
@@ -9,8 +8,6 @@ import game.deck.Deck;
import game.deck.DeckRegistery; import game.deck.DeckRegistery;
public class Joueur { public class Joueur {
private Socket connection;
private ComsJoueur coms; private ComsJoueur coms;
private Deck deck; private Deck deck;
private Hand hand; private Hand hand;
@@ -19,9 +16,8 @@ public class Joueur {
private int mana; private int mana;
public Joueur(Socket connectionJoueur) throws IOException { public Joueur(ComsJoueur connectionJoueur) throws IOException {
connection=connectionJoueur; coms=connectionJoueur;
coms = new ComsJoueur(connection);
pV =20; pV =20;
} }
@@ -75,13 +71,17 @@ public class Joueur {
//debut de l'écoude des action client //debut de l'écoude des action client
while(phaseActive) while(phaseActive)
{ {
String command = (String) coms.recieve(); String command;
command = (String) coms.recieve();
System.out.println("GOT COMMAND :"+command);
if(command != null) if(command != null)
{ {
switch (command) { switch (command) {
case Command.PING: case Command.PING:
coms.send(Command.PONG); coms.send(Command.PONG);
System.out.println("pong");
break; break;
case Command.PASS_TURN: case Command.PASS_TURN:
@@ -91,6 +91,9 @@ public class Joueur {
case Command.PUT_CARD: case Command.PUT_CARD:
putCard(adversaire,board); putCard(adversaire,board);
break; break;
case "time out":
System.exit(0);
break;
default: default:
coms.send(Command.POPUP); coms.send(Command.POPUP);
@@ -162,8 +165,8 @@ public class Joueur {
} }
public Socket getConnection() { public ComsJoueur getComs() {
return connection; return coms;
} }
public void setPV(int pV) { public void setPV(int pV) {
+7 -8
View File
@@ -1,23 +1,22 @@
package game; package game;
import java.io.IOException; import java.io.IOException;
import java.net.Socket;
public class Match extends Thread { public class Match extends Thread {
Joueur joueur1, joueur2; Joueur joueur1, joueur2;
int tour = 1; int tour = 1;
private Board board; private Board board;
public Match(Socket j1, Socket j2) throws IOException { public Match(ComsJoueur joueur1Com, ComsJoueur joueur2Com) throws IOException {
super(); super();
this.board=new Board(); this.board=new Board();
// random j2/j1 // random j2/j1
if (Math.random() > 0.5F) { if (Math.random() > 0.5F) {
joueur1 = new Joueur(j1); joueur1 = new Joueur(joueur1Com);
joueur2 = new Joueur(j2); joueur2 = new Joueur(joueur2Com);
} else { } else {
joueur1 = new Joueur(j2); joueur1 = new Joueur(joueur2Com);
joueur2 = new Joueur(j1); joueur2 = new Joueur(joueur1Com);
} }
joueur1.requestDataEarlyGameData(); joueur1.requestDataEarlyGameData();
@@ -47,7 +46,7 @@ public class Match extends Thread {
} }
private void endGameAfterIssue() { private void endGameAfterIssue() {
if(PlayerTesteur.playerTest(joueur1.getConnection())) if(PlayerTesteur.playerTest(joueur1.getComs()))
{ {
try { try {
joueur1.win(); joueur1.win();
@@ -55,7 +54,7 @@ public class Match extends Thread {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(PlayerTesteur.playerTest(joueur2.getConnection())) if(PlayerTesteur.playerTest(joueur2.getComs()))
{ {
try { try {
joueur2.win(); joueur2.win();
+2 -16
View File
@@ -1,20 +1,12 @@
package game; package game;
import java.io.IOException; import java.io.IOException;
import java.net.Socket;
public class PlayerTesteur { 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 { try {
com.send(Command.PING); com.send(Command.PING);
} catch (IOException e) { } 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"); System.out.println("le client a été trop long pour répondre ou une erreur reseau c'est porduite");
return false; return false;
} }
return true;
try {
com.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
} }
} }
+9 -1
View File
@@ -97,7 +97,7 @@ import game.cards.Renaissance.Rennaissance_Vlad;
public class CardRegistery { public class CardRegistery {
public static List<Class<? extends Card>> registry; public static List<Class<? extends Card>> registry;
public CardRegistery() private static void initCardRegistery()
{ {
registry = new ArrayList<>(); registry = new ArrayList<>();
@@ -205,17 +205,25 @@ public class CardRegistery {
} }
public static int get(Class<? extends Card> class1) { public static int get(Class<? extends Card> class1) {
checkregistry();
int a = registry.indexOf(class1); int a = registry.indexOf(class1);
return a != -1 ? a : 0; return a != -1 ? a : 0;
} }
public static Class<? extends Card> get(int index) { public static Class<? extends Card> get(int index) {
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) { public static int get(Card card) {
checkregistry();
return get(card.getClass()); return get(card.getClass());
} }
public static void checkregistry()
{
if(registry == null)initCardRegistery();
}
} }