splitter. still in progress.

This commit is contained in:
magenoxx 2011-08-05 01:52:51 +04:00
parent 2b44c3ae2b
commit 98d99d01c6
4 changed files with 173 additions and 67 deletions

View file

@ -50,7 +50,6 @@ import java.util.zip.GZIPOutputStream;
import mage.Constants.Zone;
import mage.abilities.Ability;
import mage.abilities.Modes;
import mage.cards.Card;
import mage.cards.Cards;
import mage.cards.decks.Deck;
@ -64,6 +63,7 @@ import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.server.ChatManager;
import mage.server.UserManager;
import mage.server.util.Splitter;
import mage.server.util.ThreadExecutor;
import mage.sets.Sets;
import mage.view.*;
@ -344,75 +344,94 @@ public class GameController implements GameCallback {
}
}
private synchronized void ask(UUID playerId, String question) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).ask(question);
informOthers(playerId);
}
private synchronized void chooseAbility(UUID playerId, Collection<? extends Ability> choices) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).chooseAbility(new AbilityPickerView(choices));
informOthers(playerId);
}
private synchronized void chooseMode(UUID playerId, Map<UUID, String> modes) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).chooseAbility(new AbilityPickerView(modes));
informOthers(playerId);
}
private synchronized void choose(UUID playerId, String message, Set<String> choices) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).choose(message, choices);
informOthers(playerId);
}
private synchronized void target(UUID playerId, String question, Cards cards, List<Permanent> perms, Set<UUID> targets, boolean required, Map<String, Serializable> options) throws MageException {
if (gameSessions.containsKey(playerId)) {
if (cards != null)
gameSessions.get(playerId).target(question, new CardsView(cards.getCards(game)), targets, required, options);
else if (perms != null) {
CardsView permsView = new CardsView();
for (Permanent perm: perms) {
permsView.put(perm.getId(), new PermanentView(perm, game.getCard(perm.getId())));
}
gameSessions.get(playerId).target(question, permsView, targets, required, options);
private synchronized void ask(final UUID playerId, final String question) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).ask(question);
}
else
gameSessions.get(playerId).target(question, new CardsView(), targets, required, options);
}
informOthers(playerId);
});
}
private synchronized void target(UUID playerId, String question, Collection<? extends Ability> abilities, boolean required, Map<String, Serializable> options) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).target(question, new CardsView(abilities, game), null, required, options);
informOthers(playerId);
private synchronized void chooseAbility(final UUID playerId, final Collection<? extends Ability> choices) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).chooseAbility(new AbilityPickerView(choices));
}
});
}
private synchronized void select(UUID playerId, String message) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).select(message);
informOthers(playerId);
private synchronized void chooseMode(final UUID playerId, final Map<UUID, String> modes) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).chooseAbility(new AbilityPickerView(modes));
}
});
}
private synchronized void playMana(UUID playerId, String message) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).playMana(message);
informOthers(playerId);
private synchronized void choose(final UUID playerId, final String message, final Set<String> choices) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).choose(message, choices);
}
});
}
private synchronized void playXMana(UUID playerId, String message) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).playXMana(message);
informOthers(playerId);
private synchronized void target(final UUID playerId, final String question, final Cards cards, final List<Permanent> perms, final Set<UUID> targets, final boolean required, final Map<String, Serializable> options) throws MageException {
perform(playerId, new Command() {
public void execute() {
if (cards != null) {
gameSessions.get(playerId).target(question, new CardsView(cards.getCards(game)), targets, required, options);
} else if (perms != null) {
CardsView permsView = new CardsView();
for (Permanent perm: perms) {
permsView.put(perm.getId(), new PermanentView(perm, game.getCard(perm.getId())));
}
gameSessions.get(playerId).target(question, permsView, targets, required, options);
} else
gameSessions.get(playerId).target(question, new CardsView(), targets, required, options);
}
});
}
private synchronized void amount(UUID playerId, String message, int min, int max) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).getAmount(message, min, max);
informOthers(playerId);
private synchronized void target(final UUID playerId, final String question, final Collection<? extends Ability> abilities, final boolean required, final Map<String, Serializable> options) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).target(question, new CardsView(abilities, game), null, required, options);
}
});
}
private synchronized void select(final UUID playerId, final String message) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).select(message);
}
});
}
private synchronized void playMana(final UUID playerId, final String message) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).playMana(message);
}
});
}
private synchronized void playXMana(final UUID playerId, final String message) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).playXMana(message);
}
});
}
private synchronized void amount(final UUID playerId, final String message, final int min, final int max) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).getAmount(message, min, max);
}
});
}
private synchronized void revealCards(String name, Cards cards) throws MageException {
@ -421,9 +440,12 @@ public class GameController implements GameCallback {
}
}
private synchronized void lookAtCards(UUID playerId, String name, Cards cards) throws MageException {
if (gameSessions.containsKey(playerId))
gameSessions.get(playerId).revealCards(name, new CardsView(cards.getCards(game)));
private synchronized void lookAtCards(final UUID playerId, final String name, final Cards cards) throws MageException {
perform(playerId, new Command() {
public void execute() {
gameSessions.get(playerId).revealCards(name, new CardsView(cards.getCards(game)));
}
}, false);
}
private void informOthers(UUID playerId) throws MageException {
@ -438,6 +460,24 @@ public class GameController implements GameCallback {
}
}
private void informOthers(List<UUID> players) throws MageException {
// first player is always original controller
final String message = "Waiting for " + game.getPlayer(players.get(0)).getName();
for (final Entry<UUID, GameSession> entry: gameSessions.entrySet()) {
boolean skip = false;
for (UUID uuid : players) {
if (entry.getKey().equals(uuid)) {
skip = true;
break;
}
}
if (!skip) entry.getValue().inform(message);
}
for (final GameWatcher watcher: watchers.values()) {
watcher.inform(message);
}
}
private void error(String message) {
for (final Entry<UUID, GameSession> entry: gameSessions.entrySet()) {
entry.getValue().gameError(message);
@ -576,4 +616,27 @@ public class GameController implements GameCallback {
}
return null;
}
private void perform(UUID playerId, Command command) throws MageException {
perform(playerId, command, true);
}
private void perform(UUID playerId, Command command, boolean informOthers) throws MageException {
if (game.getPlayer(playerId).isGameUnderControl()) {
if (gameSessions.containsKey(playerId))
command.execute();
if (informOthers) informOthers(playerId);
} else {
List<UUID> players = Splitter.split(game, playerId);
for (UUID uuid : players) {
if (gameSessions.containsKey(uuid))
command.execute();
}
if (informOthers) informOthers(players);
}
}
interface Command {
public void execute();
}
}

View file

@ -0,0 +1,24 @@
package mage.server.util;
import mage.game.Game;
import mage.players.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author nantuko
*/
public class Splitter {
public static List<UUID> split(Game game, UUID playerId) {
List<UUID> players = new ArrayList<UUID>();
players.add(playerId); // add original player
Player player = game.getPlayer(playerId);
if (player != null && player.getTurnControlledBy() != null) {
players.add(player.getTurnControlledBy());
}
return players;
}
}

View file

@ -111,24 +111,33 @@ public interface Player extends MageItem, Copyable<Player> {
public Set<UUID> getPlayersUnderYourControl();
/**
* Defines player whose turn you control at the moment.
* Defines player whose turn this player controls at the moment.
* @param playerId
*/
public void controlPlayersTurn(Game game, UUID playerId);
/**
* Sets player {@link UUID} who controls this player's turn.
*
* @param playerId
*/
public void setTurnControlledBy(UUID playerId);
public UUID getTurnControlledBy();
/**
* Resets players whose turns you control at the moment.
*/
public void resetOtherTurnsControlled();
/**
* Returns false in case you don't control the game.
* Returns false in case player don't control the game.
*
* Note: For effects like "You control target player during that player's next turn".
*
* @return
*/
public boolean isGameUnderYourControl();
public boolean isGameUnderControl();
/**
* Returns false in case you don't control the game.

View file

@ -95,6 +95,7 @@ public abstract class PlayerImpl<T extends PlayerImpl<T>> implements Player, Ser
protected boolean isTestMode = false;
protected boolean lifeTotalCanChange = true;
protected boolean isGameUnderYourControl = true;
protected UUID turnController;
protected Set<UUID> playersUnderYourControl = new HashSet<UUID>();
@Override
@ -237,11 +238,12 @@ public abstract class PlayerImpl<T extends PlayerImpl<T>> implements Player, Ser
@Override
public void controlPlayersTurn(Game game, UUID playerId) {
if (!playerId.equals(getId())) {
if (!playerId.equals(this.getId())) {
this.playersUnderYourControl.add(playerId);
Player player = game.getPlayer(playerId);
if (!player.hasLeft() && !player.hasLost()) {
player.setGameUnderYourControl(false);
player.setTurnControlledBy(this.getId());
}
DelayedTriggeredAbility ability = new AtTheEndOfTurnDelayedTriggeredAbility(new LoseControlOnOtherPlayersControllerEffect());
ability.setSourceId(getId());
@ -250,13 +252,21 @@ public abstract class PlayerImpl<T extends PlayerImpl<T>> implements Player, Ser
}
}
public void setTurnControlledBy(UUID playerId) {
this.turnController = playerId;
}
public UUID getTurnControlledBy() {
return this.turnController;
}
@Override
public void resetOtherTurnsControlled() {
playersUnderYourControl.clear();
}
@Override
public boolean isGameUnderYourControl() {
public boolean isGameUnderControl() {
return isGameUnderYourControl;
}