forked from External/mage
Introducing Optionals
This commit is contained in:
parent
97261fdcca
commit
f98afdf4ad
21 changed files with 407 additions and 483 deletions
|
|
@ -27,25 +27,13 @@
|
|||
*/
|
||||
package mage.server;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import mage.MageException;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.cards.decks.InvalidDeckException;
|
||||
import mage.constants.RangeOfInfluence;
|
||||
import mage.constants.TableState;
|
||||
import mage.game.Game;
|
||||
import mage.game.GameException;
|
||||
import mage.game.GameOptions;
|
||||
import mage.game.Seat;
|
||||
import mage.game.Table;
|
||||
import mage.game.*;
|
||||
import mage.game.draft.Draft;
|
||||
import mage.game.draft.DraftPlayer;
|
||||
import mage.game.events.Listener;
|
||||
|
|
@ -72,6 +60,15 @@ import mage.server.util.ThreadExecutor;
|
|||
import mage.view.ChatMessage;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
|
|
@ -97,9 +94,14 @@ public class TableController {
|
|||
this.options = options;
|
||||
match = GameFactory.getInstance().createMatch(options.getGameType(), options);
|
||||
if (userId != null) {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
Optional<User> user = UserManager.getInstance().getUser(userId);
|
||||
// TODO: Handle if user == null
|
||||
controllerName = user.getName();
|
||||
if(user.isPresent()) {
|
||||
controllerName = user.get().getName();
|
||||
}
|
||||
else{
|
||||
controllerName = "undefined";
|
||||
}
|
||||
} else {
|
||||
controllerName = "System";
|
||||
}
|
||||
|
|
@ -112,12 +114,12 @@ public class TableController {
|
|||
this.userId = userId;
|
||||
tournament = TournamentFactory.getInstance().createTournament(options.getTournamentType(), options);
|
||||
if (userId != null) {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user == null) {
|
||||
Optional<User> user = UserManager.getInstance().getUser(userId);
|
||||
if (!user.isPresent()) {
|
||||
logger.fatal(new StringBuilder("User for userId ").append(userId).append(" could not be retrieved from UserManager").toString());
|
||||
controllerName = "[unknown]";
|
||||
} else {
|
||||
controllerName = user.getName();
|
||||
controllerName = user.get().getName();
|
||||
}
|
||||
} else {
|
||||
controllerName = "System";
|
||||
|
|
@ -151,11 +153,12 @@ public class TableController {
|
|||
if (seat == null) {
|
||||
throw new GameException("No available seats.");
|
||||
}
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user == null) {
|
||||
Optional<User> _user = UserManager.getInstance().getUser(userId);
|
||||
if (!_user.isPresent()) {
|
||||
logger.fatal(new StringBuilder("couldn't get user ").append(name).append(" for join tournament userId = ").append(userId).toString());
|
||||
return false;
|
||||
}
|
||||
User user = _user.get();
|
||||
// check password
|
||||
if (!table.getTournament().getOptions().getPassword().isEmpty() && playerType.equals("Human")) {
|
||||
if (!table.getTournament().getOptions().getPassword().equals(password)) {
|
||||
|
|
@ -247,10 +250,11 @@ public class TableController {
|
|||
}
|
||||
|
||||
public synchronized boolean joinTable(UUID userId, String name, String playerType, int skill, DeckCardLists deckList, String password) throws MageException {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user == null) {
|
||||
Optional<User> _user = UserManager.getInstance().getUser(userId);
|
||||
if (!_user.isPresent()) {
|
||||
return false;
|
||||
}
|
||||
User user = _user.get();
|
||||
if (userPlayerMap.containsKey(userId) && playerType.equals("Human")) {
|
||||
user.showUserMessage("Join Table", new StringBuilder("You can join a table only one time.").toString());
|
||||
return false;
|
||||
|
|
@ -398,10 +402,10 @@ public class TableController {
|
|||
private void submitDeck(UUID userId, UUID playerId, Deck deck) {
|
||||
if (table.getState() == TableState.SIDEBOARDING) {
|
||||
match.submitDeck(playerId, deck);
|
||||
UserManager.getInstance().getUser(userId).removeSideboarding(table.getId());
|
||||
UserManager.getInstance().getUser(userId).ifPresent(user -> user.removeSideboarding(table.getId()));
|
||||
} else {
|
||||
TournamentManager.getInstance().submitDeck(tournament.getId(), playerId, deck);
|
||||
UserManager.getInstance().getUser(userId).removeConstructing(playerId);
|
||||
UserManager.getInstance().getUser(userId).ifPresent(user -> user.removeConstructing(playerId));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -421,7 +425,7 @@ public class TableController {
|
|||
|
||||
public boolean watchTable(UUID userId) {
|
||||
if (table.isTournament()) {
|
||||
UserManager.getInstance().getUser(userId).ccShowTournament(table.getTournament().getId());
|
||||
UserManager.getInstance().getUser(userId).ifPresent(user -> user.ccShowTournament(table.getTournament().getId()));
|
||||
return true;
|
||||
} else {
|
||||
if (table.isTournamentSubTable() && !table.getTournament().getOptions().isWatchingAllowed()) {
|
||||
|
|
@ -434,7 +438,7 @@ public class TableController {
|
|||
if (userPlayerMap.get(userId) != null) {
|
||||
return false;
|
||||
}
|
||||
return UserManager.getInstance().getUser(userId).ccWatchGame(match.getGame().getId());
|
||||
return UserManager.getInstance().getUser(userId).get().ccWatchGame(match.getGame().getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -474,8 +478,7 @@ public class TableController {
|
|||
logger.error("No tournament object - userId: " + userId + " table: " + table.getId());
|
||||
return;
|
||||
}
|
||||
if (table != null
|
||||
&& this.userId != null && this.userId.equals(userId) // tourn. sub tables have no creator user
|
||||
if (this.userId != null && this.userId.equals(userId) // tourn. sub tables have no creator user
|
||||
&& (table.getState() == TableState.WAITING
|
||||
|| table.getState() == TableState.READY_TO_START)) {
|
||||
// table not started yet and user is the owner, remove the table
|
||||
|
|
@ -490,11 +493,11 @@ public class TableController {
|
|||
} else {
|
||||
match.quitMatch(playerId);
|
||||
}
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
ChatManager.getInstance().broadcast(chatId, user.getName(), "has left the table", ChatMessage.MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, ChatMessage.SoundToPlay.PlayerLeft);
|
||||
Optional<User> user = UserManager.getInstance().getUser(userId);
|
||||
if (!user.isPresent()) {
|
||||
ChatManager.getInstance().broadcast(chatId, user.get().getName(), "has left the table", ChatMessage.MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, ChatMessage.SoundToPlay.PlayerLeft);
|
||||
if (!table.isTournamentSubTable()) {
|
||||
user.removeTable(playerId);
|
||||
user.get().removeTable(playerId);
|
||||
}
|
||||
} else {
|
||||
logger.debug("User not found - userId: " + userId + " tableId:" + table.getId());
|
||||
|
|
@ -551,9 +554,10 @@ public class TableController {
|
|||
if (table.isTournamentSubTable()) {
|
||||
logger.info("Tourn. match started id:" + match.getId() + " tournId: " + table.getTournament().getId());
|
||||
} else {
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
logger.info("MATCH started [" + match.getName() + "] " + match.getId() + "(" + user.getName() + ")");
|
||||
logger.debug("- " + match.getOptions().getGameType() + " - " + match.getOptions().getDeckType());
|
||||
UserManager.getInstance().getUser(userId).ifPresent(user -> {
|
||||
logger.info("MATCH started [" + match.getName() + "] " + match.getId() + "(" + user.getName() + ")");
|
||||
logger.debug("- " + match.getOptions().getGameType() + " - " + match.getOptions().getDeckType());
|
||||
});
|
||||
}
|
||||
match.startMatch();
|
||||
startGame(null);
|
||||
|
|
@ -576,8 +580,9 @@ public class TableController {
|
|||
StringBuilder opponent = new StringBuilder();
|
||||
for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) { // no AI players
|
||||
if (match.getPlayer(entry.getValue()) != null && !match.getPlayer(entry.getValue()).hasQuit()) {
|
||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (user != null) {
|
||||
Optional<User> _user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (!_user.isPresent()) {
|
||||
User user = _user.get();
|
||||
user.ccGameStarted(match.getGame().getId(), entry.getValue());
|
||||
|
||||
if (creator == null) {
|
||||
|
|
@ -634,11 +639,10 @@ public class TableController {
|
|||
tournament.setStartTime();
|
||||
TournamentManager.getInstance().createTournamentSession(tournament, userPlayerMap, table.getId());
|
||||
for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
|
||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (user != null) {
|
||||
UserManager.getInstance().getUser(entry.getKey()).ifPresent(user -> {
|
||||
logger.info(new StringBuilder("User ").append(user.getName()).append(" tournament started: ").append(tournament.getId()).append(" userId: ").append(user.getId()));
|
||||
user.ccTournamentStarted(tournament.getId(), entry.getValue());
|
||||
}
|
||||
});
|
||||
}
|
||||
ServerMessagesUtil.getInstance().incTournamentsStarted();
|
||||
}
|
||||
|
|
@ -653,10 +657,10 @@ public class TableController {
|
|||
table.initDraft();
|
||||
DraftManager.getInstance().createDraftSession(draft, userPlayerMap, table.getId());
|
||||
for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
|
||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (user != null) {
|
||||
logger.info(new StringBuilder("User ").append(user.getName()).append(" draft started: ").append(draft.getId()).append(" userId: ").append(user.getId()));
|
||||
user.ccDraftStarted(draft.getId(), entry.getValue());
|
||||
Optional<User> user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (user.isPresent()) {
|
||||
logger.info(new StringBuilder("User ").append(user.get().getName()).append(" draft started: ").append(draft.getId()).append(" userId: ").append(user.get().getId()));
|
||||
user.get().ccDraftStarted(draft.getId(), entry.getValue());
|
||||
} else {
|
||||
logger.fatal(new StringBuilder("Start draft user not found userId: ").append(entry.getKey()));
|
||||
}
|
||||
|
|
@ -664,12 +668,13 @@ public class TableController {
|
|||
}
|
||||
|
||||
private void sideboard(UUID playerId, Deck deck) throws MageException {
|
||||
|
||||
for (Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
|
||||
if (entry.getValue().equals(playerId)) {
|
||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||
Optional<User> user = UserManager.getInstance().getUser(entry.getKey());
|
||||
int remaining = (int) futureTimeout.getDelay(TimeUnit.SECONDS);
|
||||
if (user != null) {
|
||||
user.ccSideboard(deck, table.getId(), remaining, options.isLimited());
|
||||
if (user.isPresent()) {
|
||||
user.get().ccSideboard(deck, table.getId(), remaining, options.isLimited());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -775,9 +780,8 @@ public class TableController {
|
|||
// opponent(s) left during sideboarding
|
||||
if (matchPlayer != null) {
|
||||
if (!matchPlayer.hasQuit()) {
|
||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (user != null) {
|
||||
if (table.getState()==TableState.SIDEBOARDING) {
|
||||
UserManager.getInstance().getUser(entry.getKey()).ifPresent(user -> {
|
||||
if (table.getState() == TableState.SIDEBOARDING) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (table.isTournamentSubTable()) {
|
||||
sb.append("Your tournament match of round ");
|
||||
|
|
@ -797,12 +801,12 @@ public class TableController {
|
|||
if (!table.isTournamentSubTable()) {
|
||||
user.removeTable(entry.getValue());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// free resources no longer needed
|
||||
match.cleanUpOnMatchEnd(ConfigSettings.getInstance().isSaveGameActivated(), table.isTournament());
|
||||
}
|
||||
// free resources no longer needed
|
||||
match.cleanUpOnMatchEnd(ConfigSettings.getInstance().isSaveGameActivated(), table.isTournament());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -842,7 +846,7 @@ public class TableController {
|
|||
}
|
||||
|
||||
public void swapSeats(int seatNum1, int seatNum2) {
|
||||
if (table.getState()==TableState.READY_TO_START) {
|
||||
if (table.getState() == TableState.READY_TO_START) {
|
||||
if (seatNum1 >= 0 && seatNum2 >= 0 && seatNum1 < table.getSeats().length && seatNum2 < table.getSeats().length) {
|
||||
Player swapPlayer = table.getSeats()[seatNum1].getPlayer();
|
||||
String swapType = table.getSeats()[seatNum1].getPlayerType();
|
||||
|
|
@ -884,8 +888,7 @@ public class TableController {
|
|||
}
|
||||
} else {
|
||||
// check if table creator is still a valid user, if not remove table
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
return user != null;
|
||||
return UserManager.getInstance().getUser(userId).isPresent();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
|
@ -951,8 +954,8 @@ public class TableController {
|
|||
|| table.getState() == TableState.READY_TO_START)
|
||||
|| !match.isDoneSideboarding()
|
||||
|| (!matchPlayer.hasQuit() && match.getGame() != null && matchPlayer.getPlayer().isInGame())) {
|
||||
User user = UserManager.getInstance().getUser(userPlayerEntry.getKey());
|
||||
if (user == null) {
|
||||
Optional<User> user = UserManager.getInstance().getUser(userPlayerEntry.getKey());
|
||||
if (!user.isPresent()) {
|
||||
logger.debug("- Active user of match is missing: " + matchPlayer.getName());
|
||||
logger.debug("-- matchId:" + match.getId());
|
||||
logger.debug("-- userId:" + userPlayerEntry.getKey());
|
||||
|
|
@ -975,11 +978,10 @@ public class TableController {
|
|||
void cleanUp() {
|
||||
if (!table.isTournamentSubTable()) {
|
||||
for (Map.Entry<UUID, UUID> entry : userPlayerMap.entrySet()) {
|
||||
User user = UserManager.getInstance().getUser(entry.getKey());
|
||||
if (user != null) {
|
||||
user.removeTable(entry.getValue());
|
||||
}
|
||||
UserManager.getInstance().getUser(entry.getKey()).ifPresent(user ->
|
||||
user.removeTable(entry.getValue()));
|
||||
}
|
||||
|
||||
}
|
||||
ChatManager.getInstance().destroyChatSession(chatId);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue