patch test

This commit is contained in:
marc barbier
2020-12-13 13:27:05 +01:00
parent 7f4362cf33
commit 6b9fade19d
10 changed files with 49 additions and 46 deletions
+19 -12
View File
@@ -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) {
+12 -9
View File
@@ -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) {
+7 -8
View File
@@ -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();
+2 -16
View File
@@ -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;
}
}
+9 -1
View File
@@ -97,7 +97,7 @@ import game.cards.Renaissance.Rennaissance_Vlad;
public class CardRegistery {
public static List<Class<? extends Card>> registry;
public CardRegistery()
private static void initCardRegistery()
{
registry = new ArrayList<>();
@@ -205,17 +205,25 @@ public class CardRegistery {
}
public static int get(Class<? extends Card> class1) {
checkregistry();
int a = registry.indexOf(class1);
return a != -1 ? a : 0;
}
public static Class<? extends Card> 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();
}
}