mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 04:52:07 -08:00
* Server - Some changes to user / match / table handling.
This commit is contained in:
parent
5b34b46eac
commit
c3c77e3974
13 changed files with 164 additions and 70 deletions
|
|
@ -72,8 +72,9 @@ public class ChatManager {
|
|||
}
|
||||
|
||||
public void leaveChat(UUID chatId, UUID userId) {
|
||||
if (chatSessions.containsKey(chatId)) {
|
||||
chatSessions.get(chatId).kill(userId, DisconnectReason.CleaningUp);
|
||||
ChatSession chatSession = chatSessions.get(chatId);
|
||||
if (chatSession != null && chatSession.hasUser(userId)) {
|
||||
chatSession.kill(userId, DisconnectReason.CleaningUp);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,30 +107,33 @@ public class ChatManager {
|
|||
}
|
||||
|
||||
public void broadcast(UUID chatId, String userName, String message, MessageColor color, boolean withTime, MessageType messageType, SoundToPlay soundToPlay) {
|
||||
if (message.startsWith("\\")) {
|
||||
User user = UserManager.getInstance().findUser(userName);
|
||||
if (user != null && performUserCommand(user, message, chatId)) {
|
||||
return;
|
||||
ChatSession chatSession = chatSessions.get(chatId);
|
||||
if (chatSession != null) {
|
||||
if (message.startsWith("\\") || message.startsWith("/")) {
|
||||
User user = UserManager.getInstance().findUser(userName);
|
||||
if (user != null && performUserCommand(user, message, chatId)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
chatSession.broadcast(userName, message, color, withTime, messageType, soundToPlay);
|
||||
}
|
||||
chatSessions.get(chatId).broadcast(userName, message, color, withTime, messageType, soundToPlay);
|
||||
}
|
||||
|
||||
|
||||
private boolean performUserCommand(User user, String message, UUID chatId) {
|
||||
String command = message.trim().toUpperCase(Locale.ENGLISH);
|
||||
if (command.equals("\\I") || command.equals("\\INFO")) {
|
||||
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));
|
||||
if (command.startsWith("I ") || command.startsWith("INFO ")) {
|
||||
user.setInfo(message.substring(command.startsWith("I ") ? 3 : 6));
|
||||
chatSessions.get(chatId).broadcastInfoToUser(user,message);
|
||||
return true;
|
||||
}
|
||||
if (command.startsWith("\\W ") || command.startsWith("\\WHISPER ") || command.startsWith("/W ") || command.startsWith("/WHISPER ")) {
|
||||
String rest = message.substring(command.startsWith("\\W ") || command.startsWith("/W ")? 3 : 9);
|
||||
if (command.startsWith("W ") || command.startsWith("WHISPER ")) {
|
||||
String rest = message.substring(command.startsWith("W ")? 3 : 9);
|
||||
int first = rest.indexOf(" ");
|
||||
if (first > 1) {
|
||||
String userToName = rest.substring(0,first);
|
||||
|
|
@ -147,7 +151,7 @@ public class ChatManager {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
if (command.equals("\\L") || command.equals("\\LIST")) {
|
||||
if (command.equals("L") || command.equals("LIST")) {
|
||||
message += new StringBuilder("\nList of commands:")
|
||||
.append("\n\\info <text> - set a info text to your player")
|
||||
.append("\n\\list - Show a list of commands")
|
||||
|
|
@ -191,8 +195,10 @@ public class ChatManager {
|
|||
}
|
||||
|
||||
public void removeUser(UUID userId, DisconnectReason reason) {
|
||||
for (ChatSession chat: chatSessions.values()) {
|
||||
chat.kill(userId, reason);
|
||||
for (ChatSession chatSession: chatSessions.values()) {
|
||||
if (chatSession.hasUser(userId)) {
|
||||
chatSession.kill(userId, reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ package mage.server;
|
|||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import mage.interfaces.callback.ClientCallback;
|
||||
|
|
@ -52,6 +53,7 @@ public class ChatSession {
|
|||
private final Date createTime;
|
||||
private final String info;
|
||||
private final DateFormat timeFormatter = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
|
||||
private final HashSet<UUID> clientsToRemove = new HashSet<>();
|
||||
|
||||
public ChatSession(String info) {
|
||||
chatId = UUID.randomUUID();
|
||||
|
|
@ -148,7 +150,8 @@ public class ChatSession {
|
|||
}
|
||||
|
||||
public void broadcast(String userName, String message, MessageColor color, boolean withTime, MessageType messageType, SoundToPlay soundToPlay) {
|
||||
if (!message.isEmpty()) {
|
||||
if (!message.isEmpty()) {
|
||||
boolean remove = false;
|
||||
final String msg = message;
|
||||
final String time = (withTime ? timeFormatter.format(new Date()):"");
|
||||
final String username = userName;
|
||||
|
|
@ -160,8 +163,16 @@ public class ChatSession {
|
|||
}
|
||||
else {
|
||||
logger.error("User not found but connected to chat - userId: " + userId + " chatId: " + chatId);
|
||||
clientsToRemove.add(userId);
|
||||
remove = true;
|
||||
}
|
||||
}
|
||||
if (remove) {
|
||||
for (UUID userIdToRemove: clientsToRemove) {
|
||||
clients.remove(userIdToRemove);
|
||||
}
|
||||
clientsToRemove.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1016,6 +1016,13 @@ public class MageServerImpl implements MageServer {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin console - Remove table
|
||||
*
|
||||
* @param sessionId
|
||||
* @param tableId
|
||||
* @throws MageException
|
||||
*/
|
||||
@Override
|
||||
public void removeTable(final String sessionId, final UUID tableId) throws MageException {
|
||||
execute("removeTable", sessionId, new Action() {
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ public class SessionManager {
|
|||
if (session != null && session.getUserId() != null && UserManager.getInstance().getUser(session.getUserId()) == null) {
|
||||
logger.error("User for session " + sessionId + " with userId " + session.getUserId() + " is missing. Session removed.");
|
||||
// can happen if user from same host signs in multiple time with multiple clients, after he disconnects with one client
|
||||
disconnect(sessionId, DisconnectReason.LostConnection);
|
||||
disconnect(sessionId, DisconnectReason.ConnectingOtherInstance);
|
||||
return null;
|
||||
}
|
||||
return session;
|
||||
|
|
|
|||
|
|
@ -372,6 +372,13 @@ public class TableController {
|
|||
return player;
|
||||
}
|
||||
|
||||
public void leaveTableAll() {
|
||||
for (UUID leavingUserId: userPlayerMap.keySet()) {
|
||||
leaveTable(leavingUserId);
|
||||
}
|
||||
closeTable();
|
||||
}
|
||||
|
||||
public synchronized void leaveTable(UUID userId) {
|
||||
if (table == null) {
|
||||
logger.error("No table object - userId: " + userId);
|
||||
|
|
@ -413,14 +420,23 @@ public class TableController {
|
|||
TournamentManager.getInstance().quit(tournament.getId(), userId);
|
||||
} else {
|
||||
MatchPlayer matchPlayer = match.getPlayer(playerId);
|
||||
if (matchPlayer != null) {
|
||||
if (table.getState().equals(TableState.SIDEBOARDING)) {
|
||||
// submit deck to finish sideboarding and trigger match start / end
|
||||
matchPlayer.submitDeck(matchPlayer.getDeck());
|
||||
if (matchPlayer != null && !match.hasEnded() && !matchPlayer.hasQuit()) {
|
||||
Game game = match.getGame();
|
||||
if (game != null && !game.hasEnded()){
|
||||
Player player = match.getPlayer(playerId).getPlayer();
|
||||
if (player != null && player.isInGame()) {
|
||||
GameManager.getInstance().quitMatch(game.getId(), userId);
|
||||
}
|
||||
} else {
|
||||
if (table.getState().equals(TableState.SIDEBOARDING)) {
|
||||
if (!matchPlayer.isDoneSideboarding()) {
|
||||
// submit deck to finish sideboarding and trigger match start / end
|
||||
matchPlayer.submitDeck(matchPlayer.getDeck());
|
||||
}
|
||||
}
|
||||
match.leave(playerId);
|
||||
}
|
||||
matchPlayer.setQuit(true);
|
||||
}
|
||||
match.leave(playerId);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
@ -829,11 +845,11 @@ public class TableController {
|
|||
continue;
|
||||
}
|
||||
if (matchPlayer.getPlayer().isHuman()) {
|
||||
humanPlayers++;
|
||||
User user = UserManager.getInstance().getUser(userPlayerEntry.getKey());
|
||||
humanPlayers++;
|
||||
if (!matchPlayer.hasQuit()) {
|
||||
User user = UserManager.getInstance().getUser(userPlayerEntry.getKey());
|
||||
if (user == null) {
|
||||
logger.debug("- Active user of match is missing:");
|
||||
logger.debug("- Active user of match is missing: " + matchPlayer.getName());
|
||||
logger.debug("-- matchId:" + match.getId());
|
||||
logger.debug("-- userId:" + userPlayerEntry.getKey());
|
||||
logger.debug("-- playerId:" + userPlayerEntry.getValue());
|
||||
|
|
@ -869,10 +885,14 @@ public class TableController {
|
|||
}
|
||||
break;
|
||||
case STARTING:
|
||||
if (!getTable().getState().equals(TableState.READY_TO_START)){
|
||||
if (!getTable().getState().equals(TableState.READY_TO_START)) {
|
||||
// tournament is not ready, can't start
|
||||
return false;
|
||||
}
|
||||
if (!table.allSeatsAreOccupied()) {
|
||||
logger.debug("Not alle Seats are occupied: stop start tableId:" + table.getId());
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
getTable().setState(newTableState);
|
||||
|
|
|
|||
|
|
@ -31,8 +31,8 @@ package mage.server;
|
|||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
|
|
@ -155,7 +155,13 @@ public class TableManager {
|
|||
if (controllers.containsKey(tableId)) {
|
||||
return controllers.get(tableId).submitDeck(userId, deckList);
|
||||
}
|
||||
return false;
|
||||
User user = UserManager.getInstance().getUser(userId);
|
||||
if (user != null) {
|
||||
user.removeSideboarding(tableId);
|
||||
user.showUserMessage("Submit deck", "Table no longer active");
|
||||
}
|
||||
// return true so the panel closes
|
||||
return true;
|
||||
}
|
||||
|
||||
public void updateDeck(UUID userId, UUID tableId, DeckCardLists deckList) throws MageException {
|
||||
|
|
@ -201,10 +207,11 @@ public class TableManager {
|
|||
}
|
||||
|
||||
public boolean removeTable(UUID userId, UUID tableId) {
|
||||
if (isTableOwner(tableId, userId) || UserManager.getInstance().isAdmin(userId)) {
|
||||
leaveTable(userId, tableId);
|
||||
if (UserManager.getInstance().isAdmin(userId)) {
|
||||
logger.debug("Table remove request - userId: " + userId + " tableId: " + tableId);
|
||||
TableController tableController = controllers.get(tableId);
|
||||
if (tableController != null) {
|
||||
tableController.leaveTableAll();
|
||||
ChatManager.getInstance().destroyChatSession(tableController.getChatId());
|
||||
removeTable(tableId);
|
||||
}
|
||||
|
|
@ -372,22 +379,30 @@ public class TableManager {
|
|||
}
|
||||
logger.debug("TABLE HEALTH CHECK");
|
||||
List<UUID> toRemove = new ArrayList<>();
|
||||
for (Table table : tables.values()) {
|
||||
if (!table.getState().equals(TableState.FINISHED)) {
|
||||
// remove tables and games not valid anymore
|
||||
logger.debug(table.getId() + " [" + table.getName()+ "] " + formatter.format(table.getStartTime()) +" (" + table.getState().toString() + ") " + (table.isTournament() ? "- Tournament":""));
|
||||
TableController tableController = getController(table.getId());
|
||||
if (tableController != null) {
|
||||
if (table.isTournament()) {
|
||||
if (!tableController.isTournamentStillValid()) {
|
||||
toRemove.add(table.getId());
|
||||
}
|
||||
} else {
|
||||
if (!tableController.isMatchTableStillValid()) {
|
||||
toRemove.add(table.getId());
|
||||
|
||||
ArrayList<Table> tableCopy = new ArrayList<>();
|
||||
tableCopy.addAll(tables.values());
|
||||
for (Table table : tableCopy) {
|
||||
try {
|
||||
if (!table.getState().equals(TableState.FINISHED)) {
|
||||
// remove tables and games not valid anymore
|
||||
logger.debug(table.getId() + " [" + table.getName()+ "] " + formatter.format(table.getStartTime()) +" (" + table.getState().toString() + ") " + (table.isTournament() ? "- Tournament":""));
|
||||
TableController tableController = getController(table.getId());
|
||||
if (tableController != null) {
|
||||
if (table.isTournament()) {
|
||||
if (!tableController.isTournamentStillValid()) {
|
||||
toRemove.add(table.getId());
|
||||
}
|
||||
} else {
|
||||
if (!tableController.isMatchTableStillValid()) {
|
||||
toRemove.add(table.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
logger.debug("Table Health check error tableId: " + table.getId());
|
||||
logger.debug(Arrays.toString(ex.getStackTrace()));
|
||||
}
|
||||
}
|
||||
for (UUID tableId : toRemove) {
|
||||
|
|
|
|||
|
|
@ -193,7 +193,7 @@ 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()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -219,15 +219,15 @@ class TableListSorter implements Comparator<Table> {
|
|||
@Override
|
||||
public int compare(Table one, Table two) {
|
||||
// priority 1 - Not started yet
|
||||
if (one.getState().equals(TableState.READY_TO_START) || one.getState().equals(TableState.WAITING)) {
|
||||
if (two.getState().equals(TableState.READY_TO_START) || two.getState().equals(TableState.WAITING)) {
|
||||
if (one.getState().equals(TableState.READY_TO_START) || one.getState().equals(TableState.WAITING) || one.getState().equals(TableState.STARTING)) {
|
||||
if (two.getState().equals(TableState.READY_TO_START) || two.getState().equals(TableState.WAITING) || two.getState().equals(TableState.STARTING)) {
|
||||
return two.getCreateTime().compareTo(one.getCreateTime());
|
||||
} else {
|
||||
return -1; // one has higher priority
|
||||
}
|
||||
}
|
||||
// priority 2 - Not finished yet -> Sorted by time started
|
||||
if (two.getState().equals(TableState.READY_TO_START) || two.getState().equals(TableState.WAITING)) {
|
||||
if (two.getState().equals(TableState.READY_TO_START) || two.getState().equals(TableState.WAITING) || two.getState().equals(TableState.STARTING)) {
|
||||
return 1; // two has higher priority
|
||||
} else if (one.getEndTime() == null) {
|
||||
if (two.getEndTime() == null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue