From b506afdadedd2588cfea830045ec73edb06f9acf Mon Sep 17 00:00:00 2001 From: Ticho Hidding Date: Mon, 27 Oct 2025 14:57:15 +0100 Subject: [PATCH] can start game from playerlist screen --- app/src/main/java/org/toop/app/Server.java | 74 +++++++++++-------- .../java/org/toop/app/game/TicTacToeGame.java | 4 +- .../app/view/views/SendChallengeView.java | 4 +- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/toop/app/Server.java b/app/src/main/java/org/toop/app/Server.java index 91a89ef..c4227c0 100644 --- a/app/src/main/java/org/toop/app/Server.java +++ b/app/src/main/java/org/toop/app/Server.java @@ -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,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) { - if (!isPolling) return; + if (!isPolling) return; String challengerName = extractQuotedValue(response.challengerName()); String gameType = extractQuotedValue(response.gameType()); @@ -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.")); - } - } - }); + //}); })); } diff --git a/app/src/main/java/org/toop/app/game/TicTacToeGame.java b/app/src/main/java/org/toop/app/game/TicTacToeGame.java index 6448835..59ba72d 100644 --- a/app/src/main/java/org/toop/app/game/TicTacToeGame.java +++ b/app/src/main/java/org/toop/app/game/TicTacToeGame.java @@ -181,7 +181,9 @@ public final class TicTacToeGame { view.gameOver(false, information.players[1].name); } } 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, ""); + } } } diff --git a/app/src/main/java/org/toop/app/view/views/SendChallengeView.java b/app/src/main/java/org/toop/app/view/views/SendChallengeView.java index 5d88a13..7f2b8d6 100644 --- a/app/src/main/java/org/toop/app/view/views/SendChallengeView.java +++ b/app/src/main/java/org/toop/app/view/views/SendChallengeView.java @@ -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 nodes = new ArrayList<>();