Added connectedLayer with a playerList

This commit is contained in:
Bas de Jong
2025-10-06 19:52:31 +02:00
parent 0d771088e1
commit 98755b6bed
4 changed files with 84 additions and 16 deletions

View File

@@ -95,7 +95,11 @@ public final class App extends Application {
final int childrenCount = root.getChildren().size(); final int childrenCount = root.getChildren().size();
for (int i = 0; i < childrenCount; i++) { for (int i = 0; i < childrenCount; i++) {
try {
root.getChildren().removeLast(); root.getChildren().removeLast();
} catch (Exception e) {
IO.println(e);
}
} }
stack.removeAllElements(); stack.removeAllElements();

View File

@@ -0,0 +1,57 @@
package org.toop.app.layer.layers;
import org.toop.app.layer.Layer;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.networking.events.NetworkEvents;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.List;
public final class ConnectedLayer extends Layer {
long clientId;
String user;
List<String> onlinePlayers = new CopyOnWriteArrayList<>();
public ConnectedLayer(long clientId, String user) {
super("multiplayer.css");
this.clientId = clientId;
this.user = user;
reload();
new EventFlow().addPostEvent(new NetworkEvents.SendLogin(this.clientId, this.user)).postEvent();
new Thread(this::populatePlayerList).start();
}
private void populatePlayerList() {
EventFlow sendGetPlayerList = new EventFlow().addPostEvent(new NetworkEvents.SendGetPlayerlist(this.clientId));
new EventFlow().listen(NetworkEvents.PlayerlistResponse.class, e -> {
if (e.clientId() == this.clientId) {
List<String> playerList = new java.util.ArrayList<>(List.of(e.playerlist())); // TODO: Garbage, but works
playerList.removeIf(name -> name.equalsIgnoreCase(user));
if (this.onlinePlayers != playerList) {
this.onlinePlayers.clear();
this.onlinePlayers.addAll(playerList);
}
}
});
TimerTask task = new TimerTask() {
public void run() {
sendGetPlayerList.postEvent();
}
};
Timer pollTimer = new Timer();
pollTimer.schedule(task, 0L, 5000L);
}
@Override
public void reload() {
popAll();
}
}

View File

@@ -1,5 +1,6 @@
package org.toop.app.layer.layers; package org.toop.app.layer.layers;
import javafx.application.Platform;
import org.toop.app.App; import org.toop.app.App;
import org.toop.app.GameInformation; import org.toop.app.GameInformation;
import org.toop.app.layer.Container; import org.toop.app.layer.Container;
@@ -7,6 +8,8 @@ import org.toop.app.layer.Layer;
import org.toop.app.layer.containers.HorizontalContainer; import org.toop.app.layer.containers.HorizontalContainer;
import org.toop.app.layer.containers.VerticalContainer; import org.toop.app.layer.containers.VerticalContainer;
import org.toop.app.layer.layers.game.TicTacToeLayer; import org.toop.app.layer.layers.game.TicTacToeLayer;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.local.AppContext; import org.toop.local.AppContext;
import javafx.geometry.Pos; import javafx.geometry.Pos;
@@ -27,6 +30,7 @@ public final class MultiplayerLayer extends Layer {
private String serverIP = ""; private String serverIP = "";
private String serverPort = ""; private String serverPort = "";
private long clientId = -1;
public MultiplayerLayer() { public MultiplayerLayer() {
super("multiplayer.css"); super("multiplayer.css");
@@ -59,12 +63,20 @@ public final class MultiplayerLayer extends Layer {
playersContainer.addContainer(player2Container, true); playersContainer.addContainer(player2Container, true);
mainContainer.addButton(isConnectionLocal? AppContext.getString("start") : AppContext.getString("connect"), () -> { mainContainer.addButton(isConnectionLocal? AppContext.getString("start") : AppContext.getString("connect"), () -> {
App.activate(new TicTacToeLayer(new GameInformation( // App.activate(new TicTacToeLayer(new GameInformation(
new String[] { player1Name, player2Name }, // new String[] { player1Name, player2Name },
new boolean[] { isPlayer1Human, isPlayer2Human }, // new boolean[] { isPlayer1Human, isPlayer2Human },
new int[] { computer1Difficulty, computer2Difficulty }, // new int[] { computer1Difficulty, computer2Difficulty },
isConnectionLocal, "127.0.0.1", "7789"))); // isConnectionLocal, "127.0.0.1", "7789")));
// serverIP, serverPort)));
new EventFlow()
.addPostEvent(NetworkEvents.StartClient.class, serverIP, Integer.parseInt(serverPort))
.onResponse(NetworkEvents.StartClientResponse.class,
e -> Platform.runLater(
() -> App.activate(new ConnectedLayer(e.clientId(), player1Name))
))
.postEvent();
}); });
player1Container.addToggle(AppContext.getString("human"), AppContext.getString("computer"), !isPlayer1Human, (computer) -> { player1Container.addToggle(AppContext.getString("human"), AppContext.getString("computer"), !isPlayer1Human, (computer) -> {

View File

@@ -179,16 +179,10 @@ public final class TicTacToeLayer extends Layer {
.listen(this::handleReceivedMessage) .listen(this::handleReceivedMessage)
.listen(this::onMoveResponse); .listen(this::onMoveResponse);
new EventFlow().addPostEvent(new NetworkEvents.SendLogin(clientId, information.playerName()[0]))
.postEvent();
new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, "tic-tac-toe"))
.postEvent();
while (running) { while (running) {
try { try {
Thread.sleep(250); Thread.sleep(250);
}catch (InterruptedException exception) {} } catch (InterruptedException exception) {}
boolean hasStarted = gameHasStarted.get(); boolean hasStarted = gameHasStarted.get();
if (hasStarted) { if (hasStarted) {
onlineGameState.firstPlayerIsMe = firstPlayerIsMe.get(); onlineGameState.firstPlayerIsMe = firstPlayerIsMe.get();
@@ -227,7 +221,8 @@ public final class TicTacToeLayer extends Layer {
} }
} }
} }
private void drawSymbol(Game.Move move){
private void drawSymbol(Game.Move move) {
if (move.value() == 'X') { if (move.value() == 'X') {
canvas.drawX(Color.RED, move.position()); canvas.drawX(Color.RED, move.position());
} else if (move.value() == 'O') { } else if (move.value() == 'O') {
@@ -251,7 +246,7 @@ public final class TicTacToeLayer extends Layer {
|| !resp.player().equals(information.playerName()[0]) && !firstPlayerIsMe.get()) { || !resp.player().equals(information.playerName()[0]) && !firstPlayerIsMe.get()) {
playerChar = 'X'; playerChar = 'X';
} }
else{ else {
playerChar = 'O'; playerChar = 'O';
} }
Game.Move move =new Game.Move(Integer.parseInt(resp.move()),playerChar); Game.Move move =new Game.Move(Integer.parseInt(resp.move()),playerChar);