Commit c38886df authored by Corentin Boulanger's avatar Corentin Boulanger
Browse files

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

Fix package names once and for all

See merge request !54
parents 00496074 1010d63a
......@@ -29,7 +29,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
......@@ -74,7 +74,7 @@
</plugins>
<resources>
<resource>
<targetPath>main/java/client/main/fxml</targetPath>
<targetPath>client/main/fxml</targetPath>
<directory>src/main/java/client/main/fxml</directory>
<includes>
<include>**/*.fxml</include>
......
package main.java;
import main.java.client.ClientApp;
import main.java.server.ServerApp;
import client.ClientApp;
import server.ServerApp;
/**
* Main application (use following arguments to run) <br>
......
package main.java.client;
package client;
import client.data.DataCoreClient;
import client.game.GameCore;
import client.main.controller.MainControllerFX;
import client.main.controller.MainCore;
import client.network.ComCoreClient;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.stage.Stage;
import main.java.client.data.DataCoreClient;
import main.java.client.game.GameCore;
import main.java.client.main.controller.MainControllerFX;
import main.java.client.main.controller.MainCore;
import main.java.client.network.ComCoreClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......
package main.java.client.data;
package client.data;
import common.dataModel.*;
import common.interfaces.client.IComToData;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import main.java.common.dataModel.Board;
import main.java.common.dataModel.GameHeavy;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.Message;
import main.java.common.dataModel.UserLight;
import main.java.common.interfaces.client.IComToData;
public class ComToDataClientImpl implements IComToData {
DataCoreClient dataCoreClient;
......@@ -30,10 +26,10 @@ public class ComToDataClientImpl implements IComToData {
@Override
public void addNewUser(UserLight user) {
if(!dataCoreClient.getLocalUserLight().getId().equals(user.getId()) ) {
if (!dataCoreClient.getLocalUserLight().getId().equals(user.getId())) {
dataCoreClient.getListConnectedUsers().add(user);
dataCoreClient.getDataToMain().updateUserList(user);
}
}
}
@Override
......
package main.java.client.data;
package client.data;
import java.io.File;
......
package main.java.client.data;
package client.data;
import main.java.common.dataModel.*;
import main.java.common.interfaces.client.*;
import common.dataModel.*;
import common.interfaces.client.*;
import java.util.ArrayList;
import java.util.List;
......@@ -27,7 +27,7 @@ public class DataCoreClient {
gameToDataImpl = new GameToDataImpl(this);
listConnectedUsers = new ArrayList<UserLight>();
listGameLight = new ArrayList<GameLight>();
}
public List<UserLight> getListConnectedUsers() {
......
package main.java.client.data;
package client.data;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.Message;
import main.java.common.dataModel.UserZero;
import main.java.common.interfaces.client.IGameToData;
import common.dataModel.GameLight;
import common.dataModel.Message;
import common.dataModel.UserZero;
import common.interfaces.client.IGameToData;
import java.sql.Timestamp;
import java.util.Date;
......
package main.java.client.data;
package client.data;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import common.dataModel.*;
import common.interfaces.client.IMainToData;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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;
public MainToDataImpl(DataCoreClient dataCoreClient) {
this.dataCoreClient = dataCoreClient;
}
public static void createUser(UserLight userLight, UserHeavy userHeavy) throws Exception {
String userFilePath = Configuration.SAVE_DIR + File.separator + userLight.getPseudo() + ".json";
File userFile = new File(userFilePath);
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) {
userJson.addProperty("dateOfBirth", (String) null);
} 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());
userJson.addProperty("password", userHeavy.getPassword());
userJson.addProperty("serverAddress", userHeavy.getServerAddress());
userJson.addProperty("serverPort", userHeavy.getServerPort());
userFile.getParentFile().mkdirs();
FileWriter fw = new FileWriter(userFilePath);
fw.write(userJson.toString());
fw.close();
}
@Override
public void selectGames(GameLight g) {
}
@Override
public void acceptanceAcquired(UUID u_id, Boolean validation) {
}
@Override
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.getDataToCom().addNewGameAvailable(gameCreated);
dataCoreClient.getDataToGame().getGameScreen(gameCreated, stageFx);
return stageFx;
}
@Override
public void updatedProfile(UserLight profile) {
}
@Override
public void requestToDataToSpecialGame(GameLight game, UserLight user, Stage stageFx) {
}
@Override
public void sendServerParams(String ip, int port) {
}
@Override
public ArrayList<GameLight> displaySavedGames(UUID id) {
return null;
}
@Override
public GameHeavy getSavedGame(UUID id) {
return null;
}
@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) {
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));
}
@Override
public void downloadUserProfile(String filePath) {
}
@Override
public void updateProfile(UserLight profileLight, UserHeavy profileHeavy) throws Exception {
String userFilePath = Configuration.SAVE_DIR + File.separator + dataCoreClient.getLocalUserHeavy().getPseudo()
+ ".json";
File userFile = new File(userFilePath);
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()) {
throw new Exception("Error while backuping old data");
}
userFile.renameTo(oldFile);
try {
createUser(profileLight, profileHeavy);
// dataCoreClient.getDataToCom().notifyUpdateProfile(profileLight); TO DISCUSS
// WITH COM
} catch (Exception e) {
System.out.println(e.getMessage());
oldFile.renameTo(userFile);
}
oldFile.delete();
}
@Override
public void identify(String login, String password, String address) throws Exception {
// blank playername or password
if (login.isEmpty() || password.isEmpty()) {
throw new Exception("Data error: due to empty login or password");
}
login(login, password);
dataCoreClient.getDataToCom().connectUserOnline(getUserLightFromFile(login), address);
}
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()) {
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());
return userLight;
}
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()) {
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);
return userHeavy;
}
public void login(String login, String password) throws Exception {
String path = Configuration.SAVE_DIR + File.separator + login + ".json";
File userFile = new File(path);
if (!userFile.exists()) {
throw new Exception("Data error: User profile does not exist"); // throw related error
} else {
UserLight userLight = getUserLightFromFile(login);
UserHeavy userHeavy = getUserHeavyFromFile(login);
if (!userHeavy.getPassword().equals(password)) {
throw new Exception("Data error: User password does not match"); // throw related error
} else {
dataCoreClient.setLocalUserHeavy(userHeavy);
dataCoreClient.setLocalUserLight(userLight);
dataCoreClient.getDataToMain().sendUserInfo(userLight, userHeavy);
}
}
}
@Override
public void disconnect() {
}
DataCoreClient dataCoreClient;
public MainToDataImpl(DataCoreClient dataCoreClient) {
this.dataCoreClient = dataCoreClient;
}
public static void createUser(UserLight userLight, UserHeavy userHeavy) throws Exception {
String userFilePath = Configuration.SAVE_DIR + File.separator + userLight.getPseudo() + ".json";
File userFile = new File(userFilePath);
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) {
userJson.addProperty("dateOfBirth", (String) null);
} 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());
userJson.addProperty("password", userHeavy.getPassword());
userJson.addProperty("serverAddress", userHeavy.getServerAddress());
userJson.addProperty("serverPort", userHeavy.getServerPort());
userFile.getParentFile().mkdirs();
FileWriter fw = new FileWriter(userFilePath);
fw.write(userJson.toString());
fw.close();
}
@Override
public void selectGames(GameLight g) {
}
@Override
public void acceptanceAcquired(UUID u_id, Boolean validation) {
}
@Override
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.getDataToCom().addNewGameAvailable(gameCreated);
dataCoreClient.getDataToGame().getGameScreen(gameCreated, stageFx);
return stageFx;
}
@Override
public void updatedProfile(UserLight profile) {
}
@Override
public void requestToDataToSpecialGame(GameLight game, UserLight user, Stage stageFx) {
}
@Override
public void sendServerParams(String ip, int port) {
}
@Override
public ArrayList<GameLight> displaySavedGames(UUID id) {
return null;
}
@Override
public GameHeavy getSavedGame(UUID id) {
return null;
}
@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) {
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));
}
@Override
public void downloadUserProfile(String filePath) {
}
@Override
public void updateProfile(UserLight profileLight, UserHeavy profileHeavy) throws Exception {
String userFilePath = Configuration.SAVE_DIR + File.separator + dataCoreClient.getLocalUserHeavy().getPseudo()
+ ".json";
File userFile = new File(userFilePath);
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()) {
throw new Exception("Error while backuping old data");
}
userFile.renameTo(oldFile);
try {
createUser(profileLight, profileHeavy);
// dataCoreClient.getDataToCom().notifyUpdateProfile(profileLight); TO DISCUSS
// WITH COM
} catch (Exception e) {
System.out.println(e.getMessage());
oldFile.renameTo(userFile);
}
oldFile.delete();
}
@Override
public void identify(String login, String password, String address) throws Exception {
// blank playername or password
if (login.isEmpty() || password.isEmpty()) {
throw new Exception("Data error: due to empty login or password");
}
login(login, password);
dataCoreClient.getDataToCom().connectUserOnline(getUserLightFromFile(login), address);
}
public UserLight getUserLightFromFile(String pseudo) throws Exception {
String path = Configuration.SAVE_DIR + File.separator + pseudo + ".json";
File userFile = new File(path);
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());
return userLight;
}
public UserHeavy getUserHeavyFromFile(String pseudo) throws Exception {
String path = Configuration.SAVE_DIR + File.separator + pseudo + ".json";
File userFile = new File(path);
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);
return userHeavy;
}
public void login(String login, String password) throws Exception {
String path = Configuration.SAVE_DIR + File.separator + login + ".json";
File userFile = new File(path);
if (!userFile.exists()) {
throw new Exception("Data error: User profile does not exist"); // throw related error
} else {
UserLight userLight = getUserLightFromFile(login);
UserHeavy userHeavy = getUserHeavyFromFile(login);
if (!userHeavy.getPassword().equals(password)) {
throw new Exception("Data error: User password does not match"); // throw related error
} else {
dataCoreClient.setLocalUserHeavy(userHeavy);
dataCoreClient.setLocalUserLight(userLight);
dataCoreClient.getDataToMain().sendUserInfo(userLight, userHeavy);
}
}
}
@Override
public void disconnect() {
}
}
package main.java.client.game;
package client.game;
import client.game.controller.MainGameController;
import client.game.model.DataToGameImpl;
import common.interfaces.client.IGameToData;
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.DataToGameImpl;
import main.java.common.interfaces.client.IGameToData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -69,7 +69,7 @@ public class GameCore {
fxmlLoader.setControllerFactory(c -> {
return controller;
});
BorderPane root = (BorderPane) fxmlLoader.load();
BorderPane root = fxmlLoader.load();
Scene scene = new Scene(root, 1200, 800);
scene.getStylesheets().add("view/game.css");
......
package main.java.client.game.controller;
import java.text.SimpleDateFormat;