Fixed: TUI endGame loop
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package it.polimi.ingsw.gc14;
|
package it.polimi.ingsw.gc14;
|
||||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||||
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character;
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character;
|
||||||
|
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||||
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
|
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
|
||||||
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
|
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
|
||||||
@@ -47,68 +48,70 @@ public class ClientLauncherTUI {
|
|||||||
admissibleChar.add("5");
|
admissibleChar.add("5");
|
||||||
admissibleChar.add("6");
|
admissibleChar.add("6");
|
||||||
admissibleChar.add("7");
|
admissibleChar.add("7");
|
||||||
|
admissibleChar.add("8");
|
||||||
admissibleChar.add("A");
|
admissibleChar.add("A");
|
||||||
admissibleChar.add("B");
|
admissibleChar.add("B");
|
||||||
admissibleChar.add("C");
|
admissibleChar.add("C");
|
||||||
admissibleChar.add("a");
|
admissibleChar.add("a");
|
||||||
admissibleChar.add("b");
|
admissibleChar.add("b");
|
||||||
admissibleChar.add("c");
|
admissibleChar.add("c");
|
||||||
|
|
||||||
ClientController controller = new ClientController(view);
|
ClientController controller = new ClientController(view);
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
System.out.println("Insert username: ");
|
while (true) {
|
||||||
String username = scanner.next();
|
System.out.println("Insert username: ");
|
||||||
view.setUsername(username);
|
String username = scanner.next();
|
||||||
controller.setMyUsername(username);
|
view.setUsername(username);
|
||||||
System.out.println("Insert preferred number of players: ");
|
controller.setMyUsername(username);
|
||||||
int proposedNumPlayers = scanner.nextInt();
|
System.out.println("Insert preferred number of players: ");
|
||||||
System.out.println("Select RMI[0] o TCP[1]: ");
|
int proposedNumPlayers = scanner.nextInt();
|
||||||
int networkType = scanner.nextInt();
|
System.out.println("Select RMI[0] o TCP[1]: ");
|
||||||
System.out.println("Insert server IP: ");
|
int networkType = scanner.nextInt();
|
||||||
String IP = scanner.next();
|
System.out.println("Insert server IP: ");
|
||||||
|
String IP = scanner.next();
|
||||||
|
|
||||||
// RMI
|
// RMI
|
||||||
if (networkType == 0) {
|
if (networkType == 0) {
|
||||||
// Connect
|
// Connect
|
||||||
String myIP;
|
String myIP;
|
||||||
try {
|
try {
|
||||||
myIP = chooseNetworkInterface(scanner);
|
myIP = chooseNetworkInterface(scanner);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
System.setProperty("java.rmi.server.hostname", myIP);
|
System.setProperty("java.rmi.server.hostname", myIP);
|
||||||
RMIClient client = new RMIClient(controller, IP, 1099, myIP);
|
RMIClient client = new RMIClient(controller, IP, 1099, myIP);
|
||||||
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
|
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
|
||||||
if (serverResponse==null) {
|
if (serverResponse==null) {
|
||||||
System.out.println("Succesfully connected to RMI server\n\n");
|
System.out.println("Succesfully connected to RMI server\n\n");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("RMI connection refused\n\n");
|
System.out.println("RMI connection refused\n\n");
|
||||||
controller.view.showError(serverResponse,serverResponse.toString());
|
controller.view.showError(serverResponse,serverResponse.toString());
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
controller.setClient(client);
|
controller.setClient(client);
|
||||||
while (true) {
|
while (controller.getClient() != null) {
|
||||||
getInput(scanner, controller, username);
|
getInput(scanner, controller, username);
|
||||||
}
|
}
|
||||||
// TCP
|
// TCP
|
||||||
} else if (networkType == 1) {
|
} else if (networkType == 1) {
|
||||||
// Connect
|
// Connect
|
||||||
TCPClient client = new TCPClient(controller, IP, 8080,8081);
|
TCPClient client = new TCPClient(controller, IP, 8080,8081);
|
||||||
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
|
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
|
||||||
if (serverResponse==null) {
|
if (serverResponse==null) {
|
||||||
System.out.println("Succesfully connected to TCP server\n\n");
|
System.out.println("Succesfully connected to TCP server\n\n");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("TCP connection refused\n\n");
|
System.out.println("TCP connection refused\n\n");
|
||||||
controller.view.showError(serverResponse,serverResponse.toString());
|
controller.view.showError(serverResponse,serverResponse.toString());
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
controller.setClient(client);
|
controller.setClient(client);
|
||||||
// Play
|
// Play
|
||||||
while (true) {
|
while (controller.getClient() != null) {
|
||||||
getInput(scanner, controller, username);
|
getInput(scanner, controller, username);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scanner.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -139,9 +142,23 @@ public class ClientLauncherTUI {
|
|||||||
* @param username the username of the current player.
|
* @param username the username of the current player.
|
||||||
*/
|
*/
|
||||||
private void getInput(Scanner scanner, ClientController controller, String username) {
|
private void getInput(Scanner scanner, ClientController controller, String username) {
|
||||||
String action = scanner.next();
|
String action = scanner.nextLine().trim();
|
||||||
int pos = -1;
|
int pos = -1;
|
||||||
|
if(controller.getClient() == null)
|
||||||
|
{
|
||||||
|
if(action.equals("0"))
|
||||||
|
{
|
||||||
|
System.out.println("Are you sure? Y/N");
|
||||||
|
String c= scanner.next();
|
||||||
|
if(c.equals("Y") || c.equals("y"))
|
||||||
|
{
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(action.isEmpty())
|
||||||
|
return;
|
||||||
if(admissibleChar.contains(action))
|
if(admissibleChar.contains(action))
|
||||||
{
|
{
|
||||||
if(action.equals("0"))
|
if(action.equals("0"))
|
||||||
@@ -153,7 +170,7 @@ public class ClientLauncherTUI {
|
|||||||
view.render();
|
view.render();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}else if (!action.equals("6") && !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) {
|
}else if (!action.equals("6")&&!action.equals("8") && !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) {
|
||||||
try {
|
try {
|
||||||
System.out.println("Insert the required position:");
|
System.out.println("Insert the required position:");
|
||||||
pos = scanner.nextInt();
|
pos = scanner.nextInt();
|
||||||
@@ -163,10 +180,6 @@ public class ClientLauncherTUI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "0" -> {
|
|
||||||
controller.disconnect();
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
case "1" -> controller.slotChoice(pos);
|
case "1" -> controller.slotChoice(pos);
|
||||||
case "2" -> controller.drawUpperTribeCard(pos);
|
case "2" -> controller.drawUpperTribeCard(pos);
|
||||||
case "3" -> controller.drawUpperBuildingCard(pos);
|
case "3" -> controller.drawUpperBuildingCard(pos);
|
||||||
@@ -174,6 +187,13 @@ public class ClientLauncherTUI {
|
|||||||
case "5" -> controller.drawLowerBuildingCard(pos);
|
case "5" -> controller.drawLowerBuildingCard(pos);
|
||||||
case "6" -> controller.skipTurn();
|
case "6" -> controller.skipTurn();
|
||||||
case "7" -> controller.totemChoice(pos);
|
case "7" -> controller.totemChoice(pos);
|
||||||
|
case "8" ->{
|
||||||
|
if(controller.miniModel.currentState.getGameStage().equals(GameStages.ENDED)) {
|
||||||
|
controller.disconnect();
|
||||||
|
controller.setClient(null);
|
||||||
|
controller.setModel(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
case "A", "a" -> view.fullRender();
|
case "A", "a" -> view.fullRender();
|
||||||
case "B", "b" -> view.renderBoard();
|
case "B", "b" -> view.renderBoard();
|
||||||
case "C", "c" -> view.renderPlayer();
|
case "C", "c" -> view.renderPlayer();
|
||||||
|
|||||||
@@ -49,6 +49,15 @@ public class ClientController {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the network client.
|
||||||
|
*
|
||||||
|
* @return client return the client, either TCP or RMI.
|
||||||
|
*/
|
||||||
|
public IClient getClient() {
|
||||||
|
return this.client;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the local mini model and updates the view accordingly.
|
* Sets the local mini model and updates the view accordingly.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -126,6 +126,8 @@ public class RMIClient implements IClient {
|
|||||||
if (!running) return;
|
if (!running) return;
|
||||||
running = false;
|
running = false;
|
||||||
if (pingSender != null) pingSender.shutdownNow();
|
if (pingSender != null) pingSender.shutdownNow();
|
||||||
|
controller.miniModel=null;
|
||||||
|
controller.setClient(null);
|
||||||
controller.view.showError(ErrorType.SERVER_CRASHED,ErrorType.SERVER_CRASHED.toString());
|
controller.view.showError(ErrorType.SERVER_CRASHED,ErrorType.SERVER_CRASHED.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -153,6 +153,8 @@ public class TCPClient implements IClient {
|
|||||||
running = false;
|
running = false;
|
||||||
try { communicationSocket.close(); } catch (IOException ignored) {}
|
try { communicationSocket.close(); } catch (IOException ignored) {}
|
||||||
try { heartbeatSocket.close(); } catch (IOException ignored) {}
|
try { heartbeatSocket.close(); } catch (IOException ignored) {}
|
||||||
|
controller.setClient(null);
|
||||||
|
controller.setModel(null);
|
||||||
controller.view.showError(ErrorType.SERVER_CRASHED,ErrorType.SERVER_CRASHED.toString());
|
controller.view.showError(ErrorType.SERVER_CRASHED,ErrorType.SERVER_CRASHED.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -101,6 +101,7 @@ public class TUI implements IView {
|
|||||||
else if(model.currentState.getGameStage().equals(GameStages.ENDED))
|
else if(model.currentState.getGameStage().equals(GameStages.ENDED))
|
||||||
{
|
{
|
||||||
renderStanding();
|
renderStanding();
|
||||||
|
System.out.println("Press 8 to start another game or press 0 to exit");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -238,6 +239,8 @@ public class TUI implements IView {
|
|||||||
clearTerminal();
|
clearTerminal();
|
||||||
render();
|
render();
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
|
if(error==ErrorType.SERVER_CRASHED)
|
||||||
|
System.out.println("Press any key to continue");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user