[ all classes ]
[ it.polimi.ingsw.view.GUI ]
Coverage Summary for Class: MainController (it.polimi.ingsw.view.GUI)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
MainController | 0% (0/ 1) | 0% (0/ 17) | 0% (0/ 69) |
1 package it.polimi.ingsw.view.GUI;
2
3 import com.google.gson.Gson;
4 import it.polimi.ingsw.utils.Observable;
5 import it.polimi.ingsw.utils.Observer;
6 import it.polimi.ingsw.utils.model.Command;
7 import it.polimi.ingsw.view.model.Cell;
8 import it.polimi.ingsw.view.model.Player;
9 import it.polimi.ingsw.view.socket.Chat;
10 import it.polimi.ingsw.view.socket.Connection;
11 import it.polimi.ingsw.view.socket.Parser;
12
13 import java.util.List;
14
15 public class MainController extends Observable<String> implements Observer<String> {
16 private Connection connection;
17 private Boolean statusRequest;
18 private Parser parser;
19 private AppGUI appGUI;
20 private String username = null;
21
22 /**
23 * Set parser and application to controllers
24 *
25 * @param parser parser
26 * @param appGUI application
27 */
28 public void set(Parser parser, AppGUI appGUI) {
29 this.appGUI = appGUI;
30 this.parser = parser;
31 }
32
33 /**
34 * Set chat
35 *
36 * @return chat set
37 */
38 public Chat setChat() {
39 Chat chat = new Chat(connection);
40 return chat;
41 }
42
43 /**
44 * Restart the application
45 */
46 public void quit() {
47 appGUI.reStart();
48
49 }
50
51 /**
52 * Close the connection
53 */
54 public void closeConnection() {
55 if (connection != null && connection.getStatus())
56 connection.close();
57 username = null;
58 connection = null;
59 }
60
61 /**
62 * Send username to server
63 *
64 * @param name username
65 * @return if username is valid
66 */
67 public synchronized boolean sendUsername(String name) {
68 try {
69 statusRequest = null;
70 notify(name);
71 while (statusRequest == null) {
72 Thread.sleep(300);
73 }
74 if (statusRequest == false) {
75 return false;
76 }
77 username = name;
78 return true;
79 } catch (Exception e) {
80 return false;
81 }
82 }
83
84 /**
85 * Send chosen game mode to server
86 *
87 * @param mode game mode
88 */
89 public void setMode(String mode) {
90 try {
91 statusRequest = null;
92 notify(mode);
93 while (statusRequest == null) {
94 Thread.sleep(300);
95 }
96 } catch (Exception ignored) {
97 }
98 }
99
100 /**
101 * Setup connection
102 *
103 * @param ip server ip
104 * @param port server port
105 * @return if the conenction is set successfully
106 */
107 public synchronized boolean setConnection(String ip, int port) {
108 if (connection != null && connection.getStatus())
109 return false;
110 try {
111 connection = new Connection(ip, port);
112 this.addObservers(connection);
113 connection.addObservers(this);
114 connection.addObservers(parser);
115 connection.setMaster(appGUI);
116 new Thread(connection).start();
117 return true;
118
119 } catch (Exception e) {
120 return false;
121 }
122 }
123
124 /**
125 * Send players'actions to server
126 *
127 * @param name player's username
128 */
129 public void send(String name) {
130 String toSend = "";
131 List<Command> commands = parser.getUsableCommandList();
132 for (Command command : commands) {
133 if (command.getFuncData() == null) {
134 if (name == null) {
135 toSend = new Gson().toJson(command);
136 }
137 } else if (command.getFuncData().equals(name)) {
138 toSend = new Gson().toJson(command);
139 break;
140 }
141 }
142 notify(toSend);
143
144 }
145
146 /**
147 * Get updated board
148 *
149 * @return Refreshed board
150 */
151 public Cell[][] getBoard() {
152 return parser.getBoard();
153 }
154
155 /**
156 * Get commands
157 *
158 * @return List of available commands
159 */
160 public List<Command> getCommand() {
161 return parser.getUsableCommandList();
162 }
163
164 /**
165 * Get player's information
166 *
167 * @return List of players'new information
168 */
169 public List<Player> getUserInfo() {
170 return parser.getPlayers();
171 }
172
173 /**
174 * Get the current player
175 *
176 * @return Player
177 */
178 public String getCurrentPlayer() {
179 return parser.getCurrentPlayer();
180 }
181
182 /**
183 * Get player's username
184 *
185 * @return player's username
186 */
187 public String getPlayer() {
188 return username;
189 }
190
191 /**
192 * Get Game Phase
193 *
194 * @return Current game phase
195 */
196 public String getGamePhase() {
197 return parser.getGamePhase();
198 }
199
200 /**
201 * Change the scene
202 */
203 public void changeScene() {
204 appGUI.changeScene();
205 }
206
207 /**
208 * Receive answers from server
209 *
210 * @param message message sent
211 */
212 @Override
213 public void update(String message) {
214 if (message == null || message.equals("")) {
215 return;
216 }
217
218 if (message.equals("ok"))
219 statusRequest = true;
220 if (message.equals("ko"))
221 statusRequest = false;
222 }
223
224 }