-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
713 lines (545 loc) · 35.6 KB
/
Client.java
File metadata and controls
713 lines (545 loc) · 35.6 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
/**
* [Client.java]
* The client program used by the players to communicate with the server and launch the game.
* @author Vivian Dai, Damon Ma, and Edward Yang
* @version 1.0 on January 4, 2021
*/
public class Client {
/**The port that the client has connected to.*/
private int port;
/**Used to write messages to the server.*/
private PrintWriter output;
/**Used to receive messages from the server.*/
private BufferedReader input;
/**Holds relevant information about the player's game objects and stats.*/
private Player player;
/**The IP address of the connected server. */
private String ipAddress;
/**The client socket. */
private Socket socket;
/**The username of the player. */
private String username;
/** Used to send and receive messages with the server*/
private MessageHandler messageHandler;
/**Used to check if the program should still run or not. */
private boolean running;
/**login window for the game. */
private LoginWindow loginWindow;
/**standby window that appears when the player is the first to join and waiting for the next to join. */
private StandbyWindow standbyWindow = null;
/** string storing the last thing the client requested */
private String lastRequest = "";
/**
* The main method of the program to run.
* @param args Java command-line arguments
*/
public static void main(String[] args){
new Client().login(); //initiate login process
}
/**
* The login process for the client.
*/
public void login(){
loginWindow = new LoginWindow();
}
/**
* Opens the window telling the player that the game is waiting for the second player to join.
*/
public void startStandby(){
standbyWindow = new StandbyWindow();
}
/**
* Sends a message to the server.
* @param message The message being sent to the server.
*/
public void sendMessage(String message){
messageHandler.sendMessage(message);
}
/**
* Gets the window used to login.
* @return the login window.
*/
public LoginWindow getLoginWindow(){
return this.loginWindow;
}
/**
* Gets the widow used for the player to standby.
* @return The standby window.
*/
public StandbyWindow getStandbyWindow(){
return this.standbyWindow;
}
/**
* Gets the player object from this client.
* @return The player object that this client is holding.
*/
public Player getPlayer(){
return this.player;
}
/**
* sets the widow used for the player to standby.
* @standbyWindow The new standby window.
*/
public void setStandbyWindow(StandbyWindow standbyWindow){
this.standbyWindow = standbyWindow;
}
public void setLastRequest(String newRequest){
this.lastRequest = newRequest;
}
//start of inner classes
/**
* An inner class used to receives messages from the server.
*/
private class MessageHandler implements Runnable{
/** A string storing the last thing the client requested */
public void run(){
while(running){
try{
String prefix;
if(input.ready()){
prefix = input.readLine();
if(prefix.equals("<w>")){ //waiting for another player to join
Client.this.startStandby(); //start standby window
}else if(prefix.equals("<s>")){ //start game
String side = input.readLine();
String opponent = input.readLine();
int startingCurrency = Integer.parseInt(input.readLine());
if(side.equals("s")){ //this player is on the SCP side
Client.this.player = new SCP(username, Client.this, opponent, startingCurrency);
}else if(side.equals("t")){ //this player is on the town side
int startingFood = Integer.parseInt(input.readLine());
Client.this.player = new Town(username, Client.this, opponent, startingCurrency, startingFood);
}
Client.this.player.start();
/*
Thread gameThread = new Thread(Client.this.player);
gameThread.start();
*/
if(Client.this.getStandbyWindow() != null){ //if standby window is open
Client.this.getStandbyWindow().close(); //close the standby window
Client.this.setStandbyWindow(null);
}
}else if(prefix.equals("<ts>")){ //server says to start the next turn
player.startTurn();
}else if(prefix.equals("<te>")){ //server says to end the current turn
player.endTurn();
String objectInfo;
String [] objectValues;
SCP0492.level = Integer.parseInt(input.readLine());
int objectNum = Integer.parseInt(input.readLine());
ArrayList<SCP0492> scpList = new ArrayList<SCP0492>();
for(int i = 0; i < objectNum; i ++){
objectInfo = input.readLine();
objectValues = objectInfo.split(" ");
scpList.add(new SCP0492(Integer.parseInt(objectValues[0]), Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4])));
}
objectNum = Integer.parseInt(input.readLine());
ArrayList<Event> eventList = new ArrayList<Event>();
for(int i = 0; i < objectNum; i ++){
objectInfo = input.readLine();
objectValues = objectInfo.split(" ");
String eventType = objectValues[0];
if(eventType.equals("Infect")){
eventList.add(new Infect(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]) ));
}else if (eventType.equals("Earthquake")){
eventList.add(new Earthquake(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]) ));
}else if(eventType.equals("Fire")){
eventList.add(new Fire(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]) ));
}else if(eventType.equals("Mutate")){
eventList.add(new Mutate(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2])));
}else if(eventType.equals("Riot")){
eventList.add(new Riot(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2])));
}else if(eventType.equals("Thunderstorm")){
eventList.add(new Thunderstorm(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]) ));
}else if(eventType.equals("Tornado")){
eventList.add(new Tornado(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]) ));
}else if(eventType.equals("WarpReality")){
eventList.add(new WarpReality(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2])));
}else if(eventType.equals("Stonks")){
eventList.add(new Stonks(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2])));
}
}
objectNum = Integer.parseInt(input.readLine());
ArrayList<Building> buildingList = new ArrayList<Building>();
for(int i = 0; i < objectNum; i++){
objectInfo = input.readLine();
objectValues = objectInfo.split(" ");
String buildingType = objectValues[0];
if(buildingType.equals("Bank")){
buildingList.add(new Bank(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]) ));
}else if(buildingType.equals("FoodBuilding")){
buildingList.add(new FoodBuilding(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]) ));
}else if(buildingType.equals("Hospital")){
buildingList.add(new Hospital(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]), Integer.parseInt(objectValues[6])));
}else if(buildingType.equals("MilitaryBase")){
buildingList.add(new MilitaryBase(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]) ));
}else if(buildingType.equals("ResearchLab")){
buildingList.add(new ResearchLab(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]) ));
}else if(buildingType.equals("Residency")){
buildingList.add(new Residency(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]), Integer.parseInt(objectValues[6])));
}
}
objectNum = Integer.parseInt(input.readLine());
if(Client.this.getPlayer() instanceof SCP){
ArrayList<Human> humansList = new ArrayList<Human>();
for(int i = 0; i < objectNum; i++){
objectInfo = input.readLine();
objectValues = objectInfo.split(" ");
String humanType = objectValues[0];
if(humanType.equals("Cadet")){
humansList.add(new Cadet(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4])));
}else if(humanType.equals("Citizen")){
humansList.add(new Citizen(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4])));
}else if(humanType.equals("Doctor")){
humansList.add(new Doctor(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]) ));
}else if(humanType.equals("Researcher")){
humansList.add(new Researcher(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4])));
}else if(humanType.equals("Soldier")){
humansList.add(new Soldier(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]), Integer.parseInt(objectValues[6]) ));
}else if(humanType.equals("Spy")){
humansList.add(new Spy(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Double.parseDouble(objectValues[5]), Double.parseDouble(objectValues[6])));
}
}
((SCP)Client.this.getPlayer()).updateGameObjects(humansList, buildingList, eventList, scpList); //call method to update the game objects
((SCP)Client.this.getPlayer()).setHume(Integer.parseInt(input.readLine()));
}else if (Client.this.getPlayer() instanceof Town){
HashMap<Integer, Human> humanMap = new HashMap<>();
for(int i = 0; i < objectNum; i++){
int key = Integer.parseInt(input.readLine());
objectInfo = input.readLine();
objectValues = objectInfo.split(" ");
String humanType = objectValues[0];
if(humanType.equals("Cadet")){
Cadet add = new Cadet(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]));
humanMap.put(key,add);
for(int j = 0; j < buildingList.size(); j ++){
if(buildingList.get(j).getX() == add.getX() && buildingList.get(j).getY() == add.getY()){
((MilitaryBase)buildingList.get(j)).add(add);
}
}
}else if(humanType.equals("Citizen")){
Citizen add =new Citizen(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]));
humanMap.put(key, add);
for(int j = 0; j < buildingList.size(); j ++){
if(buildingList.get(j).getX() == add.getX() && buildingList.get(j).getY() == add.getY()){
((Residency)buildingList.get(j)).createCitizen(add);
}
}
}else if(humanType.equals("Doctor")){
Doctor add = new Doctor(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]) );
humanMap.put(key, add);
for(int j = 0; j < buildingList.size(); j ++){
if(buildingList.get(j).getX() == add.getX() && buildingList.get(j).getY() == add.getY()){
((Hospital)buildingList.get(j)).addDoctors(add);
}
}
}else if(humanType.equals("Researcher")){
Researcher add = new Researcher(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]));
humanMap.put(key, add);
for(int j = 0; j < buildingList.size(); j ++){
if(buildingList.get(j).getX() == add.getX() && buildingList.get(j).getY() == add.getY()){
((ResearchLab)buildingList.get(j)).add(add);
}
}
}else if(humanType.equals("Soldier")){
Soldier add = new Soldier(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Integer.parseInt(objectValues[5]), Integer.parseInt(objectValues[6]) );
humanMap.put(key, add );
for(int j = 0; j < buildingList.size(); j ++){
if(buildingList.get(j).getX() == add.getX() && buildingList.get(j).getY() == add.getY()){
((MilitaryBase)buildingList.get(j)).add(add);
}
}
}else if(humanType.equals("Spy")){
Spy add = new Spy(Integer.parseInt(objectValues[1]), Integer.parseInt(objectValues[2]), Integer.parseInt(objectValues[3]), Integer.parseInt(objectValues[4]), Double.parseDouble(objectValues[5]), Double.parseDouble(objectValues[6]));
humanMap.put(key,add );
for(int j = 0; j < buildingList.size(); j ++){
if(buildingList.get(j).getX() == add.getX() && buildingList.get(j).getY() == add.getY()){
((MilitaryBase)buildingList.get(j)).add(add);
}
}
}
}
((Town)Client.this.getPlayer()).updateGameObjects(humanMap, buildingList, eventList, scpList); //call method to update the game objects
((Town)Client.this.getPlayer()).setMoney(Integer.parseInt(input.readLine()));
((Town)Client.this.getPlayer()).setFood(Integer.parseInt(input.readLine()));
}
}else if(prefix.equals("<f>")){ //requested transaction could not be completed
Client.this.setLastRequest(""); //clear the last request
System.out.println(" request failed f");
}else if(prefix.equals("<st>")){ //transaction is successful
String[] requests = lastRequest.split(" ");
//Actually implement the last request
if(requests.length > 1){
if(requests[0].equals("<b>")){
((Town)player).buildBuilding(requests[1], Integer.parseInt(requests[2]), Integer.parseInt(requests[3]));
}else if(requests[0].equals("<e>")){
if(requests.length == 3){
((SCP)player).startEvent(requests[1], Integer.parseInt(requests[2]));
}else if(requests.length == 5){
((SCP)player).startEvent(requests[1], Integer.parseInt(requests[2]), Integer.parseInt(requests[3]), Integer.parseInt(requests[4]));
}
}else if(requests[0].equals("<u>")){
((Town)player).upgradeBuilding(Integer.parseInt(requests[1]), Integer.parseInt(requests[2]));
}else if(requests[0].equals("<residency train>")){ // adding new citizens
int x = Integer.parseInt(requests[1]);
int y = Integer.parseInt(requests[2]);
int amount = Integer.parseInt(requests[3]);
((Town)player).residencyTrain(x,y,amount);
}else if(requests[0].equals("<residency convert>")){
String newType = requests[1];
int x = Integer.parseInt(requests[2]);
int y = Integer.parseInt(requests[3]);
int amount = Integer.parseInt(requests[4]);
int[] keys = new int[amount];
for(int i = 5; i < requests.length - 5;i ++ ){
keys[i] = Integer.parseInt(requests[i]);
}
((Town)player).residencyConvert(newType, x, y, amount, keys);
}else if(requests[0].equals("<military soldier>")){
int level = Integer.parseInt(requests[1]);
int x = Integer.parseInt(requests[2]);
int y = Integer.parseInt(requests[3]);
int amount = Integer.parseInt(requests[4]);
int[] keys = new int[amount];
for(int i = 5; i < requests.length - 5;i ++ ){
keys[i] = Integer.parseInt(requests[i]);
}
((Town)player).trainSoldier(level, x, y, amount, keys);
}else if(requests[0].equals("<military spy>")){
int x = Integer.parseInt(requests[1]);
int y = Integer.parseInt(requests[2]);
int amount = Integer.parseInt(requests[3]);
int[] keys = new int[amount];
for(int i = 4; i < requests.length - 4;i ++ ){
keys[i] = Integer.parseInt(requests[i]);
}
((Town)player).trainSpy(x,y,amount,keys);
}
}else{//this might be one of the research upgrade requests
if(requests[0].equals("<research weapon>")){
((Town)player).researchWeapon();
}else if(requests[0].equals("<research armour>")){
((Town)player).researchArmour();
}
}
}else if(prefix.equals("<r>")){ //change in resources
String resourceType = input.readLine();
int resourceChange = Integer.parseInt(input.readLine());
if(resourceType.equals("Money")){
((Town)player).changeMoney(resourceChange);
}else if(resourceType.equals("Food")){
((Town)player).changeFood(resourceChange);
}else if(resourceType.equals("Hume")){
((SCP)player).changeHume(resourceChange);
}
}else if (prefix.equals("<i>")){
int enemyHume = Integer.parseInt(input.readLine());
if(Client.this.player instanceof Town){
((Town)Client.this.player).displayIntel(enemyHume);
}
}else if(prefix.equals("<ge>")){ //if server says game is finished
Client.this.getPlayer().endGame(input.readLine());
Client.this.running = false;
}//end of if statements
}//end of if statement to check if there is input
}catch(IOException e){
System.out.println("Incorrect input received");
}//end of try catch statement
}//end of while loop
}//end of method
/**
* Sends a message to the server
* @param message The message being sent to the server.
*/
public void sendMessage(String message){
output.println(message);
output.flush();
}//end of method
}//end of inner class
/**
* An inner class for a window that players will use to login.
*/
public class LoginWindow{
/**The JFrame for the login popup window.*/
private JFrame window;
/**The JPanel for the login popup window.*/
private JPanel panel;
/**JLabel that will ask the user to enter login information. */
private JLabel loginLabel;
/**The JLabel that will prompt the user for a username.*/
private JLabel usernameLabel;
/**The JLabel that will prompt the user for a the IP address of the server.*/
private JLabel addressLabel;
/**The JLabel that will prompt the user for the port.*/
private JLabel portLabel;
/**The JLabel that will tell the user if there were any issues connecting. */
private JLabel connectionErrorLabel;
/**The JTextField that will receive the user's username. */
private JTextField usernameEntry;
/**The JTextField that will receive the server's IP address. */
private JTextField addressEntry;
/**The JTextField that will receive the port to connect to. */
private JTextField portEntry;
/**The JButton used to submit entered information*/
private JButton enterButton;
/**
* Constructor for the Login Window
*/
public LoginWindow(){
//set JFrame
window = new JFrame("Welcome to Code-049!");
window.setSize(400,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JPanel
panel = new JPanel();
panel.setLayout(null);
window.add(panel);
//Label to show players where to login
loginLabel = new JLabel("Login:");
loginLabel.setBounds(10,20,200,25);
panel.add(loginLabel);
//Prompt player for a username
usernameLabel = new JLabel("Select your username:");
usernameLabel.setBounds(10, 65, 210, 25);
panel.add(usernameLabel);
//Prompt player for IP Address
addressLabel = new JLabel("Enter IP Address:");
addressLabel.setBounds(10, 95, 210, 25);
panel.add(addressLabel);
//Prompt player for port
portLabel = new JLabel("Enter Port #:");
portLabel.setBounds(10, 125, 210, 25);
panel.add(portLabel);
//Label Will only appear if there is an error when attempting to connect
connectionErrorLabel = new JLabel("");
connectionErrorLabel.setBounds(10, 200, 300, 25 );
connectionErrorLabel.setForeground(Color.red);
panel.add(connectionErrorLabel);
//Receives the selected username from the player.
usernameEntry = new JTextField(20);
usernameEntry.setBounds(140,65, 165, 25);
panel.add(usernameEntry);
//get IP address from the player.
addressEntry = new JTextField(20);
addressEntry.setBounds(140, 95, 165,25);
panel.add(addressEntry);
//Receive port from user
portEntry = new JTextField(20);
portEntry.setBounds(140,125, 165, 25);
panel.add(portEntry);
//Submit button
enterButton = new JButton("Connect");
enterButton.setBounds(150,165, 100, 25);
panel.add(enterButton);
enterButton.addActionListener(new EnterButtonListener());
window.setVisible(true); //make the window visible after adding everything
}//end of constructor
/**
* Method connects the player's client program to the server.
* @param address The IP address of the server.
* @param port The port.
*/
public void connect(String username, String address, int port){
try {
Client.this.socket = new Socket(address, port); //attempt socket connection
//input to server
InputStreamReader stream1= new InputStreamReader(socket.getInputStream());
Client.this.input = new BufferedReader(stream1);
//output to server
Client.this.output = new PrintWriter(socket.getOutputStream()); //assign printwriter to network stream
//get messages from server
try{
//if there is input
while(!input.ready()){}
sendMessage("<c>");
sendMessage(username);
window.dispose();
}catch(IOException e){
connectionErrorLabel.setText("A communications error has occured.");
}
} catch (IOException e) { //connection error occured
connectionErrorLabel.setText("Error: Could not connect to server.");
}
//close the login window since there's no need for it anymore
}//end of method
/**
* Inner class for the submit button on the login page.
*/
class EnterButtonListener implements ActionListener {
/**
* Attempts to login when the button is pressed.
*/
public void actionPerformed(ActionEvent event) {
//set the username the client selected
username = usernameEntry.getText();
//attempt to get the port
try {
port = Integer.parseInt(portEntry.getText()); //set the port the user selected
} catch(NumberFormatException e) { //if the input was not an integer tyoe
connectionErrorLabel.setText("Error: Invalid port entered."); //tell user that they didn't give the right information
return; //end method
}
//store the IP Address the user entered
ipAddress = addressEntry.getText();
//attempt to connect to the server
messageHandler = new MessageHandler();
connect(username, ipAddress, port);
running = true;
Thread t = new Thread(messageHandler); //start the server communication in a new thread
t.start(); //start thread*/
}
}//end of inner class for submit button
}//end of inner class
/**
* An inner class for a window that displays when a player is waiting for another player to join the game.
*/
public class StandbyWindow{
/**Window that is opened to tell player to wait while another player connects. */
private JFrame window;
/**The JPanel for the window. */
private JPanel panel;
/**The JLabel to tell user to wait. */
private JLabel standbyLabel;
/**
* Constructor that opens the window to tell user to standby while the next player joins.
*/
public StandbyWindow(){
//set JFrame
window = new JFrame("Welcome to Code-049!");
window.setSize(400,300);
window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//set JPanel
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0,0,400,300);
window.add(panel);
//Label to show players where to login
standbyLabel = new JLabel("Waiting for player 2 to join...");
standbyLabel.setBounds(20,30,200,25);
panel.add(standbyLabel);
window.setVisible(true);
}//end of method
/**
* Closes the standby window if the other user has joined.
*/
public void close(){
window.dispose();
}
}//end of inner class
//end of inner classes
}//end of class