feat: improved LobbyConnection

This commit is contained in:
Yannik Bretschneider 2021-06-07 01:45:22 +02:00
parent f3e3f9f7d7
commit cc8277ff99

View File

@ -12,6 +12,7 @@ import uulm.teamname.marvelous.gamelibrary.messages.client.RequestMessage;
import uulm.teamname.marvelous.gamelibrary.messages.server.*;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.UserManager;
import java.util.*;
import java.util.concurrent.BlockingQueue;
@ -106,7 +107,6 @@ public class LobbyConnection implements Runnable {
Logger.info("Lobby '{}' is terminating. Exiting...", gameID);
return;
} else if (currentMessage == null) {
// Logger.trace("Message was null, continuing"); // TODO: remove for production
continue;
} else if (currentMessage.item2 instanceof CharacterSelectionMessage) {
var origin = currentMessage.item1;
@ -172,13 +172,18 @@ public class LobbyConnection implements Runnable {
Logger.info("Lobby '{}' is terminating. Exiting...", gameID);
return;
} else if (currentMessage == null) {
Logger.trace("Message was null, continuing"); // TODO: remove for production
// Logger.trace("Message was null, continuing"); // TODO: remove for production
continue;
} else if (currentMessage.item2 instanceof CharacterSelectionMessage) {
var origin = currentMessage.item1;
var message = (CharacterSelectionMessage) currentMessage.item2;
// var message = (CharacterSelectionMessage) currentMessage.item2;
Logger.debug("CharacterSelectionMessage sent by Player '{}' during ingame phase", origin.name);
receiveCharacterSelection(origin, message);
sendError(
currentMessage.item1,
"CharacterSelection rejected as character selection phase is over already");
} else if (currentMessage.item2 instanceof RequestMessage) {
var origin = currentMessage.item1;
var message = (RequestMessage) currentMessage.item2;
@ -204,10 +209,6 @@ public class LobbyConnection implements Runnable {
private Tuple<Participant, BasicMessage> getMessageAsync(int timeoutMillis) {
Tuple<Participant, BasicMessage> currentMessage = null;
try {
Logger.trace("Checking for messages. Currently the amount of messages is {}",
incomingMessages.size());
Logger.trace("Polling incoming message queue");
currentMessage = incomingMessages.poll(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
@ -219,11 +220,18 @@ public class LobbyConnection implements Runnable {
/** Send messages to the lobbyConnection. This is thread-safe, and meant to be called from the outside. */
public void receiveMessage(Participant origin, BasicMessage message) {
Logger.trace("Lobby '{}' received message from participant '{}'", gameID, origin.name);
this.incomingMessages.add(Tuple.of(origin, message));
try {
Logger.trace("Adding to queue...");
this.incomingMessages.put(Tuple.of(origin, message));
} catch (InterruptedException e) {
Logger.warn("Adding message to lobby was interrupted!");
}
Logger.trace("Message placed inside queue. Current length is {}", incomingMessages.size());
}
private void receiveRequests(Participant origin, RequestMessage message) {
// TODO: implement
Logger.trace("Relaying requests through LobbyConnection");
lobby.receiveRequests(message.messages, origin);
}
/**
@ -423,6 +431,9 @@ public class LobbyConnection implements Runnable {
}
public boolean removeParticipant(Participant player) {
Logger.info("Removing participant '{}' with type {} from lobby '{}'",
player.name, player.type, gameID);
UserManager.getInstance().removeUser(player.getConnection());
if (player1 == player) {
player1 = null;
return true;
@ -442,21 +453,29 @@ public class LobbyConnection implements Runnable {
// Methods to send messages
private void sendMessage(Participant recipient, BasicMessage message) {
sendMessageCallback.accept(recipient.getConnection(), message);
Logger.trace("Sending message to participant '{}'", recipient);
if (recipient == null) {
Logger.debug("Sent message to non-existent participant, ignoring...");
} else {
sendMessageCallback.accept(recipient.getConnection(), message);
}
}
private void broadcast(BasicMessage message) {
Logger.trace("Broadcasting message to all participants");
sendMessage(player1, message);
sendMessage(player2, message);
spectators.forEach(spectator -> sendMessage(spectator, message));
}
private void broadcastToSpectators(BasicMessage message) {
Logger.trace("Broadcasting message to all spectators");
spectators.forEach(spectator -> sendMessage(spectator, message));
}
private void broadcastToAllExcept(Participant except, BasicMessage message) {
Logger.trace("Broadcasting message to all participants except for '{}' with role {}",
except.name, except.type);
if (!except.equals(player1)) sendMessage(player1, message);
if (!except.equals(player2)) sendMessage(player2, message);
spectators.stream().filter(spectator -> !except.equals(spectator))
@ -464,12 +483,19 @@ public class LobbyConnection implements Runnable {
}
private void sendError(Participant recipient, String errorMessage) {
sendErrorCallback.accept(recipient.getConnection(), errorMessage);
Logger.trace("Sending error '{}' to participant '{}' with role {}",
errorMessage, recipient.name, recipient.type);
if (recipient == null) {
Logger.debug("Sent error to non-existent participant, ignoring...");
} else {
sendErrorCallback.accept(recipient.getConnection(), errorMessage);
}
}
// Methods to send events
public void sendEvents(Participant recipient, Event... events) {
Logger.trace("Sending {} events to participant '{}'", events.length, recipient.name);
var message = new EventMessage();
message.messages = events;
@ -477,7 +503,8 @@ public class LobbyConnection implements Runnable {
}
public void broadcastEvents(Event... events) {
var message = new EventMessage();
Logger.trace("Broadcasting {} events to all participants", events.length);
var message = new EventMessage();
message.messages = events;
broadcast(message);
@ -512,12 +539,14 @@ public class LobbyConnection implements Runnable {
return characterSelectionActive == that.characterSelectionActive && inGame == that.inGame && Objects.equals(lobby, that.lobby) && Objects.equals(gameID, that.gameID) && Objects.equals(player1, that.player1) && Objects.equals(player2, that.player2) && Objects.equals(spectators, that.spectators) && Objects.equals(incomingMessages, that.incomingMessages);
}
private Integer hashCode;
@Override
public int hashCode() {
int result = Objects.hash(gameID, player1, player2, characterSelectionActive, inGame, active, spectators, incomingMessages, sendMessageCallback, sendErrorCallback, selectionPossibilities);
result = 31 * result + Arrays.hashCode(playerOneSelection);
result = 31 * result + Arrays.hashCode(playerTwoSelection);
return result;
if (hashCode == null) {
hashCode = Objects.hash(gameID, player1, player2, characterSelectionActive, inGame, active, spectators, incomingMessages, sendMessageCallback, sendErrorCallback, selectionPossibilities);
}
return hashCode;
}
@Override