This commit is contained in:
@@ -5,6 +5,8 @@ import java.net.ServerSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import network.protocol.Packet;
|
||||
|
||||
public class Server {
|
||||
@@ -13,6 +15,8 @@ public class Server {
|
||||
final List<ServerConnexion> connexions;
|
||||
private final ServerAcceptThread acceptThread;
|
||||
private final ServerLogicThread logicThread;
|
||||
private final Game game;
|
||||
private int nextPlayerId = 0;
|
||||
|
||||
public Server(short port) throws IOException {
|
||||
this.serverSocket = new ServerSocket(port);
|
||||
@@ -21,6 +25,7 @@ public class Server {
|
||||
this.acceptThread.start();
|
||||
this.logicThread = new ServerLogicThread(this);
|
||||
this.logicThread.start();
|
||||
this.game = new Game();
|
||||
}
|
||||
|
||||
public void broadcastPacket(Packet packet) {
|
||||
@@ -32,8 +37,9 @@ public class Server {
|
||||
public void update() {
|
||||
for (var it = connexions.iterator(); it.hasNext();) {
|
||||
ServerConnexion connexion = it.next();
|
||||
if(!connexion.update()) {
|
||||
if (!connexion.update()) {
|
||||
connexion.close();
|
||||
connexion.nukeConnection();
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
@@ -47,4 +53,15 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public Player addPlayer(String pseudo) {
|
||||
Player p = new Player(nextPlayerId, pseudo);
|
||||
this.game.addPlayer(p);
|
||||
nextPlayerId++;
|
||||
return p;
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,16 +3,21 @@ package network.server;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
|
||||
public class ServerConnexion extends Connexion {
|
||||
|
||||
private final Server server;
|
||||
private final KeepAliveHandler keepAliveHandler;
|
||||
private boolean shouldClose = false;
|
||||
private Player player = null;
|
||||
|
||||
public ServerConnexion(Socket socket, Server server) throws IOException {
|
||||
super(socket);
|
||||
@@ -21,21 +26,36 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
if (shouldClose)
|
||||
if (shouldClose | isClosed())
|
||||
return false;
|
||||
return this.keepAliveHandler.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
sendPacket(new DisconnectPacket("Server stopped"));
|
||||
super.close();
|
||||
shouldClose = true;
|
||||
public void nukeConnection() {
|
||||
if (player != null) {
|
||||
sendPacket(new DisconnectPacket("Server stopped"));
|
||||
this.server.broadcastPacket(new PlayerLeavePacket(player.getId()));
|
||||
this.server.getGame().removePlayer(player.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ConnexionInfoPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
public synchronized void close() {
|
||||
if(shouldClose)
|
||||
return;
|
||||
super.close();
|
||||
shouldClose = true;
|
||||
System.out.println("[Server] Closing connexion !");
|
||||
}
|
||||
|
||||
private void finishLogin() {
|
||||
// send players that have already joined (excluding this one)
|
||||
for (Player p : this.server.getGame().getPlayers().values()) {
|
||||
if (p.getId() != player.getId())
|
||||
sendPacket(new PlayerJoinPacket(p));
|
||||
}
|
||||
this.server.broadcastPacket(new PlayerJoinPacket(player));
|
||||
sendPacket(new ConnexionInfoPacket(player.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -48,4 +68,27 @@ public class ServerConnexion extends Connexion {
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(LoginPacket packet) {
|
||||
if (this.player != null)
|
||||
return;
|
||||
this.player = this.server.addPlayer(packet.getPseudo());
|
||||
finishLogin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ConnexionInfoPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketConnexionInfo'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(PlayerJoinPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerJoin'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(PlayerLeavePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerLeave'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user