Fixed: TUI endGame loop

This commit is contained in:
rubenpirreram
2026-05-26 20:15:19 +02:00
parent bfbf7890d3
commit f3d4c181c4
5 changed files with 94 additions and 58 deletions
@@ -1,6 +1,7 @@
package it.polimi.ingsw.gc14;
import it.polimi.ingsw.gc14.Controller.ClientController;
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.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
@@ -47,68 +48,70 @@ public class ClientLauncherTUI {
admissibleChar.add("5");
admissibleChar.add("6");
admissibleChar.add("7");
admissibleChar.add("8");
admissibleChar.add("A");
admissibleChar.add("B");
admissibleChar.add("C");
admissibleChar.add("a");
admissibleChar.add("b");
admissibleChar.add("c");
ClientController controller = new ClientController(view);
Scanner scanner = new Scanner(System.in);
System.out.println("Insert username: ");
String username = scanner.next();
view.setUsername(username);
controller.setMyUsername(username);
System.out.println("Insert preferred number of players: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println("Select RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println("Insert server IP: ");
String IP = scanner.next();
while (true) {
System.out.println("Insert username: ");
String username = scanner.next();
view.setUsername(username);
controller.setMyUsername(username);
System.out.println("Insert preferred number of players: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println("Select RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println("Insert server IP: ");
String IP = scanner.next();
// RMI
if (networkType == 0) {
// Connect
String myIP;
try {
myIP = chooseNetworkInterface(scanner);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.setProperty("java.rmi.server.hostname", myIP);
RMIClient client = new RMIClient(controller, IP, 1099, myIP);
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
if (serverResponse==null) {
System.out.println("Succesfully connected to RMI server\n\n");
} else {
System.out.println("RMI connection refused\n\n");
controller.view.showError(serverResponse,serverResponse.toString());
return;
}
controller.setClient(client);
while (true) {
getInput(scanner, controller, username);
}
// TCP
} else if (networkType == 1) {
// Connect
TCPClient client = new TCPClient(controller, IP, 8080,8081);
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
if (serverResponse==null) {
System.out.println("Succesfully connected to TCP server\n\n");
} else {
System.out.println("TCP connection refused\n\n");
controller.view.showError(serverResponse,serverResponse.toString());
return;
}
controller.setClient(client);
// Play
while (true) {
getInput(scanner, controller, username);
// RMI
if (networkType == 0) {
// Connect
String myIP;
try {
myIP = chooseNetworkInterface(scanner);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.setProperty("java.rmi.server.hostname", myIP);
RMIClient client = new RMIClient(controller, IP, 1099, myIP);
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
if (serverResponse==null) {
System.out.println("Succesfully connected to RMI server\n\n");
} else {
System.out.println("RMI connection refused\n\n");
controller.view.showError(serverResponse,serverResponse.toString());
continue;
}
controller.setClient(client);
while (controller.getClient() != null) {
getInput(scanner, controller, username);
}
// TCP
} else if (networkType == 1) {
// Connect
TCPClient client = new TCPClient(controller, IP, 8080,8081);
ErrorType serverResponse=client.connect(username, proposedNumPlayers);
if (serverResponse==null) {
System.out.println("Succesfully connected to TCP server\n\n");
} else {
System.out.println("TCP connection refused\n\n");
controller.view.showError(serverResponse,serverResponse.toString());
continue;
}
controller.setClient(client);
// Play
while (controller.getClient() != null) {
getInput(scanner, controller, username);
}
}
}
scanner.close();
}
/**
@@ -139,9 +142,23 @@ public class ClientLauncherTUI {
* @param username the username of the current player.
*/
private void getInput(Scanner scanner, ClientController controller, String username) {
String action = scanner.next();
String action = scanner.nextLine().trim();
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(action.equals("0"))
@@ -153,7 +170,7 @@ public class ClientLauncherTUI {
view.render();
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 {
System.out.println("Insert the required position:");
pos = scanner.nextInt();
@@ -163,10 +180,6 @@ public class ClientLauncherTUI {
}
}
switch (action) {
case "0" -> {
controller.disconnect();
System.exit(0);
}
case "1" -> controller.slotChoice(pos);
case "2" -> controller.drawUpperTribeCard(pos);
case "3" -> controller.drawUpperBuildingCard(pos);
@@ -174,6 +187,13 @@ public class ClientLauncherTUI {
case "5" -> controller.drawLowerBuildingCard(pos);
case "6" -> controller.skipTurn();
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 "B", "b" -> view.renderBoard();
case "C", "c" -> view.renderPlayer();