Projet Darts  0.2
BTS SNIR LaSalle Avignon 2020
Partie.java
Aller à la documentation de ce fichier.
1 package projet.lasalle84.darts;
2 
3 import android.bluetooth.BluetoothAdapter;
4 import android.bluetooth.BluetoothDevice;
5 import android.os.Bundle;
6 import android.os.Handler;
7 import android.os.Message;
8 import android.util.Log;
9 
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.Set;
13 
14 import static android.os.SystemClock.sleep;
15 
26 public class Partie
27 {
31  private final static String TAG = "Partie";
32  private final static String NOM_PERIPHERIQUE_BLUETOOTH_ECRAN = "ecran-darts";
33  private final static String NOM_PERIPHERIQUE_BLUETOOTH_CIBLE = "impact-darts";
34  public final static int NB_FLECHETTE = 3;
38  public final static int JOUEUR_SUIVANT = 0;
39  public final static int SET_SCORE = 1;
40  public final static int IMPACT = 2;
41  public final static int GAGNANT = 3;
42  public final static int CONNEXION_CIBLE = 4;
46  private int nbJoueurs;
47  private int nbManche;
48  private TypeJeu typeJeu;
49  private ArrayList<Joueur> lesJoueurs;
50  private BluetoothAdapter bluetoothAdapter;
51  private Set<BluetoothDevice> devices;
52  private Peripherique ecran = null;
53  private Peripherique cible = null;
54  private Handler handlerUI = null;
55  private int impact[] = null;
56  private boolean impactEstRecuperer;
57  private boolean estFini = false;
58  private boolean estDoubleImpact = false;
59  private boolean afficheRegle = true;
60 
68  public Partie(ArrayList<Joueur> lesJoueurs, TypeJeu typeJeu, boolean AfficheRegle, BluetoothAdapter bluetoothAdapter, Handler handlerUI)
69  {
70  this.lesJoueurs = lesJoueurs;
71  this.typeJeu = typeJeu;
72  this.bluetoothAdapter = bluetoothAdapter;
73  this.handlerUI = handlerUI;
74  this.nbManche = 0;
75  this.nbJoueurs = this.lesJoueurs.size();
76  impact = new int[2];
77  this.afficheRegle = AfficheRegle;
80 
81  }
82 
89  public void recupererPeripheriques()
90  {
91  Log.d(TAG,"recupererPeripheriques()");
92  devices = bluetoothAdapter.getBondedDevices();
93  }
94 
101  {
102  Log.d(TAG,"connecterPeripheriquesBluetooth()");
103 
104  String nomPeripherique = NOM_PERIPHERIQUE_BLUETOOTH_ECRAN;
105  for (BluetoothDevice device : devices)
106  {
107  if(device.getName().contains(nomPeripherique))
108  {
109  ecran = new Peripherique(device, handlerBluetooth);
110  Log.d(TAG,"Adresse du péripherique raspberrypi " + device.getAddress());
111  break;
112  }
113  }
114 
115  nomPeripherique = NOM_PERIPHERIQUE_BLUETOOTH_CIBLE;
116  for (BluetoothDevice device : devices)
117  {
118  if(device.getName().contains(nomPeripherique))
119  {
120  cible = new Peripherique(device, handlerBluetooth);
121  Log.d(TAG,"Adresse du péripherique darts " + device.getAddress());
122  break;
123  }
124  }
125 
126  if(ecran != null)
127  ecran.connecter();
128  if(cible != null)
129  cible.connecter();
130  }
131 
139  {
140  Log.d(TAG, "deconnecterPeripheriquesBluetooth()");
141  if (ecran != null)
142  {
143  ecran.deconnecter();
144  }
145 
146  if (cible != null)
147  {
148  cible.deconnecter();
149  }
150  }
151 
158  public void envoyerTrame(Peripherique peripherique, String trame)
159  {
160  if(peripherique != null)
161  {
162  Log.d(TAG,"envoyerTrame() " + peripherique.getNom());
163  peripherique.envoyer(trame);
164  }
165  }
172  public void demarrer() {
173  Log.d(TAG, "demarrer()");
175 
176  do
177  {
178  nbManche++;
179  Iterator<Joueur> it = lesJoueurs.iterator();
180  Log.d(TAG, "Manche numéro " + nbManche);
181  while (it.hasNext())
182  {
183  Joueur monJoueur = it.next();
184  Log.d(TAG, "c'est le tour à " + monJoueur.getNom());
185  actualiserJoueurIHM(monJoueur.getNom());
186  int pointVolley = 0;
187 
188  for(int i = 0; i < NB_FLECHETTE; i++)
189  {
190  if (monJoueur.estEliminer())
191  {
192  i = NB_FLECHETTE;
193  }
194  else
195  {
196  impactEstRecuperer = false;
197  attendreImpact();
198  pointVolley =+ impact[0]*impact[1];
199  if(!monJoueur.retirerPoint(pointVolley, this))
200  {
201  i = NB_FLECHETTE;
202  pointVolley = 0;
203  }
204  else if (monJoueur.getScore() == 0 && !typeJeu.estDoubleOut())
205  {
206  envoyerGagnantIHM(monJoueur);
207  i = NB_FLECHETTE;
208  estFini = true;
209  }
210  else if (monJoueur.getScore() == 0 && typeJeu.estDoubleOut() && estDoubleImpact)
211  {
212  envoyerGagnantIHM(monJoueur);
213  i = NB_FLECHETTE;
214  estFini = true;
215  }
216  }
217 
218  }
219  actualiserScoreIHM(monJoueur, monJoueur.getScore());
220  }
221  }while (!estFini);
222  }
223 
230  public void initialiserPartie() {
231  Log.d(TAG, "initialiserPartie()");
232  Iterator<Joueur> it = lesJoueurs.iterator();
233  String nomJoueurTrame = new String();
234  while(it.hasNext())
235  {
236  Joueur monJoueur = it.next();
237  monJoueur.setScore(typeJeu.getPointDepart());
238  actualiserScoreIHM(monJoueur,typeJeu.getPointDepart());
239  nomJoueurTrame = nomJoueurTrame.concat(monJoueur.getNom() + ";");
240  }
241  int afficheRegleInt = afficheRegle ? 1 : 0;
242  envoyerTrame(ecran,"$DARTS;START;" + typeJeu.getTypeJeu() + ";" + afficheRegleInt + ";" + lesJoueurs.size() + ";" + nomJoueurTrame + "\r\n");
243  }
244 
245 
246  private Handler handlerBluetooth = new Handler()
247  {
248  @Override
249  public void handleMessage(Message msg)
250  {
251  super.handleMessage(msg);
252  Bundle b = msg.getData();
253 
254  switch(b.getInt("etat"))
255  {
257  Log.d(TAG,"<Bluetooth> Erreur " + b.getString("nom") + " [" + b.getString("adresse") + "] connecter");
259  break;
261  Log.d(TAG,"<Bluetooth> Erreur " + b.getString("nom") + " [" + b.getString("adresse") + "] envoyer");
263  break;
265  Log.d(TAG,"<Bluetooth> Erreur " + b.getString("nom") + " [" + b.getString("adresse") + "] envoyer");
267  break;
269  Log.d(TAG,"<Bluetooth> Connexion " + b.getString("nom") + " [" + b.getString("adresse") + "] ok");
270  envoyerConnexionIHM(b.getString("nom"));
271  break;
273  String donnees = b.getString("donnees");
274  if(donnees.contains("\r\n"))
275  {
276  Log.d(TAG,"<Bluetooth> Données reçues " + b.getString("nom") + " [" + b.getString("adresse") + "] : " + donnees.replace("\r\n", ""));
277  traitementTrame(donnees);
278  }
279  else
280  {
281  Log.d(TAG,"<Bluetooth> Données reçues " + b.getString("nom") + " [" + b.getString("adresse") + "] : " + donnees);
282  traitementTrame(donnees);
283  }
284  break;
286  Log.d(TAG,"<Bluetooth> Déconnexion " + b.getString("nom") + " [" + b.getString("adresse") + "] ok");
287  break;
288  default:
289  Log.d(TAG,"<Bluetooth> code état inconnu ! ");
290  }
291  }
292  };
293 
300  public void attendreImpact() {
301  Log.d(TAG, "attendreImpact()");
302  while (!impactEstRecuperer)
303  {
304  sleep(1000);
305 
306  }
307  envoyerTrame(ecran,"$DART;GAME;" + impact[0] + ";" + impact[1] + "\r\n");
308  impactIHM(impact[0],impact[1]);
309 
310  if (impact[0] == 2)
311  {
312  estDoubleImpact = true;
313  }
314  else
315  {
316  estDoubleImpact = false;
317  }
318 
319  Log.d(TAG, "Type cible: " + impact[0] + "Numero Cible:" + impact[1]);
320 
321  }
322 
329  public void traitementTrame(String trame)
330  {
331  trame = trame.replace("\r\n", "");
332  String[] trameDecoupe = trame.split(";",4);
333  Bundle bundle = new Bundle();
334 
335  switch (trameDecoupe[1])
336  {
337  case "GAME":
338  Log.d(TAG, "traitementTrame: GAME");
339  impact[0] = Integer.parseInt(trameDecoupe[2]);
340  impact[1] = Integer.parseInt(trameDecoupe[3]);
341  impactEstRecuperer = true;
342 
343  break;
344  case "PAUSE":
345  //TODO PAUSE
346  break;
347  case "PLAY":
348  //TODO PLAY
349  break;
350  case "STOP":
351  //TODO STOP
352  break;
353  case "RESET":
354  //TODO RESET
355  break;
356  case "HEARTBEAT ":
357  //TODO HEARTBEAT
358  break;
359  case "ACK":
360  //TODO ACK
361  break;
362  }
363  }
364 
371  public void actualiserScoreIHM(Joueur monJoueur, int score)
372  {
373  Log.d(TAG, "actualiserScoreIHM()");
374  Message msg = Message.obtain();
375  Bundle b = new Bundle();
376  b.putInt("action",SET_SCORE);
377  b.putString("joueur", monJoueur.getNom());
378  b.putInt("score", score);
379  msg.setData(b);
380  handlerUI.sendMessage(msg);
381  }
382 
389  public void actualiserJoueurIHM(String monJoueur)
390  {
391  Log.d(TAG, "actualiserJoueurIHM()");
392  Message msg = Message.obtain();
393  Bundle b = new Bundle();
394  b.putInt("action",JOUEUR_SUIVANT);
395  b.putString("joueur", monJoueur);
396  msg.setData(b);
397  handlerUI.sendMessage(msg);
398  }
399 
406  public void impactIHM(int typePoint, int numeroCible)
407  {
408  Log.d(TAG, "impactIHM()");
409  Message msg = Message.obtain();
410  Bundle b = new Bundle();
411  b.putInt("action",IMPACT);
412  b.putInt("typePoint",typePoint);
413  b.putInt("numeroCible",numeroCible);
414  msg.setData(b);
415  handlerUI.sendMessage(msg);
416  }
417 
424  public void envoyerGagnantIHM(Joueur monJoueur) {
425  Log.d(TAG, "envoyerGagnantIHM()");
426  estFini = true;
428  Message msg = Message.obtain();
429  Bundle b = new Bundle();
430  b.putInt("action",GAGNANT);
431  b.putString("gagnant",monJoueur.getNom());
432  msg.setData(b);
433  handlerUI.sendMessage(msg);
434  }
435 
436  public void cibleManquer()
437  {
438  impact[0] = 0;
439  impact[1] = 0;
440  impactEstRecuperer = true;
441  }
442 
443  public void envoyerConnexionIHM(String peripherique)
444  {
445  if (peripherique.equals(NOM_PERIPHERIQUE_BLUETOOTH_CIBLE))
446  {
447  Log.d(TAG, "envoyerConnexionIHM()");
448  Message msg = Message.obtain();
449  Bundle b = new Bundle();
450  b.putInt("action",CONNEXION_CIBLE);
451  msg.setData(b);
452  handlerUI.sendMessage(msg);
453  }
454  }
455 
456  public void pause()
457  {
458  //TODO pause()
459  }
460 
461  public void reprendre()
462  {
463  //TODO reprendre()
464  }
465 
466  public TypeJeu getTypeJeu() {
467  return typeJeu;
468  }
469 }
void actualiserJoueurIHM(String monJoueur)
Actualiser le joueur à IHM.
Definition: Partie.java:389
static final int CODE_RECEPTION
Code de Reception.
static final int JOUEUR_SUIVANT
Definition: Partie.java:38
static final int IMPACT
Definition: Partie.java:40
void attendreImpact()
Attendre l&#39;impact.
Definition: Partie.java:300
static final String NOM_PERIPHERIQUE_BLUETOOTH_ECRAN
le nom du périphérique Bluetooth du module écran
Definition: Partie.java:32
void initialiserPartie()
Initialiser la partie.
Definition: Partie.java:230
void connecter()
Méthode pour se connecter sur le perihperique en Bluetooth.
boolean afficheRegle
Booléen si on doit affichier les règles de la partie.
Definition: Partie.java:59
static final int CODE_ERREUR_ENVOYER
Code erreur lors de l&#39;envoi.
boolean retirerPoint(int scoreLancer, Partie maPartie)
fonction qui permet de retirer le score et retourner si le score a était retiré
Definition: Joueur.java:88
Déclaration de la classe Peripherique.
static final int CODE_DECONNEXION
Code de Deconnexion.
String getNom()
Accesseur get du nom du joueur.
Definition: Joueur.java:44
int nbJoueurs
Nombre de joueur.
Definition: Partie.java:46
String getNom()
Méthode qui retourne le nom du périphérique.
boolean deconnecter()
Méthode pour se deconnecter sur le perihperique en Bluetooth.
Déclaration de la classe Joueur.
Definition: Joueur.java:18
Handler handlerUI
Handler pour gérer l&#39;interface.
Definition: Partie.java:54
static final int CODE_ERREUR_RECEVOIR
Code erreur lors de la réception.
void envoyerConnexionIHM(String peripherique)
Definition: Partie.java:443
Peripherique ecran
Peripherique raspberry connecté en Bluetooth.
Definition: Partie.java:52
void deconnecterPeripheriquesBluetooth()
Deconnecter les periphériques bluetooth.
Definition: Partie.java:138
static final int NB_FLECHETTE
Definition: Partie.java:34
Déclaration de la classe Partie.
Definition: Partie.java:26
void envoyerTrame(Peripherique peripherique, String trame)
Envoie une trame à un péripherique.
Definition: Partie.java:158
void connecterPeripheriquesBluetooth()
Se connecter sur un péripherique via son adresse.
Definition: Partie.java:100
Set< BluetoothDevice > devices
Les peripheriques qui sont appairés.
Definition: Partie.java:51
ArrayList< Joueur > lesJoueurs
Les objets Joueur stocker dans un conteneur (Queue)
Definition: Partie.java:49
Déclaration de la classe TypeJeu.
Definition: TypeJeu.java:15
static final String NOM_PERIPHERIQUE_BLUETOOTH_CIBLE
le nom du périphérique Bluetooth du module cible
Definition: Partie.java:33
static final int CONNEXION_CIBLE
Definition: Partie.java:42
static final int CODE_CONNEXION
Code de Connection.
boolean impactEstRecuperer
Booléen pour savoir quand on reçoit une trame impact.
Definition: Partie.java:56
void envoyer(final String data)
Méthode pour envoyer une trame en Bluetooth.
static final int SET_SCORE
Definition: Partie.java:39
void setScore(int score)
Accesseur set du score du joueur.
Definition: Joueur.java:77
static final String TAG
Tag pour Log.
Definition: Partie.java:31
void impactIHM(int typePoint, int numeroCible)
Envoyer impact à IHM.
Definition: Partie.java:406
Peripherique cible
Peripherique darts connecté en Bluetooth.
Definition: Partie.java:53
int getScore()
Accesseur get du score du joueur.
Definition: Joueur.java:66
TypeJeu typeJeu
Mode de jeu.
Definition: Partie.java:48
boolean estFini
Booléen si la partie est fini.
Definition: Partie.java:57
void traitementTrame(String trame)
traiter la trame reçu par Handler
Definition: Partie.java:329
void demarrer()
Démarrer la partie.
Definition: Partie.java:172
int impact[]
Les impacts.
Definition: Partie.java:55
static final int GAGNANT
Definition: Partie.java:41
void actualiserScoreIHM(Joueur monJoueur, int score)
Actualiser le score du joueur à IHM.
Definition: Partie.java:371
Partie(ArrayList< Joueur > lesJoueurs, TypeJeu typeJeu, boolean AfficheRegle, BluetoothAdapter bluetoothAdapter, Handler handlerUI)
Constructeur de la classe Partie.
Definition: Partie.java:68
BluetoothAdapter bluetoothAdapter
Bluetooth Adapteur.
Definition: Partie.java:50
void envoyerGagnantIHM(Joueur monJoueur)
Envoyer le gagnant a IHM.
Definition: Partie.java:424
void recupererPeripheriques()
Récupère les péripheriques Bluetooth qui sont appairés.
Definition: Partie.java:89
static final int CODE_ERREUR_CONNECTER
Code erreur lors de la connexion.
int nbManche
Nombre de manche.
Definition: Partie.java:47
boolean estDoubleImpact
Booléen si la derniere impact est du type double.
Definition: Partie.java:58