Commit f0e885b6 authored by Thomas Lecluse's avatar Thomas Lecluse
Browse files

Merge branch 'feature/fix/v2' into 'int/v2'

Feature/fix/v2

See merge request !30
parents 77555add 40b175b6
......@@ -9,7 +9,6 @@ import javafx.application.Application;
import javafx.stage.Stage;
import main.java.client.data.DataCoreClient;
import main.java.client.game.GameCore;
import main.java.client.game.controller.MainGameController;
import main.java.client.main.controller.MainControllerFX;
import main.java.client.main.controller.MainCore;
import main.java.client.network.ComCoreClient;
......@@ -23,7 +22,7 @@ public class ClientApp extends Application {
private static ComCoreClient comCoreClient;
private static DataCoreClient dataCoreClient;
public static MainCore mainCore;
private static GameCore gameCore;
public static GameCore gameCore;
public static final String APPLICATION_NAME = "Othello";
public static final int APPLICATION_WIDTH = 889;
public static final int APPLICATION_HEIGHT = 500;
......@@ -55,7 +54,7 @@ public class ClientApp extends Application {
dataCoreClient = new DataCoreClient();
// IHM Game Core
gameCore = new GameCore(new MainGameController());
gameCore = new GameCore();
// IHM Main Core
mainCore = new MainCore(new MainControllerFX());
......@@ -63,9 +62,10 @@ public class ClientApp extends Application {
// Set implementations
dataCoreClient.setDataToCom(comCoreClient.getDataToComClientImpl());
dataCoreClient.setDataToMain(mainCore.getDataToMainImpl());
dataCoreClient.setDataToGame(gameCore.getDataToGame());
comCoreClient.setComToDataClient(dataCoreClient.getClientComToDataImpl());
mainCore.setMainToData(dataCoreClient.getMainToDataImpl());
// TODO missing: gameCore.setGameToData(dataCoreClient.getDataToGameImpl());
gameCore.setGameToData(dataCoreClient.getGameToDataImpl());
}
@Override
......
package main.java.client.data;
import main.java.common.dataModel.*;
import main.java.common.interfaces.client.*;
import java.util.List;
import main.java.common.dataModel.Board;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.Move;
import main.java.common.dataModel.UserHeavy;
import main.java.common.dataModel.UserLight;
import main.java.common.interfaces.client.IComToData;
import main.java.common.interfaces.client.IDataToCom;
import main.java.common.interfaces.client.IDataToGame;
import main.java.common.interfaces.client.IDataToMain;
import main.java.common.interfaces.client.IMainToData;
public class DataCoreClient {
private UserHeavy localUserHeavy;
private UserLight localUserLight;
......@@ -66,7 +74,7 @@ public class DataCoreClient {
this.listGameLight = listGameLight;
}
public List<GameLight>getListGameLight(){
public List<GameLight> getListGameLight() {
return listGameLight;
}
......@@ -86,6 +94,10 @@ public class DataCoreClient {
return dataToGame;
}
public void setDataToGame(IDataToGame dataToGame) {
this.dataToGame = dataToGame;
}
public GameToDataImpl getGameToDataImpl() {
return gameToDataImpl;
}
......
package main.java.client.data;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.Message;
import main.java.common.dataModel.UserLight;
import main.java.common.dataModel.UserZero;
import main.java.common.interfaces.client.IGameToData;
import java.sql.Timestamp;
import java.util.Date;
import java.util.ListIterator;
import java.util.UUID;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.UserZero;
import main.java.common.interfaces.client.IGameToData;
public class GameToDataImpl implements IGameToData {
DataCoreClient dataCoreClient;
......@@ -40,7 +37,8 @@ public class GameToDataImpl implements IGameToData {
}
@Override
public void newMessage(String m, UUID pid, UUID gameId) throws Exception {
dataCoreClient.getDataToCom().sendChatMessage(m, new Timestamp(new Date().getTime()), pid, gameId);
public void newMessage(String m, UUID gameId) throws Exception {
dataCoreClient.getDataToCom().sendChatMessage(m, new Timestamp(new Date().getTime()),
dataCoreClient.getLocalUserHeavy().getId(), gameId);
}
}
package main.java.client.data;
import main.java.common.dataModel.*;
import main.java.common.interfaces.client.IMainToData;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import javafx.scene.Scene;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
......@@ -17,6 +11,17 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import javafx.stage.Stage;
import main.java.common.dataModel.GameHeavy;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.GameStatus;
import main.java.common.dataModel.UserHeavy;
import main.java.common.dataModel.UserLight;
import main.java.common.interfaces.client.IMainToData;
public class MainToDataImpl implements IMainToData {
DataCoreClient dataCoreClient;
......@@ -29,18 +34,20 @@ public class MainToDataImpl implements IMainToData {
String userFilePath = Configuration.SAVE_DIR + File.separator + userLight.getPseudo() + ".json";
File userFile = new File(userFilePath);
if (userFile.exists())
if (userFile.exists()) {
throw new Exception("This user already exists on this computer");
}
JsonObject userJson = new JsonObject();
userJson.addProperty("id", userLight.getId().toString());
userJson.addProperty("pseudo", userLight.getPseudo());
userJson.addProperty("lastName", userLight.getLastName());
userJson.addProperty("firstName", userLight.getFirstName());
if (userLight.getDateOfBirth() == null)
if (userLight.getDateOfBirth() == null) {
userJson.addProperty("dateOfBirth", (String) null);
else
} else {
userJson.addProperty("dateOfBirth", new SimpleDateFormat("dd-MM-yyyy").format(userLight.getDateOfBirth()));
}
userJson.addProperty("avatarId", userLight.getAvatarId());
userJson.addProperty("playedGames", userLight.getPlayedGames());
userJson.addProperty("wonGames", userLight.getWonGames());
......@@ -65,10 +72,12 @@ public class MainToDataImpl implements IMainToData {
}
@Override
public Scene newGame(UserLight ul, Boolean spectatorsOk, Boolean chatOk, Boolean creatorPlayWhite, int limitMove, Scene sceneFx) {
GameLight gameCreated = new GameLight(UUID.randomUUID(), ul, null, GameStatus.PENDING, spectatorsOk, chatOk, creatorPlayWhite, limitMove);
dataCoreClient.getDataToGame().getGameScreen(gameCreated, sceneFx);
return sceneFx;
public Stage newGame(UserLight ul, Boolean spectatorsOk, Boolean chatOk, Boolean creatorPlayWhite, int limitMove,
Stage stageFx) {
GameLight gameCreated = new GameLight(UUID.randomUUID(), ul, null, GameStatus.PENDING, spectatorsOk, chatOk,
creatorPlayWhite, limitMove);
dataCoreClient.getDataToGame().getGameScreen(gameCreated, stageFx);
return stageFx;
}
@Override
......@@ -77,7 +86,7 @@ public class MainToDataImpl implements IMainToData {
}
@Override
public void requestToDataToSpecialGame(GameLight game, UserLight user, Scene sceneFx) {
public void requestToDataToSpecialGame(GameLight game, UserLight user, Stage stageFx) {
}
......@@ -97,11 +106,14 @@ public class MainToDataImpl implements IMainToData {
}
@Override
public void sendUserInfo(String pseudo, String lastName, String firstName, Date dateOfBirth, int avatarId, String password) throws Exception {
if (lastName == null || firstName == null || password == null)
public void sendUserInfo(String pseudo, String lastName, String firstName, Date dateOfBirth, int avatarId,
String password) throws Exception {
if (lastName == null || firstName == null || password == null) {
throw new Exception("One or more required field is empty");
}
UUID id = UUID.randomUUID();
createUser(new UserLight(id, pseudo, lastName, firstName, dateOfBirth, avatarId, 0, 0), new UserHeavy(id, pseudo, password, null, null, null));
createUser(new UserLight(id, pseudo, lastName, firstName, dateOfBirth, avatarId, 0, 0),
new UserHeavy(id, pseudo, password, null, null, null));
}
@Override
......@@ -111,18 +123,22 @@ public class MainToDataImpl implements IMainToData {
@Override
public void updateProfile(UserLight profileLight, UserHeavy profileHeavy) throws Exception {
String userFilePath = Configuration.SAVE_DIR + File.separator + dataCoreClient.getLocalUserHeavy().getPseudo() + ".json";
String userFilePath = Configuration.SAVE_DIR + File.separator + dataCoreClient.getLocalUserHeavy().getPseudo()
+ ".json";
File userFile = new File(userFilePath);
if (!userFile.exists())
if (!userFile.exists()) {
throw new Exception("File for user " + dataCoreClient.getLocalUserHeavy().getPseudo() + " not found");
}
String oldFilePath = userFilePath + profileLight.getId().toString();
File oldFile = new File(oldFilePath);
if (oldFile.exists())
if (oldFile.exists()) {
throw new Exception("Error while backuping old data");
}
userFile.renameTo(oldFile);
try {
createUser(profileLight, profileHeavy);
//dataCoreClient.getDataToCom().notifyUpdateProfile(profileLight); TO DISCUSS WITH COM
// dataCoreClient.getDataToCom().notifyUpdateProfile(profileLight); TO DISCUSS
// WITH COM
} catch (Exception e) {
System.out.println(e.getMessage());
oldFile.renameTo(userFile);
......@@ -132,7 +148,7 @@ public class MainToDataImpl implements IMainToData {
@Override
public void identify(String login, String password, String address) throws Exception {
//blank playername or password
// blank playername or password
if (login.isEmpty() || password.isEmpty()) {
throw new Exception("Data error: due to empty login or password");
}
......@@ -143,15 +159,19 @@ public class MainToDataImpl implements IMainToData {
public UserLight getUserLightFromFile(String pseudo) throws Exception, IOException, ParseException {
String path = Configuration.SAVE_DIR + File.separator + pseudo + ".json";
File userFile = new File(path);
if (!userFile.exists())
if (!userFile.exists()) {
throw new Exception("Data error: User profile does not exist");
}
JsonParser parser = new JsonParser();
JsonObject userObject = parser.parse(new String(Files.readAllBytes(Paths.get(path)))).getAsJsonObject();
UserLight userLight = new UserLight(UUID.fromString(userObject.get("id").getAsString()), userObject.get("pseudo").getAsString(),
userObject.get("lastName").getAsString(), userObject.get("firstName").getAsString(),
userObject.get("dateOfBirth").isJsonNull() ? null : new SimpleDateFormat("dd-MM-yyyy").parse(userObject.get("dateOfBirth").getAsString()),
userObject.get("avatarId").getAsInt(), userObject.get("playedGames").getAsInt(), userObject.get("wonGames").getAsInt());
UserLight userLight = new UserLight(UUID.fromString(userObject.get("id").getAsString()),
userObject.get("pseudo").getAsString(), userObject.get("lastName").getAsString(),
userObject.get("firstName").getAsString(),
userObject.get("dateOfBirth").isJsonNull() ? null
: new SimpleDateFormat("dd-MM-yyyy").parse(userObject.get("dateOfBirth").getAsString()),
userObject.get("avatarId").getAsInt(), userObject.get("playedGames").getAsInt(),
userObject.get("wonGames").getAsInt());
return userLight;
}
......@@ -159,14 +179,15 @@ public class MainToDataImpl implements IMainToData {
public UserHeavy getUserHeavyFromFile(String pseudo) throws Exception, IOException, ParseException {
String path = Configuration.SAVE_DIR + File.separator + pseudo + ".json";
File userFile = new File(path);
if (!userFile.exists())
if (!userFile.exists()) {
throw new Exception("Data error: User profile does not exist");
}
JsonParser parser = new JsonParser();
JsonObject userObject = parser.parse(new String(Files.readAllBytes(Paths.get(path)))).getAsJsonObject();
UserHeavy userHeavy = new UserHeavy(UUID.fromString(userObject.get("id").getAsString()), userObject.get("pseudo").getAsString(),
userObject.get("password").getAsString(), userObject.get("serverAddress").toString(),
userObject.get("serverPort").toString(), null);
UserHeavy userHeavy = new UserHeavy(UUID.fromString(userObject.get("id").getAsString()),
userObject.get("pseudo").getAsString(), userObject.get("password").getAsString(),
userObject.get("serverAddress").toString(), userObject.get("serverPort").toString(), null);
return userHeavy;
}
......
package main.java.client.game;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import main.java.client.game.controller.MainGameController;
import main.java.client.game.model.DataToGame;
import main.java.client.game.model.DataToGameImpl;
import main.java.common.interfaces.client.IGameToData;
public class GameCore {
/**
* Class logger
*/
private static final Logger LOGGER = LogManager.getLogger(GameCore.class);
/**
* JavaFX stage
*/
private Stage fxStage;
/**
* MainGameController reference
*/
......@@ -18,16 +37,43 @@ public class GameCore {
/**
* Reference to interface implementation for calls from the outside
*/
private DataToGame dataToGame;
private DataToGameImpl dataToGame;
/**
* Class constructor
*/
public GameCore() {
controller = new MainGameController();
controller.setCore(this);
dataToGame = new DataToGameImpl(this);
}
/**
* Getter for the Game module main controller
*
* @return
*/
public MainGameController getMainGameController() {
return controller;
}
/**
* Resets the FX stage content in order to put the game module one inside.
*
* @param ig2d : IGameToData
* @param fxmlResource
* @param stage
* @throws IOException
*/
public GameCore(MainGameController gameController) {
controller = gameController;
dataToGame = new DataToGame(this);
public void initializeFxStage(String fxmlResource, Stage stage) throws IOException {
fxStage = stage;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxmlResource));
BorderPane root = (BorderPane) fxmlLoader.load();
Scene scene = new Scene(root, 1200, 800);
scene.getStylesheets().add("view/game.css");
fxStage.setScene(scene);
fxStage.show();
}
/**
......@@ -53,7 +99,7 @@ public class GameCore {
*
* @return
*/
public final DataToGame getDataToGame() {
public final DataToGameImpl getDataToGame() {
return dataToGame;
}
}
package main.java.client.game.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import main.java.client.ClientApp;
import main.java.client.game.GameCore;
import main.java.common.dataModel.Message;
/**
* IHM-Game main controller class.
......@@ -12,6 +18,10 @@ import javafx.scene.input.KeyEvent;
* Links the FXML elements to the correct methods in the specialized controllers
*/
public class MainGameController {
/**
* Class logger
*/
private static final Logger LOGGER = LogManager.getLogger(MainGameController.class);
@FXML
Button openButton;
......@@ -19,6 +29,9 @@ public class MainGameController {
@FXML
TextArea areaSendMessage;
@FXML
TextArea areaChatHistory;
/**
* Side controller
*/
......@@ -29,6 +42,11 @@ public class MainGameController {
*/
private MainGameControllerBoard boardController;
/**
* Application game core
*/
private GameCore gameCore;
/**
* Controller constructor, instantiates the secondary controllers
*/
......@@ -37,7 +55,29 @@ public class MainGameController {
boardController = new MainGameControllerBoard();
}
/**
* Sets the module Core reference
*
* @param core
*/
public void setCore(GameCore core) {
gameCore = core;
sideController.setCore(core);
}
/**
* Method called after the display of the game Java FX scene
*/
public void initialize() {
/*
* TODO: TEMPORARY FIX
*
* Another MainGameController is automatically created by JavaFX once the stage
* has been set using the related ihm game fxml file. Because of that, the
* current core is no longer set.
*/
setCore(ClientApp.gameCore);
}
/**
......@@ -50,6 +90,14 @@ public class MainGameController {
}
}
/**
* Displays a message to the chat history area after its receiving.
*/
public void newMessageToHistory(Message m) {
areaChatHistory.appendText("-------\n");
areaChatHistory.appendText(m.getMessage());
}
/**
* Called upon a click on the forfeit button
*/
......
......@@ -2,6 +2,10 @@ package main.java.client.game.controller;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import main.java.client.game.GameCore;
import main.java.common.dataModel.UserLight;
/**
......@@ -9,6 +13,16 @@ import main.java.common.dataModel.UserLight;
*/
public class MainGameControllerSide {
/**
* Class logger
*/
private static final Logger LOGGER = LogManager.getLogger(MainGameControllerSide.class);
/**
* Application game core
*/
private GameCore gameCore;
/**
* Constructor
*/
......@@ -16,10 +30,27 @@ public class MainGameControllerSide {
}
/**
* Sets the module Core reference
*
* @param core
*/
public void setCore(GameCore core) {
gameCore = core;
}
/**
* Sends a chat message to Data module, in order to distribute it to all clients
* connected to the game
*
* @param message
*/
public void onSendMessage(String message) {
System.out.println("->" + message);
LOGGER.debug("Message sent -> " + message);
try {
gameCore.getGameToData().newMessage(message, gameCore.getDataToGame().getGameUUID());
} catch (Exception e) {
LOGGER.error("No game set yet");
}
}
/**
......
package main.java.client.game.model;
import java.io.IOException;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.scene.Scene;
import javafx.stage.Stage;
import main.java.client.game.GameCore;
import main.java.common.dataModel.Board;
import main.java.common.dataModel.GameHeavy;
......@@ -9,15 +16,21 @@ import main.java.common.dataModel.Message;
import main.java.common.dataModel.UserLight;
import main.java.common.interfaces.client.IDataToGame;
public class DataToGame implements IDataToGame {
public class DataToGameImpl implements IDataToGame {
/**
* Class logger
*/
private static final Logger LOGGER = LogManager.getLogger(DataToGameImpl.class);
private GameCore core;
private GameLight currentGame;
/**
* @param moduleCore
*/
public DataToGame(GameCore moduleCore) {
core = core;
public DataToGameImpl(GameCore moduleCore) {
core = moduleCore;
}
/**
......@@ -38,7 +51,7 @@ public class DataToGame implements IDataToGame {
*/
@Override
public void displayMessage(Message m) {
core.getMainGameController().newMessageToHistory(m);
}
/**
......@@ -51,19 +64,34 @@ public class DataToGame implements IDataToGame {
/**
* @param gl : Game data
* @param sc : JavaFX Scene (shared with IHM-MAIN module)
* @param s : JavaFX Stage (shared with IHM-MAIN module)
*/
@Override
public void getGameScreen(GameLight gl, Scene sc) {
public void getGameScreen(GameLight gl, Stage s) {
currentGame = gl;
try {
core.initializeFxStage("view/ihm_game.fxml", s);
} catch (IOException e) {
LOGGER.error("Could not instantiate FX scene", e);
}
}
/**
* Gets the current game UUID (null if no game set)
*
* @return
*/
public UUID getGameUUID() {
return currentGame != null ? currentGame.getId() : null;
}
/**
* @param gl : Game data
* @param sc : JavaFX Scene (shared with IHM-MAIN module)
* @param s : JavaFX Stage (shared with IHM-MAIN module)
*/
@Override
public void beginGameDisplay(GameLight gl, Scene sc) {