can start game from playerlist screen

This commit is contained in:
Ticho Hidding
2025-10-27 14:57:15 +01:00
parent bd096df2c2
commit b506afdade
3 changed files with 50 additions and 32 deletions

View File

@@ -40,8 +40,8 @@ public final class Server {
return GameInformation.Type.REVERSI;
} else if (game.equalsIgnoreCase("connect4")) {
return GameInformation.Type.CONNECT4;
} else if (game.equalsIgnoreCase("battleship")) {
return GameInformation.Type.BATTLESHIP;
// } else if (game.equalsIgnoreCase("battleship")) {
// return GameInformation.Type.BATTLESHIP;
}
return null;
@@ -62,7 +62,7 @@ public final class Server {
return;
}
if (user.isEmpty()) {
if (user.isEmpty() || user.matches("^[0-9].*")) {
ViewStack.push(new ErrorView(AppContext.getString("invalid-username")));
return;
}
@@ -86,7 +86,8 @@ public final class Server {
}).postEvent();
new EventFlow().listen(this::handleReceivedChallenge);
new EventFlow().listen(this::handleReceivedChallenge)
.listen(this::handleMatchResponse);
}
private void sendChallenge(String opponent) {
@@ -123,6 +124,42 @@ public final class Server {
}));
}
private void handleMatchResponse(NetworkEvents.GameMatchResponse response) {
if (!isPolling) return;
String gameType = extractQuotedValue(response.gameType());
if (response.clientId() == clientId) {
isPolling = false;
onlinePlayers.clear();
final GameInformation.Type type = gameToType(gameType);
if (type == null) {
ViewStack.push(new ErrorView("Unsupported game type: " + gameType));
return;
}
final int myTurn = response.playerToMove().equalsIgnoreCase(response.opponent()) ? 1 : 0;
final GameInformation information = new GameInformation(type);
//information.players[0] = playerInformation;
information.players[0].name = user;
information.players[0].isHuman = false;
information.players[0].computerDifficulty = 5;
information.players[1].name = response.opponent();
switch (type) {
case TICTACTOE ->
new TicTacToeGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage);
case REVERSI ->
new ReversiGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage);
case CONNECT4 ->
new Connect4Game(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage);
default -> ViewStack.push(new ErrorView("Unsupported game type."));
}
}
}
private void handleReceivedChallenge(NetworkEvents.ChallengeResponse response) {
if (!isPolling) return;
@@ -133,35 +170,12 @@ public final class Server {
ViewStack.push(new ChallengeView(challengerName, gameType, (playerInformation) -> {
final int challengeId = Integer.parseInt(response.challengeId().replaceAll("\\D", ""));
new EventFlow().addPostEvent(new NetworkEvents.SendAcceptChallenge(clientId, challengeId)).postEvent();
ViewStack.pop();
new EventFlow().listen(NetworkEvents.GameMatchResponse.class, e -> {
if (e.clientId() == clientId) {
isPolling = false;
onlinePlayers.clear();
//new EventFlow().listen(NetworkEvents.GameMatchResponse.class, e -> {
final GameInformation.Type type = gameToType(finalGameType);
if (type == null) {
ViewStack.push(new ErrorView("Unsupported game type: " + finalGameType));
return;
}
final int myTurn = e.playerToMove().equalsIgnoreCase(e.opponent()) ? 1 : 0;
final GameInformation information = new GameInformation(type);
information.players[0] = playerInformation;
information.players[0].name = user;
information.players[1].name = e.opponent();
switch (type) {
case TICTACTOE -> new TicTacToeGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage);
case REVERSI -> new ReversiGame(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage);
case CONNECT4 -> new Connect4Game(information, myTurn, this::forfeitGame, this::exitGame, this::sendMessage);
default -> ViewStack.push(new ErrorView("Unsupported game type."));
}
}
});
//});
}));
}

View File

@@ -181,9 +181,11 @@ public final class TicTacToeGame {
view.gameOver(false, information.players[1].name);
}
} else if (state == Game.State.DRAW) {
if(game.getLegalMoves().length == 0) { //only return draw in online multiplayer if the game is actually over.
view.gameOver(false, "");
}
}
}
if (move.value() == 'X') {
canvas.drawX(Color.RED, move.position());

View File

@@ -55,7 +55,9 @@ public final class SendChallengeView extends View {
final Button cancelButton = button();
cancelButton.setText(AppContext.getString("cancel"));
cancelButton.setOnAction(_ -> { ViewStack.pop(); });
cancelButton.setOnAction(_ -> {
IO.println("tried to click cancel");
ViewStack.pop(); });
final List<Node> nodes = new ArrayList<>();