mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Added connectedLayer with a playerList
This commit is contained in:
@@ -95,7 +95,11 @@ public final class App extends Application {
|
||||
final int childrenCount = root.getChildren().size();
|
||||
|
||||
for (int i = 0; i < childrenCount; i++) {
|
||||
root.getChildren().removeLast();
|
||||
try {
|
||||
root.getChildren().removeLast();
|
||||
} catch (Exception e) {
|
||||
IO.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
stack.removeAllElements();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.toop.app.layer.layers;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import org.toop.app.App;
|
||||
import org.toop.app.GameInformation;
|
||||
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.VerticalContainer;
|
||||
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 javafx.geometry.Pos;
|
||||
@@ -27,6 +30,7 @@ public final class MultiplayerLayer extends Layer {
|
||||
|
||||
private String serverIP = "";
|
||||
private String serverPort = "";
|
||||
private long clientId = -1;
|
||||
|
||||
public MultiplayerLayer() {
|
||||
super("multiplayer.css");
|
||||
@@ -59,12 +63,20 @@ public final class MultiplayerLayer extends Layer {
|
||||
playersContainer.addContainer(player2Container, true);
|
||||
|
||||
mainContainer.addButton(isConnectionLocal? AppContext.getString("start") : AppContext.getString("connect"), () -> {
|
||||
App.activate(new TicTacToeLayer(new GameInformation(
|
||||
new String[] { player1Name, player2Name },
|
||||
new boolean[] { isPlayer1Human, isPlayer2Human },
|
||||
new int[] { computer1Difficulty, computer2Difficulty },
|
||||
isConnectionLocal, "127.0.0.1", "7789")));
|
||||
// serverIP, serverPort)));
|
||||
// App.activate(new TicTacToeLayer(new GameInformation(
|
||||
// new String[] { player1Name, player2Name },
|
||||
// new boolean[] { isPlayer1Human, isPlayer2Human },
|
||||
// new int[] { computer1Difficulty, computer2Difficulty },
|
||||
// isConnectionLocal, "127.0.0.1", "7789")));
|
||||
|
||||
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) -> {
|
||||
|
||||
@@ -179,16 +179,10 @@ public final class TicTacToeLayer extends Layer {
|
||||
.listen(this::handleReceivedMessage)
|
||||
.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) {
|
||||
try {
|
||||
Thread.sleep(250);
|
||||
}catch (InterruptedException exception) {}
|
||||
} catch (InterruptedException exception) {}
|
||||
boolean hasStarted = gameHasStarted.get();
|
||||
if (hasStarted) {
|
||||
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') {
|
||||
canvas.drawX(Color.RED, move.position());
|
||||
} else if (move.value() == 'O') {
|
||||
@@ -251,7 +246,7 @@ public final class TicTacToeLayer extends Layer {
|
||||
|| !resp.player().equals(information.playerName()[0]) && !firstPlayerIsMe.get()) {
|
||||
playerChar = 'X';
|
||||
}
|
||||
else{
|
||||
else {
|
||||
playerChar = 'O';
|
||||
}
|
||||
Game.Move move =new Game.Move(Integer.parseInt(resp.move()),playerChar);
|
||||
|
||||
Reference in New Issue
Block a user