-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhostMenu.java
232 lines (207 loc) · 5.14 KB
/
hostMenu.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
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.application.*;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.layout.*;
import javafx.css.*;
import javafx.util.Callback;
import java.net.*;
import java.util.*;
import javafx.stage.*;
import javafx.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
public class hostMenu extends VBox{
private boolean hote;
private ArrayList<Socket> sockets;
private ArrayList<String> names;
private ServerSocket socServ;
private waitClients waitCli;
private Socket me;
private TilePane Players;
private MazeFloors maze;
private String myName;
private Stage stageChat;
public hostMenu(){
super();
Players=new TilePane();
names = new ArrayList<String>();
}
public void initHost(String name,MazeFloors m){
System.out.println("je creer un hote");
maze=m;
clear();
hote=true;
getChildren().addAll(new Label("You are the host."),Players);
try{
socServ = new ServerSocket(netFunc.PORT);
sockets = new ArrayList<Socket>();
initMe(name,"localHost");
getClient();
makeChat();
}catch(Exception e){e.printStackTrace();}
}
public void makeChat(){
chatPane chat=new chatPane();
chat.initHost(myName);
makeStage(chat);
}
public void makeChat(String addr){
chatPane chat=new chatPane();
chat.initClient(myName,addr);
makeStage(chat);
}
public void makeStage(chatPane chat){
stageChat = new Stage();
stageChat.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
chat.close();
stageChat.close();
}
});
stageChat.setTitle("Chat");
StackPane layout = new StackPane();
layout.getChildren().add(chat);
Scene scene=new Scene(layout,500,500);
stageChat.setScene(scene);
stageChat.setResizable(false);
stageChat.show();
}
public void initClient(String name){
clear();
VBox waitIP= new VBox();
Label lab= new Label("Put the IP adress of the host.");
TextField text= new TextField();
Button but = new Button("Validate");
waitIP.getChildren().addAll(lab,text,but);
waitIP.getStyleClass().add("vbox");
but.setOnMouseClicked(e->{
clear();
if(initMe(name,text.getText()))
makeChat(text.getText());
else {
MazeInterface.warning("Cannot connect to server.");
initClient(name);
}
});
this.getChildren().add(waitIP);
}
public boolean initMe(String name, String addr){
System.out.println("J'ai l'addr "+addr);
try{
me=new Socket(addr,netFunc.PORT);
myName=name;
netFunc.sendString(me,name);
waitInfo();
Button play = new Button("Play");
play.setDisable(!hote);
play.setOnMouseClicked(e->{
waitCli.stop();
sendMaze();
});
getChildren().add(play);
printPlayer();
return true;
}catch(Exception e){return false;}
}
public void getClient(){
waitCli = new waitClients();
waitCli.start();
}
private void printPlayer(){
this.getChildren().remove(Players);
Players =new TilePane();
//Players.setSpacing(30);
for(String str:names) {
Label lab=new Label(str);
lab.getStyleClass().add("noms");
Players.getChildren().add(lab);
}
Players.getStyleClass().add("hbox");
this.getChildren().add(getChildren().size()-1,Players);
}
private void waitInfo(){
waitInfo tmp = new waitInfo();
tmp.start();
}
public void stopGetClient(){
waitCli.Stop();
}
public void clear(){
getChildren().clear();
}
public void sendMaze(){
for (Socket tmp : sockets) {
netFunc.sendObject(tmp,maze);
}
}
public void lancerLabi(MazeFloors tmp){
try{
Stage stage=new Stage();
SoloVersion sv=new SoloVersion(tmp,myName,new Scores());
System.out.println("reussi 1");
netView NV=new netView(sv,me);
stage.setScene(NV);
System.out.println("reussi 2");
stage.setFullScreen(true);
stage.show();
if(hote){
Server server = new Server(socServ,names, sockets);
}
}catch(FormatNotSupported e){
}catch(IOException e){}
}
public <K> K[] toTab(ArrayList<K> list){
K[] tab =(K[]) new Object[list.size()];
for(int i=0;i<tab.length;i++){
tab[i]=list.get(i);
}
return tab;
}
class waitClients extends Thread{
private volatile boolean exit = false;
public void run(){
System.out.println("Attente de clients...");
while(!exit){
try{
Socket tmp = socServ.accept();
String str = netFunc.readString(tmp);
if(!exit){
sockets.add(tmp);
names.add(str);
System.out.println("Un nouveau client a été ajouté.");
for(Socket soc:sockets){
netFunc.sendObject(soc,names);
}
System.out.println("J'envoie les listes");
}
}catch(Exception e){}
}
}
public void Stop(){
exit=true;
}
}
class waitInfo extends Thread{
public void run(){
while(true){
try{
Object tmp=netFunc.readObject(me);
if(tmp instanceof ArrayList){
names=(ArrayList<String>)tmp;
Platform.runLater(() -> printPlayer());
}else if(tmp instanceof MazeFloors){
Platform.runLater(()-> lancerLabi((MazeFloors) tmp));
break;
}else{
break;
}
}catch(Exception e){}
}
}
}
}