Ban ignored users from watching

This commit bans ignored users from watching
games where the creator has them on their ignore list.
This commit is contained in:
Anders Åstrand 2017-05-29 21:19:40 +02:00
parent 62c14a9c24
commit ad0507e739
8 changed files with 60 additions and 19 deletions

View file

@ -305,10 +305,14 @@ public final class GamePanel extends javax.swing.JPanel {
this.players.clear();
this.playersWhoLeft.clear();
jLayeredPane.remove(abilityPicker);
if (jLayeredPane!= null) {
jLayeredPane.remove(abilityPicker);
}
this.abilityPicker.cleanUp();
jLayeredPane.remove(DialogManager.getManager(gameId));
if (jLayeredPane != null) {
jLayeredPane.remove(DialogManager.getManager(gameId));
}
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, () -> {
Optional<Session> session = SessionManager.instance.getSession(sessionId);
if (!session.isPresent()) {
logger.error("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
GameManager.instance.watchGame(gameId, userId);
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();
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();
}