* Fixed some possible java exception causes.

This commit is contained in:
LevelX2 2015-06-27 09:32:50 +02:00
parent b668209f7d
commit c774e95335
5 changed files with 173 additions and 167 deletions

View file

@ -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.
@ -43,6 +43,7 @@ import mage.game.Table;
import mage.game.tournament.TournamentPlayer;
import mage.interfaces.callback.ClientCallback;
import mage.players.net.UserData;
import mage.players.net.UserGroup;
import mage.server.draft.DraftSession;
import mage.server.game.GameManager;
import mage.server.game.GameSessionPlayer;
@ -53,7 +54,6 @@ import mage.server.util.SystemUtil;
import mage.view.TableClientMessage;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
@ -63,12 +63,13 @@ public class User {
private static final Logger logger = Logger.getLogger(User.class);
public enum UserState {
Created, Connected, Disconnected, Reconnected, Expired;
}
private final UUID userId;
private final String userName;
private final String host;
private final String userName;
private final String host;
private final Date connectionTime;
private final Map<UUID, Table> tables;
private final ArrayList<UUID> tablesToDelete;
@ -90,10 +91,10 @@ public class User {
this.userName = userName;
this.host = host;
this.userState = UserState.Created;
this.connectionTime = new Date();
this.lastActivity = new Date();
this.tables = new ConcurrentHashMap<>();
this.gameSessions = new ConcurrentHashMap<>();
this.draftSessions = new ConcurrentHashMap<>();
@ -103,6 +104,8 @@ public class User {
this.watchedGames = new ArrayList<>();
this.tablesToDelete = new ArrayList<>();
this.sessionId = "";
// default these to avaiod NPE -> will be updated from client short after
this.userData = new UserData(UserGroup.PLAYER, 0, false, false, false, null, "world.png", false);
}
public String getName() {
@ -155,16 +158,16 @@ public class User {
long secondsDisconnected = getSecondsDisconnected();
long secondsLeft;
String sign = "";
if (secondsDisconnected > (3 * 60)) {
sign="-";
secondsLeft = secondsDisconnected - (3 *60);
if (secondsDisconnected > (3 * 60)) {
sign = "-";
secondsLeft = secondsDisconnected - (3 * 60);
} else {
secondsLeft = (3 * 60) - secondsDisconnected;
secondsLeft = (3 * 60) - secondsDisconnected;
}
int minutes = (int) secondsLeft / 60;
int seconds = (int) secondsLeft % 60;
return new StringBuilder(sign).append(Integer.toString(minutes)).append(":").append(seconds > 9 ? seconds: "0" + Integer.toString(seconds)).toString();
return new StringBuilder(sign).append(Integer.toString(minutes)).append(":").append(seconds > 9 ? seconds : "0" + Integer.toString(seconds)).toString();
}
public long getSecondsDisconnected() {
@ -217,11 +220,11 @@ public class User {
fireCallback(new ClientCallback("showGameEndDialog", gameId));
}
public void showUserMessage(final String titel, String message) {
public void showUserMessage(final String titel, String message) {
List<String> messageData = new LinkedList<>();
messageData.add(titel);
messageData.add(message);
fireCallback(new ClientCallback("showUserMessage", null, messageData ));
fireCallback(new ClientCallback("showUserMessage", null, messageData));
}
public boolean ccWatchGame(final UUID gameId) {
@ -248,7 +251,7 @@ public class User {
GameManager.getInstance().sendPlayerManaType(gameId, playerId, userId, data);
}
public void sendPlayerBoolean(final UUID gameId, final Boolean data) {
public void sendPlayerBoolean(final UUID gameId, final Boolean data) {
lastActivity = new Date();
GameManager.getInstance().sendPlayerBoolean(gameId, userId, data);
}
@ -275,14 +278,15 @@ public class User {
return true;
}
logger.trace(new StringBuilder("isExpired: User ").append(userName).append(" lastActivity: ").append(lastActivity).append(" expired: ").append(expired).toString());
return false; /*userState == UserState.Disconnected && */
return false; /*userState == UserState.Disconnected && */
}
private void reconnect() {
for (Entry<UUID, Table> entry: tables.entrySet()) {
for (Entry<UUID, Table> entry : tables.entrySet()) {
ccJoinedTable(entry.getValue().getRoomId(), entry.getValue().getId(), entry.getValue().isTournament());
}
for (Entry<UUID, UUID> entry: userTournaments.entrySet()) {
for (Entry<UUID, UUID> entry : userTournaments.entrySet()) {
TournamentController tournamentController = TournamentManager.getInstance().getTournamentController(entry.getValue());
if (tournamentController != null) {
ccTournamentStarted(entry.getValue(), entry.getKey());
@ -290,22 +294,22 @@ public class User {
}
}
for (Entry<UUID, GameSessionPlayer> entry: gameSessions.entrySet()) {
for (Entry<UUID, GameSessionPlayer> entry : gameSessions.entrySet()) {
ccGameStarted(entry.getValue().getGameId(), entry.getKey());
entry.getValue().init();
GameManager.getInstance().sendPlayerString(entry.getValue().getGameId(), userId, "");
}
for (Entry<UUID, DraftSession> entry: draftSessions.entrySet()) {
for (Entry<UUID, DraftSession> entry : draftSessions.entrySet()) {
ccDraftStarted(entry.getValue().getDraftId(), entry.getKey());
entry.getValue().init();
entry.getValue().update();
}
for (Entry<UUID, TournamentSession> entry: constructing.entrySet()) {
for (Entry<UUID, TournamentSession> entry : constructing.entrySet()) {
entry.getValue().construct(0); // TODO: Check if this is correct
}
for (Entry<UUID, Deck> entry: sideboarding.entrySet()) {
for (Entry<UUID, Deck> entry : sideboarding.entrySet()) {
TableController controller = TableManager.getInstance().getController(entry.getKey());
ccSideboard(entry.getValue(), entry.getKey(), controller.getRemainingTime(), controller.getOptions().isLimited());
}
@ -355,32 +359,32 @@ public class User {
sideboarding.remove(tableId);
}
public void remove(DisconnectReason reason) {
public void remove(DisconnectReason reason) {
logger.trace("REMOVE " + getName() + " Draft sessions " + draftSessions.size());
for (DraftSession draftSession: draftSessions.values()) {
for (DraftSession draftSession : draftSessions.values()) {
draftSession.setKilled();
}
draftSessions.clear();
logger.trace("REMOVE " + getName() + " Tournament sessions " + userTournaments.size());
for (UUID tournamentId: userTournaments.values()) {
for (UUID tournamentId : userTournaments.values()) {
TournamentManager.getInstance().quit(tournamentId, getId());
}
userTournaments.clear();
logger.trace("REMOVE " + getName() + " Tables " + tables.size());
for (Entry<UUID, Table> entry: tables.entrySet()) {
for (Entry<UUID, Table> entry : tables.entrySet()) {
logger.debug("-- leave tableId: " + entry.getValue().getId());
TableManager.getInstance().leaveTable(userId, entry.getValue().getId());
}
tables.clear();
logger.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size() );
for (GameSessionPlayer gameSessionPlayer: gameSessions.values()) {
logger.debug("-- kill game session of gameId: " + gameSessionPlayer.getGameId() );
logger.trace("REMOVE " + getName() + " Game sessions: " + gameSessions.size());
for (GameSessionPlayer gameSessionPlayer : gameSessions.values()) {
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());
for (UUID gameId: watchedGames) {
for (UUID gameId : watchedGames) {
GameManager.getInstance().stopWatching(gameId, userId);
}
watchedGames.clear();
@ -428,7 +432,7 @@ public class User {
}
if (!isConnected()) {
tournamentPlayer.setDisconnectInfo(" (discon. "+ getDisconnectDuration() + ")");
tournamentPlayer.setDisconnectInfo(" (discon. " + getDisconnectDuration() + ")");
} else {
tournamentPlayer.setDisconnectInfo("");
}
@ -460,7 +464,7 @@ public class User {
}
}
if (!tablesToDelete.isEmpty()) {
for(UUID keyId: tablesToDelete) {
for (UUID keyId : tablesToDelete) {
removeTable(keyId);
}
tablesToDelete.clear();
@ -513,8 +517,8 @@ public class User {
if (isConnected()) {
return pingInfo;
} else {
return " (discon. "+ getDisconnectDuration() + ")";
return " (discon. " + getDisconnectDuration() + ")";
}
}
}