Fix mindslaver effects.

- Fix bug where game would become unsreponsive after mind slaving ends;
- Fix bug where players could concede for mind-slaved opponents;
- Cleanup, managed to remove some fields.
This commit is contained in:
elcnesh
2015-05-19 06:56:57 +00:00
parent c32dbb1012
commit 9a329d515d
7 changed files with 77 additions and 59 deletions

View File

@@ -244,7 +244,6 @@ public final class CMatchUI
private void initMatch(final FCollectionView<PlayerView> sortedPlayers, final Collection<PlayerView> myPlayers) {
this.sortedPlayers = sortedPlayers;
this.setLocalPlayers(myPlayers);
allHands = sortedPlayers.size() == getLocalPlayerCount();
final String[] indices = FModel.getPreferences().getPref(FPref.UI_AVATARS).split(",");

View File

@@ -59,31 +59,26 @@ public class ControlWinLose {
/** Action performed when "continue" button is pressed in default win/lose UI. */
public void actionOnContinue() {
SOverlayUtils.hideOverlay();
saveOptions();
for (final IGameController controller : matchUI.getGameControllers()) {
controller.nextGameDecision(NextGameDecision.CONTINUE);
}
nextGameAction(NextGameDecision.CONTINUE);
}
/** Action performed when "restart" button is pressed in default win/lose UI. */
public void actionOnRestart() {
SOverlayUtils.hideOverlay();
saveOptions();
for (final IGameController controller : matchUI.getGameControllers()) {
controller.nextGameDecision(NextGameDecision.NEW);
}
nextGameAction(NextGameDecision.NEW);
}
/** Action performed when "quit" button is pressed in default win/lose UI. */
public void actionOnQuit() {
// Reset other stuff
saveOptions();
for (final IGameController controller : matchUI.getGameControllers()) {
controller.nextGameDecision(NextGameDecision.QUIT);
}
nextGameAction(NextGameDecision.QUIT);
Singletons.getControl().setCurrentScreen(FScreen.HOME_SCREEN);
}
private void nextGameAction(final NextGameDecision decision) {
SOverlayUtils.hideOverlay();
saveOptions();
for (final IGameController controller : matchUI.getOriginalGameControllers()) {
controller.nextGameDecision(decision);
}
}
/**

View File

@@ -110,7 +110,6 @@ public class MatchController extends AbstractGuiGame {
@Override
public void openView(final TrackableCollection<PlayerView> myPlayers) {
setLocalPlayers(myPlayers);
final boolean noHumans = !hasLocalPlayers();
final FCollectionView<PlayerView> allPlayers = getGameView().getPlayers();

View File

@@ -235,9 +235,15 @@ public class FControlGameEventHandler extends IGameEventVisitor.Base<Void> {
if (ev.player.getGame().isGameOver()) {
return null;
}
final PlayerControllerHuman newController;
if (ev.newController instanceof PlayerControllerHuman) {
matchController.setGameController(PlayerView.get(ev.player), (PlayerControllerHuman) ev.newController);
newController = (PlayerControllerHuman) ev.newController;
} else {
newController = null;
}
matchController.setGameController(PlayerView.get(ev.player), newController);
needPlayerControlUpdate = true;
return processEvent();
}

View File

@@ -25,7 +25,9 @@ import forge.util.ITriggerEvent;
public interface IGuiGame {
void setGameView(GameView gameView);
void setOriginalGameController(PlayerView view, IGameController gameController);
void setGameController(PlayerView player, IGameController gameController);
void setSpectator(IGameController spectator);
void openView(TrackableCollection<PlayerView> myPlayers);
void afterGameEnd();
void showCombat();

View File

@@ -5,6 +5,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
@@ -27,37 +28,24 @@ import forge.game.player.PlayerView;
import forge.interfaces.IGameController;
import forge.interfaces.IGuiGame;
import forge.interfaces.IMayViewCards;
import forge.util.FCollection;
import forge.util.FCollectionView;
public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards {
private FCollection<PlayerView> localPlayers = new FCollection<PlayerView>();
private PlayerView currentPlayer = null;
protected final void setLocalPlayers(final Collection<PlayerView> myPlayers) {
this.localPlayers = myPlayers == null ? new FCollection<PlayerView>() : new FCollection<PlayerView>(myPlayers);
this.currentPlayer = Iterables.getFirst(this.localPlayers, null);
this.autoPassUntilEndOfTurn.retainAll(myPlayers);
}
private void addLocalPlayer(final PlayerView player) {
this.localPlayers.add(player);
}
private void removeLocalPlayer(final PlayerView player) {
this.localPlayers.remove(player);
this.autoPassUntilEndOfTurn.remove(player);
}
private IGameController spectator = null;
private final Map<PlayerView, IGameController> gameControllers = Maps.newHashMap();
private final Map<PlayerView, IGameController> originalGameControllers = Maps.newHashMap();
public final boolean hasLocalPlayers() {
return !localPlayers.isEmpty();
return !gameControllers.isEmpty();
}
public final FCollectionView<PlayerView> getLocalPlayers() {
return localPlayers;
public final Set<PlayerView> getLocalPlayers() {
return gameControllers.keySet();
}
public final int getLocalPlayerCount() {
return localPlayers.size();
return gameControllers.size();
}
public final boolean isLocalPlayer(final PlayerView player) {
return localPlayers.contains(player);
return gameControllers.containsKey(player);
}
public final PlayerView getCurrentPlayer() {
@@ -65,6 +53,9 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards {
}
@Override
public final void setCurrentPlayer(final PlayerView player) {
if (!gameControllers.containsKey(player)) {
throw new IllegalArgumentException();
}
this.currentPlayer = player;
updateCurrentPlayer(player);
}
@@ -79,24 +70,54 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards {
this.gameView = gameView;
}
private final Map<PlayerView, IGameController> gameControllers = Maps.newHashMap();
public final IGameController getGameController() {
return gameControllers.get(getCurrentPlayer());
return getGameController(getCurrentPlayer());
}
public final IGameController getGameController(final PlayerView p0) {
return gameControllers.get(p0);
public final IGameController getGameController(final PlayerView player) {
if (player == null) {
return spectator;
}
public final Collection<IGameController> getGameControllers() {
return gameControllers.values();
return gameControllers.get(player);
}
public final Collection<IGameController> getOriginalGameControllers() {
return originalGameControllers.values();
}
@Override
public void setOriginalGameController(final PlayerView player, final IGameController gameController) {
if (player == null || gameController == null) {
throw new IllegalArgumentException();
}
originalGameControllers.put(player, gameController);
gameControllers.put(player, gameController);
}
@Override
public void setGameController(final PlayerView player, final IGameController gameController) {
this.gameControllers.put(player, gameController);
if (gameController == null) {
addLocalPlayer(player);
} else {
removeLocalPlayer(player);
if (player == null) {
throw new IllegalArgumentException();
}
if (gameController == null) {
if (originalGameControllers.containsKey(player)) {
gameControllers.put(player, originalGameControllers.get(player));
} else {
gameControllers.remove(player);
autoPassUntilEndOfTurn.remove(player);
final PlayerView currentPlayer = getCurrentPlayer();
if (Objects.equals(player, currentPlayer)) {
// set current player to a value known to be legal
setCurrentPlayer(Iterables.getFirst(gameControllers.keySet(), null));
}
}
} else {
this.gameControllers.put(player, gameController);
}
}
@Override
public void setSpectator(final IGameController spectator) {
this.spectator = spectator;
}
@Override
@@ -177,8 +198,8 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards {
}
if (hasLocalPlayers()) {
if (showConfirmDialog("This will concede the current game and you will lose.\n\nConcede anyway?", "Concede Game?", "Concede", "Cancel")) {
for (final IGameController c : getGameControllers()) {
// Concede each player on this Gui
for (final IGameController c : getOriginalGameControllers()) {
// Concede each player on this Gui (except mind-controlled players)
c.concede();
}
}
@@ -204,9 +225,6 @@ public abstract class AbstractGuiGame implements IGuiGame, IMayViewCards {
updateButtons(owner, "OK", "Cancel", okEnabled, cancelEnabled, focusOk);
}
@Override
public abstract void updateButtons(PlayerView owner, String label1, String label2, boolean enable1, boolean enable2, boolean focus1);
// Auto-yield and other input-related code
private final Set<PlayerView> autoPassUntilEndOfTurn = Sets.newHashSet();

View File

@@ -169,7 +169,7 @@ public class HostedMatch {
final IGuiGame gui = guis.get(p.getRegisteredPlayer());
humanController.setGui(gui);
gui.setGameView(gameView);
gui.setGameController(p.getView(), humanController);
gui.setOriginalGameController(p.getView(), humanController);
game.subscribeToEvents(new FControlGameEventHandler(humanController));
playersPerGui.add(gui, p.getView());
@@ -189,7 +189,7 @@ public class HostedMatch {
final PlayerControllerHuman humanController = new WatchLocalGame(game, new LobbyPlayerHuman("Spectator"), gui);
game.subscribeToEvents(new FControlGameEventHandler(humanController));
humanControllers.add(humanController);
gui.setGameController(null, humanController);
gui.setSpectator(humanController);
gui.openView(null);
}
@@ -230,12 +230,11 @@ public class HostedMatch {
}
}
});
}
public void registerSpectator(final IGuiGame gui) {
final PlayerControllerHuman humanController = new WatchLocalGame(game, null, gui);
gui.setGameController(null, humanController);
gui.setSpectator(humanController);
gui.openView(null);
game.subscribeToEvents(new FControlGameEventHandler(humanController));