patch crash + phase 1

This commit is contained in:
marc barbier
2020-12-16 16:33:00 +01:00
parent 6b9fade19d
commit 611c9f8d29
13 changed files with 65 additions and 52 deletions
+4
View File
@@ -59,6 +59,10 @@ public class Serveur {
if (PlayerTesteur.playerTest(joueur2Com)) {
new Match(joueur1Com,joueur2Com);
System.out.println("match lancé");
joueur2EnAttente = null;
joueur2Com=null;
joueur1EnAttente = null;
joueur1Com=null;
} else {
// le joueur est cassé donc on le retire de la file d'attente
System.out.println("j2 invalid");
+3 -4
View File
@@ -18,8 +18,7 @@ public final class Command {
public static final String WIN = "win";
public static final String LOSE = "lose";
public static final String PING = "ping";
public static final Object PONG = "pong";
public static final Object NOK = "nok";
public static final Object OK = "ok";
public static final String PONG = "pong";
public static final String NOK = "nok";
public static final String OK = "ok";
}
+17 -5
View File
@@ -8,24 +8,27 @@ import java.net.Socket;
public class ComsJoueur {
private ObjectOutputStream serverOut;
private ObjectInputStream serverIn;
private Socket socket;
public ComsJoueur(Socket connection) throws IOException
{
serverOut = new ObjectOutputStream(connection.getOutputStream());
serverIn = new ObjectInputStream(connection.getInputStream());
this.socket=connection;
}
public void send(Object object) throws IOException
public void send(String message) throws IOException
{
serverOut.writeObject(object);
serverOut.writeObject(message);
System.out.println("Sending "+message);
}
public Object recieve()
public String recieve()
{
try {
return serverIn.readObject();
return (String) 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");
System.err.println("ERREUR une classe non trouvé a éte transimise , essayer de n'evoyer que des String");
return "";
} catch (IOException e) {
return "time out";
@@ -44,4 +47,13 @@ public class ComsJoueur {
serverOut.close();
serverIn.close();
}
public Socket getSocket() {
return socket;
}
public void send(int cardId) throws IOException {
send(""+cardId);
}
}
+16 -5
View File
@@ -5,18 +5,29 @@ import game.deck.Deck;
public class DeckSerializer {
public static String serializeDeck(Deck d)
{
return forwardSerialisze(d);
}
private static String reverseSerialisze(Deck d)
{
StringBuilder str = new StringBuilder(d.getCards().size()*2);
for(Card c : d.getCards())
Card[] cardArray = d.getCards().toArray(new Card[0]);
for(int i = cardArray.length -1 ; i >= 0 ; i--)
{
str.append(c.toString()+",");
str.append(cardArray[i]+",");
}
return str.toString();
}
public static Deck deSerializeDeck(String g)
private static String forwardSerialisze(Deck d)
{
return null;
StringBuilder str = new StringBuilder(d.getCards().size()*2);
for(Card c : d.getCards())
{
str.append(c+",");
}
return str.toString();
}
}
+20 -34
View File
@@ -66,22 +66,24 @@ public class Joueur {
//on affiche le debut de tour
coms.send(Command.YOURTURN);
System.out.println("NOW WE SHOULDN'T SEND COMMAND");
boolean phaseActive=true;
//debut de l'écoude des action client
adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE);
while(phaseActive)
{
String command;
command = (String) coms.recieve();
System.out.println("GOT COMMAND :"+command);
if(command != null)
coms.getSocket().setSoTimeout(500);
String command = (String) coms.recieve();
coms.getSocket().setSoTimeout(Integer.MAX_VALUE);
if(command != null && !command.equals("time out"))
{
System.out.println("GOT COMMAND :"+command);
switch (command) {
case Command.PING:
coms.send(Command.PONG);
System.out.println("pong");
break;
case Command.PASS_TURN:
@@ -89,34 +91,32 @@ public class Joueur {
break;
case Command.PUT_CARD:
System.out.println("entering put card");
putCard(adversaire,board);
break;
case "time out":
System.exit(0);
break;
default:
coms.send(Command.POPUP);
coms.send("vous ne pouvez pas faire sa a la premiére phase");
break;
System.err.println("unknown command "+command);
}
}
//on maintien l'adversaire actif en lui envoyant un ping
adversaire.ping();
}
}
private void putCard(Joueur adversaire, Board board) throws IOException {
Object info = coms.recieve();
if(info instanceof String) {
System.err.println((String)info);
//crash here
String info = (String) coms.recieve();
//si ce n'est pas un nombre
if(!info.matches("-?\\d+")) {
System.err.println(info);
}else {
int cardId = (int)info;
int cardId = Integer.parseInt(info);
Class<? extends Card> cardClass = CardRegistery.get(cardId);
System.out.println(cardClass.getName());
int zone = (int)coms.recieve();
int zone = Integer.parseInt(coms.recieve());
int handIndex = hand.contains(cardClass);
if(handIndex == -1)coms.send(Command.NOK);
@@ -141,20 +141,6 @@ public class Joueur {
coms.send(zone);
}
/**
* envoie ping au jour et retourne vrai si le joueur a repondu
* @return
*/
private boolean ping() {
try {
coms.send(Command.PING);
} catch (IOException e) {
return false;
}
return coms.recieve().equals(Command.PONG);//pong
}
public void mainPhase2() {
// TODO Auto-generated method stub
+5 -4
View File
@@ -3,14 +3,15 @@ package game.deck;
public class DeckRegistery {
public static Deck get(String name)
{
switch (name) {
case "mythes":
switch (name.trim()) {
case "mythes et legendes grecs":
return new Mythes();
case "moyenage":
case "moyen-age francais":
return new MoyeneAge();
case "rennaissance":
case "renaissance":
return new Renaissance();
default:
System.out.println(name);
return new MoyeneAge();
}
}