Extending CentralServerData to enable lobby creation and joining

This commit is contained in:
Sebastian Lenzlinger 2022-04-08 16:34:25 +02:00
parent 4557b4801b
commit b0ebbc9c77
3 changed files with 58 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger; import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ServerPinger;
import ch.unibas.dmi.dbis.cs108.sebaschi.Lobby;
import java.io.*; import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
@ -205,6 +206,10 @@ public class ClientHandler implements Runnable {
disconnectClient(); disconnectClient();
} }
public void createNewLobby() {
Lobby newGame = new Lobby(this);
}
/** /**
* Closes the client's socket, in, and out. * Closes the client's socket, in, and out.
*/ */

View File

@ -1,6 +1,7 @@
package ch.unibas.dmi.dbis.cs108.multiplayer.server; package ch.unibas.dmi.dbis.cs108.multiplayer.server;
import ch.unibas.dmi.dbis.cs108.BudaLogConfig; import ch.unibas.dmi.dbis.cs108.BudaLogConfig;
import ch.unibas.dmi.dbis.cs108.sebaschi.CentralServerData;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@ -12,6 +13,7 @@ import org.apache.logging.log4j.Logger;
public class Server { public class Server {
public static final Logger LOGGER = LogManager.getLogger(); public static final Logger LOGGER = LogManager.getLogger();
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
private static CentralServerData allData;
private static final int gamePort = 42069; private static final int gamePort = 42069;
private HashSet<ClientHandler> connectedClients = new HashSet<>(); private HashSet<ClientHandler> connectedClients = new HashSet<>();

View File

@ -5,6 +5,10 @@ import ch.unibas.dmi.dbis.cs108.gamelogic.Game;
import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client; import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client;
import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler; import ch.unibas.dmi.dbis.cs108.multiplayer.server.ClientHandler;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -21,10 +25,52 @@ public class CentralServerData {
public static final BudaLogConfig l = new BudaLogConfig(LOGGER); public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
private Set<ClientHandler> clientsOnServer; private Set<ClientHandler> clientsOnServer;
private Set<Game> activeGames;
private Set<Game> gamesOpenToJoin;
private Map<ClientHandler, Socket> clientSocketMap; private List<Lobby> allLobbies;
private Map<Socket, ClientHandler> socketClientMap; private Map<Integer, Lobby> lobbyIDMap;
private Map<Game, ClientHandler> gameClientMap;
public CentralServerData() {
clientsOnServer = new HashSet<>();
allLobbies = new ArrayList<>();
lobbyIDMap = new HashMap<>();
}
//Getters
/**
* Getter for set of all clients.
* @return the set of all clients.
*/
public Set<ClientHandler> getClientsOnServer() {
return clientsOnServer;
}
/**
* Used to add the client to the set of all clients on server.
* @param client
*/
public void addClientToSetOfAllClients(ClientHandler client) {
this.getClientsOnServer().add(client);
}
public void removeClientFromSetOfAllClients(){
//TODO implement or make sure something equivalent is implemented somewhere else
}
/**
* Getter for List of all lobbies.
* @return a list of all lobbies
*/
public List<Lobby> getAllLobbies() {
return allLobbies;
}
/**
* Mapping from an Integer that repesents a LobbyID to the lobby
* should be set in {@link Lobby} and is then used by clients to join a lobby.
* @return a mapping from Integer to Lobby.
*/
public Map<Integer, Lobby> getLobbyIDMap() {
return lobbyIDMap;
}
} }