passage du projet en eclipse compatible + ajout de ping + ajout de timeout + ajout verification client
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
.settings
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Pts3Serveur</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,77 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import game.Match;
|
||||||
|
import game.PlayerTesteur;
|
||||||
|
|
||||||
|
public class Serveur {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String host = "localhost";
|
||||||
|
final int port = 10430;
|
||||||
|
|
||||||
|
ServerSocket tCPserver = null;
|
||||||
|
try {
|
||||||
|
tCPserver = new ServerSocket(port, 50, InetAddress.getByName(host));
|
||||||
|
tCPserver.setSoTimeout(250);// 250 de ping max
|
||||||
|
} catch (UnknownHostException e1) {// cette exception ne s'affichera jammais vu que l'on utilise localhost
|
||||||
|
} catch (IOException e1) {
|
||||||
|
System.err.println(
|
||||||
|
"erreur le serveur n'a pas put dermarer \nil est probablement blocké par un autre processus paralélle");
|
||||||
|
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;
|
||||||
|
Socket joueur2EnAttente = null;
|
||||||
|
while (true) {
|
||||||
|
if (joueur1EnAttente == null) {
|
||||||
|
try {
|
||||||
|
joueur1EnAttente = tCPserver.accept();
|
||||||
|
System.out.println("joueur1 trouvé");
|
||||||
|
} catch (IOException e) {
|
||||||
|
joueur1EnAttente = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (joueur2EnAttente == null) {
|
||||||
|
try {
|
||||||
|
joueur2EnAttente = tCPserver.accept();
|
||||||
|
System.out.println("joueur2 trouvé");
|
||||||
|
} catch (IOException e) {
|
||||||
|
joueur2EnAttente = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (joueur1EnAttente != null && joueur2EnAttente != null) {
|
||||||
|
try {
|
||||||
|
if (PlayerTesteur.playerTest(joueur1EnAttente)) {
|
||||||
|
if (PlayerTesteur.playerTest(joueur2EnAttente)) {
|
||||||
|
new Match(joueur1EnAttente, joueur2EnAttente);
|
||||||
|
System.out.println("match lancé");
|
||||||
|
} else {
|
||||||
|
// le joueur est cassé donc on le retire de la file d'attente
|
||||||
|
joueur2EnAttente = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// le joueur est cassé donc on le retire de la file d'attente
|
||||||
|
joueur1EnAttente = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("client dead");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class ComsJoueur {
|
||||||
|
private ObjectOutputStream serverOut;
|
||||||
|
private ObjectInputStream serverIn;
|
||||||
|
|
||||||
|
public ComsJoueur(Socket connection) throws IOException
|
||||||
|
{
|
||||||
|
serverOut = new ObjectOutputStream(connection.getOutputStream());
|
||||||
|
serverIn = new ObjectInputStream(connection.getInputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(Object object) throws IOException
|
||||||
|
{
|
||||||
|
serverOut.writeObject(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object recieve()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return serverIn.readObject();
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.err.println("ERREUR une classe non trouvé a éte transimise , essayer de n'evoyer que des String au possible");
|
||||||
|
return "";
|
||||||
|
} catch (IOException e) {
|
||||||
|
return "time out";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object waitAndRecieve()
|
||||||
|
{
|
||||||
|
String returnVal;
|
||||||
|
while((returnVal =(String) recieve())!= null);
|
||||||
|
return returnVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException
|
||||||
|
{
|
||||||
|
serverOut.close();
|
||||||
|
serverIn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import game.deck.Deck;
|
||||||
|
import game.deck.DemoDeck;
|
||||||
|
|
||||||
|
public class DeckRegistery {
|
||||||
|
public static Deck get(String name)
|
||||||
|
{
|
||||||
|
switch (name) {
|
||||||
|
default:
|
||||||
|
return new DemoDeck();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import game.cards.Card;
|
||||||
|
import game.deck.Deck;
|
||||||
|
|
||||||
|
public class DeckSerializer {
|
||||||
|
public static String serializeDeck(Deck d)
|
||||||
|
{
|
||||||
|
StringBuilder str = new StringBuilder(d.getCards().size()*2);
|
||||||
|
for(Card c : d.getCards())
|
||||||
|
{
|
||||||
|
str.append(c.toString()+",");
|
||||||
|
}
|
||||||
|
return str.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Deck deSerializeDeck(String g)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
public class Hand {
|
||||||
|
|
||||||
|
public void draw(int i) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
import game.deck.Deck;
|
||||||
|
|
||||||
|
public class Joueur {
|
||||||
|
|
||||||
|
Socket connection;
|
||||||
|
ComsJoueur coms;
|
||||||
|
Deck deck;
|
||||||
|
Hand hand;
|
||||||
|
int PV;
|
||||||
|
|
||||||
|
public Joueur(Socket connectionJoueur) throws IOException {
|
||||||
|
connection=connectionJoueur;
|
||||||
|
coms = new ComsJoueur(connection);
|
||||||
|
hand = new Hand();
|
||||||
|
PV =20;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestDataEarlyGameData() throws IOException {
|
||||||
|
coms.send("getDeck");
|
||||||
|
String deckname = (String) coms.recieve();
|
||||||
|
deck = DeckRegistery.get(deckname);
|
||||||
|
deck.shuffle();
|
||||||
|
coms.send(DeckSerializer.serializeDeck(deck));
|
||||||
|
draw(5);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(int amount) throws IOException
|
||||||
|
{
|
||||||
|
coms.send("draw");
|
||||||
|
coms.send(amount);
|
||||||
|
hand.draw(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void standyPhase() throws IOException {
|
||||||
|
coms.send("yourTurn");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void win() throws IOException {
|
||||||
|
coms.send("win");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void lose() throws IOException {
|
||||||
|
coms.send("lose");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mainPhase1() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mainPhase2() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void endPhase() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class Match extends Thread {
|
||||||
|
Joueur joueur1, joueur2;
|
||||||
|
int tour = 1;
|
||||||
|
|
||||||
|
public Match(Socket j1, Socket j2) throws IOException {
|
||||||
|
super();
|
||||||
|
// random j2/j1
|
||||||
|
if (Math.random() > 0.5F) {
|
||||||
|
joueur1 = new Joueur(j1);
|
||||||
|
joueur2 = new Joueur(j2);
|
||||||
|
} else {
|
||||||
|
joueur1 = new Joueur(j2);
|
||||||
|
joueur2 = new Joueur(j1);
|
||||||
|
}
|
||||||
|
|
||||||
|
joueur1.requestDataEarlyGameData();
|
||||||
|
joueur2.requestDataEarlyGameData();
|
||||||
|
Thread connect = new Thread();
|
||||||
|
connect.run();
|
||||||
|
System.out.println("ready");
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// gameplay
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (joueur1.PV != 0 || joueur2.PV != 0) {
|
||||||
|
try {
|
||||||
|
tour(joueur1);
|
||||||
|
tour(joueur2);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
endMatch();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//annonce du vainqueur et du perdant
|
||||||
|
private void endMatch() throws IOException {
|
||||||
|
if(joueur1.PV == 0) {
|
||||||
|
joueur2.win();
|
||||||
|
joueur1.lose();
|
||||||
|
}else {
|
||||||
|
joueur1.win();
|
||||||
|
joueur2.lose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//enchainement d'action que fait un joueur durant son tour
|
||||||
|
private void tour(Joueur joueur) throws IOException {
|
||||||
|
joueur1.coms.send("pupup");
|
||||||
|
joueur1.coms.send("tour : " + tour);
|
||||||
|
joueur2.coms.send("pupup");
|
||||||
|
joueur2.coms.send("tour : " + tour);
|
||||||
|
joueur.draw(1); //pioche 1 carte
|
||||||
|
joueur.standyPhase(); //Activation des effets avant que le tour du @joueur commence
|
||||||
|
joueur.mainPhase1(); //Joue autant de carte qu'il veut/peut
|
||||||
|
battlePhase(joueur);//lance des attaques à son adversaire qui peut répliquer
|
||||||
|
joueur.mainPhase2();//Nouvelle phase ou le @joueur peut jouer ses cartes si il veut/peut
|
||||||
|
joueur.endPhase();//fin du tour du @joueur
|
||||||
|
}
|
||||||
|
|
||||||
|
private void battlePhase(Joueur joueur) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class PlayerTesteur {
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean playerTest(Socket joueur)
|
||||||
|
{
|
||||||
|
ComsJoueur com;
|
||||||
|
try {
|
||||||
|
com = new ComsJoueur(joueur);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
com.send("ping");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.err.println("une erreur c'est produite l'ors du ping");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
//pong
|
||||||
|
com.recieve();
|
||||||
|
} catch (Exception e) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package game.cards;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public abstract class Card implements Serializable{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retourne un entier coresspondant a l'attaque de la carte
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract int getAttack();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retourne la santé de la carte
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract int getHealth();
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return ""+CardRegistery.get(this.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract int getCost();
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package game.cards;
|
||||||
|
|
||||||
|
import java.rmi.registry.Registry;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CardRegistery {
|
||||||
|
public static List<Class> registry;
|
||||||
|
|
||||||
|
private static void init()
|
||||||
|
{
|
||||||
|
registry = new ArrayList<>();
|
||||||
|
|
||||||
|
registry.add(DemoCard.class);
|
||||||
|
registry.add(DemoCard2.class);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int get(Class<? extends Card> class1) {
|
||||||
|
if(registry == null)init();
|
||||||
|
int a = registry.indexOf(class1);
|
||||||
|
return a != -1 ? a : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package game.cards;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class DemoCard extends Card{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAttack() {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHealth() {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCost() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package game.cards;
|
||||||
|
|
||||||
|
public class DemoCard2 extends Card{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAttack() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getHealth() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCost() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package game.deck;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import game.cards.Card;
|
||||||
|
|
||||||
|
public abstract class Deck implements Serializable{
|
||||||
|
private static final long serialVersionUID = -4305303353908447343L;
|
||||||
|
|
||||||
|
public abstract Stack<Card> getCards();
|
||||||
|
|
||||||
|
public void shuffle()
|
||||||
|
{
|
||||||
|
Collections.shuffle(getCards());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retourne le nom du deck ex: ww2 , science
|
||||||
|
* ce nom doit être unique
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract String getDeckName();
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package game.deck;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import game.cards.Card;
|
||||||
|
import game.cards.DemoCard;
|
||||||
|
import game.cards.DemoCard2;
|
||||||
|
|
||||||
|
public class DemoDeck extends Deck{
|
||||||
|
Stack<Card> cards;
|
||||||
|
public DemoDeck()
|
||||||
|
{
|
||||||
|
cards = new Stack<>();
|
||||||
|
for(int i = 0 ; i < 50 ; i++)
|
||||||
|
{
|
||||||
|
if(Math.random() > 0.5)cards.add(new DemoCard());
|
||||||
|
else cards.add(new DemoCard2());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stack<Card> getCards() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return cards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDeckName() {
|
||||||
|
return "DemoDeck";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user