Merge pull request #79 from rubenpirreram/TUI

T UI
This commit is contained in:
rubenpirreram
2026-05-05 13:50:12 +02:00
committed by GitHub
4 changed files with 66 additions and 16 deletions
@@ -5,6 +5,8 @@ import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import it.polimi.ingsw.gc14.View.IView;
import java.util.Objects;
/**
* Controller class that holds all the components of the client, such as view, network client and Game Controller.
* It provides methods to set the client components and to execute requested actions.
@@ -68,7 +70,15 @@ public class ClientController {
* @param pos the index of the card to draw
*/
public void drawUpperTribeCard(String playerUsername, int pos) {
client.doEvent(new DrawUpperTribeCard(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
{
view.showError("It's not your turn!");
}
else
{
client.doEvent(new DrawUpperTribeCard(playerUsername,pos));
}
}
@@ -79,7 +89,10 @@ public class ClientController {
* @param pos the index of the card to draw
*/
public void drawLowerTribeCard(String playerUsername,int pos) {
client.doEvent(new DrawLowerTribeCard(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new DrawLowerTribeCard(playerUsername,pos));
}
@@ -90,7 +103,11 @@ public class ClientController {
* @param pos the index of the card to draw
*/
public void drawUpperBuildingCard(String playerUsername,int pos) {
client.doEvent(new DrawUpperBuildingCard(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new DrawUpperBuildingCard(playerUsername,pos));
}
@@ -101,7 +118,10 @@ public class ClientController {
* @param pos the index of the card to draw
*/
public void drawLowerBuildingCard(String playerUsername,int pos) {
client.doEvent(new DrawLowerBuildingCard(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new DrawLowerBuildingCard(playerUsername,pos));
}
@@ -111,7 +131,10 @@ public class ClientController {
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) {
client.doEvent(new SkipUpper(playerUsername));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new SkipUpper(playerUsername));
}
@@ -120,7 +143,11 @@ public class ClientController {
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) { client.doEvent(new SkipLower(playerUsername));}
public void skipLower(String playerUsername) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new SkipLower(playerUsername));}
/**
@@ -130,7 +157,10 @@ public class ClientController {
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) {
client.doEvent(new PickOptionalTribeCard(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new PickOptionalTribeCard(playerUsername,pos));
}
@@ -141,7 +171,10 @@ public class ClientController {
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) {
client.doEvent(new PickOptionalBuildingCard(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new PickOptionalBuildingCard(playerUsername,pos));
}
@@ -151,7 +184,10 @@ public class ClientController {
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) {
client.doEvent(new NoOptionalCard(playerUsername));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new NoOptionalCard(playerUsername));
}
@@ -161,7 +197,10 @@ public class ClientController {
* @param pos the index of the selected slot
*/
public void slotChoice(String playerUsername,int pos) {
client.doEvent(new SlotChoice(playerUsername,pos));
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else
client.doEvent(new SlotChoice(playerUsername,pos));
}
}
@@ -117,8 +117,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param action The desired action
*/
public void notifyAll(NetworkEvent action) throws RemoteException {
for (IClientCallback cb : clients.values()) {
cb.onAction(action);
for (Map.Entry<String,IClientCallback> entry : clients.entrySet()) {
if(!action.getIsError() ||(action.getIsError()&& action.getUsername().equals(entry.getKey())))
entry.getValue().onAction(action);
}
}
@@ -15,7 +15,13 @@ import java.util.concurrent.BlockingQueue;
* It is also responsible to send events and game model updates back to the client.
*/
public class ClientHandler implements Runnable {
/**
* The username of the connected client on this handler
*/
private final String username;
public String getUsername() {
return username;
}
/** The TCP socket */
private Socket clientSocket;
@@ -41,7 +47,8 @@ public class ClientHandler implements Runnable {
* @param clientHandlers The shared list of all active client handlers
* @param actionQueue The queue containing incoming events
*/
public ClientHandler(Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
public ClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
this.username=username;
this.clientSocket = clientSocket;
this.in = in;
this.out = out;
@@ -115,7 +115,7 @@ public class TCPServer {
clientSocket.getOutputStream().write((int) (1));
System.out.println("Accepted player: " + eventAddPlayer.getUsername());
ClientHandler clientHandler = new ClientHandler(clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
ClientHandler clientHandler = new ClientHandler(eventAddPlayer.getUsername(),clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
clientHandlers.add(clientHandler);
ConnectedPlayers++;
@@ -141,7 +141,10 @@ public class TCPServer {
/** Sends an action to all TCP clients */
public void notifyAll(NetworkEvent event){
clientHandlers.forEach((x) -> x.notifyEvent(event));
clientHandlers.forEach((x) -> {
if(!event.getIsError()||(event.getIsError()&& event.getUsername().equals(x.getUsername())))
x.notifyEvent(event);
});
}