-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnline.java
369 lines (294 loc) · 11.6 KB
/
Online.java
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
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.*;
import java.util.concurrent.CountDownLatch;
import java.io.*;
public class Online extends Player
{
Socket peer;
CountDownLatch getrev;
public Online(Color c, String nam) {
super(c, nam);
renamable=false;
}
@Override
public String getRevivalCandidate()
{
//getrev=new CountDownLatch(1);
//USE A FUCKING LATCH!
Environment.log("want revival candidate ");
int o=0;
while(revival.toString().equals(""))
{
Environment.log("awaiting "+o);
o++;
System.out.println("fdsfs"+o);
try
{
/**
* for some reason it never executes
* the signal for releasing mouse comes
* with the revival candidate chosen by the user
* why so?
* string encountered revv:?
4840350936700 Waiting for instructions
4840353627600 want revival candidate
revival candidate is received before the receiver needs it
*/
revival.wait();
//getrev.await();
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
String s=revival.toString();
revival.setLength(0);
return s;
}
@Override
public void playMove() {
}
@Override
public void destroy()
{
try
{
peer.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
@Override
public JComponent getMouseEventCauser() {
return this;
}
/**
* maybe online k liye 2 classes are not needed
* just one class representing the online player
* and the other class be Human
* will added functionality of sending user movement to peer
* by overriding its playMove method
*/
public static JPanel getClientEntryMenu(JFrame container)
{
JPanel client=new utility.GradientPanel();
client.setBorder(BorderFactory.createTitledBorder("Join a game, you will get black color"));
client.setLayout(null);
Font font=new Font("",Font.PLAIN,28);
Font font2=new Font("",Font.PLAIN,25);
JLabel lab=new JLabel("Enter ipv4 address of the host");
lab.setBounds(10,10,420,50);
lab.setFont(font);
JTextField tf=new JTextField();
tf.setBounds(10,60,190,40);
tf.setFont(font2);
JLabel nam=new JLabel("Enter your name");
nam.setFont(font);
nam.setBounds(10,112,250,50);
JTextField name=new JTextField("");
name.setFont(font2);
name.setBounds(10,158,250,40);
JLabel status=new JLabel("");
status.setForeground(Color.yellow);
status.setBounds(145,199,300,50);
status.setFont(font);
utility.rgbButton procc=new utility.rgbButton("Join game");
procc.setForeground(Color.black.darker());
procc.setBounds(2,250,container.getWidth()-5,119);
procc.setFont(new Font("",Font.PLAIN,35));
procc.setFocusable(false);
try
{
InetAddress b=InetAddress.getLocalHost();
String ip=b.getHostAddress().trim();
tf.setText(ip);
}
catch (UnknownHostException uhe)
{
status.setForeground(Color.red);
status.setText("Unknown host error");
procc.setEnabled(false);
}
JToggleButton grab=new JToggleButton("Record this game");
grab.setBounds(10,200,150,30);
grab.setOpaque(false);
grab.setFocusable(false);
client.add(grab);
client.add(procc);
client.add(status);
client.add(name);
client.add(nam);
client.add(tf);
client.add(lab);
MouseListener mouse=new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==procc && procc.isEnabled()==true)
{
String nom=name.getText();
if(nom.equals(""))
nom="player2";
NetHuman player=new NetHuman(goti.colBlak,nom);
Online host=new Online(goti.colWhit,"<none>");
//CLIENT
new Thread(new Runnable()
{
public void run()
{
try {
Environment.log("about to send ip");
sendIPToOpponent(tf.getText());
Environment.log("sent ip");
new Thread(new Runnable() {
public void run()
{
new Networks(host,2222);
}
}).start();
player.doHandShakeWith(tf.getText(),1111);
Thread gt=new utility.fadeout(container,false,false);
gt.start();
new game(new Player[]{player,host},new Object[]{0,grab.getModel().isSelected()});
} catch (Exception ioException) {
ioException.printStackTrace();
}
}
}).start();
name.setEnabled(false);
procc.setEnabled(false);
grab.setEnabled(false);
entryPoint.disableAllOptions();
}
}
};
procc.addMouseListener(mouse);
return client;
}
public static JPanel getHostEntryMenu(JFrame container)
{
utility.GradientPanel host=new utility.GradientPanel();
host.setBorder(BorderFactory.createTitledBorder("Host a game, you will get white color"));
host.setLayout(null);
Font font=new Font("",Font.PLAIN,28);
Font font2=new Font("",Font.PLAIN,25);
JLabel lab=new JLabel("Your ipv4 address is-");
lab.setBounds(10,10,280,50);
lab.setFont(font);
JTextField tf=new JTextField();
tf.setBounds(10,60,190,40);
tf.setFont(font2);
tf.setOpaque(false);
tf.setEditable(false);
JLabel nam=new JLabel("Enter your name");
nam.setFont(font);
nam.setBounds(10,112,250,50);
JTextField name=new JTextField("");
name.setFont(font2);
name.setBounds(10,158,250,40);
JLabel status=new JLabel("");
status.setForeground(Color.yellow);
status.setBounds(125,199,350,50);
status.setFont(font);
utility.rgbButton proch=new utility.rgbButton("Host game");
proch.setForeground(Color.black);
proch.setBounds(2,250,container.getWidth()-5,119);
proch.setFont(new Font("",Font.PLAIN,35));
proch.setFocusable(false);
JToggleButton grab=new JToggleButton("Record this game");
grab.setBounds(10,200,150,30);
grab.setOpaque(false);
grab.setFocusable(false);
JComboBox<String> jcb=new JComboBox<>(TimeType.times);
jcb.setBounds(190,200,120,30);
host.add(proch);
host.add(status);
host.add(name);
host.add(nam);
host.add(tf);
host.add(lab);
host.add(grab);
host.add(jcb);
try
{
InetAddress b=InetAddress.getLocalHost();
tf.setText(b.getHostAddress().trim());
}
catch (UnknownHostException uhe)
{
status.setForeground(Color.red);
status.setText("Unknown host exception");
}
MouseListener mouse=new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==proch && proch.isEnabled()==true)
{
String nom=name.getText();
if(nom.equals(""))
nom="player1";
NetHuman player=new NetHuman(goti.colWhit,nom);
Online client=new Online(goti.colBlak,"<client>");
//HOST
new Thread(new Runnable()
{
public void run()
{
try
{
Environment.log("about to get ip");
String ipadd= getOpponentIP();
Environment.log("The client's ip address is "+ipadd);
new Thread(new Runnable()
{
public void run()
{
new Networks(client,1111);
}
}).start();
player.doHandShakeWith(ipadd,2222);
Thread gt=new utility.fadeout(container,false,false);
gt.start();
new game(new Player[]{player,client},new Object[]{0,grab.getModel().isSelected()});
} catch (Exception ioException) {
ioException.printStackTrace();
}
}
}).start();
name.setEnabled(false);
proch.setEnabled(false);
grab.setEnabled(false);
entryPoint.disableAllOptions();
}
}
};
proch.addMouseListener(mouse);
return host;
}
public static String getOpponentIP() throws IOException {
ServerSocket ss=new ServerSocket(2707);
Socket s=ss.accept();
String ip=(((InetSocketAddress) s.getRemoteSocketAddress()).getAddress()).toString().replace("/","");
s.close();
ss.close();
return ip;
}
public static void sendIPToOpponent(String ip) throws IOException {
//connect to the server so that it gets the ip address of the client, then connects its own client object with this client.
//meanwhile this machine will connect its host player with the host machines player
Socket s=new Socket(ip,2707);
s.close();
}
public void endMove()
{
//no use
}
}