Changed that player history string of all players were recreated every time the player list is updated (every 2 seconds).

This commit is contained in:
LevelX2 2016-01-23 12:33:12 +01:00
parent 9ccebfdff4
commit 24dddd3e06
2 changed files with 102 additions and 94 deletions

View file

@ -40,6 +40,7 @@ import java.util.concurrent.TimeUnit;
import mage.cards.decks.Deck;
import mage.constants.ManaType;
import mage.game.Table;
import mage.game.result.ResultProtos;
import mage.game.tournament.TournamentPlayer;
import mage.interfaces.callback.ClientCallback;
import mage.players.net.UserData;
@ -61,7 +62,7 @@ import org.apache.log4j.Logger;
*/
public class User {
private static final Logger logger = Logger.getLogger(User.class);
private static final Logger LOGGER = Logger.getLogger(User.class);
public enum UserState {
@ -87,6 +88,7 @@ public class User {
private UserState userState;
private UserData userData;
private UserStats userStats;
private String history;
public User(String userName, String host) {
this.userId = UUID.randomUUID();
@ -106,6 +108,12 @@ public class User {
this.watchedGames = new ArrayList<>();
this.tablesToDelete = new ArrayList<>();
this.sessionId = "";
this.userStats = UserStatsRepository.instance.getUser(this.userName);
if (userStats != null) {
this.history = userStatsToString(userStats.getProto());
} else {
this.history = "<No Games yet>";
}
}
public String getName() {
@ -129,15 +137,15 @@ public class User {
if (sessionId.isEmpty()) {
userState = UserState.Disconnected;
lostConnection();
logger.trace("USER - lost connection: " + userName + " id: " + userId);
LOGGER.trace("USER - lost connection: " + userName + " id: " + userId);
} else if (userState == UserState.Created) {
userState = UserState.Connected;
logger.trace("USER - created: " + userName + " id: " + userId);
LOGGER.trace("USER - created: " + userName + " id: " + userId);
} else {
userState = UserState.Reconnected;
reconnect();
logger.trace("USER - reconnected: " + userName + " id: " + userId);
LOGGER.trace("USER - reconnected: " + userName + " id: " + userId);
}
}
@ -273,12 +281,13 @@ public class User {
public boolean isExpired(Date expired) {
if (lastActivity.before(expired)) {
logger.trace(userName + " is expired!");
LOGGER.trace(userName + " is expired!");
userState = UserState.Expired;
return true;
}
logger.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
return false; /*userState == UserState.Disconnected && */
LOGGER.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
return false;
/*userState == UserState.Disconnected && */
}
@ -360,35 +369,35 @@ public class User {
}
public void remove(DisconnectReason reason) {
logger.trace("REMOVE " + getName() + " Draft sessions " + draftSessions.size());
LOGGER.trace("REMOVE " + getName() + " Draft sessions " + draftSessions.size());
for (DraftSession draftSession : draftSessions.values()) {
draftSession.setKilled();
}
draftSessions.clear();
logger.trace("REMOVE " + getName() + " Tournament sessions " + userTournaments.size());
LOGGER.trace("REMOVE " + getName() + " Tournament sessions " + userTournaments.size());
for (UUID tournamentId : userTournaments.values()) {
TournamentManager.getInstance().quit(tournamentId, getId());
}
userTournaments.clear();
logger.trace("REMOVE " + getName() + " Tables " + tables.size());
LOGGER.trace("REMOVE " + getName() + " Tables " + tables.size());
for (Entry<UUID, Table> entry : tables.entrySet()) {
logger.debug("-- leave tableId: " + entry.getValue().getId());
LOGGER.debug("-- leave tableId: " + entry.getValue().getId());
TableManager.getInstance().leaveTable(userId, entry.getValue().getId());
}
tables.clear();
logger.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size());
LOGGER.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size());
for (GameSessionPlayer gameSessionPlayer : gameSessions.values()) {
logger.debug("-- kill game session of gameId: " + gameSessionPlayer.getGameId());
LOGGER.debug("-- kill game session of gameId: " + gameSessionPlayer.getGameId());
GameManager.getInstance().quitMatch(gameSessionPlayer.getGameId(), userId);
gameSessionPlayer.quitGame();
}
gameSessions.clear();
logger.trace("REMOVE " + getName() + " watched Games " + watchedGames.size());
LOGGER.trace("REMOVE " + getName() + " watched Games " + watchedGames.size());
for (UUID gameId : watchedGames) {
GameManager.getInstance().stopWatching(gameId, userId);
}
watchedGames.clear();
logger.trace("REMOVE " + getName() + " Chats ");
LOGGER.trace("REMOVE " + getName() + " Chats ");
ChatManager.getInstance().removeUser(userId, reason);
}
@ -446,11 +455,11 @@ public class User {
}
} else {
// can happen if tournamet has just ended
logger.debug(getName() + " tournament player missing - tableId:" + table.getId(), null);
LOGGER.debug(getName() + " tournament player missing - tableId:" + table.getId(), null);
tablesToDelete.add(tableEntry.getKey());
}
} else {
logger.error(getName() + " tournament key missing - tableId: " + table.getId(), null);
LOGGER.error(getName() + " tournament key missing - tableId: " + table.getId(), null);
}
} else {
switch (table.getState()) {
@ -540,5 +549,79 @@ public class User {
// resetUserStats loads UserStats from DB.
public void resetUserStats() {
this.userStats = UserStatsRepository.instance.getUser(this.userName);
this.history = userStatsToString(userStats.getProto());
}
public String getHistory() {
return history;
}
private static String userStatsToString(ResultProtos.UserStatsProto proto) {
List<StringBuilder> builders = new ArrayList<>();
if (proto.getMatches() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("Matches:");
builder.append(proto.getMatches());
List<String> quit = new ArrayList<>();
if (proto.getMatchesIdleTimeout() > 0) {
quit.add("I:" + Integer.toString(proto.getMatchesIdleTimeout()));
}
if (proto.getMatchesTimerTimeout() > 0) {
quit.add("T:" + Integer.toString(proto.getMatchesTimerTimeout()));
}
if (proto.getMatchesQuit() > 0) {
quit.add("Q:" + Integer.toString(proto.getMatchesQuit()));
}
if (quit.size() > 0) {
builder.append(" (");
joinStrings(builder, quit, " ");
builder.append(")");
}
builders.add(builder);
}
if (proto.getTourneys() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("Tourneys:");
builder.append(proto.getTourneys());
List<String> quit = new ArrayList<>();
if (proto.getTourneysQuitDuringDrafting() > 0) {
quit.add("D:" + Integer.toString(proto.getTourneysQuitDuringDrafting()));
}
if (proto.getTourneysQuitDuringConstruction() > 0) {
quit.add("C:" + Integer.toString(proto.getTourneysQuitDuringConstruction()));
}
if (proto.getTourneysQuitDuringRound() > 0) {
quit.add("R:" + Integer.toString(proto.getTourneysQuitDuringRound()));
}
if (quit.size() > 0) {
builder.append(" (");
joinStrings(builder, quit, " ");
builder.append(")");
}
builders.add(builder);
}
return joinBuilders(builders);
}
private static String joinBuilders(List<StringBuilder> builders) {
if (builders.isEmpty()) {
return null;
}
StringBuilder builder = builders.get(0);
for (int i = 1; i < builders.size(); ++i) {
builder.append(" ");
builder.append(builders.get(i));
}
return builder.toString();
}
private static void joinStrings(StringBuilder joined, List<String> strings, String separator) {
for (int i = 0; i < strings.size(); ++i) {
if (i > 0) {
joined.append(separator);
}
joined.append(strings.get(i));
}
}
}

View file

@ -43,13 +43,11 @@ import mage.constants.TableState;
import mage.game.GameException;
import mage.game.Table;
import mage.game.match.MatchOptions;
import mage.game.result.ResultProtos.UserStatsProto;
import mage.game.tournament.TournamentOptions;
import mage.server.RoomImpl;
import mage.server.TableManager;
import mage.server.User;
import mage.server.UserManager;
import mage.server.record.UserStats;
import mage.server.tournament.TournamentManager;
import mage.server.util.ConfigSettings;
import mage.server.util.ThreadExecutor;
@ -93,74 +91,6 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return tableView;
}
private static void joinStrings(StringBuilder joined, List<String> strings, String separator) {
for (int i = 0; i < strings.size(); ++i) {
if (i > 0) {
joined.append(separator);
}
joined.append(strings.get(i));
}
}
private static String joinBuilders(List<StringBuilder> builders) {
if (builders.isEmpty()) {
return null;
}
StringBuilder builder = builders.get(0);
for (int i = 1; i < builders.size(); ++i) {
builder.append(" ");
builder.append(builders.get(i));
}
return builder.toString();
}
private static String userStatsToString(UserStatsProto proto) {
List<StringBuilder> builders = new ArrayList<>();
if (proto.getMatches() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("Matches:");
builder.append(proto.getMatches());
List<String> quit = new ArrayList<>();
if (proto.getMatchesIdleTimeout() > 0) {
quit.add("I:" + Integer.toString(proto.getMatchesIdleTimeout()));
}
if (proto.getMatchesTimerTimeout() > 0) {
quit.add("T:" + Integer.toString(proto.getMatchesTimerTimeout()));
}
if (proto.getMatchesQuit() > 0) {
quit.add("Q:" + Integer.toString(proto.getMatchesQuit()));
}
if (quit.size() > 0) {
builder.append(" (");
joinStrings(builder, quit, " ");
builder.append(")");
}
builders.add(builder);
}
if (proto.getTourneys() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("Tourneys:");
builder.append(proto.getTourneys());
List<String> quit = new ArrayList<>();
if (proto.getTourneysQuitDuringDrafting() > 0) {
quit.add("D:" + Integer.toString(proto.getTourneysQuitDuringDrafting()));
}
if (proto.getTourneysQuitDuringConstruction() > 0) {
quit.add("C:" + Integer.toString(proto.getTourneysQuitDuringConstruction()));
}
if (proto.getTourneysQuitDuringRound() > 0) {
quit.add("R:" + Integer.toString(proto.getTourneysQuitDuringRound()));
}
if (quit.size() > 0) {
builder.append(" (");
joinStrings(builder, quit, " ");
builder.append(")");
}
builders.add(builder);
}
return joinBuilders(builders);
}
private void update() {
ArrayList<TableView> tableList = new ArrayList<>();
ArrayList<MatchView> matchList = new ArrayList<>();
@ -183,19 +113,14 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
matchView = matchList;
List<UsersView> users = new ArrayList<>();
for (User user : UserManager.getInstance().getUsers()) {
String history = null;
UserStats stats = user.getUserStats();
if (stats != null) {
history = userStatsToString(stats.getProto());
}
try {
users.add(new UsersView(user.getUserData().getFlagName(), user.getName(), history, user.getInfo(), user.getGameInfo(), user.getPingInfo()));
users.add(new UsersView(user.getUserData().getFlagName(), user.getName(), user.getHistory(), user.getInfo(), 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() : "<no name>",
history != null ? history : "<no history>",
user.getHistory() != null ? user.getHistory() : "<no history>",
user.getInfo() != null ? user.getInfo() : "<no info>",
"[exception]",
user.getPingInfo() != null ? user.getPingInfo() : "<no ping>"));