Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.boredxgames.tictactoeclient.domain.model;

/**
*
* @author moham
*/
public class GameEndInfo {

private String roomId;
private String winnerId; // If this is null, it means DRAW

public GameEndInfo() {
}

public GameEndInfo(String roomId, String winnerId) {
this.roomId = roomId;
this.winnerId = winnerId;
}

public String getRoomId() {
return roomId;
}

public String getWinnerId() {
return winnerId;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.boredxgames.tictactoeclient.domain.network.ServerConnectionManager;
import com.boredxgames.tictactoeclient.domain.services.game.OnlinePVPService;
import com.boredxgames.tictactoeclient.presentation.AuthenticationController;
import com.boredxgames.tictactoeclient.presentation.GameController;
import com.boredxgames.tictactoeclient.presentation.HomeController;
import com.google.gson.Gson;
import javafx.application.Platform;
Expand All @@ -26,6 +27,8 @@ public class MessageRouter {
private static ServerConnectionManager connection;
private Gson gson = new Gson();
private static HomeController homeController;
private static GameController gameController;


private MessageRouter() {
connection = ServerConnectionManager.getInstance();
Expand Down Expand Up @@ -56,6 +59,9 @@ public static void setHomeController(HomeController controller) {
homeController = controller;
}

public static void setGameController(GameController controller) {
gameController = controller;
}
private Message handleRequest(Message msg) {
Action action = msg.getHeader().getAction();

Expand All @@ -73,27 +79,20 @@ private Message handleRequest(Message msg) {

private void handleResponse(Message msg) {
Action action = msg.getHeader().getAction();
System.out.println(action);
System.out.println("Response Action: " + action);

switch (action) {
case LOGIN_SUCCESS -> {
System.out.println("Login Success");
AuthResponseEntity responseData = gson.fromJson(msg.getData(), AuthResponseEntity.class);
ServerConnectionManager.getInstance().setPlayer(responseData);
System.out.println(responseData);
NavigationManager.navigate(Screens.Home, NavigationAction.REPLACE);

}
case REGISTERATION_SUCCESS -> {
System.out.println("Registration success");
AuthResponseEntity responseData = gson.fromJson(msg.getData(), AuthResponseEntity.class);
AuthenticationController.showUserAlert("Registration success");

}
case USERNAME_NOT_FOUND -> {
System.out.println("Username not found");
AuthenticationController.showUserAlert("Username not found");

}
case REQUEST_GAME -> {
System.out.println("Server acknowledged Game Request.");
Expand All @@ -103,6 +102,7 @@ private void handleResponse(Message msg) {
if (homeController != null) {
Platform.runLater(() -> homeController.updatePlayersList(info));
}

}
case GAME_RESPONSE -> {
GameResponseInfo info = gson.fromJson(msg.getData(), GameResponseInfo.class);
Expand All @@ -116,47 +116,46 @@ private void handleResponse(Message msg) {
Platform.runLater(() -> homeController.updateLeaderboardUI(info));
}
}
case GAME_START -> {
handleGameStart(msg);
}
case SEND_GAME_UPDATE -> {
MoveInfo moveInfo = gson.fromJson(msg.getData(), MoveInfo.class);
OnlinePVPService.onIncomingMove(moveInfo);
}

default -> {
System.out.println("Unknown Action: " + action);

}
default -> System.out.println("Unknown Action: " + action);
}
;
}

private void handleEvent(Message msg) {
private void handleEvent(Message msg) {
Action action = msg.getHeader().getAction();
System.out.println(action);
System.out.println("Event Action: " + action);

switch (action) {
case REQUEST_GAME -> {
GameRequestInfo info = gson.fromJson(msg.getData(), GameRequestInfo.class);
if (homeController != null) {
Platform.runLater(() -> homeController.showIncomingGameRequest(info));
}

}
case GAME_START -> {
OnlineGameState.info = gson.fromJson(msg.getData(), GameStartInfo.class);
String player1Name = "You";
String player2Name = "Opponent";
NavigationManager.navigate(Screens.GAME, NavigationAction.REPLACE, new GameNavigationParams(
player1Name,player2Name, GameMode.ONLINE_PVP
));

}

default -> {
System.out.println("Unknown Action: " + action);

handleGameStart(msg);
}
default -> System.out.println("Unknown Action: " + action);
}
;
}

// *** NEW HELPER METHOD ***
private void handleGameStart(Message msg) {
OnlineGameState.info = gson.fromJson(msg.getData(), GameStartInfo.class);
String player1Name = "You";
String player2Name = "Opponent";

Platform.runLater(() -> {
NavigationManager.navigate(Screens.GAME, NavigationAction.REPLACE, new GameNavigationParams(
player1Name, player2Name, GameMode.ONLINE_PVP
));
});
}

private void handleError(Message msg) {
Expand All @@ -179,6 +178,10 @@ private void handleError(Message msg) {
if (homeController != null) {
Platform.runLater(() -> homeController.showErrorAlert(errorMessage));
}
if (gameController != null) {
Platform.runLater(() -> gameController.showErrorAlert(errorMessage));
}


}
case PLAYER_BUSY -> {
Expand All @@ -187,6 +190,7 @@ private void handleError(Message msg) {
if (homeController != null) {
Platform.runLater(() -> homeController.showErrorAlert(errorMsg));
}

}
case PENDING_REQUEST_EXISTS -> {
String errorMsg = "You already have a request pending.";
Expand All @@ -206,10 +210,12 @@ private void handleError(Message msg) {
case ROOM_NOT_FOUND -> {
String errorMsg = "The game session is no longer available.";
if (homeController != null) {
// Close the "Accept/Decline" popup if open
homeController.dismissIncomingRequest();
Platform.runLater(() -> homeController.showErrorAlert(errorMsg));
}
if (gameController != null) {
Platform.runLater(() -> gameController.showErrorAlert(errorMsg));
}
}

case INVALID_OPPONENT -> {
Expand All @@ -218,6 +224,9 @@ private void handleError(Message msg) {
homeController.dismissIncomingRequest();
Platform.runLater(() -> homeController.showErrorAlert(errorMsg));
}
if (gameController != null) {
Platform.runLater(() -> gameController.showErrorAlert(errorMsg));
}
}
case INVALID_CREDENTIAL -> {
System.out.println("INVALID_CREDENTIAL");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.boredxgames.tictactoeclient.domain.services.game;

import com.boredxgames.tictactoeclient.domain.managers.navigation.NavigationAction;
import com.boredxgames.tictactoeclient.domain.managers.navigation.NavigationManager;
import com.boredxgames.tictactoeclient.domain.managers.navigation.Screens;
import com.boredxgames.tictactoeclient.domain.model.*;
import com.boredxgames.tictactoeclient.domain.network.ServerConnectionManager;
import com.boredxgames.tictactoeclient.domain.services.GameService;
Expand Down Expand Up @@ -42,9 +45,18 @@ public OnlinePVPService setMoveListener(Consumer<Move> listener) {
return this;
}

public static void onIncomingMove(MoveInfo moveInfo) {
public static void onIncomingMove(MoveInfo moveInfo) {
Gson gson = new Gson();
Move move = gson.fromJson(moveInfo.getMove(), Move.class);
Object moveData = moveInfo.getMove();
Move move;

if (moveData instanceof String) {
move = gson.fromJson((String) moveData, Move.class);
} else {
String json = gson.toJson(moveData);
move = gson.fromJson(json, Move.class);
}

if (moveListener != null) {
moveListener.accept(move);
}
Expand All @@ -56,18 +68,33 @@ public void makeMove(Move move, char currentPlayer) {
ServerConnectionManager connectionManager = ServerConnectionManager.getInstance();
AuthResponseEntity player = connectionManager.getPlayer();
GameStartInfo sessionInfo = OnlineGameState.info;
// Prepare the data
MoveInfo info = MoveInfo.createMoveInfo(sessionInfo.getRoomId(), player.getId(), move);

// Send SEND_GAME_UPDATE action to server
Message msg = Message.createMessage(
MessageType.RESPONSE,
Action.SEND_GAME_UPDATE,
info
);
connectionManager.sendMessage(msg);
}

public void sendGameEnd(String winnerId) {
ServerConnectionManager connectionManager = ServerConnectionManager.getInstance();

if (OnlineGameState.info == null) return;

String roomId = OnlineGameState.info.getRoomId();
GameEndInfo endInfo = new GameEndInfo(roomId, winnerId);

Message msg = Message.createMessage(
MessageType.RESPONSE,
Action.GAME_END,
endInfo
);

connectionManager.sendMessage(msg);
NavigationManager.navigate(Screens.Home, NavigationAction.REPLACE);
}
@Override
public void makeMove(Move move, char currentPlayer, GameBoard board) {

Expand Down
Loading