Commit a1dbcff1 authored by Theo Boulaire's avatar Theo Boulaire
Browse files

Full rework of GameMediator's JavaDoc.

parent 700ffee3
...@@ -27,19 +27,25 @@ import java.util.logging.Logger; ...@@ -27,19 +27,25 @@ import java.util.logging.Logger;
import javafx.util.Pair; import javafx.util.Pair;
/** /**
* Game Mediator related to games features * Game Mediator related to games features.
*
* @author tboulair
*/ */
public class GameMediator { public class GameMediator {
/** /**
* reference to the data facade * Reference to the Data Facade.
*/ */
private DataFacade dataFacade; private DataFacade dataFacade;
/**
* Available games mapped by ID.
*/
private Map<String, StatGame> gamesMap; private Map<String, StatGame> gamesMap;
/**
* Game Factory.
*/
private GameFactory gameFactory; private GameFactory gameFactory;
/**
* The game wich is currently played, replayed or observed.
*/
private Game currentGame; private Game currentGame;
/** /**
...@@ -55,22 +61,35 @@ public class GameMediator { ...@@ -55,22 +61,35 @@ public class GameMediator {
} }
/**
*
* @return
*/
public Game getCurrentGame() { public Game getCurrentGame() {
return currentGame; return currentGame;
} }
/**
*
* @param currentGame
*/
public void setCurrentGame(Game currentGame) { public void setCurrentGame(Game currentGame) {
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | "); Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Data | ");
this.currentGame = currentGame; this.currentGame = currentGame;
} }
/** /**
* Create a game * Creation of a game.
*
* The game is notified to other users and it becomes the current game.
* *
* @param name * @param name Name of the game
* @param spectator * @param computerMode True if the opponent is an AI, false else.
* @param spectatorChat * @param spectator True if spectators are allowed, false else.
* @param type * @param spectatorChat True if spectators are allowed to chat, false else.
* @param type Type of the game (standard, belgian).
* @return Created game
* @throws DataException
*/ */
public Game createGame(String name, boolean computerMode, boolean spectator, boolean spectatorChat, GameType type) throws DataException { public Game createGame(String name, boolean computerMode, boolean spectator, boolean spectatorChat, GameType type) throws DataException {
//empty game name //empty game name
...@@ -104,9 +123,10 @@ public class GameMediator { ...@@ -104,9 +123,10 @@ public class GameMediator {
} }
/** /**
* add a new game
* *
* @param game * Adds a new StatGame to the available games' map.
*
* @param statgame StatGame ot add.
*/ */
public void addNewGame(StatGame statgame) { public void addNewGame(StatGame statgame) {
if (!this.gamesMap.containsKey(statgame.getId())) { if (!this.gamesMap.containsKey(statgame.getId())) {
...@@ -136,6 +156,14 @@ public class GameMediator { ...@@ -136,6 +156,14 @@ public class GameMediator {
return listGame; return listGame;
} }
/**
* Gives a ship to the playing owner.
* Checks if the ship doesn't collide with the owner's other ships and send
* the ships to the other users if it was the last one.
*
* @param ship Ship to add to the owner.
* @throws DataException
*/
public void setPlayerShip(Ship ship) throws DataException { public void setPlayerShip(Ship ship) throws DataException {
if (this.currentGame != null) { if (this.currentGame != null) {
String id = this.dataFacade.getMyPublicUserProfile().getId(); String id = this.dataFacade.getMyPublicUserProfile().getId();
...@@ -187,7 +215,7 @@ public class GameMediator { ...@@ -187,7 +215,7 @@ public class GameMediator {
} }
/** /**
* * Gives a random position to every ship of the AI.
*/ */
public void setComputerShips() { public void setComputerShips() {
Player cPlayer = this.currentGame.getComputerPlayer(); Player cPlayer = this.currentGame.getComputerPlayer();
...@@ -210,6 +238,14 @@ public class GameMediator { ...@@ -210,6 +238,14 @@ public class GameMediator {
ship.setListCoord(allCoords.get(position)); ship.setListCoord(allCoords.get(position));
} }
/**
* Creates the list of available coordinates for a ship.
* The list of already placed ships is taken into account.
*
* @param previousShips List of already placed ships.
* @param ship Ship to place.
* @return
*/
private List<List<Coordinate>> createAvailableCoordinates(List<Ship> previousShips, Ship ship) { private List<List<Coordinate>> createAvailableCoordinates(List<Ship> previousShips, Ship ship) {
int size = ship.getSize(); int size = ship.getSize();
List<List<Coordinate>> returnList = new ArrayList(); List<List<Coordinate>> returnList = new ArrayList();
...@@ -379,12 +415,17 @@ public class GameMediator { ...@@ -379,12 +415,17 @@ public class GameMediator {
} }
/** /**
* Attack of the owner.
* Checks if a mine was already placed here and if the game was just won.
* If the opponent is an AI, manages its attack. Saves the attack and previous
* events in a Memento.
* *
* Update current game's list as a new user has joined it. * @param coordinate Coordinate of the attack.
* * @param isTrueAttack
* @param user the new user who has joined * @return 1 if it's a hit, 0 if it's a miss and if a ship is destroyed this ship.
* @param id id of the stat game * @throws DataException
* @param role role of the new user * @throws IOException
* @throws ClassNotFoundException
*/ */
public void updateGameList(LightPublicUser user, String id, String role) throws DataException { public void updateGameList(LightPublicUser user, String id, String role) throws DataException {
System.out.print("liste players"); System.out.print("liste players");
...@@ -427,6 +468,14 @@ public class GameMediator { ...@@ -427,6 +468,14 @@ public class GameMediator {
System.out.println("nombre de joueurs " + this.currentGame.getPlayers().size()); System.out.println("nombre de joueurs " + this.currentGame.getPlayers().size());
} }
/**
*
* Update current game's list as a new user has joined it.
*
* @param user the new user who has joined
* @param id id of the stat game
* @param role role of the new user
*/
public void gameConnectionRequestGame(String id, String role) { public void gameConnectionRequestGame(String id, String role) {
role = role.toLowerCase(); role = role.toLowerCase();
...@@ -447,9 +496,11 @@ public class GameMediator { ...@@ -447,9 +496,11 @@ public class GameMediator {
} }
/** /**
* send a chat message * Sends a chat message.
* Doesn't send the message if the owner isn't allowed (observing a game
* with a disabled spectator chat).
* *
* @param text the text message to send * @param text Text to send.
*/ */
public void sendMessage(String text) { public void sendMessage(String text) {
//get information of sender //get information of sender
...@@ -470,9 +521,9 @@ public class GameMediator { ...@@ -470,9 +521,9 @@ public class GameMediator {
} }
/** /**
* Forward a message * Forwards a message to table for display.
* *
* @param msg message to forward * @param msg Message to forward.
*/ */
public void forwardMessage(Message msg) { public void forwardMessage(Message msg) {
IIHMTableToData ihmTablefacade = this.dataFacade.getIhmTablefacade(); IIHMTableToData ihmTablefacade = this.dataFacade.getIhmTablefacade();
...@@ -492,6 +543,8 @@ public class GameMediator { ...@@ -492,6 +543,8 @@ public class GameMediator {
/** /**
* Exit current game. * Exit current game.
* If there is no winner yet, and the owner is playing this game, gives
* victory to the opponent.
*/ */
public void leaveGame() { public void leaveGame() {
//Sauvegarde à ajouter. //Sauvegarde à ajouter.
...@@ -506,6 +559,11 @@ public class GameMediator { ...@@ -506,6 +559,11 @@ public class GameMediator {
// this.currentGame = null; // this.currentGame = null;
} }
/**
* Receive a game.
* It becomes the current game and it is forward to table.
* @param game Received game.
*/
public void receptionGame(Game game) { public void receptionGame(Game game) {
System.out.println("reception game ... "); System.out.println("reception game ... ");
Player player = null; Player player = null;
...@@ -527,6 +585,9 @@ public class GameMediator { ...@@ -527,6 +585,9 @@ public class GameMediator {
} }
/**
* Forwarding of "impossible connection" (to a game) message to main.
*/
public void connectionImpossible() { public void connectionImpossible() {
if (this.dataFacade.getIhmMainFacade() != null) { if (this.dataFacade.getIhmMainFacade() != null) {
this.dataFacade.getIhmMainFacade().connectionImpossible(); this.dataFacade.getIhmMainFacade().connectionImpossible();
...@@ -535,9 +596,11 @@ public class GameMediator { ...@@ -535,9 +596,11 @@ public class GameMediator {
} }
/** /**
* foorwardCoordinates * Reception of an attack by an other player (not AI).
* Mirror of the attack() method. If the game just ended, gives victory. Save
* state to Memento
* *
* @param mine the mine placed * @param mine Mine placed by the other player.
*/ */
public void forwardCoordinates(Mine mine) { public void forwardCoordinates(Mine mine) {
System.out.println("FORWARD COORDINATES "+mine.getOwner().getLightPublicUser().getPlayerName()+" "+mine.getCoord().getX()+","+mine.getCoord().getY()); System.out.println("FORWARD COORDINATES "+mine.getOwner().getLightPublicUser().getPlayerName()+" "+mine.getCoord().getX()+","+mine.getCoord().getY());
...@@ -626,7 +689,7 @@ public class GameMediator { ...@@ -626,7 +689,7 @@ public class GameMediator {
* Check if there is no current game or there is one but it already has a * Check if there is no current game or there is one but it already has a
* winner. * winner.
* *
* @return * @return True if finished, false else.
*/ */
public boolean isFinishedGame() { public boolean isFinishedGame() {
boolean finished = true; boolean finished = true;
...@@ -647,7 +710,7 @@ public class GameMediator { ...@@ -647,7 +710,7 @@ public class GameMediator {
* Return the status of the owner in the current game (player, spectator or * Return the status of the owner in the current game (player, spectator or
* null). * null).
* *
* @return * @return The owner's status.
*/ */
public String getOwnerStatus() { public String getOwnerStatus() {
LightPublicUser owner = this.dataFacade.getUserMediator().getMyLightPublicUserProfile(); LightPublicUser owner = this.dataFacade.getUserMediator().getMyLightPublicUserProfile();
...@@ -655,11 +718,11 @@ public class GameMediator { ...@@ -655,11 +718,11 @@ public class GameMediator {
} }
/** /**
* Return the status of user in the current game (player, spectator or * Return the status of a user in the current game (player, spectator or
* null). * null).
* *
* @param user * @param user The user whose status is requested.
* @return * @return The user's status ("player" or "spectator").
*/ */
public String getUserStatus(LightPublicUser user) { public String getUserStatus(LightPublicUser user) {
String status = null; String status = null;
...@@ -672,6 +735,11 @@ public class GameMediator { ...@@ -672,6 +735,11 @@ public class GameMediator {
} }
/**
* Set the position of the ennemy's ships.
*
* @param ships List of ships of the ennemy.
*/
public void setEnnemyShips(List<Ship> ships) { public void setEnnemyShips(List<Ship> ships) {
// Check game is instanciated // Check game is instanciated
if (this.currentGame != null) { if (this.currentGame != null) {
...@@ -691,7 +759,8 @@ public class GameMediator { ...@@ -691,7 +759,8 @@ public class GameMediator {
} }
/** /**
* Check if the two players are ready. * Check if both players are ready.
* Notify table if ready. Count the player's ships to know.
*/ */
private void checkPlayersReady() { private void checkPlayersReady() {
// If the 2 players are ready, notify IHM Table. // If the 2 players are ready, notify IHM Table.
...@@ -729,6 +798,9 @@ public class GameMediator { ...@@ -729,6 +798,9 @@ public class GameMediator {
} }
} }
/**
* Get the game to the next Memento of the caretaker.
*/
public void next() { public void next() {
if (this.currentGame != null) { if (this.currentGame != null) {
if (this.currentGame.isSave()) { if (this.currentGame.isSave()) {
......
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