Commit a230119d authored by dkonam's avatar dkonam
Browse files

Merge branch 'develop' into Data/develop

parents 03ccfe9e ddb58fb6
......@@ -87,6 +87,7 @@ public class ComFacade {
new Thread(os).start();
}
}
KnownIPController.getInstance().getHashMap().clear();
}
/**
......@@ -110,25 +111,23 @@ public class ComFacade {
* @param mine is the new placed 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);
for (LightPublicUser recipient : recipients) {
System.out.println("recipients notify: " + recipients.size());
for (LightPublicUser recipient : recipients) {
System.out.println("notifyNewCoordinates +" + recipient.getPlayerName());
if (kIpCtrl.getHashMap().get(recipient.getId()) != null) {
Sender os = new Sender(kIpCtrl.getHashMap().get(recipient.getId()).getHostAddress(), kIpCtrl.getPort(), mPlaceMine);
new Thread(os).start();
}
}
}
/**
* Called to notify everybody of the creation of a new game to update
* all users Data's module.
*
* @param game is the new created game
*/
/**
* Called to notify everybody of the creation of a new game to update
* all users Data's module.
*
* @param game is the new created game
*/
public void notifyNewGame(StatGame game) {
M_CreationGame mCreationGame = new M_CreationGame(iDataCom.getMyPublicUserProfile(), game);
for (Inet4Address ip : kIpCtrl.getHashMap().values()) {
......
......@@ -81,12 +81,12 @@ public class KnownIPController {
* @return Hashmap value
*/
public HashMap<String, Inet4Address> getHashMap() {
HashMap<String, Inet4Address> tmpHash = knownIp;
String id = iDataCom.getMyPublicUserProfile().getLightPublicUser().getId();
tmpHash.remove(id); // to avoid sending back our own Ip adress (cuz it already got it).
return tmpHash;
knownIp.remove(id); // to avoid sending back our own Ip adress (cuz it already got it).
return knownIp;
}
/**
* Called to add a new node in the attribute "knownIP".
......@@ -97,28 +97,6 @@ public class KnownIPController {
public void addNode(String id, Inet4Address ip) {
knownIp.put(id, ip);
}
/**
* Called to add non existing nodes in attribute "knownIp" from another
* hashmap.
*
* @param hashToCheck is the Hashmap containing ids of type String and
* Inet4Address
* @return the new updated HashMap
*/
public HashMap<String, Inet4Address> addNonExistingNodes(HashMap<String, Inet4Address> hashToCheck) {
Iterator it = hashToCheck.entrySet().iterator();
HashMap<String, Inet4Address> tmpHash = new HashMap<>();
while (it.hasNext()) {
HashMap.Entry pair = (HashMap.Entry) it.next();
if (!knownIp.containsKey(pair.getKey())) {
knownIp.put((String) pair.getKey(), (Inet4Address) pair.getValue());
tmpHash.put((String) pair.getKey(), (Inet4Address) pair.getValue());
}
it.remove(); // avoids a ConcurrentModificationException
}
return tmpHash;
}
/**
* Acessor
......
......@@ -53,10 +53,10 @@ public class Receiver implements Runnable {
in = new ObjectInputStream(client.getInputStream());
request = (Message) in.readObject();
request.callback(iDataCom);
Logger.getLogger(Receiver.class.getName()).log(Level.INFO, null, "Message received : " + request.getClass().toString());
//Logger.getLogger(Receiver.class.getName()).log(Level.INFO, null, "Message received : " + request.getClass().toString());
System.out.println("Message received :" + request.getClass().toString());
client.close();
in.close();
} catch (IOException|ClassNotFoundException ex) {
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, ex);
}
......
......@@ -52,7 +52,8 @@ public class Sender implements Runnable {
out = new ObjectOutputStream(socket.getOutputStream());
request.setIpSender(kIpCtrl.getMyInetAddress());
out.writeObject(request);
Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Message sent : " + request.getClass().toString());
//Logger.getLogger(ComFacade.class.getName()).log(Level.INFO, null, "Message sent : " + request.getClass().toString());
System.out.println("Message sent : " + request.getClass().toString() + " to "+ ip);
out.close();
socket.close();
} catch (IOException ex) {
......
......@@ -83,7 +83,7 @@ public class M_Bleu extends Message {
}
for (LightPublicUser key : this.usersReceived) {
if (!key.equals(iDataCom.getMyPublicUserProfile().getLightPublicUser())
if (!key.getId().equals(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId())
&& !myUsersProfile.contains(key)) {
newUsersProfile.add(key);
}
......
......@@ -4,6 +4,7 @@
* and open the template in the editor.
*/
package com.utclo23.com.messages;
import com.utclo23.com.KnownIPController;
import com.utclo23.data.facade.IDataCom;
import com.utclo23.data.structure.PublicUser;
/**
......@@ -24,5 +25,6 @@ public class M_Deconnection extends Message {
@Override
public void callback(IDataCom iDataCom){
iDataCom.removeConnectedUser(user.getLightPublicUser());
KnownIPController.getInstance().getHashMap().remove(user.getId());
}
}
......@@ -69,7 +69,7 @@ public class M_Rouge extends Message {
for(LightPublicUser key : this.usersReceived){
if(!myUsersProfile.contains(key) &&
!key.equals(iDataCom.getMyPublicUserProfile().getLightPublicUser())){
!key.getId().equals(iDataCom.getMyPublicUserProfile().getLightPublicUser().getId())){
newUsersProfile.add(key);
}
}
......
......@@ -18,6 +18,7 @@ import com.utclo23.data.structure.LightPublicUser;
import com.utclo23.data.structure.Message;
import com.utclo23.data.structure.Mine;
import com.utclo23.data.structure.Owner;
import com.utclo23.data.structure.Player;
import com.utclo23.data.structure.PublicUser;
import com.utclo23.data.structure.Ship;
import com.utclo23.data.structure.StatGame;
......@@ -95,9 +96,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
/**
* Constructor
*/
public DataFacade() {
System.out.println(this.getClass() + " Creation de la facade");
public DataFacade() {
/**
* Construction of mediators by giving them a reference to this facade
*/
......@@ -392,7 +391,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
* @return my owner profile
*/
public Owner getMyOwnerProfile() {
return this.userMediator.getMyOwnerProfile();
}
......@@ -402,6 +401,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*
* @return my public profile
*/
@Override
public PublicUser getMyPublicUserProfile() {
try {
......@@ -468,7 +468,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
*
*/
@Override
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack) {
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack, Player playerWhoPutTheMine) {
if(isAttack)
{
System.out.println("datafacade | attack "+coords.getX()+"-"+coords.getY());
......@@ -476,10 +476,10 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
}
try {
Pair<Integer, Ship> pairReturn = this.gameMediator.attack(coords, isAttack);
if(this.getComfacade() != null){
this.getComfacade().notifyNewCoordinates(this.gameMediator.getCurrentGame().getRecentMine(coords), this.gameMediator.getCurrentGame().getRecipients());
}
Pair<Integer, Ship> pairReturn = this.gameMediator.attack(coords, isAttack, playerWhoPutTheMine);
return pairReturn;
} catch (Exception ex) {
ex.printStackTrace();
......
......@@ -21,7 +21,7 @@ public interface IDataIHMTable {
public List<Ship> getTemplateShips() throws DataException;
public void setShip(Ship ship) throws DataException;
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack);
public Pair<Integer, Ship> attack(Coordinate coords, boolean isAttack, Player playerWhoPutTheMine);
public void leaveGame();
public List<Ship> getInitialBoardFromGameId(String gameid);
......
......@@ -53,7 +53,7 @@ public class GameFactory {
System.out.println("player number : "+players.size());
List<LightPublicUser> spectators = new ArrayList<>();
spectators.add(creator);
//spectators.add(creator);
StatGame statGame = new StatGame(id, type, name, computerMode, spectator, spectatorChat, creator);
......
......@@ -175,7 +175,7 @@ public class GameMediator {
//last ship
if (this.currentGame.getTemplateShips().size() == player.getShips().size()) {
if (this.dataFacade.getComfacade() != null) {
this.dataFacade.getComfacade().sendShipsToEnnemy(player.getShips(), this.currentGame.getRecipients());
this.dataFacade.getComfacade().sendShipsToEnnemy(player.getShips(), this.currentGame.getRecipients(player.getLightPublicUser().getPlayerName()));
checkPlayersReady();
}
}
......@@ -262,12 +262,15 @@ public class GameMediator {
*
* @param coordinate
* @param isTrueAttack
* @param playerWhoPutTheMine
* @return
* @throws DataException
*/
public Pair<Integer, Ship> attack(Coordinate coordinate, boolean isTrueAttack) throws DataException, IOException, ClassNotFoundException {
public Pair<Integer, Ship> attack(Coordinate coordinate, boolean isTrueAttack, Player playerWhoPutTheMine) throws DataException, IOException, ClassNotFoundException {
if (this.currentGame != null) {
if(coordinate.getX()==-1 && coordinate.getY()==-1){
return new Pair(0,null);
}
Player player = this.currentGame.getCurrentPlayer();
System.out.println("ATTACK CURRENT PLAYER " + player.getLightPublicUser().getPlayerName());
......@@ -296,7 +299,7 @@ public class GameMediator {
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
// Forward to other players.
dataFacade.getComfacade().notifyNewCoordinates(new Mine(player, coordinate), currentGame.getRecipients());
dataFacade.getComfacade().notifyNewCoordinates(new Mine(player, coordinate), currentGame.getRecipients(player.getLightPublicUser().getPlayerName()));
//save with caretaker
if (!this.currentGame.isSave()) {
......@@ -309,20 +312,22 @@ public class GameMediator {
if (this.currentGame.isComputerGame()) {
//attack
Mine m = this.currentGame.getComputerPlayer().randomMine();
Mine m = this.currentGame.getComputerPlayer().randomMine(player.getShips(), this.currentGame);
this.forwardCoordinates(m);
boolean check = false;
for (Ship ship : this.currentGame.ennemyOf(this.currentGame.getComputerPlayer()).getShips()) {
for (Ship ship : this.currentGame.getCurrentPlayer().getShips()) {
if (this.currentGame.isShipTouched(ship, m)) {
this.currentGame.getComputerPlayer().setFocus(m.getCoord());
check = true;
if (this.currentGame.isShipDestroyed(ship, this.currentGame.getComputerPlayer().getMines())) {
this.currentGame.getComputerPlayer().loseFocus();
System.out.println("");
this.currentGame.getComputerPlayer().loseFocus(ship);
System.out.println("DATA TOTAL FOCUS LOST");
} else {
System.out.println("DATA FOCUS ON " + m.getCoord().getX() + "," + m.getCoord().getY());
}
}
......@@ -331,6 +336,10 @@ public class GameMediator {
if (!check) {
this.currentGame.getComputerPlayer().setFocus(null);
System.out.println("DATA FOCUS LOST ");
if (this.currentGame.getComputerPlayer().getFocus() != null) {
System.out.println("NEW FOCUS " + this.currentGame.getComputerPlayer().getFocus().getX() + "," + this.currentGame.getComputerPlayer().getFocus().getY());
}
}
}
......@@ -340,7 +349,13 @@ public class GameMediator {
// In the case of a test, that's possible that the current player is not
// the right player to test the mine (that means the enemy of the player
// is the right person to test the mine)
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
if(playerWhoPutTheMine!=null){
pairReturn = this.currentGame.attack(playerWhoPutTheMine, coordinate, isTrueAttack);
}else{
pairReturn = this.currentGame.attack(player, coordinate, isTrueAttack);
}
if (pairReturn.getKey() == 0 && pairReturn.getValue() == null) {
//pairReturn = this.currentGame.attack(this.currentGame.ennemyOf(player), coordinate, isTrueAttack);
}
......@@ -348,11 +363,13 @@ public class GameMediator {
}
//this.currentGame.nextTurn();
if (isTrueAttack) {
if (isTrueAttack && !this.currentGame.isComputerGame()) {
//Test if this game is finished
//If this game is finished, leave the game
if (this.currentGame.getStatGame().getWinner() != null) {
this.leaveGame();
this.defWin();
}
}
......@@ -373,6 +390,14 @@ public class GameMediator {
* @param role role of the new user
*/
public void updateGameList(LightPublicUser user, String id, String role) throws DataException {
System.out.print("liste players");
for (Player p : this.currentGame.getPlayers()) {
System.out.println(p.getLightPublicUser().getId() + " " + p.getLightPublicUser().getPlayerName());
}
System.out.println("-----------");
System.out.println("id " + id);
if (user == null) {
System.out.println("user is null");
......@@ -382,21 +407,27 @@ public class GameMediator {
System.out.println("current game is null");
}
if (this.currentGame.getId().compareTo(id) == 0) {
if (this.currentGame.getId().equals(id)) {
System.out.println("add Urole " + role);
this.getCurrentGame().addUser(user, role);
if (true) {
this.getCurrentGame().addUser(user, role);
if (this.dataFacade.getComfacade() != null) {
if (this.dataFacade.getComfacade() != null) {
System.out.println("data join game resp");
this.dataFacade.getComfacade().joinGameResponse(true, user.getId(), this.currentGame.getStatGame());
System.out.println("data join game resp");
this.dataFacade.getComfacade().joinGameResponse(true, user.getId(), this.currentGame.getStatGame());
}
} else {
this.dataFacade.getComfacade().joinGameResponse(false, id, null);
}
} else {
this.dataFacade.getComfacade().joinGameResponse(false, id, null);
}
System.out.println("nombre de joueurs " + this.currentGame.getPlayers().size());
}
public void gameConnectionRequestGame(String id, String role) {
......@@ -434,7 +465,7 @@ public class GameMediator {
}
}
Message msg = new Message(sender, text, this.currentGame.getRecipients());
Message msg = new Message(sender, text, this.currentGame.getRecipients(sender.getPlayerName()));
ComFacade comFacade = this.dataFacade.getComfacade();
if (comFacade != null) {
comFacade.notifyNewMessage(msg);
......@@ -474,13 +505,22 @@ public class GameMediator {
if (this.currentGame.getStatGame().getWinner() == null) {
this.giveUp();
}
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
}
// this.currentGame = null;
}
public void receptionGame(Game game) {
System.out.println("reception game ... ");
Player player = null;
for (Player p : game.getPlayers()) {
if (p.getLightPublicUser().getId().equals(this.dataFacade.getUserMediator().getMyLightPublicUserProfile().getId())) {
player = p;
}
}
game.setCurrentPlayer(player);
this.currentGame = game;
if (this.dataFacade.getIhmMainFacade() != null) {
......@@ -503,18 +543,32 @@ public class GameMediator {
* @param mine the mine placed
*/
public void forwardCoordinates(Mine mine) {
List<Ship> ships = this.currentGame.getCurrentPlayer().getShips();
System.out.println("FORWARD COORDINATES "+mine.getOwner().getLightPublicUser().getPlayerName()+" "+mine.getCoord().getX()+","+mine.getCoord().getY());
List<Ship> ships = this.currentGame.ennemyOf(mine.getOwner()).getShips();//this.currentGame.getCurrentPlayer().getShips();
Ship shipDestroyed = null;
boolean touched = false;
for (Ship s : ships) {
if (this.currentGame.isShipTouched(s, mine)) {
System.out.println("data "+mine.getOwner().getLightPublicUser().getPlayerName()+" touched "+mine.getCoord().getX()+","+mine.getCoord().getY());
touched = true;
if (this.currentGame.isShipDestroyed(s, this.currentGame.ennemyOf(this.currentGame.getCurrentPlayer()).getMines())) {
if (this.currentGame.isShipDestroyed(s, mine.getOwner().getMines())) {
shipDestroyed = s;
System.out.println("data "+mine.getOwner().getLightPublicUser().getPlayerName()+" destroyed ");
}
}
}
if(touched)
{
System.out.println("attack manqué ");
}
//Add mine to local game
if (!this.currentGame.isComputerGame()) {
......@@ -525,23 +579,24 @@ public class GameMediator {
if (!this.currentGame.isSave()) {
this.currentGame.getCaretaker().add(this.currentGame.saveStateToMemento());
}
if (this.dataFacade.getIhmTablefacade() != null) {
if (this.dataFacade.getIhmTablefacade() != null) {
this.dataFacade.getIhmTablefacade().feedBack(mine.getCoord(), touched, shipDestroyed);
}
if (this.currentGame.isGameFinishedByEnnemy()) {
//Sauvegarde à ajouter, que l'owner soit joueur ou pas.
String status = this.getOwnerStatus();
if (status == "player") {
System.out.println("status = "+status);
if (status.equals("player")) {
if (this.currentGame.getStatGame().getWinner() == null) {
this.giveUp();
}
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
this.currentGame = null;
}
this.dataFacade.getIhmTablefacade().finishGame(this.currentGame.getStatGame());
}
}
......@@ -562,9 +617,12 @@ public class GameMediator {
if (this.currentGame == null) {
throw new DataException("Pas de partie en cours.");
}
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
if (this.getCurrentGame().getWinner() == null) {
this.win();
}
this.dataFacade.getIhmTablefacade().finishGame(this.currentGame.getStatGame());
}
/**
......@@ -671,30 +729,21 @@ public class GameMediator {
this.currentGame.getComputerPlayer().setShips(this.currentGame.getTemplateShips());
this.dataFacade.getIhmTablefacade().notifyGameReady();
}
}
}
public void next()
{
if(this.currentGame!= null)
{
if(this.currentGame.isSave())
{
Event event = this.currentGame.getCaretaker().getMemento().getLastEvent();
if(event instanceof Mine)
{
public void next() {
if (this.currentGame != null) {
if (this.currentGame.isSave()) {
Event event = this.currentGame.getCaretaker().getMemento().getLastEvent();
if (event instanceof Mine) {
Mine mine = (Mine) event;
if(this.dataFacade.getMyPublicUserProfile().getId().equals(mine.getOwner().getLightPublicUser().getId()))
{
if (this.dataFacade.getMyPublicUserProfile().getId().equals(mine.getOwner().getLightPublicUser().getId())) {
//equivalent of attack
}
else
{
} else {
//equivalent of forward
}
}
this.currentGame.getCaretaker().next();
......
......@@ -11,14 +11,10 @@ import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.server.UID;
import java.util.ArrayList;
......@@ -62,8 +58,6 @@ public class UserMediator {
* @param dataFacade reference to the facade
*/
public UserMediator(DataFacade dataFacade) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Création du mediator");
this.dataFacade = dataFacade;
this.mapConnectedUser = new HashMap<>();
}
......@@ -233,6 +227,9 @@ public class UserMediator {
//save user in json file
save();
//Disconnect the user
this.owner = null;
}
}
......@@ -313,7 +310,7 @@ public class UserMediator {
*
* @param username
* @param password
* @throws Exception
* @throws DataException
*/
public void signIn(String username, String password) throws DataException {
......@@ -387,6 +384,7 @@ public class UserMediator {