Commit 2731f183 authored by Quentin DRUAULT-AUBIN's avatar Quentin DRUAULT-AUBIN

Final commit

parents 46436322 d3ced366
......@@ -20,8 +20,37 @@
<name>lo23</name>
</organization>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.0.201403182114</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
......@@ -155,6 +184,7 @@
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.organization>lo23</sonar.organization>
<sonar.login>a7f86fb85267f6c368ef07d969a16ccf4be0db4b</sonar.login>
......
......@@ -12,9 +12,7 @@ package com.utclo23.battleship;
*/
public class GameException extends Exception {
public GameException() {
}
public GameException(String message) {
super(message);
}
......
......@@ -10,7 +10,6 @@ import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.StatGame;
import com.utclo23.com.messages.*;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.Game;
import com.utclo23.data.structure.LightPublicUser;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
......@@ -118,9 +117,9 @@ public class ComFacade {
*/
public void notifyNewCoordinates(Mine mine, List<LightPublicUser> recipients) {
M_PlaceMine mPlaceMine = new M_PlaceMine(iDataCom.getMyPublicUserProfile(), mine);
System.out.println("recipients notify: " + recipients.size());
System.out.println("recipients new coordinates notify: " + recipients.size());
for (LightPublicUser recipient : recipients) {
System.out.println("notifyNewCoordinates +" + recipient.getPlayerName());
System.out.println("notifyNewCoordinates +" + recipient.getPlayerName()+" mine "+mine.getCoord().getX()+" "+mine.getCoord().getY());
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine);
new Thread(os).start();
......@@ -159,16 +158,17 @@ public class ComFacade {
}
/**
* Called to send "leave game" notification to everybody.
* Called to send "leave game" notification to all users in the game.
* @param recipients: receivers of the notification
*/
public void leaveGame() {
public void leaveGame(List<LightPublicUser> recipients) {
M_LeaveGame mLeaveGame = new M_LeaveGame(iDataCom.getMyPublicUserProfile());
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
if (ip != null) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), mLeaveGame);
for (LightPublicUser recipient : recipients) {
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mLeaveGame);
new Thread(os).start();
}
}
}
}
/**
......@@ -243,4 +243,20 @@ public class ComFacade {
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Fail joinGame");
}
}
/**
* Called to remove a game given in parameter in the game's list of all
* connected users
* @param idGame is the game to remove
*/
public void removeGame(String idGame){
M_RemoveGame m_RemoveGame = new M_RemoveGame(iDataCom.getMyPublicUserProfile(), idGame);
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
if (ip != null) {
Sender os = new Sender(ip.getHostAddress(), kIpCtrl.getPort(), m_RemoveGame);
Thread thread = new Thread(os);
thread.start();
}
}
}
}
......@@ -9,7 +9,6 @@ import java.util.HashMap;
import java.net.Inet4Address;
import com.utclo23.data.facade.IDataCom;
import java.net.InterfaceAddress;
import java.util.Iterator;
/**
* Class containing a hashmap with UID and corresponding IP address of the known
......
......@@ -41,66 +41,68 @@ public class M_Bleu extends Message {
@Override
public void callback(IDataCom iDataCom) {
Logger.getLogger(M_Bleu.class.getName()).log(Level.INFO, null, "Blue message received");
List<String> myUsersId = new ArrayList(KnownIPController.getInstance().getHashMap().keySet());
List<LightPublicUser> myUsersProfile = new ArrayList(iDataCom.getConnectedUsers());
if(iDataCom.getMyPublicUserProfile() != null){
List<String> myUsersId = new ArrayList(KnownIPController.getInstance().getHashMap().keySet());
List<LightPublicUser> myUsersProfile = new ArrayList(iDataCom.getConnectedUsers());
HashMap<String, Inet4Address> newPairs = new HashMap();
List<LightPublicUser> newUsersProfile = new ArrayList();
HashMap<String, Inet4Address> newPairs = new HashMap();
List<LightPublicUser> newUsersProfile = new ArrayList();
// Envoi message bleu aux autres targets afin que tout le monde ait
// les mêmes joueurs connectés. Les premiers qui recevront ces messages
// seront prioritaires
HashMap<String, Inet4Address> tmpHash = new HashMap(KnownIPController.getInstance().getHashMap());
tmpHash.put(iDataCom.getMyPublicUserProfile().getId(), KnownIPController.getInstance().getMyInetAddress());
// Envoi message bleu aux autres targets afin que tout le monde ait
// les mêmes joueurs connectés. Les premiers qui recevront ces messages
// seront prioritaires
HashMap<String, Inet4Address> tmpHash = new HashMap(KnownIPController.getInstance().getHashMap());
tmpHash.put(iDataCom.getMyPublicUserProfile().getId(), KnownIPController.getInstance().getMyInetAddress());
List<LightPublicUser> tmp = new ArrayList(iDataCom.getConnectedUsers());
tmp.add(iDataCom.getMyPublicUserProfile().getLightPublicUser());
List<LightPublicUser> tmp = new ArrayList(iDataCom.getConnectedUsers());
tmp.add(iDataCom.getMyPublicUserProfile().getLightPublicUser());
if (this.otherTargets != null) {
for (Inet4Address ipDest : this.otherTargets) {
// On envoie pas si on avait déjà l'IP dans le hashMap
if (!KnownIPController.getInstance().getHashMap().containsValue(ipDest)) {
M_Bleu m_Bleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmpHash, tmp, iDataCom.getGameList(), null);
Sender os = new Sender(ipDest.getHostAddress(), KnownIPController.getInstance().getPort(), m_Bleu);
new Thread(os).start();
if (this.otherTargets != null) {
for (Inet4Address ipDest : this.otherTargets) {
// On envoie pas si on avait déjà l'IP dans le hashMap
if (!KnownIPController.getInstance().getHashMap().containsValue(ipDest)) {
M_Bleu m_Bleu = new M_Bleu(iDataCom.getMyPublicUserProfile(),
tmpHash, tmp, iDataCom.getGameList(), null);
Sender os = new Sender(ipDest.getHostAddress(), KnownIPController.getInstance().getPort(), m_Bleu);
new Thread(os).start();
}
}
}
}
// Envoi message rouge à ceux non déjà présents dans nos joueurs connectés
for (Map.Entry<String, Inet4Address> entry : this.hashMapReceived.entrySet()) {
if (!entry.getKey().equals(iDataCom.getMyPublicUserProfile().getId())
&& !myUsersId.contains(entry.getKey())) {
M_Rouge mRouge = new M_Rouge(iDataCom.getMyPublicUserProfile(), tmpHash, tmp, iDataCom.getGameList());
Sender os = new Sender(entry.getValue().getHostAddress(), KnownIPController.getInstance().getPort(), mRouge);
Thread thread = new Thread(os);
thread.start();
newPairs.put(entry.getKey(), entry.getValue());
// Envoi message rouge à tout le monde sauf nous.
for (Map.Entry<String, Inet4Address> entry : this.hashMapReceived.entrySet()) {
if (!entry.getKey().equals(iDataCom.getMyPublicUserProfile().getId())) {
if(!myUsersId.contains(entry.getKey())) {
newPairs.put(entry.getKey(), entry.getValue());
}
M_Rouge mRouge = new M_Rouge(iDataCom.getMyPublicUserProfile(), tmpHash, tmp, iDataCom.getGameList());
Sender os = new Sender(entry.getValue().getHostAddress(), KnownIPController.getInstance().getPort(), mRouge);
Thread thread = new Thread(os);
thread.start();
}
}
}
for (LightPublicUser key : this.usersReceived) {
if (!key.getId().equals(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId())
&& !myUsersProfile.contains(key)) {
newUsersProfile.add(key);
for (LightPublicUser key : this.usersReceived) {
if (!key.getId().equals(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId())
&& !myUsersProfile.contains(key)) {
newUsersProfile.add(key);
}
}
}
// Maj de nos listes avec les nouvelles données reçues
// maj de la liste des games
for (StatGame game : this.listGamesReceived) {
if (!iDataCom.getGameList().contains(game)) {
iDataCom.addNewGame(game);
// Maj de nos listes avec les nouvelles données reçues
// maj de la liste des games
for (StatGame game : this.listGamesReceived) {
if (!iDataCom.getGameList().contains(game)) {
iDataCom.addNewGame(game);
}
}
for (Map.Entry<String, Inet4Address> entry : newPairs.entrySet()) {
KnownIPController.getInstance().getHashMap().put(entry.getKey(), entry.getValue());
}
for (LightPublicUser key : newUsersProfile) {
iDataCom.addConnectedUser(key);
}
}
for (Map.Entry<String, Inet4Address> entry : newPairs.entrySet()) {
KnownIPController.getInstance().getHashMap().put(entry.getKey(), entry.getValue());
}
for (LightPublicUser key : newUsersProfile) {
iDataCom.addConnectedUser(key);
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utclo23.com.messages;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
/**
* M_LeaveGame is a Message generated when a user wants to leave a game.
* @author Thibault CHICHE
*/
public class M_RemoveGame extends Message {
private final String idGame;
/**
* Constructor.
* @param user is the message's sender
* @param idGame is the id of the game to remove
*/
public M_RemoveGame(PublicUser user, String idGame){
super(user);
this.idGame = idGame;
}
@Override
public void callback(IDataCom iDataCom){
iDataCom.removeGame(idGame);
}
}
......@@ -14,15 +14,16 @@ import java.io.File;
*/
public class Configuration {
public Configuration() {
}
/**
* directory of saved profiles
*/
public final static String SAVE_DIR = System.getProperty("user.home")+File.separator+"MyData";
public final static int WIDTH = 10;
public final static int HEIGHT = 10;
public static final String SAVE_DIR = System.getProperty("user.home")+File.separator+"MyData";
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
public static final String PLAYER = "player";
public static final String SPECTATOR = "spectator";
}
......@@ -24,14 +24,12 @@ import com.utclo23.data.structure.Ship;
import com.utclo23.data.structure.StatGame;
import com.utclo23.ihmmain.facade.IHMMainFacade;
import com.utclo23.ihmtable.IHMTableFacade;
import com.utclo23.ihmtable.IIHMTableToData;
import java.io.IOException;
import java.io.File;
import java.io.IOException;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
......@@ -79,16 +77,25 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
/**
* Set the test mode
*
* @param testMode
* @param boolean the value to set to testMode parameter
*/
public void setTestMode(boolean testMode) {
this.testMode = testMode;
}
/**
*
* @return
*/
public IHMMainFacade getIhmMainFacade() {
return ihmMainFacade;
}
/**
*
* @return
*/
public IIHMTableToData getIhmTablefacade() {
return ihmTablefacade;
}
......@@ -96,7 +103,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
/**
* Constructor
*/
public DataFacade() {
public DataFacade() {
/**
* Construction of mediators by giving them a reference to this facade
*/
......@@ -122,6 +129,12 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
return comfacade;
}
/**
*
* @param comFacade
* @param ihmTableToData
* @param ihmMainFacade
*/
public void setFacadeLinks(
ComFacade comFacade,
IIHMTableToData ihmTableToData,
......@@ -141,7 +154,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void updatePlayername(String playername) throws DataException {
this.userMediator.updatePlayername(playername);
}
......@@ -219,22 +232,23 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
}
/**
* Add new game
* Add new game
*
* @param game
* @param game
*/
@Override
public void addNewGame(StatGame game) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | new game received");
this.gameMediator.addNewGame(game);
if (!this.testMode) {
try {
this.ihmMainFacade.refreshGameList();
} catch (IOException ex) {
Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex.getMessage());
try {
this.ihmMainFacade.refreshGameList();
} catch (IOException ex) {
Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex.getMessage());
}
}
}
/**
......@@ -246,7 +260,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
@Override
public void setEnnemyShips(List<Ship> ships) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ennemy ships");
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ennemy ships");
this.gameMediator.setEnnemyShips(ships);
}
......@@ -269,20 +283,25 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void leaveGame() {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | leave game");
PublicUser user = this.userMediator.getMyPublicUserProfile();
this.comfacade.leaveGame();
this.gameMediator.leaveGame();
try {
this.ihmMainFacade.toMenu();
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("leave game");
String role = this.gameMediator.getOwnerStatus();
if (!role.equals("spectator") && this.gameMediator.getCurrentGame() != null) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | leave game");
this.comfacade.leaveGame(this.gameMediator.getCurrentGame().getRecipients(this.getMyPublicUserProfile().getPlayerName()));
this.gameMediator.leaveGame();
try {
this.ihmMainFacade.toMenu();
} catch (IOException ex) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data " + ex.getMessage());
}
this.opponentHasLeftGame();
} else {
System.out.println("leave spectator ");
this.ihmTablefacade.spectatorLeaveGame();
}
this.opponentHasLeftGame();
}
/**
......@@ -290,30 +309,35 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void opponentHasLeftGame() {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | opponent has left");
Logger.getLogger(DataFacade.class.getName()).info("data | opponent has left");
if (!this.gameMediator.isFinishedGame()) {
try {
this.gameMediator.defWin();
} catch (DataException e) {
//Rien a priori.
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data " + e.getMessage());
}
this.gameMediator.leaveGame();
this.ihmTablefacade.opponentHasLeftGame();
}
}
@Override
public void removeGame(String id) {
this.gameMediator.removeGame(id);
}
/**
* Notify that connection is lost
*/
@Override
public void connectionLostWithOpponent() {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | connection lost");
//stats.
this.gameMediator.setWinner(null);
this.gameMediator.getCurrentGame().getStatGame().setGameAbandonned(true);
this.gameMediator.leaveGame();
this.ihmTablefacade.connectionLostWithOpponent();
......@@ -347,8 +371,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
try {
this.ihmMainFacade.refreshUserList();
} catch (Exception ex) {
ex.printStackTrace();
// Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex.getMessage());
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, ex.getMessage());
}
}
......@@ -366,8 +389,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
try {
this.ihmMainFacade.refreshUserList();
} catch (Exception ex) {
ex.printStackTrace();
//Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex.getMessage());
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, ex.getMessage());
}
}
......@@ -380,7 +402,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
@Override
public void forwardMessage(Message msg) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | forward message");
this.gameMediator.forwardMessage(msg);
}
......@@ -391,7 +413,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
* @return my owner profile
*/
public Owner getMyOwnerProfile() {
return this.userMediator.getMyOwnerProfile();
}
......@@ -401,14 +423,26 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*
* @return my public profile
*/
@Override
public PublicUser getMyPublicUserProfile() {
try {
return this.userMediator.getMyPublicUserProfile();
} catch (Exception e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
PublicUser usr = this.userMediator.getMyPublicUserProfile();
if (usr != null) {
try {
usr.setNumberDefeatsClassic(this.getNumberDefeatsClassic());
usr.setNumberVictoriesClassic(this.getNumberVictoriesClassic());
usr.setNumberAbandonsClassic(this.getNumberAbandonsClassic());
usr.setNumberDefeatsBelgian(this.getNumberDefeatsBelgian());
usr.setNumberVictoriesBelgian(this.getNumberVictoriesBelgian());
usr.setNumberAbandonsBelgian(this.getNumberAbandonsBelgian());
} catch (DataException ex) {
Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex);
}
}
return usr;
}
/**
......@@ -421,7 +455,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void updateGameList(LightPublicUser user, String id, String role) throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | update game list "+user.getPlayerName()+" "+id+" "+role);
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | update game list " + user.getPlayerName() + " " + id + " " + role);
this.gameMediator.updateGameList(user, id, role);
}
......@@ -433,8 +467,8 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public List<Ship> getTemplateShips() throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | get template ships");
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | get template ships");
if (this.gameMediator.getCurrentGame() != null) {
return this.gameMediator.getCurrentGame().getTemplateShips();
} else {
......@@ -450,7 +484,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
public void setShip(Ship ship) throws DataException {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | set ship");
this.gameMediator.setPlayerShip(ship);
}
......@@ -459,8 +493,6 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
* Attack a given location
*
* @param coords the location to attack
* @param isTrueAttack true = this is a true attack ; false = this is just a
* test
* @return Pair<Integer, Ship>
* Integer = 0 if the mine is not in a right place ; Integer = 1 if the mine
* is in the place of a ship. Ship = null if the ship isn't destroyed ; ship
......@@ -469,20 +501,13 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack, Player playerWhoPutTheMine) {
if(isAttack)
{
System.out.println("datafacade | attack "+coords.getX()+"-"+coords.getY());
}
try {
Pair<Integer, Ship> pairReturn = this.gameMediator.attack(coords, isAttack, playerWhoPutTheMine);
return this.gameMediator.attack(coords, isAttack, playerWhoPutTheMine);
return pairReturn;
} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | " + ex.getMessage());
return null;
}
}
......@@ -496,7 +521,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public List<Event> getPreviousBoard() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return new ArrayList<Event>();
}
/**
......@@ -506,9 +531,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public List<Event> getNextBoard() {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return null;
return new ArrayList<Event>();
}
/**
......@@ -518,9 +541,9 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*/
@Override
public void sendMessage(String text) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | send message "+text);
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | send message " + text);
this.gameMediator.sendMessage(text);
}
......@@ -573,7 +596,6 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
* Get a user profile
*
* @param id the id of the user to get his profile
* @return the public user
*
*/