@@ -5,6 +5,8 @@ import it.polimi.ingsw.gc14.Network.IClient;
|
|||||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
|
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
|
||||||
import it.polimi.ingsw.gc14.View.IView;
|
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.
|
* 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.
|
* It provides methods to set the client components and to execute requested actions.
|
||||||
@@ -68,8 +70,16 @@ public class ClientController {
|
|||||||
* @param pos the index of the card to draw
|
* @param pos the index of the card to draw
|
||||||
*/
|
*/
|
||||||
public void drawUpperTribeCard(String playerUsername, int pos) {
|
public void drawUpperTribeCard(String playerUsername, int pos) {
|
||||||
|
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
{
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
client.doEvent(new DrawUpperTribeCard(playerUsername,pos));
|
client.doEvent(new DrawUpperTribeCard(playerUsername,pos));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,6 +89,9 @@ public class ClientController {
|
|||||||
* @param pos the index of the card to draw
|
* @param pos the index of the card to draw
|
||||||
*/
|
*/
|
||||||
public void drawLowerTribeCard(String playerUsername,int pos) {
|
public void drawLowerTribeCard(String playerUsername,int pos) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new DrawLowerTribeCard(playerUsername,pos));
|
client.doEvent(new DrawLowerTribeCard(playerUsername,pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +103,11 @@ public class ClientController {
|
|||||||
* @param pos the index of the card to draw
|
* @param pos the index of the card to draw
|
||||||
*/
|
*/
|
||||||
public void drawUpperBuildingCard(String playerUsername,int pos) {
|
public void drawUpperBuildingCard(String playerUsername,int pos) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new DrawUpperBuildingCard(playerUsername,pos));
|
client.doEvent(new DrawUpperBuildingCard(playerUsername,pos));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -101,6 +118,9 @@ public class ClientController {
|
|||||||
* @param pos the index of the card to draw
|
* @param pos the index of the card to draw
|
||||||
*/
|
*/
|
||||||
public void drawLowerBuildingCard(String playerUsername,int pos) {
|
public void drawLowerBuildingCard(String playerUsername,int pos) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new DrawLowerBuildingCard(playerUsername,pos));
|
client.doEvent(new DrawLowerBuildingCard(playerUsername,pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,6 +131,9 @@ public class ClientController {
|
|||||||
* @param playerUsername the name of the player performing the action
|
* @param playerUsername the name of the player performing the action
|
||||||
*/
|
*/
|
||||||
public void skipUpper(String playerUsername) {
|
public void skipUpper(String playerUsername) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new SkipUpper(playerUsername));
|
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.
|
* 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
|
* @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,6 +157,9 @@ public class ClientController {
|
|||||||
* @param pos the index of the card to draw
|
* @param pos the index of the card to draw
|
||||||
*/
|
*/
|
||||||
public void pickOptionalTribeCard(String playerUsername,int pos) {
|
public void pickOptionalTribeCard(String playerUsername,int pos) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new PickOptionalTribeCard(playerUsername,pos));
|
client.doEvent(new PickOptionalTribeCard(playerUsername,pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,6 +171,9 @@ public class ClientController {
|
|||||||
* @param pos the index of the card to draw
|
* @param pos the index of the card to draw
|
||||||
*/
|
*/
|
||||||
public void pickOptionalBuildingCard(String playerUsername,int pos) {
|
public void pickOptionalBuildingCard(String playerUsername,int pos) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new PickOptionalBuildingCard(playerUsername,pos));
|
client.doEvent(new PickOptionalBuildingCard(playerUsername,pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,6 +184,9 @@ public class ClientController {
|
|||||||
* @param playerUsername the name of the player performing the action
|
* @param playerUsername the name of the player performing the action
|
||||||
*/
|
*/
|
||||||
public void noOptionalCard(String playerUsername) {
|
public void noOptionalCard(String playerUsername) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new NoOptionalCard(playerUsername));
|
client.doEvent(new NoOptionalCard(playerUsername));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,6 +197,9 @@ public class ClientController {
|
|||||||
* @param pos the index of the selected slot
|
* @param pos the index of the selected slot
|
||||||
*/
|
*/
|
||||||
public void slotChoice(String playerUsername,int pos) {
|
public void slotChoice(String playerUsername,int pos) {
|
||||||
|
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
|
||||||
|
view.showError("It's not your turn!");
|
||||||
|
else
|
||||||
client.doEvent(new SlotChoice(playerUsername,pos));
|
client.doEvent(new SlotChoice(playerUsername,pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -117,8 +117,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
|||||||
* @param action The desired action
|
* @param action The desired action
|
||||||
*/
|
*/
|
||||||
public void notifyAll(NetworkEvent action) throws RemoteException {
|
public void notifyAll(NetworkEvent action) throws RemoteException {
|
||||||
for (IClientCallback cb : clients.values()) {
|
for (Map.Entry<String,IClientCallback> entry : clients.entrySet()) {
|
||||||
cb.onAction(action);
|
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.
|
* It is also responsible to send events and game model updates back to the client.
|
||||||
*/
|
*/
|
||||||
public class ClientHandler implements Runnable {
|
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 */
|
/** The TCP socket */
|
||||||
private Socket clientSocket;
|
private Socket clientSocket;
|
||||||
|
|
||||||
@@ -41,7 +47,8 @@ public class ClientHandler implements Runnable {
|
|||||||
* @param clientHandlers The shared list of all active client handlers
|
* @param clientHandlers The shared list of all active client handlers
|
||||||
* @param actionQueue The queue containing incoming events
|
* @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.clientSocket = clientSocket;
|
||||||
this.in = in;
|
this.in = in;
|
||||||
this.out = out;
|
this.out = out;
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ public class TCPServer {
|
|||||||
clientSocket.getOutputStream().write((int) (1));
|
clientSocket.getOutputStream().write((int) (1));
|
||||||
|
|
||||||
System.out.println("Accepted player: " + eventAddPlayer.getUsername());
|
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);
|
clientHandlers.add(clientHandler);
|
||||||
ConnectedPlayers++;
|
ConnectedPlayers++;
|
||||||
|
|
||||||
@@ -141,7 +141,10 @@ public class TCPServer {
|
|||||||
|
|
||||||
/** Sends an action to all TCP clients */
|
/** Sends an action to all TCP clients */
|
||||||
public void notifyAll(NetworkEvent event){
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user