Coverage Summary for Class: GameInProgressPanel (it.polimi.ingsw.Client.GUI.Panels)
Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
GameInProgressPanel |
0%
(0/1)
|
0%
(0/5)
|
0%
(0/60)
|
0%
(0/94)
|
1 package it.polimi.ingsw.Client.GUI.Panels;
2
3 import it.polimi.ingsw.Client.GUI.Context;
4 import it.polimi.ingsw.Client.GUI.Listeners.GUISocketListener;
5 import it.polimi.ingsw.Client.GUI.Window;
6 import it.polimi.ingsw.Controller.Actions.*;
7 import it.polimi.ingsw.Exceptions.Container.InvalidContainerIndexException;
8 import it.polimi.ingsw.Model.Enums.GameMode;
9 import it.polimi.ingsw.Model.Model;
10 import it.polimi.ingsw.Model.PlayerBoard;
11 import it.polimi.ingsw.Network.SocketWrapper;
12
13 import javax.swing.*;
14 import java.awt.*;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 /**
20 * Class container of all others Panel inside a JTabbedPane
21 */
22 public class GameInProgressPanel extends JTabbedPane {
23
24 /**
25 * nickname of GUI's player
26 */
27 private final String ownNickname;
28 /**
29 * Frame containing all others components
30 */
31 private final Window window;
32
33 /**
34 * SocketWrapper used to communicate with Server
35 */
36 private final SocketWrapper sw;
37 /**
38 * Contains GuiReader's information necessary to record user's requests during his turn
39 */
40 private final GUISocketListener guiSocketListener;
41
42 /**
43 * Constructor that should be called only upon the creation of first GameInProgressPanel
44 *
45 * @param ctx Context to use during the game
46 */
47 public GameInProgressPanel(Context ctx) {
48 // unwrapping context into useful variables
49 this.ownNickname = ctx.getNickname();
50 this.window = ctx.getWindow();
51 this.sw = ctx.getSocketWrapper();
52 this.window.changeView(this);
53 this.guiSocketListener = new GUISocketListener(ctx);
54 //run GuiReader thread
55 Thread readerThread = new Thread(guiSocketListener);
56 readerThread.start();
57 }
58
59 /**
60 * Public constructor to create GameInProgressPanel's object starting from the second creation; it should never be called the first time
61 *
62 * @param ctx Context to use during the game
63 * @param model Model containing all game's information
64 * @param guiSocketListener created upon first gameInProgressPanel creation
65 * @param previousPanel if not null, signifies an old panel was previously drawn and will keep the displayed
66 * tab the same in the new panel
67 */
68 public GameInProgressPanel(Context ctx, Model model, GUISocketListener guiSocketListener, GameInProgressPanel previousPanel) {
69 this(ctx, guiSocketListener);
70 if (!model.isGameOver()) {
71 //add IslandFieldPanel to JTabbedPane
72 this.add("Islands", new IslandFieldPanel(model, sw, guiSocketListener));
73 Map<String, PlayerBoardPanel> playerTabs = new HashMap<>();
74 ArrayList<String> tooltips = new ArrayList<>(model.getMutablePlayerBoards().size());
75 //set ToolTip font
76 UIManager.put("ToolTip.font", new Font("Arial", Font.BOLD, 15));
77 for (PlayerBoard pb : model.getMutablePlayerBoards()) {
78 //for all playerBoards create and add a PlayerBoardPanel to JTabbedPane
79 playerTabs.put(pb.getNickname(), new PlayerBoardPanel(pb, model, this.sw, guiSocketListener));
80 }
81 for (Map.Entry<String, PlayerBoardPanel> pbp : playerTabs.entrySet()) {
82 //set a proper name to playerBoardPanel's tab inside jTabbedPane
83 if (pbp.getKey().equals(this.ownNickname)) {
84 if (pbp.getKey().equals(model.getMutableTurnOrder().getMutableCurrentPlayer().getNickname()))
85 this.add("Your board (current player)", pbp.getValue());
86 else
87 this.add("Your board", pbp.getValue());
88 } else {
89 if (pbp.getKey().equals(model.getMutableTurnOrder().getMutableCurrentPlayer().getNickname()))
90 this.add(pbp.getKey() + "'s PlayerBoard (current player)", pbp.getValue());
91 else
92 this.add(pbp.getKey() + "'s PlayerBoard", pbp.getValue());
93 }
94 PlayerBoard pb = null;
95 try {
96 pb = model.getMutablePlayerBoard(pbp.getValue().getPlayerBoardId());
97 } catch (InvalidContainerIndexException e) {
98 e.printStackTrace();
99 }
100 String tooltip;
101 //create ToolTip's string for all playerBoardPanels' tabs (containing assistantCard that has been played and eventual coin balance)
102 if (model.getMutableTurnOrder().getMutableSelectedCard(pb).isPresent()) {
103 tooltip = "<html>Played assistant card: #" + model.getMutableTurnOrder().getMutableSelectedCard(pb).get().getPriority() + "<br>";
104 } else {
105 tooltip = "<html>No assistant card has been played<br>";
106 }
107 if (model.getGameMode() == GameMode.ADVANCED)
108 tooltip = tooltip + "Available coins:" + (pb != null ? pb.getCoinBalance() : 0);
109 tooltip = tooltip + "</html>";
110 tooltips.add(tooltip);
111 }
112 //add ToolTip to all playerBoardPanels' tabs
113 for (int i = 1; i <= tooltips.size(); i++) {
114 this.setToolTipTextAt(i, tooltips.get(i - 1));
115 }
116 //If the game is an advanced game then create and add a new CharacterCardsPanel to JTabbedPane
117 if (model.getGameMode() == GameMode.ADVANCED) {
118 final JPanel characterCardsPanel = new CharacterCardsPanel(model, sw, guiSocketListener);
119 this.add("CharacterCards", characterCardsPanel);
120 }
121 //create and add CloudPanel to JTabbedPane
122 this.add("Clouds", new CloudPanel(model.getClouds(), model.getMutableTurnOrder().getMutableCurrentPlayer(), guiSocketListener, sw));
123
124 if (ownNickname.equals(model.getMutableTurnOrder().getMutableCurrentPlayer().getNickname())) {
125 this.add("NEXT ACTION: " + getNextAction(model), null);
126 } else {
127 this.add("WAIT YOUR TURN", null);
128 }
129 this.setEnabledAt(this.getTabCount() - 1, false);
130
131 //set JTabbedPane to the the last tab the user had opened
132 if (previousPanel != null) {
133 this.setSelectedIndex(previousPanel.getSelectedIndex());
134 }
135
136 // show an "it's your turn" dialog if appropriate
137 if (model.getMutableTurnOrder().getMutableCurrentPlayer().getNickname().equals(ownNickname) &&
138 (guiSocketListener.getSuccessfulRequestsByType(PlayAssistantCard.class) == 0 ||
139 (guiSocketListener.getSuccessfulRequestsByType(PlayAssistantCard.class) == 1 &&
140 guiSocketListener.getSuccessfulRequestsByType(MoveStudent.class) == 0))) {
141 // when selecting an assistant card, remember the user what other players have chosen
142 StringBuilder text = new StringBuilder("<html>It's your turn!!");
143 if (guiSocketListener.getSuccessfulRequestsByType(PlayAssistantCard.class) == 0) {
144 for (PlayerBoard playerBoard : model.getMutableTurnOrder().getCurrentTurnOrder()) {
145 if (playerBoard.getNickname().equals(ownNickname)) break;
146 text.append(playerBoard.getNickname()).append("<br> has played assistantCard: #").append(model.getMutableTurnOrder().getMutableSelectedCard(playerBoard).get().getPriority());
147 }
148 }
149 text.append("</html>");
150 JLabel resLabel = new JLabel(text.toString());
151 resLabel.setFont(new Font("Monospaced", Font.BOLD, 17));
152 SwingUtilities.invokeLater(() ->
153 JOptionPane.showMessageDialog(null, resLabel, "Turn change", JOptionPane.INFORMATION_MESSAGE));
154 }
155 } else {
156 this.add("WINNERS", new EndGamePanel(model.getWinners().get(), ctx));
157 }
158 }
159
160 /**
161 * Should be used upon the second creation of GameInProgressPanel
162 *
163 * @param ctx Context to use during the game
164 * @param guiSocketListener created upon first gameInProgressPanel creation
165 */
166 private GameInProgressPanel(Context ctx, GUISocketListener guiSocketListener) {
167 this.ownNickname = ctx.getNickname();
168 this.window = ctx.getWindow();
169 this.sw = ctx.getSocketWrapper();
170 this.guiSocketListener = guiSocketListener;
171 Thread readerThread = new Thread(guiSocketListener);
172 readerThread.start();
173 }
174
175 private String getNextAction(Model model) {
176 String nextAction = "";
177 if (guiSocketListener.getSuccessfulRequestsByType(PlayCharacterCard.class) == 0 && model.getGameMode() == GameMode.ADVANCED) {
178 nextAction = "play character card or ";
179 }
180 if (guiSocketListener.getSuccessfulRequestsByType(PlayAssistantCard.class) == 0) {
181 nextAction = nextAction + "play assistant card";
182 return nextAction;
183 }
184 if ((model.getMutablePlayerBoards().size() != 3 && guiSocketListener.getSuccessfulRequestsByType(MoveStudent.class) < 3) ||
185 (model.getMutablePlayerBoards().size() == 3 && guiSocketListener.getSuccessfulRequestsByType(MoveStudent.class) < 4)) {
186 nextAction = nextAction + "move Student";
187 return nextAction;
188 }
189 if (guiSocketListener.getSuccessfulRequestsByType(MoveMotherNature.class) == 0) {
190 nextAction = nextAction + "move MotherNature";
191 return nextAction;
192 }
193 if (guiSocketListener.getSuccessfulRequestsByType(ChooseCloudTile.class) == 0) {
194 nextAction = nextAction + "choose cloud";
195 return nextAction;
196 }
197 if (guiSocketListener.getSuccessfulRequestsByType(EndTurnOfActionPhase.class) == 0) {
198 nextAction = nextAction + "end the turn";
199 return nextAction;
200 }
201 return "no action can be executed";
202 }
203 }