forked from External/mage
* Some chnages to chat, user and player handling.
This commit is contained in:
parent
58627bfcf6
commit
52897094b3
9 changed files with 258 additions and 88 deletions
|
|
@ -27,6 +27,13 @@
|
|||
*/
|
||||
package mage.server;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import mage.cards.repository.CardInfo;
|
||||
import mage.cards.repository.CardRepository;
|
||||
import mage.server.exceptions.UserNotFoundException;
|
||||
|
|
@ -36,11 +43,6 @@ import mage.view.ChatMessage.MessageType;
|
|||
import mage.view.ChatMessage.SoundToPlay;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
|
|
@ -51,6 +53,7 @@ public enum ChatManager {
|
|||
private static final HashMap<String, String> userMessages = new HashMap<>();
|
||||
|
||||
private final ConcurrentHashMap<UUID, ChatSession> chatSessions = new ConcurrentHashMap<>();
|
||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
public UUID createChatSession(String info) {
|
||||
ChatSession chatSession = new ChatSession(info);
|
||||
|
|
@ -68,6 +71,10 @@ public enum ChatManager {
|
|||
|
||||
}
|
||||
|
||||
public void clearUserMessageStorage() {
|
||||
userMessages.clear();
|
||||
}
|
||||
|
||||
public void leaveChat(UUID chatId, UUID userId) {
|
||||
ChatSession chatSession = chatSessions.get(chatId);
|
||||
if (chatSession != null && chatSession.hasUser(userId)) {
|
||||
|
|
@ -81,7 +88,13 @@ public enum ChatManager {
|
|||
if (chatSession != null) {
|
||||
synchronized (chatSession) {
|
||||
if (chatSessions.containsKey(chatId)) {
|
||||
chatSessions.remove(chatId);
|
||||
final Lock w = lock.writeLock();
|
||||
w.lock();
|
||||
try {
|
||||
chatSessions.remove(chatId);
|
||||
} finally {
|
||||
w.unlock();
|
||||
}
|
||||
logger.trace("Chat removed - chatId: " + chatId);
|
||||
} else {
|
||||
logger.trace("Chat to destroy does not exist - chatId: " + chatId);
|
||||
|
|
@ -263,10 +276,11 @@ public enum ChatManager {
|
|||
* @param userId
|
||||
* @param message
|
||||
* @param color
|
||||
* @throws mage.server.exceptions.UserNotFoundException
|
||||
*/
|
||||
public void broadcast(UUID userId, String message, MessageColor color) throws UserNotFoundException {
|
||||
UserManager.instance.getUser(userId).ifPresent(user -> {
|
||||
chatSessions.values()
|
||||
getChatSessions()
|
||||
.stream()
|
||||
.filter(chat -> chat.hasUser(userId))
|
||||
.forEach(session -> session.broadcast(user.getName(), message, color, true, MessageType.TALK, null));
|
||||
|
|
@ -276,15 +290,15 @@ public enum ChatManager {
|
|||
|
||||
public void sendReconnectMessage(UUID userId) {
|
||||
UserManager.instance.getUser(userId).ifPresent(user
|
||||
-> chatSessions.values()
|
||||
.stream()
|
||||
.filter(chat -> chat.hasUser(userId))
|
||||
.forEach(chatSession -> chatSession.broadcast(null, user.getName() + " has reconnected", MessageColor.BLUE, true, MessageType.STATUS, null)));
|
||||
-> getChatSessions()
|
||||
.stream()
|
||||
.filter(chat -> chat.hasUser(userId))
|
||||
.forEach(chatSession -> chatSession.broadcast(null, user.getName() + " has reconnected", MessageColor.BLUE, true, MessageType.STATUS, null)));
|
||||
|
||||
}
|
||||
|
||||
public void removeUser(UUID userId, DisconnectReason reason) {
|
||||
for (ChatSession chatSession : chatSessions.values()) {
|
||||
for (ChatSession chatSession : getChatSessions()) {
|
||||
if (chatSession.hasUser(userId)) {
|
||||
chatSession.kill(userId, reason);
|
||||
}
|
||||
|
|
@ -292,7 +306,13 @@ public enum ChatManager {
|
|||
}
|
||||
|
||||
public List<ChatSession> getChatSessions() {
|
||||
return new ArrayList<>(chatSessions.values());
|
||||
final Lock r = lock.readLock();
|
||||
r.lock();
|
||||
try {
|
||||
return new ArrayList<>(chatSessions.values());
|
||||
} finally {
|
||||
r.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue