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; return GameInformation.Type.REVERSI;
} else if (game.equalsIgnoreCase("connect4")) { } else if (game.equalsIgnoreCase("connect4")) {
return GameInformation.Type.CONNECT4; return GameInformation.Type.CONNECT4;
} else if (game.equalsIgnoreCase("battleship")) { // } else if (game.equalsIgnoreCase("battleship")) {
return GameInformation.Type.BATTLESHIP; // return GameInformation.Type.BATTLESHIP;
} }
return null; return null;
@@ -62,7 +62,7 @@ public final class Server {
return; return;
} }
if (user.isEmpty()) { if (user.isEmpty() || user.matches("^[0-9].*")) {
ViewStack.push(new ErrorView(AppContext.getString("invalid-username"))); ViewStack.push(new ErrorView(AppContext.getString("invalid-username")));
return; return;
} }
@@ -86,7 +86,8 @@ public final class Server {
}).postEvent(); }).postEvent();
new EventFlow().listen(this::handleReceivedChallenge); new EventFlow().listen(this::handleReceivedChallenge)
.listen(this::handleMatchResponse);
} }
private void sendChallenge(String opponent) { private void sendChallenge(String opponent) {
@@ -123,8 +124,44 @@ 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) { private void handleReceivedChallenge(NetworkEvents.ChallengeResponse response) {
if (!isPolling) return; if (!isPolling) return;
String challengerName = extractQuotedValue(response.challengerName()); String challengerName = extractQuotedValue(response.challengerName());
String gameType = extractQuotedValue(response.gameType()); String gameType = extractQuotedValue(response.gameType());
@@ -133,35 +170,12 @@ public final class Server {
ViewStack.push(new ChallengeView(challengerName, gameType, (playerInformation) -> { ViewStack.push(new ChallengeView(challengerName, gameType, (playerInformation) -> {
final int challengeId = Integer.parseInt(response.challengeId().replaceAll("\\D", "")); final int challengeId = Integer.parseInt(response.challengeId().replaceAll("\\D", ""));
new EventFlow().addPostEvent(new NetworkEvents.SendAcceptChallenge(clientId, challengeId)).postEvent(); new EventFlow().addPostEvent(new NetworkEvents.SendAcceptChallenge(clientId, challengeId)).postEvent();
ViewStack.pop(); ViewStack.pop();
new EventFlow().listen(NetworkEvents.GameMatchResponse.class, e -> { //new EventFlow().listen(NetworkEvents.GameMatchResponse.class, e -> {
if (e.clientId() == clientId) {
isPolling = false;
onlinePlayers.clear();
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,7 +181,9 @@ public final class TicTacToeGame {
view.gameOver(false, information.players[1].name); view.gameOver(false, information.players[1].name);
} }
} else if (state == Game.State.DRAW) { } else if (state == Game.State.DRAW) {
view.gameOver(false, ""); if(game.getLegalMoves().length == 0) { //only return draw in online multiplayer if the game is actually over.
view.gameOver(false, "");
}
} }
} }

View File

@@ -55,7 +55,9 @@ public final class SendChallengeView extends View {
final Button cancelButton = button(); final Button cancelButton = button();
cancelButton.setText(AppContext.getString("cancel")); 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<>(); final List<Node> nodes = new ArrayList<>();