This repository was archived by the owner on Dec 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClient.java
More file actions
357 lines (286 loc) · 8.37 KB
/
MainClient.java
File metadata and controls
357 lines (286 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import java.util.ArrayList;
import javax.swing.JOptionPane;
import Interfaces.CheckersClient;
import chatHandler.ChatManager;
import game.Game;
import lobby.Lobby;
import lobby.LobbyInterface;
import observatory.Observer;
import serverCommunication.ServerInterface;
import serverCommunication.ServerCommunicator;
import setup.LoginInitializer;
import userInterface.ErrorPopups;
import userInterface.LobbyWindow;
import userInterface.LobbyWindowInterface;
public class MainClient extends Thread implements CheckersClient {
ServerInterface serverInterface;
LobbyInterface lobbyInterface;
Thread loginInitializer;
ErrorPopups errorPopup;
String clientUsername;
ChatManager chatManager;
Game game;
Observer observer;
ArrayList<Observer> observerList = new ArrayList<Observer>();
public MainClient() {
serverInterface = new ServerCommunicator(this);
errorPopup = new ErrorPopups();
chatManager = new ChatManager(serverInterface);
//lobbyInterface = new Lobby();
}
public MainClient(String user, String ip) {
serverInterface = new ServerCommunicator(this);
errorPopup = new ErrorPopups();
chatManager = new ChatManager(serverInterface);
serverInterface.connectToServer(ip, user);
}
@Override
public void run() {
login();
// game(420);
// game.enableBoard(true);
// game.setColor("red");
// game.startGame();
// for (int i = 0; i < 100; i++) {
// serverInterface.connectToServer("127.0.0.1", Integer.toString(i));
// try {
// sleep(100);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// //serverInterface.makeTable(Integer.toString(i));
// //System.out.println(i);
// }
}
public void login() {
loginInitializer = new LoginInitializer(serverInterface);
loginInitializer.run();
}
public void lobby() {
if (lobbyInterface == null) {
lobbyInterface = new Lobby(serverInterface, chatManager);
lobbyInterface.startLobby();
lobbyInterface.setUsername(clientUsername);
} else {
lobbyInterface.toggleWindow();
}
}
public void game(int tableID) {
//lobbyInterface.toggleWindow();
game = new Game(serverInterface, chatManager, clientUsername, tableID);
game.setupGame();
//System.out.println("Is the game a null: " + game == null);
}
public void observe(int tableID) {
observer = new Observer(tableID, clientUsername, serverInterface);
observer.start();
observerList.add(observer);
}
public void setUsername(String username) {
clientUsername = username;
}
@Override
public void connectionOK() {
System.out.println("Connection Successfull");
}
@Override
public void youInLobby() {
synchronized(loginInitializer) {
loginInitializer.notify();
}
//nullifies the loginInitializer so it can be picked up by the garbage collector
//loginInitializer = null;
lobby();
}
@Override
public void youLeftLobby() {
lobbyInterface.toggleWindow();
//lobbyInterface = null;
}
@Override
public void newMsg(String user, String msg, boolean pm) {
lobbyInterface.displayMessage(user, msg, pm);
game.displayMessage(user, msg, pm);
}
@Override
public void usersInLobby(String[] users) {
lobbyInterface.refreshUsers(users, clientUsername);
}
@Override
public void nowJoinedLobby(String user) {
lobbyInterface.addUser(user);
lobbyInterface.displayGeneralMessage(user + " has joined the lobby.");
}
@Override
public void nowLeftLobby(String user) {
lobbyInterface.removeUser(user);
lobbyInterface.displayGeneralMessage(user + " has left the lobby.");
}
@Override
public void newTable(int tid) {
// TODO Auto-generated method stub
}
@Override
public void joinedTable(int tid) {
game(tid);
}
@Override
public void alertLeftTable() {
//synchronized (gameInitializer) {
// gameInitializer.notify();
//}
game.stopGame();
game = null;
}
@Override
public void gameStart() {
game.startGame();
}
@Override
public void colorBlack() {
game.setColor("black");
}
@Override
public void colorRed() {
game.setColor("red");
}
@Override
public void oppMove(int fr, int fc, int tr, int tc) {
// TODO Auto-generated method stub
}
@Override
public void curBoardState(int tid, byte[][] boardState) {
if (game != null && game.getID() == tid)
game.refreshBoardState(boardState);
for (Observer o : observerList) {
if (o != null && o.getTableId() == tid) {
o.updateBoard(boardState);
}
}
}
@Override
public void youWin() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Winner, You are the best that ever was!", "Winner", JOptionPane.INFORMATION_MESSAGE);
game.enableBoard(false);
}
@Override
public void youLose() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "You lost, but the other guy was probably cheating.", "Loser", JOptionPane.INFORMATION_MESSAGE);
game.enableBoard(false);
}
@Override
public void onTable(int tid, String blackSeat, String redSeat) {
lobbyInterface.incomingTableInfo(tid, blackSeat, redSeat);
if (game != null && game.getID() == tid) {
if (blackSeat.equals("-1") || redSeat.equals("-1")) {
game.removeOpponent();
} else {
if (!blackSeat.equals(clientUsername))
game.addOpponent(blackSeat);
else {
game.addOpponent(redSeat);
}
}
}
for (Observer o : observerList) {
if (o != null && o.getTableId() == tid) {
o.setUsers(blackSeat, redSeat);
}
}
}
@Override
public void tableList(int[] tids) {
lobbyInterface.refreshTables(tids);
}
@Override
public void yourTurn() {
game.enableBoard(true);
game.setYourTurn(true);
}
@Override
public void nowObserving(int tid) {
observe(tid);
}
@Override
public void stoppedObserving(int tid) {
for (Observer o : observerList) {
if (o != null && o.getTableId() == tid) {
observerList.remove(o);
o = null;
}
}
}
@Override
public void networkException(String msg) {
// TODO Auto-generated method stub
}
@Override
public void nameInUseError() {
errorPopup.usernameTakenError();
}
@Override
public void nameIllegal() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Name is Illegal please pick another", "User name error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void illegalMove() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "That is an illegal move.", "Illegal Move Error", JOptionPane.INFORMATION_MESSAGE);
game.enableBoard(true);
game.undoMove();
}
@Override
public void tableFull() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "That table is full.", "Full Table Error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void tblNotExists() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "That table dos not exist.", "void table Error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void gameNotCreatedYet() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Game has not been Created", "No Game Error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void notYourTurn() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "It is not your turn", "Turn error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void notObserving() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "You are not Observing", "Observing error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void oppNotReady() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Opponent is not ready", "ready-up error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void errorInLobby() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "A lobby error has occured", "Lobby error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void badMessage() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Error Bad Message", "Message error", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void oppLeftTable() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Opponent has left the table", "Opponent left", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void notInLobby() {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "You are not in the lobby", "Lobby error", JOptionPane.INFORMATION_MESSAGE);
}
}