Skip to content

Commit

Permalink
Merge pull request #36 from LBF38:Debug/multiplayer
Browse files Browse the repository at this point in the history
bugfix on server connection
  • Loading branch information
LBF38 authored Mar 13, 2023
2 parents dce3256 + af81d9e commit f28fc1f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,29 @@ public class PlayerComponent extends Component {
private Direction side_shoot = Direction.LEFT;
private Settings.Direction direction = Settings.Direction.UP;
private int id;
private static int counter = 1;
private static int counter;
private int score = 0;
private int life = 5;

/**
* Permet de récupérer l'identifiant du composant
*
* @return un entier quelconque
*/
public int getId() {
return id;
}

/**
* Permet de récupérer le numéro du joueur (1 ou 2), quelque soit l'id du
* composant.
*
* @return 1 ou 2, en fonction de l'id du composant
*/
public int getPlayerId() {
return id % 2 + 1;
}

public PlayerComponent() {
super();
this.id = counter++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static com.almasb.fxgl.dsl.FXGL.getNotificationService;
import static com.almasb.fxgl.dsl.FXGL.getb;
import static com.almasb.fxgl.dsl.FXGL.run;
import static com.almasb.fxgl.dsl.FXGL.runOnce;
import static com.almasb.fxgl.dsl.FXGL.showConfirm;
import static org.enstabretagne.UI.UI_Factory.gameOverScreen;
import static org.enstabretagne.UI.UI_Factory.winScreen;
Expand Down Expand Up @@ -57,6 +56,9 @@ private class BundleType {
}

private boolean isServer = false;
private boolean newGame = true;
private long clientConnectionAttempt = System.currentTimeMillis();
private int clientConnectionWaitingTime = 15000;
private Server<Bundle> server;
private Client<Bundle> client;
private int serverPort = 55555;
Expand All @@ -65,7 +67,6 @@ private class BundleType {
@Override
public void initGameMode() {
super.initTwoPlayerGameMode();
dialogQueue().play();
}

@Override
Expand All @@ -81,7 +82,7 @@ public PlayerComponent getPlayerComponent2() {

@Override
public PlayerComponent getPlayerComponent1() {
if (GameVariableNames.isShooting) {
if (GameVariableNames.isShooting && GameVariableNames.multiplayerGameInProgress) {
onShootBroadcastLogic();
}
if (isServer) {
Expand All @@ -93,16 +94,38 @@ public PlayerComponent getPlayerComponent1() {

@Override
public void onUpdate(double tpf) {
if (newGame) {
dialogQueue().play();
newGame = false;
}
if (GameVariableNames.multiplayerGameInProgress) {
onUpdateBroadcastLogic();
super.onUpdate(tpf);
aliensShootInPlayersDirection();
} else {
// Synchronise le début de la partie entre les deux joueurs
if (!isServer && GameVariableNames.multiplayerGameWaiting) {
client.broadcast(new Bundle(BundleType.CLIENT_CONNECTED));
return;
}
// Synchronise le début de la partie entre les deux joueurs
if (!isServer && GameVariableNames.multiplayerGameWaiting) {
client.broadcast(new Bundle(BundleType.CLIENT_CONNECTED));
boolean isConnectionAttempt = System.currentTimeMillis()
- clientConnectionAttempt > clientConnectionWaitingTime;
if (client.getConnections().size() == 0
&& isConnectionAttempt) {
clientConnectionAttempt = System.currentTimeMillis();
Logger.get(getClass()).warning("Client failed to connect to server ! ");
getNotificationService().pushNotification("Client failed to connect to server ! ");
getDialogService().showConfirmationBox("Connection failed !\nDo you want to try again ?",
(yes) -> {
if (yes) {
client.connectAsync();
getGameController().gotoPlay();
} else
getGameController().gotoMainMenu();
});
} else if (isConnectionAttempt) {
Logger.get(getClass()).info("Client connected to server !");
getNotificationService().pushNotification("Connected to the server !");
}
runOnce(() -> waitingForConnection(), Duration.seconds(3));
}
}

Expand All @@ -116,6 +139,7 @@ public void gameFinished() {
GameEndBroadcastLogic(BundleType.GAME_WIN);
winScreen(playerComponent1.getScore(), playerComponent2.getScore());
}
newGame = true;
}

/**
Expand All @@ -131,34 +155,6 @@ private void onUpdateBroadcastLogic() {
}
}

/**
* Wait for a connexion to be established
*/
private void waitingForConnection() {
if (isServer) {
if (server.getConnections().size() == 0) {
getDialogService().showConfirmationBox("En attente d'un joueur...\n"
+ "Voulez-vous quitter la partie ?", (yes) -> {
if (yes) {
server.stop();
getGameController().gotoMainMenu();
}
});
}
}
if (client == null)
return;
if (client.getConnections().size() == 0) {
getDialogService().showConfirmationBox(
"En attente de la connexion...\n" + "Voulez-vous quitter la partie ?", (yes) -> {
if (yes) {
client.disconnect();
getGameController().gotoMainMenu();
}
});
}
}

private void aliensShootInPlayersDirection() {
run(() -> {
getGameWorld().getEntitiesByType(EntityType.ALIEN).forEach((alien) -> {
Expand Down Expand Up @@ -342,8 +338,6 @@ private void initializeClient() {
onReceiveMessageClient();
client.connectAsync();
GameVariableNames.multiplayerGameWaiting = true;
Logger.get(getClass()).info("Client connected to server !");
getNotificationService().pushNotification("Connected to the server !");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.almasb.fxgl.dsl.FXGL.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand All @@ -25,17 +26,17 @@
public class UI_Factory {
private static VBox playersUI = new VBox();

public static VBox showPlayersLivesAndScores(GameWorld gameWorld,GameScene gameScene) {
public static VBox showPlayersLivesAndScores(GameWorld gameWorld, GameScene gameScene) {
if (getGameScene().getContentRoot().getChildren().contains(playersUI))
getGameScene().removeChild(playersUI);
// System.out.println("Players Lives and Scores");

List<HBox> playersViews = new ArrayList<>();
List<PlayerComponent> players = gameWorld.getEntitiesByType(EntityType.PLAYER).stream()
.map(player -> player.getComponent(PlayerComponent.class)).collect(Collectors.toList());
players.sort(Comparator.comparing(PlayerComponent::getPlayerId));
for (PlayerComponent playerComponent : players) {
HBox scoreUI = createScoreUI(playerComponent.getScore(), playerComponent.getId());
scoreUI.setTranslateY(scoreUI.getHeight() * playerComponent.getId());
HBox scoreUI = createScoreUI(playerComponent.getScore(), playerComponent.getPlayerId());
scoreUI.setTranslateY(scoreUI.getHeight() * playerComponent.getPlayerId());
HBox lifeUI = createLifeUI(playerComponent.getLife());
var playerUI = new HBox(30, scoreUI, lifeUI);
playersViews.add(playerUI);
Expand Down

0 comments on commit f28fc1f

Please sign in to comment.