From e31b12325e428d0f1bdf35bfd2deaf5c13397d61 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sat, 23 Jan 2016 16:59:15 +0100 Subject: [PATCH] Added "info [username]" command to get history for offline players. Remove user info column. --- .../mage/client/table/PlayersChatPanel.java | 14 ++-- Mage.Common/src/mage/view/UsersView.java | 10 +-- .../main/java/mage/server/ChatManager.java | 63 +++++++-------- .../src/main/java/mage/server/User.java | 11 +-- .../main/java/mage/server/UserManager.java | 77 +++++++++++-------- .../java/mage/server/game/GamesRoomImpl.java | 3 +- 6 files changed, 84 insertions(+), 94 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/table/PlayersChatPanel.java b/Mage.Client/src/main/java/mage/client/table/PlayersChatPanel.java index 0aa6e20af9..795a8618cd 100644 --- a/Mage.Client/src/main/java/mage/client/table/PlayersChatPanel.java +++ b/Mage.Client/src/main/java/mage/client/table/PlayersChatPanel.java @@ -26,7 +26,7 @@ * or implied, of BetaSteward_at_googlemail.com. */ -/* + /* * ChatPanel.java * * Created on 15-Dec-2009, 11:04:31 PM @@ -61,7 +61,7 @@ public class PlayersChatPanel extends javax.swing.JPanel { private final List players = new ArrayList<>(); private final UserTableModel userTableModel; - private static final int[] defaultColumnsWidth = {20, 100, 100, 100, 80, 80}; + private static final int[] DEFAULT_COLUMNS_WIDTH = {20, 100, 100, 80, 80}; /* @@ -78,7 +78,7 @@ public class PlayersChatPanel extends javax.swing.JPanel { jTablePlayers.setForeground(Color.white); jTablePlayers.setRowSorter(new MageTableRowSorter(userTableModel)); - TableUtil.setColumnWidthAndOrder(jTablePlayers, defaultColumnsWidth, KEY_USERS_COLUMNS_WIDTH, KEY_USERS_COLUMNS_ORDER); + TableUtil.setColumnWidthAndOrder(jTablePlayers, DEFAULT_COLUMNS_WIDTH, KEY_USERS_COLUMNS_WIDTH, KEY_USERS_COLUMNS_ORDER); jTablePlayers.setDefaultRenderer(Icon.class, new CountryCellRenderer()); jScrollPaneTalk.setSystemMessagesPane(colorPaneSystem); @@ -118,7 +118,7 @@ public class PlayersChatPanel extends javax.swing.JPanel { class UserTableModel extends AbstractTableModel { - private final String[] columnNames = new String[]{"Loc", "Players", "History", "Info", "Games", "Connection"}; + private final String[] columnNames = new String[]{"Loc", "Players", "History", "Games", "Connection"}; private UsersView[] players = new UsersView[0]; public void loadData(Collection roomUserInfoList) throws MageRemoteException { @@ -128,7 +128,7 @@ public class PlayersChatPanel extends javax.swing.JPanel { TableColumnModel tcm = th.getColumnModel(); tcm.getColumn(jTablePlayers.convertColumnIndexToView(1)).setHeaderValue("Players (" + this.players.length + ")"); - tcm.getColumn(jTablePlayers.convertColumnIndexToView(4)).setHeaderValue( + tcm.getColumn(jTablePlayers.convertColumnIndexToView(3)).setHeaderValue( "Games " + roomUserInfo.getNumberActiveGames() + (roomUserInfo.getNumberActiveGames() != roomUserInfo.getNumberGameThreads() ? " (T:" + roomUserInfo.getNumberGameThreads() : " (") + " limit: " + roomUserInfo.getNumberMaxGames() + ")"); @@ -156,10 +156,8 @@ public class PlayersChatPanel extends javax.swing.JPanel { case 2: return players[arg0].getHistory(); case 3: - return players[arg0].getInfoState(); - case 4: return players[arg0].getInfoGames(); - case 5: + case 4: return players[arg0].getInfoPing(); } return ""; diff --git a/Mage.Common/src/mage/view/UsersView.java b/Mage.Common/src/mage/view/UsersView.java index 6e9d649461..40a39fc41e 100644 --- a/Mage.Common/src/mage/view/UsersView.java +++ b/Mage.Common/src/mage/view/UsersView.java @@ -24,7 +24,7 @@ * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. -*/ + */ package mage.view; import java.io.Serializable; @@ -40,15 +40,13 @@ public class UsersView implements Serializable { private final String flagName; private final String userName; private final String history; - private final String infoState; private final String infoGames; private final String infoPing; - public UsersView(String flagName, String userName, String history, String infoState, String infoGames, String infoPing) { + public UsersView(String flagName, String userName, String history, String infoGames, String infoPing) { this.flagName = flagName; this.history = history; this.userName = userName; - this.infoState = infoState; this.infoGames = infoGames; this.infoPing = infoPing; } @@ -65,10 +63,6 @@ public class UsersView implements Serializable { return history; } - public String getInfoState() { - return infoState; - } - public String getInfoGames() { return infoGames; } diff --git a/Mage.Server/src/main/java/mage/server/ChatManager.java b/Mage.Server/src/main/java/mage/server/ChatManager.java index ce1b6f5e09..1b5e1a212d 100644 --- a/Mage.Server/src/main/java/mage/server/ChatManager.java +++ b/Mage.Server/src/main/java/mage/server/ChatManager.java @@ -24,8 +24,7 @@ * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. -*/ - + */ package mage.server; import java.util.ArrayList; @@ -44,14 +43,15 @@ import org.apache.log4j.Logger; public class ChatManager { private static final Logger logger = Logger.getLogger(ChatManager.class); - + private static final ChatManager INSTANCE = new ChatManager(); public static ChatManager getInstance() { return INSTANCE; } - private ChatManager() {} + private ChatManager() { + } private final ConcurrentHashMap chatSessions = new ConcurrentHashMap<>(); @@ -66,16 +66,16 @@ public class ChatManager { if (chatSession != null) { chatSession.join(userId); } else { - logger.trace("Chat to join not found - chatId: " + chatId +" userId: " + userId); - } - + logger.trace("Chat to join not found - chatId: " + chatId + " userId: " + userId); + } + } public void leaveChat(UUID chatId, UUID userId) { ChatSession chatSession = chatSessions.get(chatId); if (chatSession != null && chatSession.hasUser(userId)) { chatSession.kill(userId, DisconnectReason.CleaningUp); - } + } } public void destroyChatSession(UUID chatId) { @@ -88,7 +88,7 @@ public class ChatManager { logger.trace("Chat removed - chatId: " + chatId); } else { logger.trace("Chat to destroy does not exist - chatId: " + chatId); - } + } } } } @@ -119,63 +119,56 @@ public class ChatManager { } } - private boolean performUserCommand(User user, String message, UUID chatId) { String command = message.substring(1).trim().toUpperCase(Locale.ENGLISH); - if (command.equals("I") || command.equals("INFO")) { - user.setInfo(""); - chatSessions.get(chatId).broadcastInfoToUser(user,message); - return true; - } if (command.startsWith("I ") || command.startsWith("INFO ")) { - user.setInfo(message.substring(command.startsWith("I ") ? 3 : 6)); - chatSessions.get(chatId).broadcastInfoToUser(user,message); + message = UserManager.getInstance().getUserHistory(message.substring(command.startsWith("I ") ? 3 : 6)); + chatSessions.get(chatId).broadcastInfoToUser(user, message); return true; } if (command.startsWith("W ") || command.startsWith("WHISPER ")) { - String rest = message.substring(command.startsWith("W ")? 3 : 9); + String rest = message.substring(command.startsWith("W ") ? 3 : 9); int first = rest.indexOf(" "); if (first > 1) { - String userToName = rest.substring(0,first); + String userToName = rest.substring(0, first); rest = rest.substring(first + 1).trim(); User userTo = UserManager.getInstance().getUserByName(userToName); if (userTo != null) { if (!chatSessions.get(chatId).broadcastWhisperToUser(user, userTo, rest)) { message += new StringBuilder("
User ").append(userToName).append(" not found").toString(); - chatSessions.get(chatId).broadcastInfoToUser(user,message); + chatSessions.get(chatId).broadcastInfoToUser(user, message); } } else { message += new StringBuilder("
User ").append(userToName).append(" not found").toString(); - chatSessions.get(chatId).broadcastInfoToUser(user,message); + chatSessions.get(chatId).broadcastInfoToUser(user, message); } return true; } } if (command.equals("L") || command.equals("LIST")) { message += new StringBuilder("
List of commands:") - .append("
\\info [text] - set a info text to your player") + .append("
\\info [username] - shows the history of a player") .append("
\\list - Show a list of commands") .append("
\\whisper [player name] [text] - whisper to the player with the given name").toString(); - chatSessions.get(chatId).broadcastInfoToUser(user,message); + chatSessions.get(chatId).broadcastInfoToUser(user, message); return true; } return false; } - - /** - * - * use mainly for announcing that a user connection was lost or that a user has reconnected - * + * + * use mainly for announcing that a user connection was lost or that a user + * has reconnected + * * @param userId * @param message - * @param color + * @param color */ public void broadcast(UUID userId, String message, MessageColor color) { User user = UserManager.getInstance().getUser(userId); if (user != null) { - for (ChatSession chat: chatSessions.values()) { + for (ChatSession chat : chatSessions.values()) { if (chat.hasUser(userId)) { chat.broadcast(user.getName(), message, color); } @@ -186,16 +179,16 @@ public class ChatManager { public void sendReconnectMessage(UUID userId) { User user = UserManager.getInstance().getUser(userId); if (user != null) { - for (ChatSession chat: chatSessions.values()) { + for (ChatSession chat : chatSessions.values()) { if (chat.hasUser(userId)) { chat.broadcast(null, user.getName() + " has reconnected", MessageColor.BLUE, true, MessageType.STATUS); - } - } + } + } } } - + public void removeUser(UUID userId, DisconnectReason reason) { - for (ChatSession chatSession: chatSessions.values()) { + for (ChatSession chatSession : chatSessions.values()) { if (chatSession.hasUser(userId)) { chatSession.kill(userId, reason); } diff --git a/Mage.Server/src/main/java/mage/server/User.java b/Mage.Server/src/main/java/mage/server/User.java index cad00ac667..b6dad40c54 100644 --- a/Mage.Server/src/main/java/mage/server/User.java +++ b/Mage.Server/src/main/java/mage/server/User.java @@ -82,7 +82,6 @@ public class User { private final Map sideboarding; private final List watchedGames; private String sessionId; - private String info = ""; private String pingInfo = ""; private Date lastActivity; private UserState userState; @@ -509,14 +508,6 @@ public class User { return sb.toString(); } - public String getInfo() { - return info; - } - - public void setInfo(String Info) { - this.info = Info; - } - public void addGameWatchInfo(UUID gameId) { watchedGames.add(gameId); } @@ -561,7 +552,7 @@ public class User { return ""; } - private static String userStatsToString(ResultProtos.UserStatsProto proto) { + public static String userStatsToString(ResultProtos.UserStatsProto proto) { List builders = new ArrayList<>(); if (proto.getMatches() > 0) { StringBuilder builder = new StringBuilder(); diff --git a/Mage.Server/src/main/java/mage/server/UserManager.java b/Mage.Server/src/main/java/mage/server/UserManager.java index 3306a6ddba..2c363b47a6 100644 --- a/Mage.Server/src/main/java/mage/server/UserManager.java +++ b/Mage.Server/src/main/java/mage/server/UserManager.java @@ -1,16 +1,16 @@ /* * Copyright 2011 BetaSteward_at_googlemail.com. All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. - * + * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. - * + * * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR @@ -20,7 +20,7 @@ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. @@ -38,6 +38,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import mage.server.User.UserState; +import mage.server.record.UserStats; +import mage.server.record.UserStatsRepository; import mage.server.util.ThreadExecutor; import org.apache.log4j.Logger; @@ -45,7 +47,7 @@ import org.apache.log4j.Logger; * * manages users - if a user is disconnected and 10 minutes have passed with no * activity the user is removed - * + * * @author BetaSteward_at_googlemail.com */ public class UserManager { @@ -56,7 +58,7 @@ public class UserManager { private final ConcurrentHashMap users = new ConcurrentHashMap<>(); private final ConcurrentHashMap usersByName = new ConcurrentHashMap<>(); - + private static final ExecutorService callExecutor = ThreadExecutor.getInstance().getCallExecutor(); private static final UserManager INSTANCE = new UserManager(); @@ -64,8 +66,8 @@ public class UserManager { public static UserManager getInstance() { return INSTANCE; } - - private UserManager() { + + private UserManager() { expireExecutor.scheduleAtFixedRate(new Runnable() { @Override public void run() { @@ -113,7 +115,7 @@ public class UserManager { public void disconnect(UUID userId, DisconnectReason reason) { if (userId != null) { User user = users.get(userId); - if (user != null) { + if (user != null) { user.setSessionId(""); // Session will be set again with new id if user reconnects } ChatManager.getInstance().removeUser(userId, reason); @@ -123,44 +125,44 @@ public class UserManager { public boolean isAdmin(UUID userId) { if (userId != null) { User user = users.get(userId); - if (user != null) { + if (user != null) { return user.getName().equals("Admin"); } } return false; } - public void removeUser(final UUID userId, final DisconnectReason reason) { + public void removeUser(final UUID userId, final DisconnectReason reason) { if (userId != null) { final User user = users.get(userId); if (user != null) { callExecutor.execute( - new Runnable() { - @Override - public void run() { - try { - logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId); - user.remove(reason); - logger.debug("USER REMOVE END - " + user.getName()); - } catch (Exception ex) { - handleException(ex); - } finally { - users.remove(userId); - usersByName.remove(user.getName()); - } + new Runnable() { + @Override + public void run() { + try { + logger.info("USER REMOVE - " + user.getName() + " (" + reason.toString() + ") userId: " + userId); + user.remove(reason); + logger.debug("USER REMOVE END - " + user.getName()); + } catch (Exception ex) { + handleException(ex); + } finally { + users.remove(userId); + usersByName.remove(user.getName()); } } + } ); } else { logger.warn("Trying to remove userId: " + userId + " - but it does not exist."); } - } + } } public boolean extendUserSession(UUID userId, String pingInfo) { if (userId != null) { User user = users.get(userId); - if (user != null) { + if (user != null) { user.updateLastActivity(pingInfo); return true; } @@ -169,7 +171,8 @@ public class UserManager { } /** - * Is the connection lost for more than 3 minutes, the user will be removed (within 3 minutes the user can reconnect) + * Is the connection lost for more than 3 minutes, the user will be removed + * (within 3 minutes the user can reconnect) */ private void checkExpired() { Calendar calendar = Calendar.getInstance(); @@ -185,13 +188,25 @@ public class UserManager { public void handleException(Exception ex) { if (ex != null) { - logger.fatal("User manager exception " + (ex.getMessage() == null ? "null":ex.getMessage())); + logger.fatal("User manager exception " + (ex.getMessage() == null ? "null" : ex.getMessage())); if (ex.getCause() != null) { - logger.debug("- Cause: " + (ex.getCause().getMessage() == null ? "null":ex.getCause().getMessage())); + logger.debug("- Cause: " + (ex.getCause().getMessage() == null ? "null" : ex.getCause().getMessage())); } ex.printStackTrace(); - }else { + } else { logger.fatal("User manager exception - null"); } } + + public String getUserHistory(String userName) { + User user = getUserByName(userName); + if (user == null) { + UserStats userStats = UserStatsRepository.instance.getUser(userName); + if (userStats == null) { + return "User " + userName + " not found"; + } + return User.userStatsToString(userStats.getProto()); + } + return "History of user " + userName + ": " + user.getUserData().getHistory(); + } } diff --git a/Mage.Server/src/main/java/mage/server/game/GamesRoomImpl.java b/Mage.Server/src/main/java/mage/server/game/GamesRoomImpl.java index 8ac189db78..30b005e19c 100644 --- a/Mage.Server/src/main/java/mage/server/game/GamesRoomImpl.java +++ b/Mage.Server/src/main/java/mage/server/game/GamesRoomImpl.java @@ -114,14 +114,13 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable { List users = new ArrayList<>(); for (User user : UserManager.getInstance().getUsers()) { try { - users.add(new UsersView(user.getUserData().getFlagName(), user.getName(), user.getHistory(), user.getInfo(), user.getGameInfo(), user.getPingInfo())); + users.add(new UsersView(user.getUserData().getFlagName(), user.getName(), user.getHistory(), user.getGameInfo(), user.getPingInfo())); } catch (Exception ex) { logger.fatal("User update exception: " + user.getName() + " - " + ex.toString(), ex); users.add(new UsersView( (user.getUserData() != null && user.getUserData().getFlagName() != null) ? user.getUserData().getFlagName() : "world", user.getName() != null ? user.getName() : "", user.getHistory() != null ? user.getHistory() : "", - user.getInfo() != null ? user.getInfo() : "", "[exception]", user.getPingInfo() != null ? user.getPingInfo() : "")); }