From 674ac149355e9fe13e36e78d735c3fe00282f04e Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 7 Dec 2021 15:59:59 +0100 Subject: [PATCH] add dialog when game finish --- .../src/main/java/fr/lnl/game/client/App.java | 15 ++++++++++++--- .../game/client/listener/ButtonListener.java | 2 +- .../client/listener/DisplayWinnerEvent.java | 12 +++++++++++- .../java/fr/lnl/game/client/view/Terminal.java | 6 +++++- .../java/fr/lnl/game/client/view/View.java | 4 ++++ .../fr/lnl/game/client/view/ViewManager.java | 4 ++++ .../java/fr/lnl/game/client/view/Window.java | 18 +++++++++++++++--- .../java/fr/lnl/game/server/games/Game.java | 12 ++++++++++-- .../game/server/listener/GameFinishEvent.java | 14 +++++++------- .../game/server/listener/ModelListener.java | 2 +- .../src/test/java/fr/lnl/game/server/Mock.java | 2 +- .../fr/lnl/game/server/MockDisplayWinner.java | 10 +++++++++- 12 files changed, 80 insertions(+), 21 deletions(-) diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index 8565f25..c6e3767 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -1,5 +1,6 @@ package fr.lnl.game.client; +import fr.lnl.game.client.listener.DisplayWinnerEvent; import fr.lnl.game.client.view.AbstractView; import fr.lnl.game.client.view.Terminal; import fr.lnl.game.client.view.ViewManager; @@ -22,8 +23,8 @@ public class App extends Application { private static LinkedList argsList; public static HashMap playerList = new HashMap<>(); - public static Game game; - public static ViewManager viewManager; + private static Game game; + private static ViewManager viewManager; public static void main(String[] args) { argsList = new LinkedList<>(Arrays.asList(args)); @@ -46,7 +47,7 @@ public class App extends Application { List players = parsePlayers(); Grid grid = new Grid(12, 12, players); BuildStrategy buildStrategy = new LockStrategy(grid, 0.80F, 0.95F); - game = new Game(buildStrategy, players, new GameFinishEvent()); + game = new Game(buildStrategy, players, new DisplayWinnerEvent()); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } @@ -135,4 +136,12 @@ public class App extends Application { } return clazz; } + + public static ViewManager getViewManager() { + return viewManager; + } + + public static Game getGame() { + return game; + } } diff --git a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java index 860c1f3..18f0e99 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java +++ b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java @@ -35,7 +35,7 @@ public class ButtonListener extends AbstractModelListening { alert.setTitle("Un joueur ordinateur a joué"); alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué"); alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); - App.viewManager.updateView(); + App.getViewManager().updateView(); alert.showAndWait(); } } diff --git a/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java b/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java index be77b9b..3862cf6 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java +++ b/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java @@ -1,2 +1,12 @@ -package fr.lnl.game.client.listener;public class displayWinnerEvent { +package fr.lnl.game.client.listener; + +import fr.lnl.game.client.App; +import fr.lnl.game.server.listener.AbstractModelListening; + +public class DisplayWinnerEvent extends AbstractModelListening { + + @Override + public void updateModel(Object obj) { + App.getViewManager().displayWinner(App.getGame().getWinner()); + } } diff --git a/client/src/main/java/fr/lnl/game/client/view/Terminal.java b/client/src/main/java/fr/lnl/game/client/view/Terminal.java index 5066261..b4a916c 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Terminal.java +++ b/client/src/main/java/fr/lnl/game/client/view/Terminal.java @@ -10,8 +10,12 @@ public class Terminal extends AbstractView { } public void show() { - // TODO: 26/10/2021 System.out.println(game.getGrid().toString()); } + @Override + public void displayWinner(Player winner) { + System.out.println("Le joueur " + winner + " a gagné la partie"); + } + } diff --git a/client/src/main/java/fr/lnl/game/client/view/View.java b/client/src/main/java/fr/lnl/game/client/view/View.java index 1f4512f..17381eb 100644 --- a/client/src/main/java/fr/lnl/game/client/view/View.java +++ b/client/src/main/java/fr/lnl/game/client/view/View.java @@ -1,6 +1,10 @@ package fr.lnl.game.client.view; +import fr.lnl.game.server.games.player.Player; + public interface View { void show(); + + void displayWinner(Player winner); } diff --git a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java index eac0fdf..164ad3c 100644 --- a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java +++ b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java @@ -35,4 +35,8 @@ public class ViewManager { System.out.println("Il a joué l'action: " + game.getSelectedAction()); } } + + public void displayWinner(Player winner) { + players.get(game.getCurrentPlayer()).getView().displayWinner(winner); + } } diff --git a/client/src/main/java/fr/lnl/game/client/view/Window.java b/client/src/main/java/fr/lnl/game/client/view/Window.java index d821314..9a50adc 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Window.java +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -1,5 +1,6 @@ package fr.lnl.game.client.view; +import fr.lnl.game.client.App; import fr.lnl.game.client.listener.ButtonListener; import fr.lnl.game.client.listener.ClientEventHandler; import fr.lnl.game.server.games.Game; @@ -10,6 +11,7 @@ import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; @@ -42,6 +44,16 @@ public class Window extends AbstractView { stage.show(); } + @Override + public void displayWinner(Player winner) { + Alert alert = new Alert(Alert.AlertType.INFORMATION); + alert.setTitle("Fin du jeu"); + alert.setHeaderText("La partie est terminé"); + alert.setContentText("Un joueur " + winner + " a gagné"); + App.getViewManager().updateView(); + alert.showAndWait(); + } + private Parent createContent() { Pane principalPane = new Pane(); principalPane.setPrefSize(width * cellSize, height * cellSize); // TODO: 04/12/2021 A corriger -> doit plutôt s'adapter à la taille de la grid (grid.getRow() et grid.getColumn()) @@ -55,12 +67,12 @@ public class Window extends AbstractView { for (int i = 0; i < grid.getRow(); i++) { for (int j = 0; j < grid.getColumn(); j++) { Pair value = grid.getBoard().get(new Point(i, j)); + if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Explosive) { + addToPrincipalPanel(value.getB(), principalPane, i, j); + } if (value.getA() != null) { addToPrincipalPanel(value.getA(), principalPane, i, j); } - if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Mine || value.getB() instanceof Bomb) { - addToPrincipalPanel(value.getB(), principalPane, i, j); - } } } putStatePlayerPane(principalPane); diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index 5d82d3f..b4d281c 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -6,6 +6,8 @@ import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.elements.CountdownBox; import fr.lnl.game.server.games.player.ComputerPlayer; import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.listener.AbstractModelListening; +import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.listener.ModelListener; import java.util.ArrayList; @@ -19,10 +21,11 @@ public class Game { private final Grid grid; private final List players; private final ModelListener gameFinishEvent; + private final AbstractModelListening displayWinnerEvent; private Player currentPlayer; private Action selectedAction = null; - public Game(BuildStrategy buildStrategy, List players, ModelListener gameFinishEvent) throws IllegalArgumentException { + public Game(BuildStrategy buildStrategy, List players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException { this.grid = buildStrategy.getGrid(); if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); @@ -32,7 +35,8 @@ public class Game { this.buildStrategy = buildStrategy; this.players = players; this.currentPlayer = players.get(0); - this.gameFinishEvent = gameFinishEvent; + this.gameFinishEvent = new GameFinishEvent(this); + this.displayWinnerEvent = displayWinnerEvent; initGame(); } @@ -125,4 +129,8 @@ public class Game { public Action getSelectedAction() { return selectedAction; } + + public AbstractModelListening getDisplayWinnerEvent() { + return displayWinnerEvent; + } } \ No newline at end of file diff --git a/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java b/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java index c706c06..dc7c5f0 100644 --- a/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java +++ b/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java @@ -1,5 +1,6 @@ package fr.lnl.game.server.listener; +import fr.lnl.game.server.games.Game; import fr.lnl.game.server.utils.CrashException; import java.util.concurrent.TimeUnit; @@ -7,17 +8,16 @@ import java.util.concurrent.TimeUnit; public class GameFinishEvent extends AbstractModelListening { private final Object lock = new Object(); + private final Game game; + + public GameFinishEvent(Game game) { + this.game = game; + } // TODO: 02/12/2021 méthode a revoir, notamment surement mettre un dialog sur la view Window et fermer dès que l'utilisateur a cliqué sur ok @Override public void updateModel(Object obj) { - synchronized (lock) { - try { - Thread.sleep(TimeUnit.SECONDS.toMillis(1)); - } catch (InterruptedException e) { - throw new CrashException(e.getMessage(), e); - } - } + game.getDisplayWinnerEvent().updateModel(null); System.exit(0); } } diff --git a/server/src/main/java/fr/lnl/game/server/listener/ModelListener.java b/server/src/main/java/fr/lnl/game/server/listener/ModelListener.java index 437bb2d..6c71cc9 100644 --- a/server/src/main/java/fr/lnl/game/server/listener/ModelListener.java +++ b/server/src/main/java/fr/lnl/game/server/listener/ModelListener.java @@ -1,6 +1,6 @@ package fr.lnl.game.server.listener; public interface ModelListener { - //à peut-être revoir + void updateModel(Object obj); } diff --git a/server/src/test/java/fr/lnl/game/server/Mock.java b/server/src/test/java/fr/lnl/game/server/Mock.java index 5bf0c0c..d8f6bc9 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -25,7 +25,7 @@ public class Mock { List players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT), new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT)); this.buildStrategy = new LockStrategy(new Grid(16,16, players),0.80F, 0.95F); - game = new Game(buildStrategy, players, new GameFinishEvent()); + game = new Game(buildStrategy, players, new MockDisplayWinner()); this.grid = buildStrategy.getGrid(); } diff --git a/server/src/test/java/fr/lnl/game/server/MockDisplayWinner.java b/server/src/test/java/fr/lnl/game/server/MockDisplayWinner.java index 6f91331..cfb7b5f 100644 --- a/server/src/test/java/fr/lnl/game/server/MockDisplayWinner.java +++ b/server/src/test/java/fr/lnl/game/server/MockDisplayWinner.java @@ -1,2 +1,10 @@ -package fr.lnl.game.server;public class MockDisplayWinner { +package fr.lnl.game.server; + +import fr.lnl.game.server.listener.AbstractModelListening; + +public class MockDisplayWinner extends AbstractModelListening { + @Override + public void updateModel(Object obj) { + + } }