refactoring

This commit is contained in:
2022-01-13 19:23:52 +01:00
parent b4b1f39a80
commit 2d3062dcc3
8 changed files with 130 additions and 84 deletions

View File

@@ -21,6 +21,7 @@ public class Lobby {
private final Map<UUID, Session> sessions = new ConcurrentHashMap<>();
private final List<Player> players = new ArrayList<>();
// read lock is required whenever players are read or sessions are modified, write lock is required when players are modified
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private Lobby() {}
@@ -79,6 +80,9 @@ public class Lobby {
}
}
/**
* Adds a player to the lobby.
*/
public void join(Player player) {
lock.writeLock().lock();
try {
@@ -89,6 +93,11 @@ public class Lobby {
}
}
/**
* Removes a player from the lobby. It is ensured that no further lobby related messages
* ({@link SessionCreatedMessage}, {@link SessionModifiedMessage}, {@link SessionRemovedMessage}) are set to this
* player after the call to this method returns.
*/
public void leave(Player player) {
lock.writeLock().lock();
try {