Fixed local server time in client's chat messages

This commit is contained in:
rsypen 2018-01-15 09:17:35 +01:00 committed by GitHub
parent bc3274b144
commit ccd121277a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 17 deletions

View file

@ -37,6 +37,8 @@ import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -55,6 +57,11 @@ import org.mage.card.arcane.ManaSymbols;
*/ */
public class ChatPanelBasic extends javax.swing.JPanel { public class ChatPanelBasic extends javax.swing.JPanel {
/**
* Time formatter
*/
protected final DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT);
protected UUID chatId; protected UUID chatId;
/** /**
* Chat message color for opponents. * Chat message color for opponents.
@ -238,10 +245,10 @@ public class ChatPanelBasic extends javax.swing.JPanel {
*/ */
Pattern cardNamePattern = Pattern.compile(".*<font bgcolor=orange.*?</font>.*"); Pattern cardNamePattern = Pattern.compile(".*<font bgcolor=orange.*?</font>.*");
public void receiveMessage(String username, String message, String time, MessageType messageType, MessageColor color) { public void receiveMessage(String username, String message, Date time, MessageType messageType, MessageColor color) {
StringBuilder text = new StringBuilder(); StringBuilder text = new StringBuilder();
if (time != null) { if (time != null) {
text.append(getColoredText(TIMESTAMP_COLOR, time + ": ")); text.append(getColoredText(TIMESTAMP_COLOR, timeFormatter.format(time) + ": "));
//this.txtConversation.append(TIMESTAMP_COLOR, time + " "); //this.txtConversation.append(TIMESTAMP_COLOR, time + " ");
} }
String userColor; String userColor;

View file

@ -28,6 +28,7 @@
package mage.client.chat; package mage.client.chat;
import java.awt.Font; import java.awt.Font;
import java.util.Date;
import mage.client.SessionHandler; import mage.client.SessionHandler;
import mage.client.components.ColorPane; import mage.client.components.ColorPane;
@ -54,7 +55,7 @@ public class ChatPanelSeparated extends ChatPanelBasic {
* @param color Preferred color. Not used. * @param color Preferred color. Not used.
*/ */
@Override @Override
public void receiveMessage(String username, String message, String time, ChatMessage.MessageType messageType, ChatMessage.MessageColor color) { public void receiveMessage(String username, String message, Date time, ChatMessage.MessageType messageType, ChatMessage.MessageColor color) {
switch (messageType) { switch (messageType) {
case TALK: case TALK:
case WHISPER_TO: case WHISPER_TO:
@ -65,7 +66,7 @@ public class ChatPanelSeparated extends ChatPanelBasic {
} }
StringBuilder text = new StringBuilder(); StringBuilder text = new StringBuilder();
if (time != null) { if (time != null) {
text.append(getColoredText(TIMESTAMP_COLOR, time + ": ")); text.append(getColoredText(TIMESTAMP_COLOR, timeFormatter.format(time) + ": "));
} }
String userColor; String userColor;
String textColor; String textColor;

View file

@ -7,7 +7,6 @@ import mage.interfaces.callback.ClientCallback;
import mage.interfaces.callback.ClientCallbackMethod; import mage.interfaces.callback.ClientCallbackMethod;
import mage.view.ChatMessage; import mage.view.ChatMessage;
import java.text.DateFormat;
import java.util.Date; import java.util.Date;
import java.util.Optional; import java.util.Optional;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -15,8 +14,6 @@ import java.util.UUID;
public final class LocalCommands { public final class LocalCommands {
private static final DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT);
/** /**
* Handler for commands that do not require server interaction, i.e settings etc * Handler for commands that do not require server interaction, i.e settings etc
* @param chatId * @param chatId
@ -60,7 +57,7 @@ public final class LocalCommands {
private static void displayLocalCommandResponse(UUID chatId, String response) { private static void displayLocalCommandResponse(UUID chatId, String response) {
final String text = new StringBuilder().append("<font color=yellow>").append(response).append("</font>").toString(); final String text = new StringBuilder().append("<font color=yellow>").append(response).append("</font>").toString();
ClientCallback chatMessage = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, ClientCallback chatMessage = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId,
new ChatMessage("", text, timeFormatter.format(new Date()), ChatMessage.MessageColor.BLUE)); new ChatMessage("", text, new Date(), ChatMessage.MessageColor.BLUE));
MageFrame.getInstance().processCallback(chatMessage); MageFrame.getInstance().processCallback(chatMessage);
} }
} }

View file

@ -29,6 +29,7 @@
package mage.view; package mage.view;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
/** /**
* *
@ -38,7 +39,7 @@ public class ChatMessage implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String username; private String username;
private String time; private Date time;
private String message; private String message;
private MessageColor color; private MessageColor color;
private SoundToPlay soundToPlay; private SoundToPlay soundToPlay;
@ -56,15 +57,15 @@ public class ChatMessage implements Serializable {
PlayerLeft, PlayerQuitTournament, PlayerSubmittedDeck, PlayerWhispered PlayerLeft, PlayerQuitTournament, PlayerSubmittedDeck, PlayerWhispered
} }
public ChatMessage(String username, String message, String time, MessageColor color) { public ChatMessage(String username, String message, Date time, MessageColor color) {
this(username, message, time, color, null); this(username, message, time, color, null);
} }
public ChatMessage(String username, String message, String time, MessageColor color, SoundToPlay soundToPlay) { public ChatMessage(String username, String message, Date time, MessageColor color, SoundToPlay soundToPlay) {
this(username, message, time, color, MessageType.TALK, soundToPlay); this(username, message, time, color, MessageType.TALK, soundToPlay);
} }
public ChatMessage(String username, String message, String time, MessageColor color, MessageType messageType, SoundToPlay soundToPlay) { public ChatMessage(String username, String message, Date time, MessageColor color, MessageType messageType, SoundToPlay soundToPlay) {
this.username = username; this.username = username;
this.message = message; this.message = message;
this.time = time; this.time = time;
@ -93,7 +94,7 @@ public class ChatMessage implements Serializable {
return username; return username;
} }
public String getTime() { public Date getTime() {
return time; return time;
} }

View file

@ -111,7 +111,7 @@ public class ChatSession {
public boolean broadcastInfoToUser(User toUser, String message) { public boolean broadcastInfoToUser(User toUser, String message) {
if (clients.containsKey(toUser.getId())) { if (clients.containsKey(toUser.getId())) {
toUser.fireCallback(new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage(null, message, timeFormatter.format(new Date()), MessageColor.BLUE, MessageType.USER_INFO, null))); toUser.fireCallback(new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage(null, message, new Date(), MessageColor.BLUE, MessageType.USER_INFO, null)));
return true; return true;
} }
return false; return false;
@ -120,10 +120,10 @@ public class ChatSession {
public boolean broadcastWhisperToUser(User fromUser, User toUser, String message) { public boolean broadcastWhisperToUser(User fromUser, User toUser, String message) {
if (clients.containsKey(toUser.getId())) { if (clients.containsKey(toUser.getId())) {
toUser.fireCallback(new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, toUser.fireCallback(new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId,
new ChatMessage(fromUser.getName(), message, timeFormatter.format(new Date()), MessageColor.YELLOW, MessageType.WHISPER_FROM, SoundToPlay.PlayerWhispered))); new ChatMessage(fromUser.getName(), message, new Date(), MessageColor.YELLOW, MessageType.WHISPER_FROM, SoundToPlay.PlayerWhispered)));
if (clients.containsKey(fromUser.getId())) { if (clients.containsKey(fromUser.getId())) {
fromUser.fireCallback(new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, fromUser.fireCallback(new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId,
new ChatMessage(toUser.getName(), message, timeFormatter.format(new Date()), MessageColor.YELLOW, MessageType.WHISPER_TO, null))); new ChatMessage(toUser.getName(), message, new Date(), MessageColor.YELLOW, MessageType.WHISPER_TO, null)));
return true; return true;
} }
} }
@ -133,7 +133,7 @@ public class ChatSession {
public void broadcast(String userName, String message, MessageColor color, boolean withTime, MessageType messageType, SoundToPlay soundToPlay) { public void broadcast(String userName, String message, MessageColor color, boolean withTime, MessageType messageType, SoundToPlay soundToPlay) {
if (!message.isEmpty()) { if (!message.isEmpty()) {
Set<UUID> clientsToRemove = new HashSet<>(); Set<UUID> clientsToRemove = new HashSet<>();
ClientCallback clientCallback = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage(userName, message, (withTime ? timeFormatter.format(new Date()) : ""), color, messageType, soundToPlay)); ClientCallback clientCallback = new ClientCallback(ClientCallbackMethod.CHATMESSAGE, chatId, new ChatMessage(userName, message, (withTime ? new Date() : null), color, messageType, soundToPlay));
List<UUID> chatUserIds = new ArrayList<>(); List<UUID> chatUserIds = new ArrayList<>();
final Lock r = lock.readLock(); final Lock r = lock.readLock();
r.lock(); r.lock();