Fix: Fixed Player Adding Logic In TCPServer.java.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
import it.polimi.ingsw.gc14.LimitedList;
|
||||||
import it.polimi.ingsw.gc14.Model.Game;
|
import it.polimi.ingsw.gc14.Model.Game;
|
||||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
|
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
|
||||||
@@ -18,7 +19,7 @@ public class TCPServer {
|
|||||||
ServerSocket serverTCP = null;
|
ServerSocket serverTCP = null;
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
BlockingQueue<NetworkEvent> actionQueue;
|
BlockingQueue<NetworkEvent> actionQueue;
|
||||||
|
private LimitedList<String> playerList;
|
||||||
private List<ClientHandler> clientHandlers;
|
private List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
|
|
||||||
@@ -48,15 +49,32 @@ public class TCPServer {
|
|||||||
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
NetworkEvent event = (NetworkEvent) clientSocketObj.readObject();
|
||||||
|
|
||||||
//sincronizzazione su gameController
|
synchronized (gameController){
|
||||||
if(!(event instanceof AddPlayer) || (ConnectedPlayers >= gameController.getModel().getCurrentPlayerNumber() && gameController.getModel() != null)){
|
if(!(event instanceof AddPlayer) || (gameController.getModel() != null && gameController.getModel().getCurrentPlayerNumber() >= gameController.getModel().getNPlayers())){
|
||||||
clientSocket.getOutputStream().write((int)(-1));
|
clientSocket.getOutputStream().write((int)(-1));
|
||||||
clientSocket.close();
|
clientSocket.close();
|
||||||
System.out.println("Invalid parameters. Connection terminated.\n");
|
System.out.println("Invalid parameters. Connection terminated.\n");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
actionQueue.add(event);
|
synchronized (gameController) {
|
||||||
clientSocket.getOutputStream().write((int)(1));
|
AddPlayer addPlayer = (AddPlayer) event;
|
||||||
|
if(playerList.isEmpty() && (addPlayer.getProposedNPlayer() < 2 || addPlayer.getProposedNPlayer() > 5)){
|
||||||
|
clientSocket.getOutputStream().write((int)(-1));
|
||||||
|
clientSocket.close();
|
||||||
|
System.out.println("Invalid parameters. Connection terminated.\n");
|
||||||
|
}
|
||||||
|
else if(gameController.addPlayer(addPlayer.getUsername())){
|
||||||
|
if(playerList.isEmpty()){
|
||||||
|
Game game = new Game(addPlayer.getProposedNPlayer());
|
||||||
|
gameController.setModel(game);
|
||||||
|
playerList.setLimit(addPlayer.getProposedNPlayer());
|
||||||
|
}
|
||||||
|
playerList.add(addPlayer.getUsername());
|
||||||
|
clientSocket.getOutputStream().write((int)(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// gestione di ADD_PLAYER
|
// gestione di ADD_PLAYER
|
||||||
}
|
}
|
||||||
@@ -93,10 +111,11 @@ public class TCPServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public TCPServer(GameController gameController, int port, BlockingQueue<NetworkEvent> actionQueue){
|
public TCPServer(GameController gameController, int port, BlockingQueue<NetworkEvent> actionQueue, LimitedList<String> players){
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.gameController = gameController;
|
this.gameController = gameController;
|
||||||
this.actionQueue = actionQueue;
|
this.actionQueue = actionQueue;
|
||||||
|
this.playerList = players;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void broadcastUpdate(NetworkEvent event){
|
public void broadcastUpdate(NetworkEvent event){
|
||||||
|
|||||||
Reference in New Issue
Block a user