This commit is contained in:
@@ -12,30 +12,68 @@ public class ConnexionStatusView extends BaseView {
|
|||||||
private Client client;
|
private Client client;
|
||||||
private Server server;
|
private Server server;
|
||||||
|
|
||||||
public ConnexionStatusView(StateMachine stateMachine, String address, short port) throws UnknownHostException, IOException {
|
private String displayText = "Connecting ...";
|
||||||
this(stateMachine, null, new Client(address, port));
|
|
||||||
|
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
|
||||||
|
throws UnknownHostException, IOException {
|
||||||
|
super(stateMachine);
|
||||||
|
Thread t = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
this.client = new Client(address, port);
|
||||||
|
bindListeners();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
onDisconnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
|
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
|
||||||
this(stateMachine, new Server(port), new Client("localhost", port));
|
super(stateMachine);
|
||||||
|
Thread t = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
this.server = new Server(port);
|
||||||
|
this.client = new Client("localhost", port);
|
||||||
|
bindListeners();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
onDisconnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindListeners() {
|
||||||
|
this.client.onConnect.connect(this::onConnect);
|
||||||
|
this.client.onClosed.connect(this::onLeave);
|
||||||
|
this.client.onDisconnect.connect(this::onDisconnect);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) {
|
private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) {
|
||||||
super(stateMachine);
|
super(stateMachine);
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.client.onConnect.connect(this::onConnect);
|
|
||||||
this.client.onClosed.connect(this::onLeave);
|
|
||||||
this.server = server;
|
this.server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onConnect() {
|
public void onConnect() {
|
||||||
// System.out.println("Connecté");
|
|
||||||
this.stateMachine.pushState(new MultiPlayerView(stateMachine, client));
|
this.stateMachine.pushState(new MultiPlayerView(stateMachine, client));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onDisconnect() {
|
||||||
|
if (client != null) {
|
||||||
|
String reason = client.getDisconnectReason();
|
||||||
|
if (reason == null)
|
||||||
|
displayText = "Le serveur a fermé la connexion !";
|
||||||
|
else
|
||||||
|
displayText = "Vous avez été déconnecté ! Raison : " + client.getDisconnectReason();
|
||||||
|
} else {
|
||||||
|
displayText = "La connexion a échoué !";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void onLeave() {
|
public void onLeave() {
|
||||||
// System.out.println("Quitté !");
|
|
||||||
this.client.onDisconnect.clear();
|
|
||||||
this.client = null;
|
this.client = null;
|
||||||
// on passe le menu de la connexion
|
// on passe le menu de la connexion
|
||||||
this.closeMenu();
|
this.closeMenu();
|
||||||
@@ -43,7 +81,14 @@ public class ConnexionStatusView extends BaseView {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
ImGui.text("Connecting ...");
|
ImGui.text(displayText);
|
||||||
|
renderReturnButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void closeMenu() {
|
||||||
|
super.closeMenu();
|
||||||
|
cleanResources();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ public class MultiMenu extends BaseView {
|
|||||||
|
|
||||||
public MultiMenu(StateMachine stateMachine) {
|
public MultiMenu(StateMachine stateMachine) {
|
||||||
super(stateMachine);
|
super(stateMachine);
|
||||||
|
address.resize(20);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderCreate() {
|
private void renderCreate() {
|
||||||
|
|||||||
@@ -20,9 +20,7 @@ public class MultiPlayerView extends BaseView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onDisconnect() {
|
public void onDisconnect() {
|
||||||
// System.out.println("ohohohohohohoho");
|
|
||||||
this.stateMachine.popState();
|
this.stateMachine.popState();
|
||||||
this.client.onDisconnect.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ public class Client {
|
|||||||
public final Signal onDisconnect = new Signal();
|
public final Signal onDisconnect = new Signal();
|
||||||
public final Signal onClosed = new Signal();
|
public final Signal onClosed = new Signal();
|
||||||
|
|
||||||
|
String disconnectReason = null;
|
||||||
|
|
||||||
public Client(String address, short port) throws UnknownHostException, IOException {
|
public Client(String address, short port) throws UnknownHostException, IOException {
|
||||||
this.clientConnection = new ClientConnexion(address, port, this);
|
this.clientConnection = new ClientConnexion(address, port, this);
|
||||||
this.game = new Game();
|
this.game = new Game();
|
||||||
@@ -39,6 +41,10 @@ public class Client {
|
|||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getDisconnectReason() {
|
||||||
|
return disconnectReason;
|
||||||
|
}
|
||||||
|
|
||||||
public void forceDisconnect() {
|
public void forceDisconnect() {
|
||||||
this.onClosed.emit();
|
this.onClosed.emit();
|
||||||
stop();
|
stop();
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ public class ClientConnexion extends Connexion {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(DisconnectPacket packet) {
|
public void visitPacket(DisconnectPacket packet) {
|
||||||
|
this.client.disconnectReason = packet.getReason();
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public class Server {
|
|||||||
this.acceptThread.cancel();
|
this.acceptThread.cancel();
|
||||||
this.logicThread.cancel();
|
this.logicThread.cancel();
|
||||||
for (ServerConnexion connexion : this.connexions) {
|
for (ServerConnexion connexion : this.connexions) {
|
||||||
|
connexion.nukeConnection();
|
||||||
connexion.close();
|
connexion.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class ServerConnexion extends Connexion {
|
|||||||
|
|
||||||
public void nukeConnection() {
|
public void nukeConnection() {
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
sendPacket(new DisconnectPacket("Server stopped"));
|
sendPacket(new DisconnectPacket("Le serveur a été fermé !"));
|
||||||
this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
|
this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
|
||||||
this.server.getGame().removePlayer(player.getId());
|
this.server.getGame().removePlayer(player.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user