Search in sources :

Example 1 with ChatMessage

use of mage.view.ChatMessage in project mage by magefree.

the class ChatSession method broadcast.

public void broadcast(String userName, String message, MessageColor color, boolean withTime, Game game, MessageType messageType, SoundToPlay soundToPlay) {
    if (!message.isEmpty()) {
        Set<UUID> clientsToRemove = new HashSet<>();
        ClientCallback clientCallback = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage(userName, message, (withTime ? new Date() : null), game, color, messageType, soundToPlay));
        List<UUID> chatUserIds = new ArrayList<>();
        final Lock r = lock.readLock();
        r.lock();
        try {
            chatUserIds.addAll(clients.keySet());
        } finally {
            r.unlock();
        }
        for (UUID userId : chatUserIds) {
            Optional<User> user = managerFactory.userManager().getUser(userId);
            if (user.isPresent()) {
                user.get().fireCallback(clientCallback);
            } else {
                clientsToRemove.add(userId);
            }
        }
        if (!clientsToRemove.isEmpty()) {
            final Lock w = lock.readLock();
            w.lock();
            try {
                clients.keySet().removeAll(clientsToRemove);
            } finally {
                w.unlock();
            }
        }
    }
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) ChatMessage(mage.view.ChatMessage) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 2 with ChatMessage

use of mage.view.ChatMessage in project mage by magefree.

the class LocalCommands method displayLocalCommandResponse.

private static void displayLocalCommandResponse(UUID chatId, String response) {
    final String text = new StringBuilder().append("<font color=yellow>").append(response).append("</font>").toString();
    ClientCallback chatMessage = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage("", text, new Date(), null, ChatMessage.MessageColor.BLUE));
    MageFrame.getInstance().processCallback(chatMessage);
}
Also used : ClientCallback(mage.interfaces.callback.ClientCallback) ChatMessage(mage.view.ChatMessage)

Aggregations

ClientCallback (mage.interfaces.callback.ClientCallback)2 ChatMessage (mage.view.ChatMessage)2 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1