Fix: Replaced Model With MiniModel in the client implementation and in the view
This commit is contained in:
@@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.View.GUI.MainView;
|
||||
import it.polimi.ingsw.gc14.View.IView;
|
||||
import javafx.application.Platform;
|
||||
@@ -11,7 +12,7 @@ public class GUI implements IView { // stessa interfaccia che implementa TUI
|
||||
|
||||
private final Stage stage;
|
||||
private final MainView mainView;
|
||||
private Game model;
|
||||
private MiniModel model;
|
||||
private ClientController controller;
|
||||
private String username;
|
||||
|
||||
@@ -21,9 +22,9 @@ public class GUI implements IView { // stessa interfaccia che implementa TUI
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setModel(Game model) {
|
||||
public void setModel(MiniModel model) {
|
||||
this.model = model;
|
||||
mainView.setModel(model, controller, username);
|
||||
mainView.setModel(model,controller, username);
|
||||
|
||||
Platform.runLater(() -> {
|
||||
stage.setScene(mainView.getScene());
|
||||
|
||||
@@ -47,12 +47,12 @@ public class MainFXMLController {
|
||||
private Popup popup;
|
||||
private HBox popupCards;
|
||||
|
||||
private Game model;
|
||||
private MiniModel model;
|
||||
private ClientController controller;
|
||||
private String username;
|
||||
private final Map<String, ImageView> cardCache = new HashMap<>();
|
||||
|
||||
public void setModel(Game model) {this.model = model;}
|
||||
public void setModel(MiniModel model) {this.model = model;}
|
||||
public void setController(ClientController controller) {this.controller = controller;}
|
||||
public void setUsername(String username) {this.username = username;}
|
||||
|
||||
@@ -116,9 +116,9 @@ public class MainFXMLController {
|
||||
img.setClip(clip);
|
||||
StackPane wrapper = new StackPane(img);
|
||||
|
||||
Player player = model.getSlotMap().get(slot);
|
||||
Player player = model.slotPlayerMap.get(slot);
|
||||
if (player != null) {
|
||||
int index = model.getPlayers().indexOf(player);
|
||||
int index = new ArrayList<>(model.players.values()).indexOf(player);
|
||||
ImageView totem = new ImageView(new Image(getClass().getResourceAsStream("/GUIImages/Totems/totem_" + index + ".png")));
|
||||
totem.setFitHeight(70);
|
||||
totem.setPreserveRatio(true);
|
||||
@@ -221,7 +221,7 @@ public class MainFXMLController {
|
||||
upperList.getChildren().clear();
|
||||
|
||||
int i = 0;
|
||||
for (TribeCard card : model.getUpperListTribeCards()) {
|
||||
for (TribeCard card : model.board.upperListTribe) {
|
||||
final int index = i;
|
||||
StackPane img = createCard(card, true, true);
|
||||
img.setOnMouseClicked(e -> controller.drawUpperTribeCard(username, index));
|
||||
@@ -229,7 +229,7 @@ public class MainFXMLController {
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
for (BuildingCard card : model.getUpperListBuilding()) {
|
||||
for (BuildingCard card : model.board.upperListBuilding) {
|
||||
final int index = i;
|
||||
StackPane img = createCard(card, true, true);
|
||||
img.setOnMouseClicked(e -> controller.drawUpperBuildingCard(username, index));
|
||||
@@ -238,7 +238,7 @@ public class MainFXMLController {
|
||||
}
|
||||
}
|
||||
private void renderOrder() {
|
||||
StackPane card = createOrder(Integer.toString(model.getNPlayers()), false, false);
|
||||
StackPane card = createOrder(Integer.toString(model.players.size()), false, false);
|
||||
|
||||
VBox overlay = new VBox(-20);
|
||||
overlay.setPickOnBounds(false);
|
||||
@@ -246,13 +246,13 @@ public class MainFXMLController {
|
||||
StackPane.setMargin(overlay, new Insets(9, 0, 0, 57));
|
||||
|
||||
int i = 0;
|
||||
for (Player player : model.getPlayers()) {
|
||||
for (Player player : model.players.values()) {
|
||||
final int index = i;
|
||||
|
||||
ImageView totem = new ImageView(new Image(getClass().getResourceAsStream("/GUIImages/Totems/totem_" + index + ".png")));
|
||||
totem.setFitHeight(70);
|
||||
totem.setPreserveRatio(true);
|
||||
if (!(model.orderLogicCard.getPosition(player.getUserName()) != -1 && !model.getSlotMap().containsValue(player))) {
|
||||
if (!(model.orderLogicCard.getPosition(player.getUserName()) != -1 && !model.slotPlayerMap.containsValue(player))) {
|
||||
totem.setOpacity(0.0);
|
||||
}
|
||||
overlay.getChildren().add(totem);
|
||||
@@ -267,7 +267,7 @@ public class MainFXMLController {
|
||||
board.getChildren().clear();
|
||||
|
||||
String path = "";
|
||||
switch (model.getCurrentState().getEra()) {
|
||||
switch (model.currentState.getEra()) {
|
||||
case 1 -> path = "/GUIImages/Backs/back-001.png";
|
||||
case 2 -> path = "/GUIImages/Backs/back-030.png";
|
||||
case 3 -> path = "/GUIImages/Backs/back-058.png";
|
||||
@@ -289,7 +289,7 @@ public class MainFXMLController {
|
||||
renderOrder();
|
||||
|
||||
int i = 0;
|
||||
for (Map.Entry<Slot, Player> entry : model.getSlotMap().entrySet()) {
|
||||
for (Map.Entry<Slot, Player> entry : model.slotPlayerMap.entrySet()) {
|
||||
Slot slot = entry.getKey();
|
||||
final int index = i;
|
||||
StackPane img = createSlot(slot, true, true);
|
||||
@@ -304,7 +304,7 @@ public class MainFXMLController {
|
||||
lowerList.getChildren().clear();
|
||||
|
||||
int i = 0;
|
||||
for (TribeCard card : model.getLowerListTribeCards()) {
|
||||
for (TribeCard card : model.board.lowerListTribe) {
|
||||
final int index = i;
|
||||
StackPane img = createCard(card, true, true);
|
||||
img.setOnMouseClicked(e -> controller.drawLowerTribeCard(username, index));
|
||||
@@ -312,7 +312,7 @@ public class MainFXMLController {
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
for (BuildingCard card : model.getLowerListBuilding()) {
|
||||
for (BuildingCard card : model.board.lowerListBuilding) {
|
||||
final int index = i;
|
||||
StackPane img = createCard(card, true, true);
|
||||
img.setOnMouseClicked(e -> controller.drawLowerBuildingCard(username, index));
|
||||
@@ -327,13 +327,13 @@ public class MainFXMLController {
|
||||
myHand.setSpacing(14);
|
||||
myHand.setAlignment(Pos.CENTER);
|
||||
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).artists);
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).gatherers);
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).inventors);
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).builders);
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).shamans);
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).hunters);
|
||||
drawMyHandList(myHand, model.getPlayerByUsername(username).buildingCards);
|
||||
drawMyHandList(myHand, model.players.get(username).artists);
|
||||
drawMyHandList(myHand, model.players.get(username).gatherers);
|
||||
drawMyHandList(myHand, model.players.get(username).inventors);
|
||||
drawMyHandList(myHand, model.players.get(username).builders);
|
||||
drawMyHandList(myHand, model.players.get(username).shamans);
|
||||
drawMyHandList(myHand, model.players.get(username).hunters);
|
||||
drawMyHandList(myHand, model.players.get(username).buildingCards);
|
||||
}
|
||||
private void drawMyHandList(HBox myHand, ArrayList<? extends PlayableCard> cardList) {
|
||||
if (!cardList.isEmpty()) {
|
||||
@@ -361,16 +361,16 @@ public class MainFXMLController {
|
||||
sidePanel.getChildren().clear();
|
||||
sidePanel.setSpacing(6);
|
||||
|
||||
era.setText(Integer.toString(model.getCurrentState().getEra()));
|
||||
round.setText(Integer.toString(model.getCurrentState().getRound()));
|
||||
player.setText(model.getCurrentState().getCurrentPlayer().getUserName());
|
||||
era.setText(Integer.toString(model.currentState.getEra()));
|
||||
round.setText(Integer.toString(model.currentState.getRound()));
|
||||
player.setText(model.currentState.getCurrentPlayer().getUserName());
|
||||
VBox.setMargin(info, new Insets(5, 5, 5, 5));
|
||||
sidePanel.getChildren().add(info);
|
||||
|
||||
renderPlayers(sidePanel);
|
||||
}
|
||||
private void renderPlayers(VBox playersHand) {
|
||||
for (Player player : model.getPlayers()) {
|
||||
for (Player player : model.players.values()) {
|
||||
// Card container
|
||||
VBox card = new VBox(3);
|
||||
VBox.setMargin(card, new Insets(5));
|
||||
@@ -382,7 +382,7 @@ public class MainFXMLController {
|
||||
// Riga 1: icona totem + nome
|
||||
HBox headerRow = new HBox(8);
|
||||
headerRow.setAlignment(Pos.CENTER);
|
||||
int index = model.getPlayers().indexOf(player);
|
||||
int index = new ArrayList<>(model.players.values()).indexOf(player);
|
||||
ImageView totem = new ImageView(new Image(getClass().getResourceAsStream("/GUIImages/Totems/totem_" + index + ".png")));
|
||||
totem.setFitHeight(20);
|
||||
totem.setPreserveRatio(true);
|
||||
@@ -465,7 +465,7 @@ public class MainFXMLController {
|
||||
}
|
||||
}
|
||||
private ArrayList<PlayableCard> getPlayerCards(String username, String type) {
|
||||
Player p = model.getPlayerByUsername(username);
|
||||
Player p = model.players.get(username);
|
||||
return switch (type) {
|
||||
case "artists" -> new ArrayList<>(p.artists);
|
||||
case "gatherers" -> new ArrayList<>(p.gatherers);
|
||||
|
||||
@@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
@@ -15,7 +16,7 @@ public class MainView {
|
||||
|
||||
private final Scene scene;
|
||||
private final MainFXMLController fxmlController;
|
||||
private Game model;
|
||||
private MiniModel model;
|
||||
private ClientController controller;
|
||||
private String username;
|
||||
|
||||
@@ -32,7 +33,7 @@ public class MainView {
|
||||
}
|
||||
|
||||
public Scene getScene() { return scene; }
|
||||
public void setModel(Game model, ClientController controller, String username) {
|
||||
public void setModel(MiniModel model, ClientController controller, String username) {
|
||||
this.model = model;
|
||||
fxmlController.setModel(model);
|
||||
this.controller = controller;
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package it.polimi.ingsw.gc14.View;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
|
||||
public interface IView {
|
||||
public void setModel(Game game);
|
||||
public void setModel(MiniModel miniModel);
|
||||
public void render();
|
||||
public void showMessage(String message);
|
||||
public void showError(String message);
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
package it.polimi.ingsw.gc14.View.TUI;
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Model.Player;
|
||||
import it.polimi.ingsw.gc14.Model.Slot;
|
||||
import it.polimi.ingsw.gc14.View.IView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Text-based User Interface (TUI) implementation of {@link IView}.
|
||||
@@ -30,9 +36,9 @@ import java.util.List;
|
||||
public class TUI implements IView {
|
||||
|
||||
/**
|
||||
* The {@link Game} model whose state is rendered.
|
||||
* The {@link MiniModel} model whose state is rendered.
|
||||
*/
|
||||
private Game model;
|
||||
private MiniModel model;
|
||||
|
||||
/**
|
||||
* The username of the local player, used to retrieve and display
|
||||
@@ -50,7 +56,7 @@ public class TUI implements IView {
|
||||
*
|
||||
* @param model the {@link Game} model to display; must not be {@code null}
|
||||
*/
|
||||
public TUI(Game model) {
|
||||
public TUI(MiniModel model) {
|
||||
this.model = model;
|
||||
this.username = "";
|
||||
}
|
||||
@@ -76,7 +82,7 @@ public class TUI implements IView {
|
||||
* @param model the new {@link Game} model; must not be {@code null}
|
||||
*/
|
||||
@Override
|
||||
public void setModel(Game model) {
|
||||
public void setModel(MiniModel model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@@ -96,9 +102,9 @@ public class TUI implements IView {
|
||||
* <p>Layout:
|
||||
* <ul>
|
||||
* <li><b>Top</b> — player status table produced by
|
||||
* {@link Game#PlayersStamp()}.</li>
|
||||
* {@link #PlayersStamp()}.</li>
|
||||
* <li><b>Bottom-left</b> — board status produced by
|
||||
* {@link Game#BoardStamp()}.</li>
|
||||
* {@link #BoardStamp()}.</li>
|
||||
* <li><b>Bottom-right</b> — menu options legend and the local
|
||||
* player's hand.</li>
|
||||
* </ul>
|
||||
@@ -107,10 +113,10 @@ public class TUI implements IView {
|
||||
*/
|
||||
public void fullRender() {
|
||||
clearTerminal();
|
||||
List<String> lines = List.of(model.BoardStamp().split("\n"));
|
||||
List<String> lines = List.of(BoardStamp().split("\n"));
|
||||
List<String> lines2 = List.of((printMenuOptions() + "\n" +
|
||||
model.getPlayerByUsername(username)).split("\n"));
|
||||
System.out.println(model.PlayersStamp() + "\n" +
|
||||
model.players.get(username)).split("\n"));
|
||||
System.out.println(PlayersStamp() + "\n" +
|
||||
AsciiTable.sideBySide(lines, lines2, 3));
|
||||
}
|
||||
|
||||
@@ -120,7 +126,7 @@ public class TUI implements IView {
|
||||
* <p>Layout:
|
||||
* <ul>
|
||||
* <li><b>Left panel</b> — turn order, upper card row, offer track,
|
||||
* and lower card row, as produced by {@link Game#BoardStamp()}.</li>
|
||||
* and lower card row, as produced by {@link #BoardStamp()}.</li>
|
||||
* <li><b>Right panel</b> — menu options legend followed by the local
|
||||
* player's hand.</li>
|
||||
* </ul>
|
||||
@@ -129,9 +135,9 @@ public class TUI implements IView {
|
||||
*/
|
||||
public void renderBoard() {
|
||||
clearTerminal();
|
||||
List<String> lines = List.of(model.BoardStamp().split("\n"));
|
||||
List<String> lines = List.of(BoardStamp().split("\n"));
|
||||
List<String> lines2 = List.of((printMenuOptions() + "\nYOUR HAND\n" +
|
||||
model.getPlayerByUsername(username)).split("\n"));
|
||||
model.players.get(username)).split("\n"));
|
||||
System.out.println(AsciiTable.sideBySide(lines, lines2, 3));
|
||||
}
|
||||
|
||||
@@ -141,7 +147,7 @@ public class TUI implements IView {
|
||||
* <p>Layout:
|
||||
* <ul>
|
||||
* <li><b>Left panel</b> — prestige, food, character deck, and building
|
||||
* deck for all players, as produced by {@link Game#PlayersStamp()}.</li>
|
||||
* deck for all players, as produced by {@link #PlayersStamp()}.</li>
|
||||
* <li><b>Right panel</b> — menu options legend.</li>
|
||||
* </ul>
|
||||
*
|
||||
@@ -149,7 +155,7 @@ public class TUI implements IView {
|
||||
*/
|
||||
public void renderPlayer() {
|
||||
clearTerminal();
|
||||
List<String> lines = List.of(model.PlayersStamp().split("\n"));
|
||||
List<String> lines = List.of(PlayersStamp().split("\n"));
|
||||
List<String> lines2 = List.of(printMenuOptions().split("\n"));
|
||||
System.out.println(AsciiTable.sideBySide(lines, lines2, 3));
|
||||
}
|
||||
@@ -212,4 +218,92 @@ public class TUI implements IView {
|
||||
pb.inheritIO().start().waitFor();
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of all the players currently in the game,
|
||||
* arranged side by side in pairs.
|
||||
* If the number of players is odd, the last player is printed on its own line.
|
||||
*
|
||||
* @return {@code String} - a string representation of all the players.
|
||||
*/
|
||||
public String PlayersStamp()
|
||||
{
|
||||
StringBuilder stringBuilder=new StringBuilder();
|
||||
ArrayList<Player> playersList=new ArrayList<>(model.players.values());
|
||||
for(int i = 0; i< model.players.size()/2; i++)
|
||||
{
|
||||
stringBuilder.append(AsciiTable.sideBySide(List.of(playersList.get((i*2)).toString().split("\n")),List.of(playersList.get((i*2+1)).toString().split("\n")),2));
|
||||
}
|
||||
stringBuilder.append("\n");
|
||||
if(playersList.size()%2!=0)
|
||||
{
|
||||
stringBuilder.append(playersList.get(playersList.size()-1).toString());
|
||||
stringBuilder.append("\n");
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the board, including the current state,
|
||||
* the offer track with slot assignments, the upper and lower tribe card lists,
|
||||
* and the upper and lower building card lists.
|
||||
*
|
||||
* @return {@code String} - a string representation of the board.
|
||||
*/
|
||||
public String BoardStamp()
|
||||
{
|
||||
var offerTrack = new AsciiTable(BorderStyle.UNICODE, model.slotPlayerMap.size());
|
||||
List<String> stringUpOffer=new ArrayList<>();
|
||||
List<String> stringDownOffer=new ArrayList<>();
|
||||
|
||||
int index=0;
|
||||
for(Map.Entry<Slot, Player> entry:model.slotPlayerMap.entrySet())
|
||||
{
|
||||
stringDownOffer.add((index++)+"."+entry.getKey().toStringTUI());
|
||||
if(entry.getValue()!=null)
|
||||
stringUpOffer.add(entry.getValue().getUserName());
|
||||
else
|
||||
stringUpOffer.add(" ");
|
||||
}
|
||||
|
||||
var TribeTableUpper = new AsciiTable(BorderStyle.UNICODE,1);
|
||||
var TribeTableLower = new AsciiTable(BorderStyle.UNICODE,1);
|
||||
|
||||
List<String> stringUpperListTribe=new ArrayList<>();
|
||||
List<String> stringLowerListTribe=new ArrayList<>();
|
||||
stringUpperListTribe.add("Char/Events");
|
||||
stringLowerListTribe.add("Char/Events");
|
||||
for(int i=0;i<Math.max(model.board.upperListTribe.size(),model.board.lowerListTribe.size());i++)
|
||||
{
|
||||
if(i<model.board.upperListTribe.size())
|
||||
{
|
||||
stringUpperListTribe.add(i+"-"+model.board.upperListTribe.get(i).toStringBoard());
|
||||
}
|
||||
if(i<model.board.lowerListTribe.size())
|
||||
{
|
||||
stringLowerListTribe.add(i+"-"+model.board.lowerListTribe.get(i).toStringBoard());
|
||||
}
|
||||
}
|
||||
var BuildTableUpper = new AsciiTable(BorderStyle.UNICODE,1);
|
||||
BuildTableUpper.addHeader("Building ");
|
||||
var BuildTableLower = new AsciiTable(BorderStyle.UNICODE,1);
|
||||
BuildTableLower.addHeader("Building ");
|
||||
|
||||
for(int i=0;i<Math.max(model.board.upperListBuilding.size(),model.board.lowerListBuilding.size());i++)
|
||||
{
|
||||
if(i<model.board.upperListBuilding.size())
|
||||
{
|
||||
BuildTableUpper.addRow(i+"-"+model.board.upperListBuilding.get(i).toString());
|
||||
}
|
||||
if(i<model.board.lowerListBuilding.size())
|
||||
{
|
||||
BuildTableLower.addRow(i+"-"+model.board.lowerListBuilding.get(i).toString());
|
||||
}
|
||||
}
|
||||
stringUpperListTribe.forEach(x->TribeTableUpper.addRow(x));
|
||||
stringLowerListTribe.forEach(x->TribeTableLower.addRow(x));
|
||||
offerTrack.addRow(stringUpOffer);
|
||||
offerTrack.addRow(stringDownOffer);
|
||||
return "CURRENT STATE\n"+model.currentState+"\n"+model.orderLogicCard.toString()+"\n"+ AsciiTable.sideBySide(Arrays.stream((TribeTableUpper.build().split("\n"))).toList(), Arrays.stream(BuildTableUpper.build().split("\n")).toList(),2)+"\n"+offerTrack.build()+"\n"+AsciiTable.sideBySide(List.of(TribeTableLower.build().split("\n")), Arrays.stream(BuildTableLower.build().split("\n")).toList(),2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user