Commit 0397aadb authored by Thomas Lecluse's avatar Thomas Lecluse
Browse files

Merge branch 'feature/game/init' into 'int/v2'

Feature/game/init

See merge request !26
parents 950d4a14 fa1036b8
......@@ -62,7 +62,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>baleine.client.ClientApp</mainClass>
<mainClass>main.java.Main</mainClass>
</configuration>
</plugin>
<plugin>
......@@ -70,14 +70,14 @@
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>baleine.client.ClientApp</mainClass>
<mainClass>main.java.Main</mainClass>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<targetPath>baleine/client/main/fxml</targetPath>
<directory>src/baleine/client/main/fxml</directory>
<targetPath>main/java/client/main/fxml</targetPath>
<directory>src/main/java/client/main/fxml</directory>
<includes>
<include>**/*.fxml</include>
</includes>
......
package baleine.client;
import baleine.client.data.DataCoreClient;
import baleine.client.game.controller.TransApplicationController;
import baleine.client.game.view.TransApplicationView;
import baleine.client.network.ComCoreClient;
import baleine.client.main.controller.MainCore;
import baleine.client.main.controller.MainControllerFX;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.File;
/**
* Main application
*
* @author thomas
*/
public class ClientApp extends Application {
private static ComCoreClient comCoreClient;
private static DataCoreClient dataCoreClient;
public static MainCore mainCore = new MainCore(new MainControllerFX());
public static final String APPLICATION_NAME = "Othello";
public static final int APPLICATION_WIDTH = 889;
public static final int APPLICATION_HEIGHT = 500;
//css definition
private static final File cssFile = new File("src/baleine/client/main/ressources/main.css");
public static final String CSS_FILE_PATH = "file:///" + ClientApp.cssFile.getAbsolutePath().replace("\\", "/");
/**
* Run the client app (JavaFX)
*
* @param args additional args
* @throws Exception TODO: remove once the Websocket is initialized in the proper class
*/
public static void main(String[] args) {
initCores();
// TODO: call this method once user is connected to a server in ihm main.
comCoreClient.initWebSocketConnection("localhost", 8080);
launch(args);
}
private static void initCores() {
comCoreClient = new ComCoreClient();
dataCoreClient = new DataCoreClient();
dataCoreClient.setDataToCom(comCoreClient.getDataToComClientImpl());
dataCoreClient.setDataToMain(mainCore.getDataToMainImpl());
comCoreClient.setComToDataClient(dataCoreClient.getClientComToDataImpl());
mainCore.setMainToData(dataCoreClient.getMainToDataImpl());
}
@Override
public void start(Stage stage) throws Exception{
System.out.println(ClientApp.APPLICATION_NAME + " is starting...");
ClientApp.mainCore.getFX().initialize("../fxml/login.fxml", stage);
}
}
package baleine.client.game.controller;
import java.io.File;
import baleine.client.game.model.TransApplicationModel;
import baleine.client.game.model.TransItem;
import baleine.client.game.view.CardShape;
import baleine.client.game.view.TransApplicationView;
import javafx.collections.SetChangeListener;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
public class TransApplicationController {
/**
* Main application view
*/
private TransApplicationView mainView;
/**
* Application model
*/
private TransApplicationModel model;
/**
* Class constructor
*
* @param view : application view
*/
public TransApplicationController(TransApplicationView view) {
mainView = view;
model = new TransApplicationModel();
initViewActions();
initModelBiding();
}
/**
* Actions mapping of all components in the view
*/
private void initViewActions() {
mainView.openButton.setOnAction(evt -> actionOpenFile());
}
/**
* Model change listener
*/
private void initModelBiding() {
// Définition de la fonction de réponse
SetChangeListener<TransItem> f;
f = change -> {
if (change.wasRemoved()) {
mainView.cardsTilePane.getChildren().clear();
}
if (change.wasAdded()) {
mainView.cardsTilePane.getChildren().add(new CardShape(change.getElementAdded()));
}
};
// Installation de la fonction
model.transItemsSet.addListener(f);
}
/**
* Method called on openButton click
*/
private void actionOpenFile() {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text file", "*.txt"));
fileChooser.setInitialDirectory(new File(System.getProperty("user.dir") + "/src/td2/transFiles"));
File f = fileChooser.showOpenDialog(new Stage());
if (f != null) {
model.parse(f);
}
}
}
package baleine.client.game.model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class TransApplicationModel {
public ObservableSet<TransItem> transItemsSet;
public TransApplicationModel() {
transItemsSet = FXCollections.observableSet();
}
/**
* Parsing of the specified file
*
* @param f
*/
public void parse(File f) {
transItemsSet.clear();
try {
List<String> lines = Files.readAllLines(Paths.get(f.getAbsolutePath()));
for (String l : lines) {
String[] content = l.replace(" ", "").split("=");
transItemsSet.add(new TransItem(content[0], content[1]));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package baleine.client.game.model;
public class TransItem {
private String word;
private String translation;
/**
* Constructor using a word and its translation
*
* @param w : word
* @param t : word's translation
*/
public TransItem(String w, String t) {
word = w;
translation = t;
}
// Getters
public final String getWord() {
return word;
}
public final String getTranslation() {
return translation;
}
}
package baleine.client.game.view;
import baleine.client.game.model.TransItem;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
public class CardShape extends StackPane {
private TransItem relatedItem;
private boolean isTranslated;
private Text currentText;
private Rectangle rect;
private static final Color REGULAR_COLOR = Color.BURLYWOOD;
private static final Color SECONDARY_COLOR = Color.CORNFLOWERBLUE;
/**
* Constructor
*
* @param item : related TransItem
*/
public CardShape(TransItem item) {
relatedItem = item;
isTranslated = false; // default value
initHmi();
new CardController().bind();
}
/**
* HMI initialization
*/
private void initHmi() {
rect = new Rectangle(147, 60);
rect.setFill(Color.BURLYWOOD);
rect.setStroke(Color.WHITESMOKE);
rect.setStrokeWidth(3);
currentText = new Text(relatedItem.getWord());
getChildren().addAll(rect, currentText);
}
/**
* Changes the displayed text
*/
private void switchToTranslation() {
isTranslated = !isTranslated;
currentText.setText(isTranslated ? relatedItem.getTranslation() : relatedItem.getWord());
// Change color
if (rect.getFill().equals(REGULAR_COLOR)) {
rect.setFill(SECONDARY_COLOR);
} else {
rect.setFill(REGULAR_COLOR);
}
}
/**
* CardShape controller
*
* @author thomas
*/
private class CardController {
/**
* Binds the clic on the card to the language switch
*/
private void bind() {
currentText.setOnMouseClicked(evt -> switchToTranslation());
rect.setOnMouseClicked(evt -> switchToTranslation());
}
}
}
package baleine.client.game.view;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
/**
* Main application view
*/
public class TransApplicationView extends BorderPane {
public Button openButton;
public TilePane cardsTilePane;
/**
* Main application view constructor
*/
public TransApplicationView() {
super();
initToolBar();
initTilePane();
}
/**
* Application toolbar initialization, places the toolbar at the top
*/
private void initToolBar() {
openButton = new Button("Ouvrir");
ToolBar bar = new ToolBar(openButton);
setTop(bar);
}
/**
* Application cards panel
*/
private void initTilePane() {
cardsTilePane = new TilePane();
setCenter(cardsTilePane);
}
}
package baleine.client;
import baleine.client.game.controller.TransApplicationController;
import baleine.client.game.view.TransApplicationView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* Main application
*
* @author thomas
*/
public class old_ClientApp extends Application {
private TransApplicationView view;
private TransApplicationController controller;
@Override
public void start(Stage stage) throws Exception {
view = new TransApplicationView();
controller = new TransApplicationController(view);
// Application content display
stage.setTitle("Translator");
stage.setScene(new Scene(view, 600, 400));
stage.setResizable(false);
stage.show();
}
/**
* Run
*
* @param args
*/
public static void main(String[] args) {
launch(args);
}
}
package baleine.common.interfaces.client;
import baleine.common.dataModel.UserZero;
import java.util.UUID;
public interface IGameToData {
void getPlayer(UUID id);
void askGame(UUID gameId, UUID userId);
void newMessage(String m, UUID pid);
void playMove(int x, int y, UserZero u);
}
package baleine.server;
import baleine.server.data.DataCoreServer;
import baleine.server.network.ComCoreServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Class running the server app
*/
@SpringBootApplication
public class ServerApp {
private static ComCoreServer comCoreServer;
private static DataCoreServer dataCoreServer;
/**
* Run the server app
*
* @param args additional args
*/
public static void main(String[] args) {
initCores();
comCoreServer.initWebSocketConnection();
SpringApplication.run(ServerApp.class, args);
}
private static void initCores() {
comCoreServer = new ComCoreServer();
dataCoreServer = new DataCoreServer();
dataCoreServer.setDataToCom(comCoreServer.getDataToComServerImpl());
comCoreServer.setComToDataImpl(dataCoreServer.getComToDataServerImpl());
}
}
package baleine.server.data;
import baleine.common.dataModel.Board;
import baleine.common.dataModel.GameHeavy;
import baleine.common.dataModel.Move;
import baleine.common.dataModel.UserLight;
import baleine.common.interfaces.server.IDataToCom;
import java.util.List;
public class DataCoreServer {
List<UserLight> listConnectedUsers;
List<GameHeavy> listGameHeavy;
ComToDataServerImpl comToDataServerImpl;
IDataToCom dataToCom;
public DataCoreServer() {
comToDataServerImpl = new ComToDataServerImpl(this);
}
public ComToDataServerImpl getComToDataServerImpl() {
return comToDataServerImpl;
}
public void setDataToCom(IDataToCom dataToCom) {
this.dataToCom = dataToCom;
}
private boolean isMoveValid(Move playerMove, Board boardGame) {
return false;
}
}
package main.java;
import main.java.client.ClientApp;
/**
* Main application (use following arguments to run) <br>
* <ul>
* <li><b>-client</b> to run ClientApp
* <li><b>-server</b> to run ServerApp
* </ul>
*/
public class Main {
/**
* Option used to launch server application
*/
private static final String SERVER_ARG_OPTION = "-server";
/**
* Option used to launch client application
*/
private static final String CLIENT_ARG_OPTION = "-client";
/**
* Main Application entry point
*
* @param args
*/
public static void main(String[] args) {
/**
* If we have an argument, we look for which application to run (server or
* client)
*/
if (args.length > 0) {
if (args[0].equals(SERVER_ARG_OPTION)) {
runServerApp(args);
} else if (args[0].equals(CLIENT_ARG_OPTION)) {
runClientApp(args);
}
} else { // Default application is the client
runClientApp(args);
}
}
/**
* Calls the main method of the server application
*
* @param args
*/
private static void runServerApp(String[] args) {
}
/**
* Calls the main method of the client application
*
* @param args
*/
private static void runClientApp(String[] args) {
ClientApp.main(args);
}
}
package main.java.client;
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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;
/**
* Main Client application
*/
public class ClientApp extends Application {
private static final Logger LOGGER = LogManager.getLogger(ClientApp.class);
private static ComCoreClient comCoreClient;
private static DataCoreClient dataCoreClient;
public static MainCore mainCore;
private static GameCore gameCore;
public static final String APPLICATION_NAME = "Othello";
public static final int APPLICATION_WIDTH = 889;
public static final int APPLICATION_HEIGHT = 500;
// css definition
private static final File cssFile = new File("src/baleine/client/main/ressources/main.css");
public static final String CSS_FILE_PATH = "file:///" + ClientApp.cssFile.getAbsolutePath().replace("\\", "/");
/**
* Run the client app (JavaFX)
*
* @param args additional args
* @throws Exception TODO: remove once the Websocket is initialized in the
* proper class
*/
public static void main(String[] args) {
LOGGER.debug("ClientApp");
initCores();
// TODO: call this method once user is connected to a server in ihm main.
comCoreClient.initWebSocketConnection("localhost", 8080);
launch(args);
}
private static void initCores() {
// Com Core
comCoreClient = new ComCoreClient();
// Data Core
dataCoreClient = new DataCoreClient();
// IHM Game Core
gameCore = new GameCore(new MainGameController());