Merge pull request #97 from rubenpirreram/TUI-Fix

T UI fix
This commit is contained in:
rubenpirreram
2026-05-13 18:05:33 +02:00
committed by GitHub
5 changed files with 74 additions and 12 deletions
@@ -47,6 +47,7 @@ public class ClientLauncherTUI {
admissibleChar.add("6"); admissibleChar.add("6");
admissibleChar.add("7"); admissibleChar.add("7");
admissibleChar.add("8"); admissibleChar.add("8");
admissibleChar.add("9");
admissibleChar.add("A"); admissibleChar.add("A");
admissibleChar.add("B"); admissibleChar.add("B");
admissibleChar.add("C"); admissibleChar.add("C");
@@ -137,7 +138,7 @@ public class ClientLauncherTUI {
if(admissibleChar.contains(action)) if(admissibleChar.contains(action))
{ {
if (!action.equals("7") && !action.equals("8")&& !action.equals("9") && !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) { if (!action.equals("7") && !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();
@@ -19,6 +19,7 @@ import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
import java.io.Serializable; import java.io.Serializable;
import java.util.*; import java.util.*;
import java.util.random.RandomGenerator;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -61,11 +62,27 @@ public class Game implements Serializable {
} }
player.totem=totem; player.totem=totem;
Player nextPlayer= totemChoiceQueue.poll(); Player nextPlayer= totemChoiceQueue.poll();
if(nextPlayer==null) if(nextPlayer==null)
{ {
currentState.GameStageUpdate(GameStages.SLOT_CHOICE); currentState.GameStageUpdate(GameStages.SLOT_CHOICE);
currentState.PlayerUpdate(orderLogicCard.pull(),null); currentState.PlayerUpdate(orderLogicCard.pull(),null);
} }
else
{
while(disconnetedPlayers.containsKey(nextPlayer)&& disconnetedPlayers.get(nextPlayer))
{
nextPlayer.totem=getAvailableTotems().get(new Random().nextInt(getAvailableTotems().size()-1));
nextPlayer=totemChoiceQueue.poll();
if(nextPlayer==null)
{
currentState.GameStageUpdate(GameStages.SLOT_CHOICE);
currentState.PlayerUpdate(orderLogicCard.pull(),null);
return true;
}
}
currentState.PlayerUpdate(nextPlayer,null);
}
return true; return true;
} }
@@ -80,7 +97,20 @@ public class Game implements Serializable {
} }
disconnetedPlayers.put(player,true); disconnetedPlayers.put(player,true);
if(currentState.getCurrentPlayer().equals(player)) if(currentState.getCurrentPlayer().equals(player))
if(!currentState.getGameStage().equals(GameStages.TOTEM_CHOICE))
nextPlayerSetup(); nextPlayerSetup();
else
{
if(totemChoiceQueue.isEmpty())
{
player.totem=getAvailableTotems().get(new Random().nextInt(getAvailableTotems().size()-1));
}
else {
totemChoiceQueue.add(player);
}
currentState.PlayerUpdate(totemChoiceQueue.poll(),null);
}
return true; return true;
} }
@@ -254,6 +254,9 @@ public class Player implements Serializable {
table.addHeader(this.getUserName()); table.addHeader(this.getUserName());
table.addRow("RESOURCES:"); table.addRow("RESOURCES:");
if(this.totem != null) {
table.addRow("Totem: " + this.totem.toString());
}
table.addRow("Food: " + this.getFoodValue()); table.addRow("Food: " + this.getFoodValue());
table.addRow("Prestige: " + this.getPrestigeValue()); table.addRow("Prestige: " + this.getPrestigeValue());
table.addSeparator(); table.addSeparator();
@@ -11,6 +11,7 @@ import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound; import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound;
import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer; import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer;
import it.polimi.ingsw.gc14.Network.NetworkEvents.TotemChoice;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
import it.polimi.ingsw.gc14.View.TUI.TUI; import it.polimi.ingsw.gc14.View.TUI.TUI;
@@ -116,21 +117,23 @@ public class ServerLauncher {
int roundPrec=gameController.getModel().getCurrentState().getRound(); int roundPrec=gameController.getModel().getCurrentState().getRound();
synchronized(gameController){ synchronized(gameController){
event.setIsError(!event.apply(gameController)); event.setIsError(!event.apply(gameController));
Game game=gameController.getModel();
if(!event.getIsError()) if(!event.getIsError())
{ {
event.setData(gameController.getModel().getSlotMap(),gameController.getModel().orderLogicCard,gameController.getModel().getCurrentState(),gameController.getModel().getPlayerByUsername(event.getUsername())); event.setData(game.getSlotMap(),game.orderLogicCard,game.getCurrentState(),game.getPlayerByUsername(event.getUsername()));
if(event.getEventType().equals(EventType.TOTEM_CHOICE))
((TotemChoice)event).setAvailableTotems(gameController.getModel().getAvailableTotems());
} }
serverRMI.notifyAll(event); serverRMI.notifyAll(event);
serverTCP.notifyAll(event); serverTCP.notifyAll(event);
if(gameController.getModel().getCurrentState().getRound()!=roundPrec) if(game.getCurrentState().getRound()!=roundPrec)
{ {
Game game=gameController.getModel();
ApplyNextRound nextRound=new ApplyNextRound(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(),game.getPlayers()); ApplyNextRound nextRound=new ApplyNextRound(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(),game.getPlayers());
serverRMI.notifyAll(nextRound); serverRMI.notifyAll(nextRound);
serverTCP.notifyAll(nextRound); serverTCP.notifyAll(nextRound);
} }
if(!event.getIsError()){ if(!event.getIsError()){
if(this.gameController.getModel().getCurrentState().getGameStage() == GameStages.ENDED){ if(game.getCurrentState().getGameStage() == GameStages.ENDED){
if(!this.deleteSave()){ if(!this.deleteSave()){
System.out.println("\n!!! Couldn't delete save !!!\n"); System.out.println("\n!!! Couldn't delete save !!!\n");
} }
@@ -1,8 +1,6 @@
package it.polimi.ingsw.gc14.View.TUI; package it.polimi.ingsw.gc14.View.TUI;
import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.*;
import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Slot;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
import java.util.ArrayList; import java.util.ArrayList;
@@ -92,8 +90,21 @@ public class TUI implements IView {
*/ */
@Override @Override
public void render() { public void render() {
if(model.currentState.getGameStage().equals(GameStages.TOTEM_CHOICE))
{
renderTotems();
}
else if(model.currentState.getGameStage().equals(GameStages.ENDED))
{
//renderStanding
renderBoard(); renderBoard();
} }
else
{
renderBoard();
}
System.out.println("\nYOUR ACTION:");
}
/** /**
* Renders a full view of the game, combining both player status * Renders a full view of the game, combining both player status
@@ -141,10 +152,23 @@ public class TUI implements IView {
System.out.println(AsciiTable.sideBySide(lines, lines2, 3)); System.out.println(AsciiTable.sideBySide(lines, lines2, 3));
} }
public void renderTotems() {
clearTerminal();
var table = new AsciiTable(BorderStyle.ROUNDED, model.availableTotems.size());
List<String> lines = model.availableTotems.stream().map(Totems::toString).toList();
int i=0;
for(String line : lines)
{
line = (i++) +". "+ line;
}
table.addRow(lines);
System.out.println(model.currentState+"\nTOTEMS AVAILABLE:\n"+table.build());
}
/** /**
* Renders the player status table only. * Renders the player status table only.
* *
* <p>Layout: * <p>Layout:SkipTurn
* <ul> * <ul>
* <li><b>Left panel</b> — prestige, food, character deck, and building * <li><b>Left panel</b> — prestige, food, character deck, and building
* deck for all players, as produced by {@link #PlayersStamp()}.</li> * deck for all players, as produced by {@link #PlayersStamp()}.</li>
@@ -201,6 +225,7 @@ public class TUI implements IView {
table.addRow(List.of("6-PickOptionalBuilding(pos)", "")); table.addRow(List.of("6-PickOptionalBuilding(pos)", ""));
table.addRow(List.of("7-NoOptional", "")); table.addRow(List.of("7-NoOptional", ""));
table.addRow(List.of("8-SkipTurn", "")); table.addRow(List.of("8-SkipTurn", ""));
table.addRow(List.of("9-TotemChoice(pos)", ""));
return table.build(); return table.build();
} }