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; 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,15 +48,16 @@ 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);
while (true) {
System.out.println("Insert username: "); System.out.println("Insert username: ");
String username = scanner.next(); String username = scanner.next();
view.setUsername(username); view.setUsername(username);
@@ -84,10 +86,10 @@ public class ClientLauncherTUI {
} 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
@@ -100,15 +102,16 @@ public class ClientLauncherTUI {
} 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");
} }
/** /**