Added: Specific Error Display
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network;
|
||||
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
|
||||
/**
|
||||
* Defines the operations that a client can request during the game.
|
||||
*/
|
||||
@@ -14,7 +16,7 @@ public interface IClient {
|
||||
* @return {@code true} if the connection is established successfully,
|
||||
* {@code false} otherwise.
|
||||
*/
|
||||
boolean connect(String username, int preferredInt);
|
||||
ErrorType connect(String username, int preferredInt);
|
||||
|
||||
/**
|
||||
* Requests to draw a tribe card from the upper tribe card list.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Model.OrderLogicCard;
|
||||
@@ -21,6 +22,11 @@ import java.util.Map;
|
||||
*/
|
||||
public abstract class NetworkEvent implements Serializable {
|
||||
|
||||
protected ErrorType errorType;
|
||||
|
||||
public ErrorType getErrorType() {
|
||||
return errorType;
|
||||
}
|
||||
/**
|
||||
* Username of the player requesting the event.
|
||||
*/
|
||||
@@ -120,10 +126,11 @@ public abstract class NetworkEvent implements Serializable {
|
||||
* @param isError {@code true} if the event represents an error,
|
||||
* {@code false} otherwise.
|
||||
*/
|
||||
protected NetworkEvent(String username, EventType eventType, boolean isError) {
|
||||
protected NetworkEvent(String username, EventType eventType, boolean isError,ErrorType errorType) {
|
||||
this.username = username;
|
||||
this.eventType = eventType;
|
||||
this.isError = isError;
|
||||
this.errorType = errorType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -14,6 +15,10 @@ import java.io.Serializable;
|
||||
public class AddPlayer extends NetworkEvent implements Serializable {
|
||||
/** Number of proposed players to add to the match */
|
||||
private int proposedNPlayer;
|
||||
public void setErrorType(ErrorType errorType)
|
||||
{
|
||||
this.errorType = errorType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the number of proposed players to add to the match
|
||||
@@ -29,7 +34,7 @@ public class AddPlayer extends NetworkEvent implements Serializable {
|
||||
* @param proposedNPlayer the number of proposed players to add to the match
|
||||
*/
|
||||
public AddPlayer(String username, int proposedNPlayer) {
|
||||
super(username, EventType.ADD_PLAYER, false);
|
||||
super(username, EventType.ADD_PLAYER, false, ErrorType.GENERIC_ERROR);
|
||||
this.proposedNPlayer = proposedNPlayer;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
|
||||
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
|
||||
@@ -38,7 +39,7 @@ public class ApplyNextRound extends NetworkEvent implements Serializable{
|
||||
* @param players the list of players in the game.
|
||||
*/
|
||||
public ApplyNextRound(Map<Slot, Player> slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List<Player> players, ArrayList<TribeCard> upperListTribeCards, ArrayList<TribeCard>lowerListTribeCards, ArrayList<BuildingCard> upperListBuildingCards, ArrayList<BuildingCard>lowerListBuildingCards) {
|
||||
super("SERVER",EventType.NEXT_ROUND,false);
|
||||
super("SERVER",EventType.NEXT_ROUND,false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.upperListBuildingCards = upperListBuildingCards;
|
||||
this.lowerListBuildingCards = lowerListBuildingCards;
|
||||
this.upperListTribeCards = upperListTribeCards;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -18,7 +19,7 @@ public class DisconnectedPlayer extends NetworkEvent implements Serializable{
|
||||
* @param username the name of the player requesting the event
|
||||
*/
|
||||
public DisconnectedPlayer(String username){
|
||||
super(username, EventType.DISCONNECTED_PLAYER, false);
|
||||
super(username, EventType.DISCONNECTED_PLAYER, false, ErrorType.GENERIC_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -22,7 +23,7 @@ public class DrawLowerBuildingCard extends NetworkEvent implements Serializable
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public DrawLowerBuildingCard(String username, int pos){
|
||||
super(username, EventType.DRAW_LOWER_BUILD, false);
|
||||
super(username, EventType.DRAW_LOWER_BUILD, false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -23,7 +24,7 @@ public class DrawLowerTribeCard extends NetworkEvent implements Serializable{
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public DrawLowerTribeCard(String username, int pos){
|
||||
super(username, EventType.DRAW_LOWER_TRIBE, false);
|
||||
super(username, EventType.DRAW_LOWER_TRIBE, false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -23,7 +24,7 @@ public class DrawUpperBuildingCard extends NetworkEvent implements Serializable
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public DrawUpperBuildingCard(String username, int pos){
|
||||
super(username, EventType.DRAW_UPPER_BUILD, false);
|
||||
super(username, EventType.DRAW_UPPER_BUILD, false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -23,7 +24,7 @@ public class DrawUpperTribeCard extends NetworkEvent implements Serializable{
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public DrawUpperTribeCard(String username, int pos){
|
||||
super(username, EventType.DRAW_UPPER_TRIBE, false);
|
||||
super(username, EventType.DRAW_UPPER_TRIBE, false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Model.OrderLogicCard;
|
||||
@@ -31,7 +32,7 @@ public class EndedGame extends NetworkEvent implements Serializable{
|
||||
* @param players the list of players at the end of the game.
|
||||
*/
|
||||
public EndedGame(Map<Slot, Player> slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, ArrayList<Player> players){
|
||||
super("SERVER",EventType.ENDED_GAME,false);
|
||||
super("SERVER",EventType.ENDED_GAME,false, ErrorType.GENERIC_ERROR);
|
||||
this.slotPlayerMap = slotPlayerMap;
|
||||
this.orderLogicCard = orderLogicCard;
|
||||
this.currentState = currentState;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -18,7 +19,7 @@ public class ReconnectPlayer extends NetworkEvent implements Serializable {
|
||||
* @param username the username of the player who reconnected.
|
||||
*/
|
||||
public ReconnectPlayer(String username) {
|
||||
super(username, EventType.RECONNECT_PLAYER, false);
|
||||
super(username, EventType.RECONNECT_PLAYER, false, ErrorType.GENERIC_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -18,7 +19,7 @@ public class SkipTurn extends NetworkEvent implements Serializable{
|
||||
* @param username the name of the player requesting the event
|
||||
*/
|
||||
public SkipTurn(String username){
|
||||
super(username, EventType.SKIP_TURN, false);
|
||||
super(username, EventType.SKIP_TURN, false, ErrorType.WRONG_GAME_ACTION);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -23,7 +24,7 @@ public class SlotChoice extends NetworkEvent implements Serializable {
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public SlotChoice(String username, int pos) {
|
||||
super(username, EventType.SLOT_CHOICE, false);
|
||||
super(username, EventType.SLOT_CHOICE, false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.NetworkEvents;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Model.Totems;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
@@ -40,7 +41,7 @@ public class TotemChoice extends NetworkEvent implements Serializable {
|
||||
* @param totem the name of the selected totem.
|
||||
*/
|
||||
public TotemChoice(String username, String totem) {
|
||||
super(username, EventType.TOTEM_CHOICE, false);
|
||||
super(username, EventType.TOTEM_CHOICE, false, ErrorType.WRONG_GAME_ACTION);
|
||||
this.totem = totem;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
|
||||
@Override
|
||||
public void onAction(NetworkEvent event) throws RemoteException {
|
||||
if(event.getIsError()) {
|
||||
clientController.view.showError(event.toString());
|
||||
clientController.view.showError(event.getErrorType());
|
||||
} else {
|
||||
event.apply(clientController.miniModel);
|
||||
clientController.view.render();
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.rmi.registry.Registry;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Network.IClient;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
|
||||
@@ -53,7 +54,7 @@ public class RMIClient implements IClient {
|
||||
* opening a second socket).
|
||||
*/
|
||||
@Override
|
||||
public boolean connect(String username, int preferredInt) {
|
||||
public ErrorType connect(String username, int preferredInt) {
|
||||
try {
|
||||
System.setProperty("java.rmi.server.hostname", this.myIP);
|
||||
Registry registry = LocateRegistry.getRegistry(host, port);
|
||||
@@ -61,16 +62,17 @@ public class RMIClient implements IClient {
|
||||
this.username = username;
|
||||
|
||||
ClientCallbackImpl callback = new ClientCallbackImpl(controller);
|
||||
boolean joined = stub.joinGame(username, preferredInt, callback);
|
||||
if (!joined) return false;
|
||||
|
||||
running = true;
|
||||
startHeartbeat();
|
||||
return true;
|
||||
|
||||
ErrorType status = stub.joinGame(username, preferredInt, callback);
|
||||
if(status==null)
|
||||
{
|
||||
running = true;
|
||||
startHeartbeat();
|
||||
return null;
|
||||
}
|
||||
return status;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
return ErrorType.GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +126,7 @@ public class RMIClient implements IClient {
|
||||
if (!running) return;
|
||||
running = false;
|
||||
if (pingSender != null) pingSender.shutdownNow();
|
||||
controller.view.showError("Connection with server closed");
|
||||
controller.view.showError(ErrorType.SERVER_CRASHED);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Common;
|
||||
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
/**
|
||||
@@ -18,7 +20,7 @@ public interface IGameServer extends Remote {
|
||||
* {@code false} otherwise.
|
||||
* @throws RemoteException if an RMI communication error occurs.
|
||||
*/
|
||||
boolean joinGame(String username, int preferredInt, IClientCallback callback) throws RemoteException;
|
||||
ErrorType joinGame(String username, int preferredInt, IClientCallback callback) throws RemoteException;
|
||||
|
||||
/**
|
||||
* Requests to draw a tribe card from the upper tribe card list.
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.LimitedMap;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
|
||||
@@ -81,9 +83,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
||||
* {@code TCPServer.acceptHeartbeat()}.
|
||||
*/
|
||||
@Override
|
||||
public boolean joinGame(String username, int preferredInt, IClientCallback callback)
|
||||
public ErrorType joinGame(String username, int preferredInt, IClientCallback callback)
|
||||
throws RemoteException {
|
||||
if (preferredInt < 2 || preferredInt > 5) return false;
|
||||
if (preferredInt < 2 || preferredInt > 5) return ErrorType.WRONG_PLAYER_NUMBER;
|
||||
|
||||
synchronized (controller) {
|
||||
|
||||
@@ -92,32 +94,47 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
||||
playerList.setLimit(preferredInt);
|
||||
System.out.println("Game Created With :"+preferredInt+" Players");
|
||||
}
|
||||
if (controller.addPlayer(username)) {
|
||||
clients.put(username, callback);
|
||||
playerList.put(username, true);
|
||||
startWatchdog(username);
|
||||
System.out.println("Accepted player: " + username);
|
||||
return true;
|
||||
if(controller.getModel().getCurrentState().getGameStage()!= GameStages.WAITING ) {
|
||||
if (!controller.getModel().getPlayers().stream().anyMatch(p -> p.getUserName().equals(username))) {
|
||||
return ErrorType.GAME_ALREADY_STARTED;
|
||||
}
|
||||
}
|
||||
if(controller.getModel().getPlayers().stream().anyMatch(p -> p.getUserName().equals(username))&& !playerList.containsKey(username)) {
|
||||
else {
|
||||
if (controller.addPlayer(username)) {
|
||||
clients.put(username, callback);
|
||||
playerList.put(username, true);
|
||||
startWatchdog(username);
|
||||
System.out.println("Accepted player: " + username);
|
||||
return null;
|
||||
} else {
|
||||
return ErrorType.USERNAME_ALREADY_USED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!playerList.containsKey(username)){
|
||||
clients.put(username, callback);
|
||||
playerList.put(username, true);
|
||||
startWatchdog(username);
|
||||
System.out.println("(After crash)Reconnected player: " + username);
|
||||
return true;
|
||||
return null;
|
||||
}
|
||||
if (playerList.containsKey(username) && !playerList.get(username)) {
|
||||
playerList.put(username, true);
|
||||
clients.put(username, callback);
|
||||
System.out.println("Reconnected player: " + username);
|
||||
startWatchdog(username);
|
||||
Game game = controller.getModel();
|
||||
callback.onGameInit(new MiniModel(game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems(),game.getUpperListTribeCards(),game.getLowerListTribeCards(),game.getUpperListBuilding(),game.getLowerListBuilding()));
|
||||
System.out.println("Model sent: " + username);
|
||||
actionQueue.add(new ReconnectPlayer(username));
|
||||
return true;
|
||||
else {
|
||||
if(!playerList.get(username))
|
||||
{
|
||||
playerList.put(username, true);
|
||||
clients.put(username, callback);
|
||||
System.out.println("Reconnected player: " + username);
|
||||
startWatchdog(username);
|
||||
Game game = controller.getModel();
|
||||
callback.onGameInit(new MiniModel(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(), game.getPlayers(), game.getAvailableTotems(), game.getUpperListTribeCards(), game.getLowerListTribeCards(), game.getUpperListBuilding(), game.getLowerListBuilding()));
|
||||
System.out.println("Model sent: " + username);
|
||||
actionQueue.add(new ReconnectPlayer(username));
|
||||
return null;
|
||||
}
|
||||
else
|
||||
return ErrorType.USER_ALREADY_CONNECTED;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.polimi.ingsw.gc14.Network.TCP.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.IClient;
|
||||
@@ -70,7 +71,7 @@ public class TCPClient implements IClient {
|
||||
* @param proposedNPlayers The desired number of players for the game
|
||||
* @return true if the connection is successful, false otherwise.
|
||||
*/
|
||||
public boolean connect(String user, int proposedNPlayers) {
|
||||
public ErrorType connect(String user, int proposedNPlayers) {
|
||||
try {
|
||||
communicationSocket = new Socket(hostname, mainPort);
|
||||
socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());
|
||||
@@ -80,7 +81,16 @@ public class TCPClient implements IClient {
|
||||
int read= communicationSocket.getInputStream().read();
|
||||
if ( read== -1) {
|
||||
System.out.println("Could not connect to server");
|
||||
return false;
|
||||
|
||||
try {
|
||||
if( socketReceive.readObject() instanceof AddPlayer x)
|
||||
{
|
||||
return x.getErrorType();
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
return ErrorType.GENERIC_ERROR;
|
||||
}
|
||||
return ErrorType.GENERIC_ERROR;
|
||||
}
|
||||
new Thread(this::receiveMessage, "tcp-reader").start();
|
||||
|
||||
@@ -95,11 +105,11 @@ public class TCPClient implements IClient {
|
||||
new Thread(this::heartbeatLoop, "heartbeat").start();
|
||||
|
||||
running = true;
|
||||
return true;
|
||||
return null;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
return ErrorType.GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +153,7 @@ public class TCPClient implements IClient {
|
||||
running = false;
|
||||
try { communicationSocket.close(); } catch (IOException ignored) {}
|
||||
try { heartbeatSocket.close(); } catch (IOException ignored) {}
|
||||
controller.view.showError("Connection with server lost");
|
||||
controller.view.showError(ErrorType.SERVER_CRASHED);
|
||||
}
|
||||
|
||||
|
||||
@@ -166,7 +176,7 @@ public class TCPClient implements IClient {
|
||||
|
||||
if (read instanceof NetworkEvent event) {
|
||||
if (event.getIsError()) {
|
||||
controller.view.showError(event.toString());
|
||||
controller.view.showError(event.getErrorType());
|
||||
} else {
|
||||
event.apply(controller.miniModel);
|
||||
controller.view.render();
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.LimitedMap;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.EventType;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
@@ -168,35 +170,52 @@ public class TCPServer {
|
||||
playerList.setLimit(eventAddPlayer.getProposedNPlayer());
|
||||
System.out.println("Game Created With :"+eventAddPlayer.getProposedNPlayer()+" Players");
|
||||
}
|
||||
|
||||
if (controller.addPlayer(username)) {
|
||||
playerList.put(username, true);
|
||||
System.out.println("Accepted player: " + username);
|
||||
|
||||
ClientHandler handler = new ClientHandler(
|
||||
username,
|
||||
clientSocket,
|
||||
clientSend,
|
||||
clientReceive,
|
||||
clientHandlers,
|
||||
playerList,
|
||||
actionQueue
|
||||
);
|
||||
|
||||
clientSocket.getOutputStream().write(1);
|
||||
pendingHeartbeat.put(username, handler);
|
||||
|
||||
Thread thread = new Thread(handler);
|
||||
thread.start();
|
||||
|
||||
clientHandlers.add(handler);
|
||||
connectedPlayers++;
|
||||
continue;
|
||||
|
||||
if(controller.getModel().getCurrentState().getGameStage()!= GameStages.WAITING ) {
|
||||
if (!controller.getModel().getPlayers().stream().anyMatch(p -> p.getUserName().equals(username))) {
|
||||
clientSocket.getOutputStream().write(-1);
|
||||
eventAddPlayer.setErrorType(ErrorType.GAME_ALREADY_STARTED);
|
||||
clientSend.writeObject(eventAddPlayer);
|
||||
clientSocket.close();
|
||||
System.out.println("Invalid parameters. Connection terminated.");
|
||||
}
|
||||
}
|
||||
if (controller.getModel().getPlayers().stream()
|
||||
.anyMatch(p -> p.getUserName().equals(username))
|
||||
&& !playerList.containsKey(username)) {
|
||||
else{
|
||||
if (controller.addPlayer(username)) {
|
||||
playerList.put(username, true);
|
||||
System.out.println("Accepted player: " + username);
|
||||
|
||||
ClientHandler handler = new ClientHandler(
|
||||
username,
|
||||
clientSocket,
|
||||
clientSend,
|
||||
clientReceive,
|
||||
clientHandlers,
|
||||
playerList,
|
||||
actionQueue
|
||||
);
|
||||
|
||||
clientSocket.getOutputStream().write(1);
|
||||
pendingHeartbeat.put(username, handler);
|
||||
|
||||
Thread thread = new Thread(handler);
|
||||
thread.start();
|
||||
|
||||
clientHandlers.add(handler);
|
||||
connectedPlayers++;
|
||||
continue;
|
||||
|
||||
}
|
||||
else {
|
||||
clientSocket.getOutputStream().write(-1);
|
||||
eventAddPlayer.setErrorType(ErrorType.USERNAME_ALREADY_USED);
|
||||
clientSend.writeObject(eventAddPlayer);
|
||||
clientSocket.close();
|
||||
System.out.println("Invalid parameters. Connection terminated.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!playerList.containsKey(username)) {
|
||||
playerList.put(username, true);
|
||||
System.out.println("(After crash)Reconnected player: " + username);
|
||||
|
||||
@@ -222,46 +241,49 @@ public class TCPServer {
|
||||
|
||||
}
|
||||
//reconnect a previously disconnected player
|
||||
if (playerList.containsKey(username)
|
||||
&& !playerList.get(username)) {
|
||||
else{
|
||||
if (!playerList.get(username)) {
|
||||
|
||||
playerList.put(username, true);
|
||||
System.out.println("Reconnected player: " + username);
|
||||
playerList.put(username, true);
|
||||
System.out.println("Reconnected player: " + username);
|
||||
|
||||
ClientHandler handler = new ClientHandler(
|
||||
username,
|
||||
clientSocket,
|
||||
clientSend,
|
||||
clientReceive,
|
||||
clientHandlers,
|
||||
playerList,
|
||||
actionQueue
|
||||
);
|
||||
ClientHandler handler = new ClientHandler(
|
||||
username,
|
||||
clientSocket,
|
||||
clientSend,
|
||||
clientReceive,
|
||||
clientHandlers,
|
||||
playerList,
|
||||
actionQueue
|
||||
);
|
||||
|
||||
clientSocket.getOutputStream().write(1);
|
||||
pendingHeartbeat.put(username, handler);
|
||||
clientSocket.getOutputStream().write(1);
|
||||
pendingHeartbeat.put(username, handler);
|
||||
|
||||
Game game = controller.getModel();
|
||||
handler.notifyMiniModel(new MiniModel(
|
||||
game.getSlotMap(),
|
||||
game.orderLogicCard,
|
||||
game.getCurrentState(),
|
||||
game.getPlayers(),
|
||||
game.getAvailableTotems(),
|
||||
game.getUpperListTribeCards(),game.getLowerListTribeCards(),game.getUpperListBuilding(),game.getLowerListBuilding()
|
||||
));
|
||||
Game game = controller.getModel();
|
||||
handler.notifyMiniModel(new MiniModel(
|
||||
game.getSlotMap(),
|
||||
game.orderLogicCard,
|
||||
game.getCurrentState(),
|
||||
game.getPlayers(),
|
||||
game.getAvailableTotems(),
|
||||
game.getUpperListTribeCards(), game.getLowerListTribeCards(), game.getUpperListBuilding(), game.getLowerListBuilding()
|
||||
));
|
||||
|
||||
Thread thread = new Thread(handler);
|
||||
thread.start();
|
||||
Thread thread = new Thread(handler);
|
||||
thread.start();
|
||||
|
||||
clientHandlers.add(handler);
|
||||
connectedPlayers++;
|
||||
actionQueue.add(new ReconnectPlayer(username));
|
||||
clientHandlers.add(handler);
|
||||
connectedPlayers++;
|
||||
actionQueue.add(new ReconnectPlayer(username));
|
||||
|
||||
} else {
|
||||
clientSocket.getOutputStream().write(-1);
|
||||
clientSocket.close();
|
||||
System.out.println("Player could not be added. Connection terminated.");
|
||||
} else {
|
||||
clientSocket.getOutputStream().write(-1);
|
||||
eventAddPlayer.setErrorType(ErrorType.USER_ALREADY_CONNECTED);
|
||||
clientSend.writeObject(eventAddPlayer);
|
||||
clientSocket.close();
|
||||
System.out.println("Invalid parameters. Connection terminated.");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
|
||||
Reference in New Issue
Block a user