changed LobbyConnection construction, and changed Lobby and DisconnectSegment according to new API

This commit is contained in:
Yannik Bretschneider 2021-06-05 23:13:46 +02:00
parent c5e8bd6ece
commit 2f2bbea213
3 changed files with 40 additions and 11 deletions

View File

@ -111,7 +111,7 @@ public class Lobby {
badRequests ++; badRequests ++;
//if the player sends 2 bad messages after one another, the player gets kicked out of the lobby. //if the player sends 2 bad messages after one another, the player gets kicked out of the lobby.
if(badRequests == 2){ if(badRequests == 2){
connection.removePlayer(source); connection.removeParticipant(source);
if(connection.hasPlayer1()){ if(connection.hasPlayer1()){
generateWin(connection.getPlayer1()); generateWin(connection.getPlayer1());
} }
@ -139,7 +139,7 @@ public class Lobby {
connection.sendEvents( connection.sendEvents(
source, source,
new EventBuilder(EventType.TimeoutEvent).buildGameStateEvent()); new EventBuilder(EventType.TimeoutEvent).buildGameStateEvent());
connection.removePlayer(source); connection.removeParticipant(source);
if(connection.hasPlayer1()){ if(connection.hasPlayer1()){
generateWin(connection.getPlayer1()); generateWin(connection.getPlayer1());

View File

@ -23,7 +23,7 @@ public class DisconnectSegment implements Segment {
Logger.trace("DisconnectSegment received {} requests.", packet.size()); Logger.trace("DisconnectSegment received {} requests.", packet.size());
if (packet.containsRequestOfType(RequestType.DisconnectRequest)) { if (packet.containsRequestOfType(RequestType.DisconnectRequest)) {
Logger.debug("Player of Type {} sent DisconnectRequest", packet.getOrigin().type); Logger.debug("Player of Type {} sent DisconnectRequest", packet.getOrigin().type);
parent.getConnection().removePlayer(packet.getOrigin()); parent.getConnection().removeParticipant(packet.getOrigin());
if (packet.getOrigin().type != ParticipantType.Spectator) { if (packet.getOrigin().type != ParticipantType.Spectator) {
if(parent.getConnection().hasPlayer1()){ if(parent.getConnection().hasPlayer1()){

View File

@ -1,5 +1,9 @@
package uulm.teamname.marvelous.server.lobbymanager; package uulm.teamname.marvelous.server.lobbymanager;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType; import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.Request;
@ -17,19 +21,26 @@ import java.util.concurrent.LinkedBlockingQueue;
*/ */
public class LobbyConnection implements Runnable { public class LobbyConnection implements Runnable {
private final Lobby lobby; private final Lobby lobby;
public final String lobbyID; public final String gameID;
private Participant player1, player2; private Participant player1, player2;
private final HashSet<Participant> spectators; private final HashSet<Participant> spectators;
private final BlockingQueue<Request[]> incomingRequests; private final BlockingQueue<Request[]> incomingRequests;
// TODO: FIX THIS JAVADOC
/** /**
* Creates a new LobbyConnection from a given lobby * Creates a new LobbyConnection, whereby a new {@link Lobby} is initialized.
* @param lobby is the lobby that will be connected to
* @param lobbyID
*/ */
public LobbyConnection(Lobby lobby, String lobbyID) { public LobbyConnection(String gameID,
this.lobby = lobby; PartyConfig partyConfig,
this.lobbyID = lobbyID; CharacterConfig characterConfig,
ScenarioConfig scenarioConfig) {
this.lobby = new Lobby(
gameID,
this,
partyConfig,
characterConfig,
scenarioConfig);
this.gameID = gameID;
this.spectators = new HashSet<>(10); this.spectators = new HashSet<>(10);
this.incomingRequests = new LinkedBlockingQueue<>(); this.incomingRequests = new LinkedBlockingQueue<>();
} }
@ -48,6 +59,11 @@ public class LobbyConnection implements Runnable {
return player2 != null; return player2 != null;
} }
/** @return whether a player can join the lobby */
public boolean hasFreePlayerSpot() {
return !(hasPlayer1() && hasPlayer2());
}
public Participant getPlayer1() { public Participant getPlayer1() {
return player1; return player1;
} }
@ -96,13 +112,26 @@ public class LobbyConnection implements Runnable {
* @return true if added successfully, and false otherwise * @return true if added successfully, and false otherwise
*/ */
public boolean addPlayer(Participant player) { public boolean addPlayer(Participant player) {
if (!player.type.equals(ParticipantType.PlayerOne) && !player.type.equals(ParticipantType.PlayerTwo)) {
Logger.warn("addPlayer called with non-player. This is very probably a bug.");
return false;
}
if (!addPlayer1(player)) { if (!addPlayer1(player)) {
return addPlayer2(player); return addPlayer2(player);
} }
return true; return true;
} }
public boolean removePlayer(Participant player) { public boolean addSpectator(Participant spectator) {
if (!spectator.type.equals(ParticipantType.Spectator)) {
Logger.warn("addSpectator called with non-spectator. This is very probably a bug.");
return false;
} else {
return spectators.add(spectator);
}
}
public boolean removeParticipant(Participant player) {
if (player1 == player) { if (player1 == player) {
player1 = null; player1 = null;
return true; return true;