Commit 25787dbc authored by Peining Li's avatar Peining Li
Browse files

Merge branch 'develop' into 'ihm-main/develop'

Develop

See merge request !169
parents a42464b6 e4daa948
......@@ -158,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();
}
}
}
}
/**
......@@ -242,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();
}
}
}
}
/*
* 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);
}
}
......@@ -284,10 +284,10 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
@Override
public void leaveGame() {
String role = this.gameMediator.getOwnerStatus();
if (!role.equals("spectator")) {
if (!role.equals("spectator") && this.gameMediator.getCurrentGame()!=null) {
Logger.getLogger(DataFacade.class.getName()).log(Level.INFO, null, "data | leave game");
this.comfacade.leaveGame();
this.comfacade.leaveGame(this.gameMediator.getCurrentGame().getRecipients(this.getMyPublicUserProfile().getPlayerName()));
this.gameMediator.leaveGame();
try {
this.ihmMainFacade.toMenu();
......@@ -308,7 +308,7 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
public void opponentHasLeftGame() {
Logger.getLogger(DataFacade.class.getName()).info("data | opponent has left");
String role = this.gameMediator.getOwnerStatus();
if (!this.gameMediator.isFinishedGame()) {
try {
this.gameMediator.defWin();
......@@ -319,6 +319,12 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
this.ihmTablefacade.opponentHasLeftGame();
}
}
@Override
public void removeGame(String id)
{
this.gameMediator.removeGame(id);
}
/**
* Notify that connection is lost
......@@ -419,7 +425,17 @@ public class DataFacade implements IDataCom, IDataIHMTable, IDataIHMMain {
@Override
public PublicUser getMyPublicUserProfile() {
return this.userMediator.getMyPublicUserProfile();
PublicUser usr = this.userMediator.getMyPublicUserProfile();
try {
usr.setNumberDefeats(this.getNumberDefeats());
usr.setNumberVictories(this.getNumberVictories());
usr.setNumberAbandons(this.getNumberAbandons());
} catch (DataException ex) {
Logger.getLogger(DataFacade.class.getName()).log(Level.SEVERE, null, ex);
}
return usr;
}
......
......@@ -20,7 +20,7 @@ public interface IDataCom {
* @param game
*/
public void addNewGame(StatGame game);
public void removeGame(String id);
/**
*
* @param ships
......
......@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -98,6 +99,7 @@ public class GameMediator {
//creat Game for realGame
Game game = this.gameFactory.createGame(name, creator, computerMode, spectator, spectatorChat, type);
this.addNewGame(game.getStatGame());
//to Com : notify a new game
ComFacade comFacade = this.dataFacade.getComfacade();
......@@ -215,7 +217,7 @@ public class GameMediator {
*/
public Pair<Integer, Ship> attack(Coordinate coordinate, boolean isTrueAttack, Player playerWhoPutTheMine) throws DataException, IOException, ClassNotFoundException {
if (this.currentGame != null) {
Player player = playerWhoPutTheMine;
Pair<Integer, Ship> pairReturn;
......@@ -247,6 +249,15 @@ public class GameMediator {
if (this.currentGame.isComputerGame() && this.currentGame.isGameFinishedByCurrentPlayer()) {
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
this.dataFacade.getIhmTablefacade().finishGame(this.currentGame.getStatGame());
//if creator => delete game
if (this.currentGame.getStatGame().getCreator().getId().equals(this.dataFacade.getMyPublicUserProfile().getId())) {
Logger.getLogger(GameMediator.class.getName()).info("delete game by creator after leave game");
this.dataFacade.getComfacade().removeGame(this.currentGame.getId());
this.removeGame(this.getCurrentGame().getId());
}
return null;
}
......@@ -281,7 +292,11 @@ public class GameMediator {
if (this.currentGame.getComputerPlayer().getFocus() != null) {
}
}
Random r = new Random();
if(r.nextInt(5)==1){
this.sendMessageIA();
}
}
} else {
......@@ -331,7 +346,7 @@ public class GameMediator {
if (role.equals("spectator") || (role.equals("player") && this.currentGame.getPlayers().size() < 2)) {
Logger.getLogger(this.getClass().toString()).info(user.getPlayerName()+" role "+role);
Logger.getLogger(this.getClass().toString()).info(user.getPlayerName() + " role " + role);
this.getCurrentGame().addUser(user, role);
if (this.dataFacade.getComfacade() != null) {
......@@ -339,9 +354,7 @@ public class GameMediator {
}
}
else
{
} else {
this.dataFacade.getComfacade().joinGameResponse(false, id, null);
}
......@@ -383,7 +396,7 @@ public class GameMediator {
LightPublicUser sender = this.dataFacade.getMyPublicUserProfile().getLightPublicUser();
//check if sender is spectator and if chat is allowed for spectators
if (this.currentGame.getSpectators().contains(sender)) {
if (this.currentGame.getSpectators().contains(sender) || this.currentGame.getRecipients("").contains(sender) ) {
if (!this.currentGame.getStatGame().isSpectatorChat()) {
return;
}
......@@ -396,6 +409,40 @@ public class GameMediator {
}
}
public void sendMessageIA() {
System.out.println("IA send message");
List<String> punchlines = new ArrayList<String>();
punchlines.add("Tu vas bientôt perdre !");
punchlines.add("Tu as autant de chance de gagner que d'avoir A en LO23");
punchlines.add("zZz zZz Je m'ennuie avec toi. Tu joues mal.");
punchlines.add("Tu crois pouvoir battre une IA ?");
punchlines.add("LOL");
Random r = new Random();
int i = r.nextInt(punchlines.size());
String text = punchlines.get(i);
//get information of sender
LightPublicUser sender = this.currentGame.getComputerPlayer().getLightPublicUser();
//check if sender is spectator and if chat is allowed for spectators
if (!this.currentGame.getStatGame().isSpectatorChat()) {
return;
}
Message msg = new Message(sender, text, this.currentGame.getRecipients(this.dataFacade.getMyPublicUserProfile().getPlayerName()));
ComFacade comFacade = this.dataFacade.getComfacade();
if (comFacade != null) {
comFacade.notifyNewMessage(msg);
}
this.forwardMessage(msg);
System.out.println("IA send message end");
}
/**
* Forward a message
*
......@@ -421,15 +468,25 @@ public class GameMediator {
* Exit current game.
*/
public void leaveGame() {
//Sauvegarde à ajouter, que l'owner soit joueur ou pas.
String status = this.getOwnerStatus();
if (status.equals(Configuration.PLAYER)) {
//Sauvegarde à ajouter.
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
if (this.currentGame.getStatGame().getWinner() == null) {
this.giveUp();
}
//if creator => delete game
if (this.currentGame.getStatGame().getCreator().getId().equals(this.dataFacade.getMyPublicUserProfile().getId())) {
Logger.getLogger(GameMediator.class.getName()).info("delete game by creator after leave game");
this.dataFacade.getComfacade().removeGame(this.currentGame.getId());
this.removeGame(this.getCurrentGame().getId());
}
}
}
......@@ -450,15 +507,13 @@ public class GameMediator {
}
game.setCurrentPlayer(player);
//do one time
if ((this.currentGame!=null && !this.currentGame.getId().equals(game.getId())|| this.currentGame ==null) && this.dataFacade.getIhmMainFacade() != null) {
if ((this.currentGame != null && !this.currentGame.getId().equals(game.getId()) || this.currentGame == null) && this.dataFacade.getIhmMainFacade() != null) {
this.currentGame = game;
this.dataFacade.getIhmMainFacade().receptionGame(game);
}
else
{
this.currentGame = game;
} else {
this.currentGame = game;
}
}
......@@ -479,14 +534,14 @@ public class GameMediator {
* @param mine the mine placed
*/
public void forwardCoordinates(Mine mine) {
System.out.println("forward coordinate "+mine.getOwner().getLightPublicUser().getPlayerName()+" mine "+mine.getCoord().getX()+" "+mine.getCoord().getY());
System.out.println("forward coordinate " + mine.getOwner().getLightPublicUser().getPlayerName() + " mine " + mine.getCoord().getX() + " " + mine.getCoord().getY());
List<Ship> ships = this.currentGame.ennemyOf(mine.getOwner()).getShips();
Ship shipDestroyed = null;
boolean touched = false;
//Add mine to local player
this.currentGame.getPlayer(mine.getOwner().getLightPublicUser().getId()).getMines().add(mine);
for (Ship s : ships) {
if (this.currentGame.isShipTouched(s, mine)) {
......@@ -499,14 +554,12 @@ public class GameMediator {
}
}
if (this.dataFacade.getIhmTablefacade() != null) {
this.dataFacade.getIhmTablefacade().feedBack(mine.getCoord(), touched, shipDestroyed);
}
if (this.currentGame.isGameFinishedByEnnemy(mine.getOwner())) {
String status = this.getOwnerStatus();
if (status.equals(Configuration.PLAYER)) {
......@@ -514,13 +567,19 @@ public class GameMediator {
this.giveUp();
}
this.dataFacade.getUserMediator().addPlayedGame(this.currentGame.getStatGame());
}
else
{
} else {
this.currentGame.getStatGame().setWinner(mine.getOwner().getLightPublicUser());
}
this.dataFacade.getIhmTablefacade().finishGame(this.currentGame.getStatGame());
//if creator => delete game
if (this.currentGame.getStatGame().getCreator().getId().equals(this.dataFacade.getMyPublicUserProfile().getId())) {
Logger.getLogger(GameMediator.class.getName()).info("delete game by creator after leave game");
this.dataFacade.getComfacade().removeGame(this.currentGame.getId());
this.removeGame(this.getCurrentGame().getId());
}
}
}
......@@ -530,7 +589,17 @@ public class GameMediator {
private void giveUp() {
String ownerID = this.dataFacade.getUserMediator().getMyPublicUserProfile().getId();
Player opponent = this.currentGame.ennemyOf(this.currentGame.getPlayer(ownerID));
this.currentGame.getStatGame().setWinner(opponent.getLightPublicUser());
if (opponent != null) {
this.currentGame.getStatGame().setWinner(opponent.getLightPublicUser());
}
}
public void removeGame(String id) {
if (this.gamesMap.containsKey(id)) {
this.gamesMap.remove(id);
Logger.getLogger("GameMediator").info("delete " + id);
}
}
/**
......@@ -605,8 +674,8 @@ public class GameMediator {
* @param ships
*/
public void setEnnemyShips(List<Ship> ships) {
System.out.println("ENNEMY SHIPS "+ships.get(0).getOwner().getLightPublicUser().getPlayerName());
System.out.println("ENNEMY SHIPS " + ships.get(0).getOwner().getLightPublicUser().getPlayerName());
// Check game is instanciated
if (this.currentGame != null) {
if (!ships.isEmpty()) {
......@@ -618,15 +687,13 @@ public class GameMediator {
// Set the ships
p.setShips(ships);
System.out.println("ADD TO "+p.getLightPublicUser().getPlayerName());
if(!this.getOwnerStatus().equals("spectator"))
{
checkPlayersReady();
System.out.println("ADD TO " + p.getLightPublicUser().getPlayerName());
if (!this.getOwnerStatus().equals("spectator")) {
checkPlayersReady();
}
}
}
}
......@@ -636,38 +703,38 @@ public class GameMediator {
*/
private void checkPlayersReady() {
// If the 2 players are ready, notify IHM Table.
if (!this.currentGame.isComputerGame()) {
boolean ready = true;
for (Player player : this.currentGame.getPlayers()) {
// If their ships are placed.
if (this.currentGame.getTemplateShips().size() != player.getShips().size()) {
ready = false;
break;
if (this.currentGame.getPlayers().size() == 2) {
if (!this.currentGame.isComputerGame()) {
boolean ready = true;
for (Player player : this.currentGame.getPlayers()) {
// If their ships are placed.
if (this.currentGame.getTemplateShips().size() != player.getShips().size()) {
ready = false;
break;
}
}
}
if (ready) {
this.dataFacade.getIhmTablefacade().notifyGameReady();
}
} else {
boolean ready = false;
for (Player player : this.currentGame.getPlayers()) {
// If their ships are placed.
if (this.currentGame.getTemplateShips().size() == player.getShips().size()) {
ready = true;
break;
if (ready) {
this.dataFacade.getIhmTablefacade().notifyGameReady();
}
}
} else {
boolean ready = false;
for (Player player : this.currentGame.getPlayers()) {
// If their ships are placed.
if (this.currentGame.getTemplateShips().size() == player.getShips().size()) {
ready = true;
break;
}
}
if (ready) //notify IA to place ships
{
Player ennemy = this.currentGame.ennemyOf(this.currentGame.getComputerPlayer());
this.currentGame.getComputerPlayer().setIAShips(this.currentGame.getTemplateShips());
this.dataFacade.getComfacade().sendShipsToEnnemy(this.currentGame.getComputerPlayer().getShips(), this.currentGame.getRecipients(ennemy.getLightPublicUser().getPlayerName()));
if (ready) //notify IA to place ships
{
Player ennemy = this.currentGame.ennemyOf(this.currentGame.getComputerPlayer());
this.currentGame.getComputerPlayer().setIAShips(this.currentGame.getTemplateShips());
this.dataFacade.getComfacade().sendShipsToEnnemy( this.currentGame.getComputerPlayer().getShips(), this.currentGame.getRecipients(ennemy.getLightPublicUser().getPlayerName()));
this.dataFacade.getIhmTablefacade().notifyGameReady();
this.dataFacade.getIhmTablefacade().notifyGameReady();
}
}
}
}
......
......@@ -338,7 +338,7 @@ public class UserMediator {
try {
user = mapper.readValue(userFile, Owner.class);
} catch (Exception e) {
Logger.getLogger("log").info(e.getMessage());
throw new DataException("Data : error in reading file");
}
......
......@@ -12,7 +12,6 @@ import java.util.Deque;
import java.util.List;
import java.util.Random;
/**
*
* @author lucillefargeau
......@@ -47,13 +46,12 @@ public class ComputerPlayer extends Player {
for (Coordinate cf : list) {
this.stackFocus.remove(cf);
}
Deque<Coordinate> tmp = new ArrayDeque<>();
while(!this.stackFocus.isEmpty())
{
while (!this.stackFocus.isEmpty()) {
tmp.push(this.stackFocus.pop());
}
this.stackFocus = tmp;
}
......@@ -95,41 +93,31 @@ public class ComputerPlayer extends Player {
if (oldFocus.getX() > focus.getX()) {
dx = -1;
dy = 0;
}
else if (oldFocus.getX() < focus.getX()) {
} else if (oldFocus.getX() < focus.getX()) {
dx = -1;
dy = 0;
}
else if (oldFocus.getY() > focus.getY()) {
} else if (oldFocus.getY() > focus.getY()) {
dy = -1;
dx = 0;
}
else if (oldFocus.getY() < focus.getY()) {
} else if (oldFocus.getY() < focus.getY()) {
dy = 1;
dx = 0;
}
}
this.stackFocus.push(focus);
}
else
{
} else {
dx = -dx;
dy = -dy;
Deque<Coordinate> tmp = new ArrayDeque<>();
while(!this.stackFocus.isEmpty())
{
while (!this.stackFocus.isEmpty()) {
tmp.push(this.stackFocus.pop());
}
this.stackFocus = tmp;
}
}
......@@ -137,7 +125,6 @@ public class ComputerPlayer extends Player {
*
* @param ships
*/
public void setIAShips(List<Ship> ships) {
System.out.println("IA SETTING SHIPS (IF YOU ARE SPECTATOR --> IT IS BIG PROBLEM");
int[][] tab = new int[10][10];
......@@ -187,7 +174,6 @@ public class ComputerPlayer extends Player {
} while (!valid);
//fill
if ((x + ship.getSize() < Configuration.WIDTH)) {
for (int i = 0; i < ship.getSize(); ++i) {
......@@ -205,15 +191,11 @@ public class ComputerPlayer extends Player {