patch connection + jouer carte + patch timeout + patch registry
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.iutlaval.myapplication">
|
package="com.iutlaval.myapplication">
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
|||||||
@@ -204,8 +204,13 @@ public class CardRegistery {
|
|||||||
registry.add(Moyen_Age_Pouvoir_Du_Franc.class);
|
registry.add(Moyen_Age_Pouvoir_Du_Franc.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int get(Class<? extends Card> class1) {
|
public static int indexOf(Class<? extends Card> class1) {
|
||||||
int a = registry.indexOf(class1);
|
int a = registry.indexOf(class1);
|
||||||
|
|
||||||
|
if(a == -1){
|
||||||
|
throw new RuntimeException("Class not found :"+class1.getName());
|
||||||
|
}
|
||||||
|
//if the class is not found default to 0
|
||||||
return a != -1 ? a : 0;
|
return a != -1 ? a : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +221,7 @@ public class CardRegistery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static int get(Card card) {
|
public static int indexOf(Card card) {
|
||||||
return get(card.getClass());
|
return indexOf(card.getClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import com.iutlaval.myapplication.Game.Cards.Card;
|
|||||||
import com.iutlaval.myapplication.Game.Cards.CardRegistery;
|
import com.iutlaval.myapplication.Game.Cards.CardRegistery;
|
||||||
import com.iutlaval.myapplication.GameActivity;
|
import com.iutlaval.myapplication.GameActivity;
|
||||||
import com.iutlaval.myapplication.R;
|
import com.iutlaval.myapplication.R;
|
||||||
import com.iutlaval.myapplication.Video.Drawables.Drawable;
|
|
||||||
import com.iutlaval.myapplication.Video.Drawables.DrawableBitmap;
|
import com.iutlaval.myapplication.Video.Drawables.DrawableBitmap;
|
||||||
import com.iutlaval.myapplication.Video.Drawables.DrawableCard;
|
import com.iutlaval.myapplication.Video.Drawables.DrawableCard;
|
||||||
import com.iutlaval.myapplication.Video.Drawables.DrawableSelfRemoving;
|
import com.iutlaval.myapplication.Video.Drawables.DrawableSelfRemoving;
|
||||||
@@ -23,8 +22,10 @@ import java.io.IOException;
|
|||||||
import java.io.InterruptedIOException;
|
import java.io.InterruptedIOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.OptionalDataException;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
|
|
||||||
public class GameLogicThread extends Thread{
|
public class GameLogicThread extends Thread{
|
||||||
|
|
||||||
@@ -49,6 +50,14 @@ public class GameLogicThread extends Thread{
|
|||||||
private String deckName;
|
private String deckName;
|
||||||
private int mana=0;
|
private int mana=0;
|
||||||
|
|
||||||
|
|
||||||
|
//ces variable permette la comunication avec le thread android du tactile et le reseau en passant par ce thread
|
||||||
|
boolean requestDone=true;
|
||||||
|
DrawableCard requestCard=null;
|
||||||
|
int requestZone=-1;
|
||||||
|
boolean requestResult;
|
||||||
|
|
||||||
|
|
||||||
public GameLogicThread(GameActivity gameActivity, String deckName, Renderer renderer)
|
public GameLogicThread(GameActivity gameActivity, String deckName, Renderer renderer)
|
||||||
{
|
{
|
||||||
new CardRegistery();
|
new CardRegistery();
|
||||||
@@ -83,8 +92,8 @@ public class GameLogicThread extends Thread{
|
|||||||
|
|
||||||
ready=true;
|
ready=true;
|
||||||
|
|
||||||
final String host = "4.tcp.ngrok.io";//192.168.43.251tcp://2.tcp.ngrok.io:
|
final String host = "0.tcp.ngrok.io";//192.168.43.251tcp://2.tcp.ngrok.io:
|
||||||
final int port = 14307;
|
final int port = 11601;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client = new Socket(host, port);
|
client = new Socket(host, port);
|
||||||
@@ -99,13 +108,26 @@ public class GameLogicThread extends Thread{
|
|||||||
|
|
||||||
//principe de fonctionnement : on attends une comande du server et on l'execute
|
//principe de fonctionnement : on attends une comande du server et on l'execute
|
||||||
//si quelquechose se passe alors c'est le server qu'il l'a dit
|
//si quelquechose se passe alors c'est le server qu'il l'a dit
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while(ready && !cancelled)
|
while(ready && !cancelled)
|
||||||
{
|
{
|
||||||
|
if(!requestDone && requestZone != -1 && requestCard != null)
|
||||||
|
{
|
||||||
|
try {//on allonge le delais car sinon on skip l'autorisation serveur
|
||||||
|
client.setSoTimeout(Integer.MAX_VALUE);
|
||||||
|
} catch (SocketException e) {}
|
||||||
|
requestResult = onCardPlayed(requestCard,requestZone);
|
||||||
|
requestZone=-1;
|
||||||
|
requestCard=null;
|
||||||
|
requestDone=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
//on limite le temps du readobject pour eviter les blockaeg
|
||||||
|
client.setSoTimeout(1000);
|
||||||
String serveurCmd = (String)clientIn.readObject();
|
String serveurCmd = (String)clientIn.readObject();
|
||||||
|
client.setSoTimeout(Integer.MAX_VALUE);
|
||||||
|
Log.e("command",serveurCmd);
|
||||||
switch (serveurCmd)
|
switch (serveurCmd)
|
||||||
{
|
{
|
||||||
case Command.GET_DECK:
|
case Command.GET_DECK:
|
||||||
@@ -113,11 +135,17 @@ public class GameLogicThread extends Thread{
|
|||||||
String deckstr = (String)clientIn.readObject();
|
String deckstr = (String)clientIn.readObject();
|
||||||
deck = new NetworkDeck(deckstr,gameActivity.getBaseContext());
|
deck = new NetworkDeck(deckstr,gameActivity.getBaseContext());
|
||||||
Log.e("got deck",deckstr);
|
Log.e("got deck",deckstr);
|
||||||
|
|
||||||
Log.e("recived","getdeck");
|
|
||||||
break;
|
break;
|
||||||
case Command.DRAW:
|
case Command.DRAW:
|
||||||
int nbcard = (Integer)clientIn.readObject();
|
Object o = clientIn.readObject();
|
||||||
|
int nbcard;
|
||||||
|
if(o instanceof String)
|
||||||
|
{
|
||||||
|
nbcard = Integer.parseInt((String) o);
|
||||||
|
}else{
|
||||||
|
nbcard = (Integer)o;
|
||||||
|
}
|
||||||
|
|
||||||
hand.pickCardFromDeck(deck,nbcard);
|
hand.pickCardFromDeck(deck,nbcard);
|
||||||
Log.e("picked",nbcard+"card");
|
Log.e("picked",nbcard+"card");
|
||||||
drawHandPreview();
|
drawHandPreview();
|
||||||
@@ -136,7 +164,6 @@ public class GameLogicThread extends Thread{
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case Command.ENEMYTURN:
|
case Command.ENEMYTURN:
|
||||||
isYourTurn=false;
|
|
||||||
renderer.addToDraw(new DrawableSelfRemoving(new DrawableBitmap(bitmapEnemyTurn,0,0,"enemyTurn",100F,50F),1));
|
renderer.addToDraw(new DrawableSelfRemoving(new DrawableBitmap(bitmapEnemyTurn,0,0,"enemyTurn",100F,50F),1));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -146,6 +173,7 @@ public class GameLogicThread extends Thread{
|
|||||||
popupToast.setText(recivedMessage);
|
popupToast.setText(recivedMessage);
|
||||||
popupToast.setDuration(Toast.LENGTH_LONG);
|
popupToast.setDuration(Toast.LENGTH_LONG);
|
||||||
popupToast.show();
|
popupToast.show();
|
||||||
|
Log.e("popup",recivedMessage);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Command.WIN:
|
case Command.WIN:
|
||||||
@@ -155,6 +183,7 @@ public class GameLogicThread extends Thread{
|
|||||||
winToast.setText("bravo ! vous avez gagné");
|
winToast.setText("bravo ! vous avez gagné");
|
||||||
winToast.setDuration(Toast.LENGTH_LONG);
|
winToast.setDuration(Toast.LENGTH_LONG);
|
||||||
winToast.show();
|
winToast.show();
|
||||||
|
renderer.updateFrame();
|
||||||
break;
|
break;
|
||||||
case Command.LOSE:
|
case Command.LOSE:
|
||||||
//on termine la partie
|
//on termine la partie
|
||||||
@@ -184,16 +213,24 @@ public class GameLogicThread extends Thread{
|
|||||||
//ajout la card au terain
|
//ajout la card au terain
|
||||||
board.setEnemyCard(zone,cardPlayed);
|
board.setEnemyCard(zone,cardPlayed);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Log.e("UNKOWN COMMAND",serveurCmd);
|
Log.e("UNKOWN COMMAND",serveurCmd);
|
||||||
}
|
}
|
||||||
}catch (InterruptedIOException e)
|
}catch (InterruptedIOException e)
|
||||||
{
|
{
|
||||||
|
e.printStackTrace();
|
||||||
Log.e("Stopping","dead thread");
|
Log.e("Stopping","dead thread");
|
||||||
|
}catch(IOException e)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
Log.e("ERROR DURING COMS","SERVER DIED");
|
Log.e("ERROR DURING COMS","SERVER DIED");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
if(e instanceof OptionalDataException)
|
||||||
|
{
|
||||||
|
System.out.println(((OptionalDataException)e).eof);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,35 +252,53 @@ public class GameLogicThread extends Thread{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* cette evenement est appelé par le touchHandler et permet d'envoyer au serveur le fait qu'un carte a été jouer
|
* cette evenement est appelé par le touchHandler et permet d'envoyer au serveur le fait qu'un carte a été jouer
|
||||||
|
* CE CODE CONTIEN DE l'ACCES RESEAU QUI N'APPARTIEN QU'A CE THREAD IS DOIT RESTER PRIVE
|
||||||
* @param card la carte jouer
|
* @param card la carte jouer
|
||||||
* @param zone ou elle a été joué sur le terrain
|
* @param zone ou elle a été joué sur le terrain
|
||||||
* @return retourne si la carte a put être jouer
|
* @return retourne si la carte a put être jouer
|
||||||
*/
|
*/
|
||||||
protected boolean onCardPlayed(DrawableCard card,int zone)
|
private boolean onCardPlayed(DrawableCard card,int zone)
|
||||||
|
{
|
||||||
|
if(isYourTurn)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
//on veut jouer une carte
|
//on veut jouer une carte
|
||||||
clientOut.writeObject(Command.PUT_CARD);
|
clientOut.writeObject(Command.PUT_CARD);
|
||||||
|
|
||||||
|
|
||||||
//on lui dit quelle caret on veut
|
//on lui dit quelle caret on veut
|
||||||
int cardId = CardRegistery.get(card.getCard());
|
int cardId = CardRegistery.indexOf(card.getCard());
|
||||||
if(cardId == -1){
|
if (cardId == -1) {
|
||||||
clientOut.writeObject("CLIENT REGISTRY ERROR");
|
clientOut.writeObject("CLIENT REGISTRY ERROR");
|
||||||
throw new UnrecognizedCard();
|
throw new UnrecognizedCard();
|
||||||
}
|
}
|
||||||
clientOut.writeObject(cardId);
|
clientOut.writeObject(cardId);
|
||||||
clientOut.writeObject(zone);
|
clientOut.writeObject(zone);
|
||||||
|
|
||||||
if(clientIn.readObject().equals(Command.OK))
|
if (clientIn.readObject().equals(Command.OK)) {
|
||||||
{
|
board.setCard(zone, card.getCard());
|
||||||
board.setCard(zone,card.getCard());
|
|
||||||
return true;
|
return true;
|
||||||
}else{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
}catch (Exception e)
|
||||||
return false;
|
{
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean setOnCardPlayedRequest(DrawableCard card,int zone)
|
||||||
|
{
|
||||||
|
//on ne peut pas avoir 2 requette simultané
|
||||||
|
while(!requestDone);
|
||||||
|
this.requestCard=card;
|
||||||
|
this.requestZone=zone;
|
||||||
|
requestDone=false;
|
||||||
|
|
||||||
|
//on attends la fin de la requette
|
||||||
|
while(!requestDone);
|
||||||
|
return requestResult;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cette fonction est appeller a chaque fin de frame
|
* cette fonction est appeller a chaque fin de frame
|
||||||
@@ -299,7 +354,6 @@ public class GameLogicThread extends Thread{
|
|||||||
cancelled=true;
|
cancelled=true;
|
||||||
|
|
||||||
interrupt();
|
interrupt();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onEndTurnButtonPushed() {
|
public void onEndTurnButtonPushed() {
|
||||||
@@ -317,4 +371,12 @@ public class GameLogicThread extends Thread{
|
|||||||
t.show();
|
t.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ObjectOutputStream getClientOut() {
|
||||||
|
return clientOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectInputStream getClientIn() {
|
||||||
|
return clientIn;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,7 @@ public class TouchHandler {
|
|||||||
//on ignore le multi touch
|
//on ignore le multi touch
|
||||||
if(event.getPointerCount() <= 1)
|
if(event.getPointerCount() <= 1)
|
||||||
{
|
{
|
||||||
|
endTurnButtonHandle(event);
|
||||||
handSelectionHandler(event);
|
handSelectionHandler(event);
|
||||||
//afficher grosse carte
|
//afficher grosse carte
|
||||||
bigCardHandler(event);
|
bigCardHandler(event);
|
||||||
@@ -146,7 +147,7 @@ public class TouchHandler {
|
|||||||
|
|
||||||
}else if(event.getAction() == MotionEvent.ACTION_UP) {
|
}else if(event.getAction() == MotionEvent.ACTION_UP) {
|
||||||
int zone = playableZonesHandler.getHoveredZone(dragAndDropCard);
|
int zone = playableZonesHandler.getHoveredZone(dragAndDropCard);
|
||||||
if(zone != -1 && gameLogic.onCardPlayed(dragAndDropCard,zone))
|
if(zone != -1 && gameLogic.setOnCardPlayedRequest(dragAndDropCard,zone))
|
||||||
{
|
{
|
||||||
dragAndDropCard.setOnBoard(true);
|
dragAndDropCard.setOnBoard(true);
|
||||||
dragAndDropCard.setCoordinates((DrawableCard.getCardWith()+1)*zone,55F);
|
dragAndDropCard.setCoordinates((DrawableCard.getCardWith()+1)*zone,55F);
|
||||||
|
|||||||
@@ -154,6 +154,7 @@ public class Renderer extends SurfaceView implements SurfaceHolder.Callback {
|
|||||||
|
|
||||||
Log.i("RENDERER","STARTING ENGINE");
|
Log.i("RENDERER","STARTING ENGINE");
|
||||||
engine = new GameLogicThread(gameActivity,deckName,renderer);
|
engine = new GameLogicThread(gameActivity,deckName,renderer);
|
||||||
|
engine.setName("gameEngine");
|
||||||
engine.start();
|
engine.start();
|
||||||
while (!engine.isReady());
|
while (!engine.isReady());
|
||||||
Log.i("RENDERER","ENGINE STARTED");
|
Log.i("RENDERER","ENGINE STARTED");
|
||||||
|
|||||||
Reference in New Issue
Block a user