mirror of
https://github.com/magefree/mage.git
synced 2025-12-23 12:02:01 -08:00
Show user name in title bar of MAGE. Clear table chat after disconnect. Remove user from chat after disconnect. Some tweaking about the messages if a user disconnected/session expired.
This commit is contained in:
parent
ae30e9a884
commit
b6ddaabe44
12 changed files with 58 additions and 28 deletions
|
|
@ -856,6 +856,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
|
|||
if (session.isConnected()) {
|
||||
if (JOptionPane.showConfirmDialog(this, "Are you sure you want to disconnect?", "Confirm disconnect", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
|
||||
session.disconnect(false);
|
||||
tablesPane.clearChat();
|
||||
showMessage("You have disconnected");
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -275,8 +275,7 @@ class TableModel extends AbstractTableModel {
|
|||
}
|
||||
|
||||
public void clear() {
|
||||
this.txtConversation.selectAll();
|
||||
this.txtConversation.replaceSelection("");
|
||||
this.txtConversation.setText("");
|
||||
}
|
||||
|
||||
/** This method is called from within the constructor to
|
||||
|
|
|
|||
|
|
@ -71,6 +71,9 @@ public class TablesPane extends MagePane {
|
|||
tablesPanel.hideTables();
|
||||
}
|
||||
|
||||
public void clearChat() {
|
||||
tablesPanel.getChatPanel().clear();
|
||||
}
|
||||
|
||||
/** This method is called from within the constructor to
|
||||
* initialize the form.
|
||||
|
|
|
|||
|
|
@ -331,6 +331,11 @@ public class TablesPanel extends javax.swing.JPanel {
|
|||
}
|
||||
}
|
||||
|
||||
public ChatPanel getChatPanel() {
|
||||
return this.chatPanel;
|
||||
}
|
||||
|
||||
|
||||
/** This method is called from within the constructor to
|
||||
* initialize the form.
|
||||
* WARNING: Do NOT modify this code. The content of this method is
|
||||
|
|
|
|||
|
|
@ -177,8 +177,8 @@ public class SessionImpl implements Session {
|
|||
if (registerResult) {
|
||||
sessionState = SessionState.CONNECTED;
|
||||
serverState = server.getServerState();
|
||||
logger.info("Connected to MAGE server at " + connection.getHost() + ":" + connection.getPort());
|
||||
client.connected("Connected to " + connection.getHost() + ":" + connection.getPort() + " ");
|
||||
logger.info(new StringBuilder("Connected as ").append(this.getUserName()).append(" to MAGE server at ").append(connection.getHost()).append(":").append(connection.getPort()).toString());
|
||||
client.connected(new StringBuilder("Connected as ").append(this.getUserName()).append(" to ").append(connection.getHost()).append(":").append(connection.getPort()).append(" ").toString());
|
||||
return true;
|
||||
}
|
||||
disconnect(false);
|
||||
|
|
@ -1189,7 +1189,9 @@ public class SessionImpl implements Session {
|
|||
public boolean ping() {
|
||||
try {
|
||||
if (isConnected()) {
|
||||
server.ping(sessionId);
|
||||
if (!server.ping(sessionId)) {
|
||||
logger.error(new StringBuilder("Ping failed: ").append(this.getUserName()).append(" Session: ").append(sessionId).append(" to MAGE server at ").append(connection.getHost()).append(":").append(connection.getPort()).toString());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (MageException ex) {
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public class ChatManager {
|
|||
}
|
||||
|
||||
public void leaveChat(UUID chatId, UUID userId) {
|
||||
chatSessions.get(chatId).kill(userId);
|
||||
chatSessions.get(chatId).kill(userId, User.DisconnectReason.CleaningUp);
|
||||
}
|
||||
|
||||
public void destroyChatSession(UUID chatId) {
|
||||
|
|
@ -87,15 +87,16 @@ public class ChatManager {
|
|||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
for (ChatSession chat: chatSessions.values()) {
|
||||
if (chat.hasUser(userId))
|
||||
if (chat.hasUser(userId)) {
|
||||
chat.broadcast(user.getName(), message, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeUser(UUID userId) {
|
||||
public void removeUser(UUID userId, User.DisconnectReason reason) {
|
||||
for (ChatSession chat: chatSessions.values()) {
|
||||
chat.kill(userId);
|
||||
chat.kill(userId, reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,12 +64,23 @@ public class ChatSession {
|
|||
}
|
||||
}
|
||||
|
||||
public void kill(UUID userId) {
|
||||
public void kill(UUID userId, User.DisconnectReason reason) {
|
||||
if (userId != null && clients.containsKey(userId)) {
|
||||
String userName = clients.get(userId);
|
||||
String message;
|
||||
clients.remove(userId);
|
||||
broadcast(userName, " has left", MessageColor.BLUE);
|
||||
logger.info(userName + " has left chat " + chatId);
|
||||
switch (reason) {
|
||||
case Disconnected:
|
||||
message = " has quit MAGE";
|
||||
break;
|
||||
case SessionExpired:
|
||||
message = " session expired";
|
||||
break;
|
||||
default:
|
||||
message = " has left chat";
|
||||
}
|
||||
broadcast(userName, message, MessageColor.BLUE);
|
||||
logger.info(userName + message + " " + chatId);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,10 +97,12 @@ public class ChatSession {
|
|||
logger.debug("Broadcasting '" + msg + "' for " + chatId);
|
||||
for (UUID userId: clients.keySet()) {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null)
|
||||
if (user != null) {
|
||||
user.fireCallback(new ClientCallback("chatMessage", chatId, new ChatMessage(username, msg, time, color)));
|
||||
else
|
||||
kill(userId);
|
||||
}
|
||||
else {
|
||||
kill(userId, User.DisconnectReason.CleaningUp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ public class Main {
|
|||
sessionName = session.getHost();
|
||||
}
|
||||
if (throwable instanceof ClientDisconnectedException) {
|
||||
SessionManager.getInstance().disconnect(client.getSessionId(), false);
|
||||
SessionManager.getInstance().disconnect(client.getSessionId(), true);
|
||||
logger.info("client disconnected - " + sessionName);
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ public class Session {
|
|||
|
||||
public void kill() {
|
||||
logger.info("session killed for user " + userId);
|
||||
UserManager.getInstance().removeUser(userId);
|
||||
UserManager.getInstance().removeUser(userId, User.DisconnectReason.Disconnected);
|
||||
}
|
||||
|
||||
synchronized void fireCallback(final ClientCallback call) {
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ public class SessionManager {
|
|||
|
||||
public synchronized void disconnect(String sessionId, boolean voluntary) {
|
||||
Session session = sessions.get(sessionId);
|
||||
sessions.remove(sessionId);
|
||||
sessions.remove(sessionId);
|
||||
if (session != null) {
|
||||
if (voluntary) {
|
||||
session.kill();
|
||||
|
|
|
|||
|
|
@ -56,6 +56,10 @@ public class User {
|
|||
Created, Connected, Disconnected, Reconnected;
|
||||
}
|
||||
|
||||
public enum DisconnectReason {
|
||||
SessionExpired, Disconnected, CleaningUp;
|
||||
}
|
||||
|
||||
private UUID userId = UUID.randomUUID();
|
||||
private String userName;
|
||||
private String sessionId = "";
|
||||
|
|
@ -64,7 +68,7 @@ public class User {
|
|||
private Date lastActivity = new Date();
|
||||
private UserState userState;
|
||||
private Map<UUID, Table> tables = new HashMap<UUID, Table>();
|
||||
private Map<UUID, GameSession> gameSessions = new HashMap<UUID, GameSession>();
|
||||
private Map<UUID, GameSession> gameSessions = new HashMap<UUID, GameSession>();
|
||||
private Map<UUID, DraftSession> draftSessions = new HashMap<UUID, DraftSession>();
|
||||
private Map<UUID, TournamentSession> tournamentSessions = new HashMap<UUID, TournamentSession>();
|
||||
private Map<UUID, TournamentSession> constructing = new HashMap<UUID, TournamentSession>();
|
||||
|
|
@ -98,14 +102,14 @@ public class User {
|
|||
this.sessionId = sessionId;
|
||||
if (sessionId.isEmpty()) {
|
||||
userState = UserState.Disconnected;
|
||||
logger.info("User " + userName + " disconnected");
|
||||
logger.info(new StringBuilder("User ").append(userName).append(" disconnected").toString());
|
||||
} else if (userState == UserState.Created) {
|
||||
userState = UserState.Connected;
|
||||
logger.info("User " + userName + " created");
|
||||
logger.info(new StringBuilder("User ").append(userName).append(" created").toString());
|
||||
} else {
|
||||
userState = UserState.Reconnected;
|
||||
reconnect();
|
||||
logger.info("User " + userName + " reconnected");
|
||||
logger.info(new StringBuilder("User ").append(userName).append(" reconnected").toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -184,6 +188,7 @@ public class User {
|
|||
}
|
||||
|
||||
public boolean isExpired(Date expired) {
|
||||
logger.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
|
||||
return /*userState == UserState.Disconnected && */ lastActivity.before(expired);
|
||||
}
|
||||
|
||||
|
|
@ -259,7 +264,7 @@ public class User {
|
|||
sideboarding.remove(tableId);
|
||||
}
|
||||
|
||||
public void kill() {
|
||||
public void kill(DisconnectReason reason) {
|
||||
for (GameSession session: gameSessions.values()) {
|
||||
session.kill();
|
||||
}
|
||||
|
|
@ -275,6 +280,7 @@ public class User {
|
|||
TableManager.getInstance().removeTable(userId, entry.getValue().getId());
|
||||
}
|
||||
}
|
||||
ChatManager.getInstance().removeUser(userId, reason);
|
||||
}
|
||||
|
||||
public void setUserData(UserData userData) {
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ public class UserManager {
|
|||
|
||||
public void disconnect(UUID userId) {
|
||||
if (userId != null) {
|
||||
ChatManager.getInstance().removeUser(userId);
|
||||
ChatManager.getInstance().removeUser(userId, User.DisconnectReason.Disconnected);
|
||||
if (users.containsKey(userId)) {
|
||||
logger.info("user disconnected " + userId);
|
||||
users.get(userId).setSessionId("");
|
||||
|
|
@ -118,12 +118,12 @@ public class UserManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void removeUser(UUID userId) {
|
||||
public void removeUser(UUID userId, User.DisconnectReason reason) {
|
||||
if (users.containsKey(userId)) {
|
||||
logger.info("user removed" + userId);
|
||||
users.get(userId).setSessionId("");
|
||||
ChatManager.getInstance().removeUser(userId, reason);
|
||||
ChatManager.getInstance().broadcast(userId, "has disconnected", MessageColor.BLACK);
|
||||
users.get(userId).kill();
|
||||
users.get(userId).kill(User.DisconnectReason.Disconnected);
|
||||
users.remove(userId);
|
||||
}
|
||||
}
|
||||
|
|
@ -142,7 +142,7 @@ public class UserManager {
|
|||
for (User user: users.values()) {
|
||||
if (user.isExpired(expired.getTime())) {
|
||||
logger.info(user.getName() + " session expired " + user.getId());
|
||||
user.kill();
|
||||
user.kill(User.DisconnectReason.SessionExpired);
|
||||
users.remove(user.getId());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue