Merge pull request #3447 from aastrand/master

Ban ignored users from watching
This commit is contained in:
LevelX2 2017-06-03 10:31:24 +02:00 committed by GitHub
commit 13ea2c33c6
8 changed files with 58 additions and 20 deletions

View file

@ -305,10 +305,11 @@ public final class GamePanel extends javax.swing.JPanel {
this.players.clear();
this.playersWhoLeft.clear();
if (jLayeredPane!= null) {
jLayeredPane.remove(abilityPicker);
this.abilityPicker.cleanUp();
jLayeredPane.remove(DialogManager.getManager(gameId));
}
this.abilityPicker.cleanUp();
DialogManager.removeGame(gameId);
if (pickNumber != null) {

View file

@ -137,7 +137,7 @@ public interface MageServer {
void joinGame(UUID gameId, String sessionId) throws MageException;
void watchGame(UUID gameId, String sessionId) throws MageException;
boolean watchGame(UUID gameId, String sessionId) throws MageException;
void stopWatching(UUID gameId, String sessionId) throws MageException;

View file

@ -1016,8 +1016,7 @@ public class SessionImpl implements Session {
public boolean watchGame(UUID gameId) {
try {
if (isConnected()) {
server.watchGame(gameId, sessionId);
return true;
return server.watchGame(gameId, sessionId);
}
} catch (MageException ex) {
handleMageException(ex);

View file

@ -879,14 +879,23 @@ public class MageServerImpl implements MageServer {
}
@Override
public void watchGame(final UUID gameId, final String sessionId) throws MageException {
execute("watchGame", sessionId, () -> {
public boolean watchGame(final UUID gameId, final String sessionId) throws MageException {
return executeWithResult("watchGame", sessionId, new ActionWithResult<Boolean>() {
@Override
public Boolean execute() throws MageException {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
return false;
} else {
UUID userId = session.get().getUserId();
GameManager.instance.watchGame(gameId, userId);
return GameManager.instance.watchGame(gameId, userId);
}
}
@Override
public Boolean negativeResult() {
return false;
}
});
}

View file

@ -605,6 +605,7 @@ public class TableController {
table.initGame();
GameOptions gameOptions = new GameOptions();
gameOptions.rollbackTurnsAllowed = match.getOptions().isRollbackTurnsAllowed();
gameOptions.bannedUsers = match.getOptions().getBannedUsers();
match.getGame().setGameOptions(gameOptions);
GameManager.instance.createGameSession(match.getGame(), userPlayerMap, table.getId(), choosingPlayerId, gameOptions);
String creator = null;

View file

@ -391,14 +391,22 @@ public class GameController implements GameCallback {
return true;
}
public void watch(UUID userId) {
public boolean watch(UUID userId) {
if (userPlayerMap.containsKey(userId)) {
// You can't watch a game if you already a player in it
return;
return false;
}
if (watchers.containsKey(userId)) {
// You can't watch a game if you already watch it
return;
return false;
}
if (!isAllowedToWatch(userId)) {
// Dont want people on our ignore list to stalk us
UserManager.instance.getUser(userId).ifPresent(user -> {
user.showUserMessage("Not allowed", "You are banned from watching this game");
ChatManager.instance.broadcast(chatId, user.getName(), " tried to join, but is banned", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
});
return false;
}
UserManager.instance.getUser(userId).ifPresent(user -> {
GameSessionWatcher gameWatcher = new GameSessionWatcher(userId, game, false);
@ -407,6 +415,7 @@ public class GameController implements GameCallback {
user.addGameWatchInfo(game.getId());
ChatManager.instance.broadcast(chatId, user.getName(), " has started watching", MessageColor.BLUE, true, ChatMessage.MessageType.STATUS, null);
});
return true;
}
public void stopWatching(UUID userId) {
@ -1011,4 +1020,13 @@ public class GameController implements GameCallback {
}
return sb.append(']').toString();
}
public boolean isAllowedToWatch(UUID userId) {
Optional<User> user = UserManager.instance.getUser(userId);
if (user.isPresent()) {
return !gameOptions.bannedUsers.contains(user.get().getName());
}
return false;
}
}

View file

@ -117,11 +117,12 @@ public enum GameManager {
}
}
public void watchGame(UUID gameId, UUID userId) {
public boolean watchGame(UUID gameId, UUID userId) {
GameController gameController = gameControllers.get(gameId);
if (gameController != null) {
gameController.watch(userId);
return gameController.watch(userId);
}
return false;
}
public void stopWatching(UUID gameId, UUID userId) {

View file

@ -1,8 +1,11 @@
package mage.game;
import java.io.Serializable;
import mage.constants.PhaseStep;
import java.io.Serializable;
import java.util.Collections;
import java.util.Set;
/**
* Game options for Mage game. Mainly used in tests to configure
* {@link GameImpl} with specific params.
@ -42,4 +45,10 @@ public class GameOptions implements Serializable {
* If true, players can rollback turn if all players agree
*/
public boolean rollbackTurnsAllowed = true;
/**
* Names of users banned from participating in the game
*/
public Set<String> bannedUsers = Collections.emptySet();
}