Commit 3279dff7 authored by JING LI's avatar JING LI
Browse files

Merge branch 'Data/develop' of https://gitlab.utc.fr/qdruault/lo23-project into data/javadoc

parents d1b43e89 61f4216b
...@@ -22,7 +22,6 @@ import java.util.logging.Level; ...@@ -22,7 +22,6 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.Set; import java.util.Set;
/** /**
* Facade for the communication module * Facade for the communication module
* *
...@@ -40,7 +39,7 @@ public class ComFacade { ...@@ -40,7 +39,7 @@ public class ComFacade {
public ComFacade(IDataCom iDataCom) { public ComFacade(IDataCom iDataCom) {
this.iDataCom = iDataCom; this.iDataCom = iDataCom;
kIpCtrl = KnownIPController.getInstance(); kIpCtrl = KnownIPController.getInstance();
receiver = new Receiver(25000, iDataCom); receiver = new Receiver(25000, iDataCom);
new Thread(receiver).start(); new Thread(receiver).start();
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Facade created"); Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Facade created");
...@@ -65,7 +64,7 @@ public class ComFacade { ...@@ -65,7 +64,7 @@ public class ComFacade {
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) { if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship); Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceship);
new Thread(os).start(); new Thread(os).start();
} }
} }
} }
...@@ -83,7 +82,7 @@ public class ComFacade { ...@@ -83,7 +82,7 @@ public class ComFacade {
public void notifyUserSignedOut() { public void notifyUserSignedOut() {
M_Deconnection mDeconnection = new M_Deconnection(iDataCom.getMyPublicUserProfile()); M_Deconnection mDeconnection = new M_Deconnection(iDataCom.getMyPublicUserProfile());
for (Inet4Address ip : kIpCtrl.getHashMap().values()) { for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
if (ip != null){ if (ip != null) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mDeconnection); Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mDeconnection);
new Thread(os).start(); new Thread(os).start();
} }
...@@ -98,7 +97,7 @@ public class ComFacade { ...@@ -98,7 +97,7 @@ public class ComFacade {
public void notifyNewMessage(com.utclo23.data.structure.Message message) { public void notifyNewMessage(com.utclo23.data.structure.Message message) {
M_Chat mChat = new M_Chat(iDataCom.getMyPublicUserProfile(), message); M_Chat mChat = new M_Chat(iDataCom.getMyPublicUserProfile(), message);
for (LightPublicUser recipient : message.getRecipients()) { for (LightPublicUser recipient : message.getRecipients()) {
if (kIpCtrl.getHashMap().get(recipient.getId()) != null){ if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mChat); Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mChat);
new Thread(os).start(); new Thread(os).start();
} }
...@@ -112,28 +111,31 @@ public class ComFacade { ...@@ -112,28 +111,31 @@ public class ComFacade {
* @param recipients are the recipients of the new mine * @param recipients are the recipients of the new mine
*/ */
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) { public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) {
M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine); M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
for (LightPublicUser recipient : recipients) { for (LightPublicUser recipient : recipients) {
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine); Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine);
new Thread(os).start(); new Thread(os).start();
} }
} }
}
/**
/** * Called to notify everybody of the creation of a new game to update
* Called to notify everybody of the creation of a new game to update all * all users Data's module.
* users Data's module. *
* * @param game is the new created game
* @param game is the new created game */
*/ public void notifyNewGame(StatGame game) {
public void notifyNewGame(StatGame game) {
M_CreationGame mCreationGame = new M_CreationGame(iDataCom.getMyPublicUserProfile(), game); M_CreationGame mCreationGame = new M_CreationGame(iDataCom.getMyPublicUserProfile(), game);
for (Inet4Address ip : kIpCtrl.getHashMap().values()) { for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
if(ip != null){ if (ip != null) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mCreationGame); Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mCreationGame);
Thread thread = new Thread(os); Thread thread = new Thread(os);
thread.start(); thread.start();
} }
} }
} }
...@@ -158,7 +160,7 @@ public class ComFacade { ...@@ -158,7 +160,7 @@ public class ComFacade {
public void leaveGame() { public void leaveGame() {
M_LeaveGame mLeaveGame = new M_LeaveGame(iDataCom.getMyPublicUserProfile()); M_LeaveGame mLeaveGame = new M_LeaveGame(iDataCom.getMyPublicUserProfile());
for (Inet4Address ip : kIpCtrl.getHashMap().values()) { for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
if(ip != null){ if (ip != null) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mLeaveGame); Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mLeaveGame);
new Thread(os).start(); new Thread(os).start();
} }
...@@ -176,22 +178,23 @@ public class ComFacade { ...@@ -176,22 +178,23 @@ public class ComFacade {
List<LightPublicUser> tmp = new ArrayList(iDataCom.getConnectedUsers()); List<LightPublicUser> tmp = new ArrayList(iDataCom.getConnectedUsers());
tmp.add(iDataCom.getMyPublicUserProfile().getLightPublicUser()); tmp.add(iDataCom.getMyPublicUserProfile().getLightPublicUser());
// Suppression des doublons et de nous même au cas où // Suppression des doublons et de nous même au cas où
// dans listIpTarget // dans listIpTarget
Set set = new HashSet(); Set set = new HashSet();
set.addAll(listIpTarget); set.addAll(listIpTarget);
if(set.contains(kIpCtrl.getMyInetAddress())) if (set.contains(kIpCtrl.getMyInetAddress())) {
set.remove(kIpCtrl.getMyInetAddress()); set.remove(kIpCtrl.getMyInetAddress());
}
List<Inet4Address> newIpTarget = new ArrayList(set); List<Inet4Address> newIpTarget = new ArrayList(set);
for (Inet4Address ipDest : newIpTarget) { for (Inet4Address ipDest : newIpTarget) {
// On vérie si l'ip n'est pas déjà dans le hashMap // On vérie si l'ip n'est pas déjà dans le hashMap
if(!kIpCtrl.getHashMap().containsValue(ipDest)){ if (!kIpCtrl.getHashMap().containsValue(ipDest)) {
List otherTargets = new ArrayList(newIpTarget); List otherTargets = new ArrayList(newIpTarget);
otherTargets.remove(ipDest); otherTargets.remove(ipDest);
M_Bleu mBleu = new M_Bleu(iDataCom.getMyPublicUserProfile(), M_Bleu mBleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmpHash, tmp, iDataCom.getGameList(), otherTargets); tmpHash, tmp, iDataCom.getGameList(), otherTargets);
Sender os = new Sender(ipDest.getHostAddress(), kIpCtrl.getPort(), mBleu); Sender os = new Sender(ipDest.getHostAddress(), kIpCtrl.getPort(), mBleu);
new Thread(os).start(); new Thread(os).start();
...@@ -224,7 +227,7 @@ public class ComFacade { ...@@ -224,7 +227,7 @@ public class ComFacade {
if (success) { if (success) {
for (Inet4Address ip : kIpCtrl.getHashMap().values()) { for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
if(ip != null) { if (ip != null) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mJoinGameResponse); Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mJoinGameResponse);
new Thread(os).start(); new Thread(os).start();
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Send success joinGame"); Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Send success joinGame");
......
...@@ -282,6 +282,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain { ...@@ -282,6 +282,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
this.opponentHasLeftGame();
} }
/** /**
......
...@@ -321,6 +321,7 @@ public class GameMediator { ...@@ -321,6 +321,7 @@ public class GameMediator {
if (this.currentGame.isShipDestroyed(ship, this.currentGame.getComputerPlayer().getMines())) { if (this.currentGame.isShipDestroyed(ship, this.currentGame.getComputerPlayer().getMines())) {
this.currentGame.getComputerPlayer().loseFocus(); this.currentGame.getComputerPlayer().loseFocus();
System.out.println("");
} }
} }
...@@ -666,7 +667,6 @@ public class GameMediator { ...@@ -666,7 +667,6 @@ public class GameMediator {
if (ready) //notify IA to place ships if (ready) //notify IA to place ships
{ {
System.out.println("Data | IA turn set ships"); System.out.println("Data | IA turn set ships");
this.currentGame.getComputerPlayer().setShips(this.currentGame.getTemplateShips()); this.currentGame.getComputerPlayer().setShips(this.currentGame.getTemplateShips());
this.dataFacade.getIhmTablefacade().notifyGameReady(); this.dataFacade.getIhmTablefacade().notifyGameReady();
......
...@@ -40,8 +40,14 @@ public class ComputerPlayer extends Player { ...@@ -40,8 +40,14 @@ public class ComputerPlayer extends Player {
} }
public void setFocus(Coordinate focus) { public void setFocus(Coordinate focus) {
if(focus!=null){
this.oldFocus = this.focus; this.oldFocus = this.focus;
this.focus = focus; this.focus = focus;
}
else
{
this.focus = this.oldFocus;
}
} }
...@@ -73,8 +79,8 @@ public class ComputerPlayer extends Player { ...@@ -73,8 +79,8 @@ public class ComputerPlayer extends Player {
do { do {
//choose a new location until empty //choose a new location until empty
x = r.nextInt(Configuration.HEIGHT - 1); x = r.nextInt(Configuration.HEIGHT );
y = r.nextInt(Configuration.WIDTH - 1); y = r.nextInt(Configuration.WIDTH);
if (tab[x][y] != 0 || (x + ship.getSize() >= Configuration.WIDTH && y + ship.getSize() >= Configuration.WIDTH)) { if (tab[x][y] != 0 || (x + ship.getSize() >= Configuration.WIDTH && y + ship.getSize() >= Configuration.WIDTH)) {
// System.out.println("("+x+","+y+") NON VALIDE"); // System.out.println("("+x+","+y+") NON VALIDE");
...@@ -191,8 +197,8 @@ public class ComputerPlayer extends Player { ...@@ -191,8 +197,8 @@ public class ComputerPlayer extends Player {
do { do {
valid = true; valid = true;
//choose a new location until empty //choose a new location until empty
x = r.nextInt(Configuration.WIDTH - 1); x = r.nextInt(Configuration.WIDTH );
y = r.nextInt(Configuration.WIDTH - 1); y = r.nextInt(Configuration.WIDTH);
if (tab[x][y] != 0 || (x >= Configuration.WIDTH || y >= Configuration.WIDTH)) { if (tab[x][y] != 0 || (x >= Configuration.WIDTH || y >= Configuration.WIDTH)) {
valid = false; valid = false;
...@@ -205,9 +211,13 @@ public class ComputerPlayer extends Player { ...@@ -205,9 +211,13 @@ public class ComputerPlayer extends Player {
//System.out.println("focus method"); //System.out.println("focus method");
if ((focus.getX() + 1 >= Configuration.WIDTH || focus.getY() >= Configuration.WIDTH) || tab[focus.getX() + 1][focus.getY()] != 0 ) { if ((focus.getX() + 1 >= Configuration.WIDTH || focus.getY() >= Configuration.WIDTH) || tab[focus.getX() + 1][focus.getY()] != 0 ) {
System.out.println("no x + 1");
if ((focus.getX() - 1 < 0 || focus.getY() >= Configuration.WIDTH) || tab[focus.getX() - 1][focus.getY()] != 0) { if ((focus.getX() - 1 < 0 || focus.getY() >= Configuration.WIDTH) || tab[focus.getX() - 1][focus.getY()] != 0) {
System.out.println("no x-1");
if ((focus.getX() >= Configuration.WIDTH || focus.getY() + 1 >= Configuration.WIDTH)|| tab[focus.getX()][focus.getY() + 1] != 0) { if ((focus.getX() >= Configuration.WIDTH || focus.getY() + 1 >= Configuration.WIDTH)|| tab[focus.getX()][focus.getY() + 1] != 0) {
System.out.println("no y+1");
if ( (focus.getX() >= Configuration.WIDTH || focus.getY() - 1 < 0) || tab[focus.getX()][focus.getY() - 1] != 0) { if ( (focus.getX() >= Configuration.WIDTH || focus.getY() - 1 < 0) || tab[focus.getX()][focus.getY() - 1] != 0) {
System.out.println("no y-1");
this.focus = null; this.focus = null;
this.oldFocus = null; this.oldFocus = null;
...@@ -217,8 +227,8 @@ public class ComputerPlayer extends Player { ...@@ -217,8 +227,8 @@ public class ComputerPlayer extends Player {
valid = true; valid = true;
//choose a new location until empty //choose a new location until empty
x = r.nextInt(Configuration.HEIGHT - 1); x = r.nextInt(Configuration.HEIGHT );
y = r.nextInt(Configuration.WIDTH - 1); y = r.nextInt(Configuration.WIDTH);
if (tab[x][y] != 0 || (x >= Configuration.WIDTH || y >= Configuration.WIDTH)) { if (tab[x][y] != 0 || (x >= Configuration.WIDTH || y >= Configuration.WIDTH)) {
...@@ -230,19 +240,24 @@ public class ComputerPlayer extends Player { ...@@ -230,19 +240,24 @@ public class ComputerPlayer extends Player {
} while (!valid); } while (!valid);
} else { } else {
System.out.println("y-1");
x = focus.getX(); x = focus.getX();
y = focus.getY() - 1; y = focus.getY() - 1;
} }
} else { } else {
System.out.println("y+1");
x = focus.getX(); x = focus.getX();
y = focus.getY() + 1; y = focus.getY() + 1;
} }
} else { } else {
System.out.println("x-1");
x = focus.getX() - 1; x = focus.getX() - 1;
y = focus.getY(); y = focus.getY();
} }
} else { } else {
System.out.println("x+1");
x = focus.getX() + 1; x = focus.getX() + 1;
y = focus.getY(); y = focus.getY();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment