commit 72f481a4a56361b0b8583339bc672df7f0bea0ba Author: marc barbier Date: Wed Dec 9 13:02:27 2020 +0100 passage du projet en eclipse compatible + ajout de ping + ajout de timeout + ajout verification client diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d54800d --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e83119 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.settings diff --git a/.project b/.project new file mode 100644 index 0000000..2494fc7 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Pts3Serveur + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/bin/Serveur.class b/bin/Serveur.class new file mode 100644 index 0000000..87534d2 Binary files /dev/null and b/bin/Serveur.class differ diff --git a/bin/game/ComsJoueur.class b/bin/game/ComsJoueur.class new file mode 100644 index 0000000..0b7f0a1 Binary files /dev/null and b/bin/game/ComsJoueur.class differ diff --git a/bin/game/DeckRegistery.class b/bin/game/DeckRegistery.class new file mode 100644 index 0000000..043405d Binary files /dev/null and b/bin/game/DeckRegistery.class differ diff --git a/bin/game/DeckSerializer.class b/bin/game/DeckSerializer.class new file mode 100644 index 0000000..67755f3 Binary files /dev/null and b/bin/game/DeckSerializer.class differ diff --git a/bin/game/Hand.class b/bin/game/Hand.class new file mode 100644 index 0000000..292260f Binary files /dev/null and b/bin/game/Hand.class differ diff --git a/bin/game/Joueur.class b/bin/game/Joueur.class new file mode 100644 index 0000000..9c5af31 Binary files /dev/null and b/bin/game/Joueur.class differ diff --git a/bin/game/Match.class b/bin/game/Match.class new file mode 100644 index 0000000..09db7f3 Binary files /dev/null and b/bin/game/Match.class differ diff --git a/bin/game/PlayerTesteur.class b/bin/game/PlayerTesteur.class new file mode 100644 index 0000000..4dd36d1 Binary files /dev/null and b/bin/game/PlayerTesteur.class differ diff --git a/bin/game/SimpleUDP.class b/bin/game/SimpleUDP.class new file mode 100644 index 0000000..76c42d4 Binary files /dev/null and b/bin/game/SimpleUDP.class differ diff --git a/bin/game/cards/Card.class b/bin/game/cards/Card.class new file mode 100644 index 0000000..809ea3d Binary files /dev/null and b/bin/game/cards/Card.class differ diff --git a/bin/game/cards/CardRegistery.class b/bin/game/cards/CardRegistery.class new file mode 100644 index 0000000..27a1b71 Binary files /dev/null and b/bin/game/cards/CardRegistery.class differ diff --git a/bin/game/cards/DemoCard.class b/bin/game/cards/DemoCard.class new file mode 100644 index 0000000..c1d1cf3 Binary files /dev/null and b/bin/game/cards/DemoCard.class differ diff --git a/bin/game/cards/DemoCard2.class b/bin/game/cards/DemoCard2.class new file mode 100644 index 0000000..24e117e Binary files /dev/null and b/bin/game/cards/DemoCard2.class differ diff --git a/bin/game/deck/Deck.class b/bin/game/deck/Deck.class new file mode 100644 index 0000000..f42a2c2 Binary files /dev/null and b/bin/game/deck/Deck.class differ diff --git a/bin/game/deck/DemoDeck.class b/bin/game/deck/DemoDeck.class new file mode 100644 index 0000000..6d92c62 Binary files /dev/null and b/bin/game/deck/DemoDeck.class differ diff --git a/src/Serveur.java b/src/Serveur.java new file mode 100644 index 0000000..1648a99 --- /dev/null +++ b/src/Serveur.java @@ -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"); + } + } + } + } + +} diff --git a/src/game/ComsJoueur.java b/src/game/ComsJoueur.java new file mode 100644 index 0000000..025e63f --- /dev/null +++ b/src/game/ComsJoueur.java @@ -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(); + } +} diff --git a/src/game/DeckRegistery.java b/src/game/DeckRegistery.java new file mode 100644 index 0000000..f616ac3 --- /dev/null +++ b/src/game/DeckRegistery.java @@ -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(); + } + } +} diff --git a/src/game/DeckSerializer.java b/src/game/DeckSerializer.java new file mode 100644 index 0000000..ceb0cb0 --- /dev/null +++ b/src/game/DeckSerializer.java @@ -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; + + } +} diff --git a/src/game/Hand.java b/src/game/Hand.java new file mode 100644 index 0000000..c37201f --- /dev/null +++ b/src/game/Hand.java @@ -0,0 +1,10 @@ +package game; + +public class Hand { + + public void draw(int i) { + // TODO Auto-generated method stub + + } + +} diff --git a/src/game/Joueur.java b/src/game/Joueur.java new file mode 100644 index 0000000..676efa6 --- /dev/null +++ b/src/game/Joueur.java @@ -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 + + } + +} diff --git a/src/game/Match.java b/src/game/Match.java new file mode 100644 index 0000000..a8b7487 --- /dev/null +++ b/src/game/Match.java @@ -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 + + } +} diff --git a/src/game/PlayerTesteur.java b/src/game/PlayerTesteur.java new file mode 100644 index 0000000..6da25d6 --- /dev/null +++ b/src/game/PlayerTesteur.java @@ -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; + } +} diff --git a/src/game/SimpleUDP.java b/src/game/SimpleUDP.java new file mode 100644 index 0000000..acd5e62 --- /dev/null +++ b/src/game/SimpleUDP.java @@ -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()); + } +} diff --git a/src/game/cards/Card.java b/src/game/cards/Card.java new file mode 100644 index 0000000..4bec437 --- /dev/null +++ b/src/game/cards/Card.java @@ -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(); +} + diff --git a/src/game/cards/CardRegistery.java b/src/game/cards/CardRegistery.java new file mode 100644 index 0000000..110f92d --- /dev/null +++ b/src/game/cards/CardRegistery.java @@ -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 registry; + + private static void init() + { + registry = new ArrayList<>(); + + registry.add(DemoCard.class); + registry.add(DemoCard2.class); + + } + + public static int get(Class class1) { + if(registry == null)init(); + int a = registry.indexOf(class1); + return a != -1 ? a : 0; + } + + +} diff --git a/src/game/cards/DemoCard.java b/src/game/cards/DemoCard.java new file mode 100644 index 0000000..d9f19d1 --- /dev/null +++ b/src/game/cards/DemoCard.java @@ -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; + } +} diff --git a/src/game/cards/DemoCard2.java b/src/game/cards/DemoCard2.java new file mode 100644 index 0000000..2f96b3d --- /dev/null +++ b/src/game/cards/DemoCard2.java @@ -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; + } + +} diff --git a/src/game/deck/Deck.java b/src/game/deck/Deck.java new file mode 100644 index 0000000..bb892a9 --- /dev/null +++ b/src/game/deck/Deck.java @@ -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 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(); +} diff --git a/src/game/deck/DemoDeck.java b/src/game/deck/DemoDeck.java new file mode 100644 index 0000000..df17f04 --- /dev/null +++ b/src/game/deck/DemoDeck.java @@ -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 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 getCards() { + // TODO Auto-generated method stub + return cards; + } + + @Override + public String getDeckName() { + return "DemoDeck"; + } +}