mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Refactor dev mode game state setting to a separate class.
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -17247,6 +17247,7 @@ forge-gui/src/main/java/forge/planarconquest/ConquestPreferences.java -text
|
|||||||
forge-gui/src/main/java/forge/planarconquest/ConquestUtil.java -text
|
forge-gui/src/main/java/forge/planarconquest/ConquestUtil.java -text
|
||||||
forge-gui/src/main/java/forge/planarconquest/IVCommandCenter.java -text
|
forge-gui/src/main/java/forge/planarconquest/IVCommandCenter.java -text
|
||||||
forge-gui/src/main/java/forge/player/GamePlayerUtil.java -text
|
forge-gui/src/main/java/forge/player/GamePlayerUtil.java -text
|
||||||
|
forge-gui/src/main/java/forge/player/GameState.java -text
|
||||||
forge-gui/src/main/java/forge/player/HumanCostDecision.java -text
|
forge-gui/src/main/java/forge/player/HumanCostDecision.java -text
|
||||||
forge-gui/src/main/java/forge/player/HumanPlay.java -text
|
forge-gui/src/main/java/forge/player/HumanPlay.java -text
|
||||||
forge-gui/src/main/java/forge/player/HumanPlaySpellAbility.java -text
|
forge-gui/src/main/java/forge/player/HumanPlaySpellAbility.java -text
|
||||||
|
|||||||
155
forge-gui/src/main/java/forge/player/GameState.java
Normal file
155
forge-gui/src/main/java/forge/player/GameState.java
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
package forge.player;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.EnumMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
import forge.card.CardStateName;
|
||||||
|
import forge.game.Game;
|
||||||
|
import forge.game.card.Card;
|
||||||
|
import forge.game.card.CardCollection;
|
||||||
|
import forge.game.card.CardCollectionView;
|
||||||
|
import forge.game.card.CounterType;
|
||||||
|
import forge.game.phase.PhaseType;
|
||||||
|
import forge.game.player.Player;
|
||||||
|
import forge.game.trigger.TriggerType;
|
||||||
|
import forge.game.zone.ZoneType;
|
||||||
|
import forge.model.FModel;
|
||||||
|
|
||||||
|
public class GameState {
|
||||||
|
private int humanLife = -1;
|
||||||
|
private int computerLife = -1;
|
||||||
|
private final Map<ZoneType, String> humanCardTexts = new EnumMap<ZoneType, String>(ZoneType.class);
|
||||||
|
private final Map<ZoneType, String> aiCardTexts = new EnumMap<ZoneType, String>(ZoneType.class);
|
||||||
|
private String tChangePlayer = "NONE";
|
||||||
|
private String tChangePhase = "NONE";
|
||||||
|
|
||||||
|
public GameState() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void parse(InputStream in) throws Exception {
|
||||||
|
final BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
||||||
|
|
||||||
|
String temp = "";
|
||||||
|
while ((temp = br.readLine()) != null) {
|
||||||
|
|
||||||
|
final String[] tempData = temp.split("=");
|
||||||
|
if (tempData.length < 2 || temp.charAt(0) == '#') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String categoryName = tempData[0].toLowerCase();
|
||||||
|
final String categoryValue = tempData[1];
|
||||||
|
|
||||||
|
if (categoryName.equals("humanlife")) humanLife = Integer.parseInt(categoryValue);
|
||||||
|
else if (categoryName.equals("ailife")) computerLife = Integer.parseInt(categoryValue);
|
||||||
|
|
||||||
|
else if (categoryName.equals("activeplayer")) tChangePlayer = categoryValue.trim().toLowerCase();
|
||||||
|
else if (categoryName.equals("activephase")) tChangePhase = categoryValue;
|
||||||
|
|
||||||
|
else if (categoryName.equals("humancardsinplay")) humanCardTexts.put(ZoneType.Battlefield, categoryValue);
|
||||||
|
else if (categoryName.equals("aicardsinplay")) aiCardTexts.put(ZoneType.Battlefield, categoryValue);
|
||||||
|
else if (categoryName.equals("humancardsinhand")) humanCardTexts.put(ZoneType.Hand, categoryValue);
|
||||||
|
else if (categoryName.equals("aicardsinhand")) aiCardTexts.put(ZoneType.Hand, categoryValue);
|
||||||
|
else if (categoryName.equals("humancardsingraveyard")) humanCardTexts.put(ZoneType.Graveyard, categoryValue);
|
||||||
|
else if (categoryName.equals("aicardsingraveyard")) aiCardTexts.put(ZoneType.Graveyard, categoryValue);
|
||||||
|
else if (categoryName.equals("humancardsinlibrary")) humanCardTexts.put(ZoneType.Library, categoryValue);
|
||||||
|
else if (categoryName.equals("aicardsinlibrary")) aiCardTexts.put(ZoneType.Library, categoryValue);
|
||||||
|
else if (categoryName.equals("humancardsinexile")) humanCardTexts.put(ZoneType.Exile, categoryValue);
|
||||||
|
else if (categoryName.equals("aicardsinexile")) aiCardTexts.put(ZoneType.Exile, categoryValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void applyToGame(final Game game) {
|
||||||
|
game.getAction().invoke(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
final Player human = game.getPlayers().get(0);
|
||||||
|
final Player ai = game.getPlayers().get(1);
|
||||||
|
|
||||||
|
Player newPlayerTurn = tChangePlayer.equals("human") ? newPlayerTurn = human : tChangePlayer.equals("ai") ? newPlayerTurn = ai : null;
|
||||||
|
PhaseType newPhase = tChangePhase.trim().equalsIgnoreCase("none") ? null : PhaseType.smartValueOf(tChangePhase);
|
||||||
|
|
||||||
|
game.getPhaseHandler().devModeSet(newPhase, newPlayerTurn);
|
||||||
|
|
||||||
|
game.getTriggerHandler().suppressMode(TriggerType.ChangesZone);
|
||||||
|
|
||||||
|
setupPlayerState(humanLife, humanCardTexts, human);
|
||||||
|
setupPlayerState(computerLife, aiCardTexts, ai);
|
||||||
|
|
||||||
|
game.getTriggerHandler().clearSuppression(TriggerType.ChangesZone);
|
||||||
|
|
||||||
|
game.getAction().checkStateEffects(true); //ensure state based effects and triggers are updated
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupPlayerState(int life, Map<ZoneType, String> cardTexts, final Player p) {
|
||||||
|
Map<ZoneType, CardCollectionView> humanCards = new EnumMap<ZoneType, CardCollectionView>(ZoneType.class);
|
||||||
|
for(Entry<ZoneType, String> kv : cardTexts.entrySet()) {
|
||||||
|
humanCards.put(kv.getKey(), processCardsForZone(kv.getValue().split(";"), p));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (life > 0) p.setLife(life, null);
|
||||||
|
for (Entry<ZoneType, CardCollectionView> kv : humanCards.entrySet()) {
|
||||||
|
if (kv.getKey() == ZoneType.Battlefield) {
|
||||||
|
for (final Card c : kv.getValue()) {
|
||||||
|
p.getZone(ZoneType.Hand).add(c);
|
||||||
|
p.getGame().getAction().moveToPlay(c);
|
||||||
|
c.setSickness(false);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
p.getZone(kv.getKey()).setCards(kv.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* processCardsForZone.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param data
|
||||||
|
* an array of {@link java.lang.String} objects.
|
||||||
|
* @param player
|
||||||
|
* a {@link forge.game.player.Player} object.
|
||||||
|
* @return a {@link forge.CardList} object.
|
||||||
|
*/
|
||||||
|
private CardCollectionView processCardsForZone(final String[] data, final Player player) {
|
||||||
|
final CardCollection cl = new CardCollection();
|
||||||
|
for (final String element : data) {
|
||||||
|
final String[] cardinfo = element.trim().split("\\|");
|
||||||
|
|
||||||
|
final Card c = Card.fromPaperCard(FModel.getMagicDb().getCommonCards().getCard(cardinfo[0]), player);
|
||||||
|
|
||||||
|
boolean hasSetCurSet = false;
|
||||||
|
for (final String info : cardinfo) {
|
||||||
|
if (info.startsWith("Set:")) {
|
||||||
|
c.setSetCode(info.substring(info.indexOf(':') + 1));
|
||||||
|
hasSetCurSet = true;
|
||||||
|
} else if (info.equalsIgnoreCase("Tapped:True")) {
|
||||||
|
c.tap();
|
||||||
|
} else if (info.startsWith("Counters:")) {
|
||||||
|
final String[] counterStrings = info.substring(info.indexOf(':') + 1).split(",");
|
||||||
|
for (final String counter : counterStrings) {
|
||||||
|
c.addCounter(CounterType.valueOf(counter), 1, true);
|
||||||
|
}
|
||||||
|
} else if (info.equalsIgnoreCase("SummonSick:True")) {
|
||||||
|
c.setSickness(true);
|
||||||
|
} else if (info.equalsIgnoreCase("FaceDown:True")) {
|
||||||
|
c.setState(CardStateName.FaceDown, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasSetCurSet) {
|
||||||
|
c.setSetCode(c.getMostRecentSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
cl.add(c);
|
||||||
|
}
|
||||||
|
return cl;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1423,15 +1423,6 @@ public class PlayerControllerHuman extends PlayerController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setupGameState() {
|
public void setupGameState() {
|
||||||
int humanLife = -1;
|
|
||||||
int computerLife = -1;
|
|
||||||
|
|
||||||
final Map<ZoneType, String> humanCardTexts = new EnumMap<ZoneType, String>(ZoneType.class);
|
|
||||||
final Map<ZoneType, String> aiCardTexts = new EnumMap<ZoneType, String>(ZoneType.class);
|
|
||||||
|
|
||||||
String tChangePlayer = "NONE";
|
|
||||||
String tChangePhase = "NONE";
|
|
||||||
|
|
||||||
File gamesDir = new File(ForgeConstants.USER_GAMES_DIR);
|
File gamesDir = new File(ForgeConstants.USER_GAMES_DIR);
|
||||||
if (!gamesDir.exists()) { // if the directory does not exist, try to create it
|
if (!gamesDir.exists()) { // if the directory does not exist, try to create it
|
||||||
gamesDir.mkdir();
|
gamesDir.mkdir();
|
||||||
@@ -1442,42 +1433,11 @@ public class PlayerControllerHuman extends PlayerController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final GameState state = new GameState();
|
||||||
try {
|
try {
|
||||||
final FileInputStream fstream = new FileInputStream(filename);
|
final FileInputStream fstream = new FileInputStream(filename);
|
||||||
final DataInputStream in = new DataInputStream(fstream);
|
state.parse(fstream);
|
||||||
final BufferedReader br = new BufferedReader(new InputStreamReader(in));
|
fstream.close();
|
||||||
|
|
||||||
String temp = "";
|
|
||||||
|
|
||||||
while ((temp = br.readLine()) != null) {
|
|
||||||
|
|
||||||
final String[] tempData = temp.split("=");
|
|
||||||
if (tempData.length < 2 || temp.charAt(0) == '#') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
final String categoryName = tempData[0].toLowerCase();
|
|
||||||
final String categoryValue = tempData[1];
|
|
||||||
|
|
||||||
if (categoryName.equals("humanlife")) humanLife = Integer.parseInt(categoryValue);
|
|
||||||
else if (categoryName.equals("ailife")) computerLife = Integer.parseInt(categoryValue);
|
|
||||||
|
|
||||||
else if (categoryName.equals("activeplayer")) tChangePlayer = categoryValue.trim().toLowerCase();
|
|
||||||
else if (categoryName.equals("activephase")) tChangePhase = categoryValue;
|
|
||||||
|
|
||||||
else if (categoryName.equals("humancardsinplay")) humanCardTexts.put(ZoneType.Battlefield, categoryValue);
|
|
||||||
else if (categoryName.equals("aicardsinplay")) aiCardTexts.put(ZoneType.Battlefield, categoryValue);
|
|
||||||
else if (categoryName.equals("humancardsinhand")) humanCardTexts.put(ZoneType.Hand, categoryValue);
|
|
||||||
else if (categoryName.equals("aicardsinhand")) aiCardTexts.put(ZoneType.Hand, categoryValue);
|
|
||||||
else if (categoryName.equals("humancardsingraveyard")) humanCardTexts.put(ZoneType.Graveyard, categoryValue);
|
|
||||||
else if (categoryName.equals("aicardsingraveyard")) aiCardTexts.put(ZoneType.Graveyard, categoryValue);
|
|
||||||
else if (categoryName.equals("humancardsinlibrary")) humanCardTexts.put(ZoneType.Library, categoryValue);
|
|
||||||
else if (categoryName.equals("aicardsinlibrary")) aiCardTexts.put(ZoneType.Library, categoryValue);
|
|
||||||
else if (categoryName.equals("humancardsinexile")) humanCardTexts.put(ZoneType.Exile, categoryValue);
|
|
||||||
else if (categoryName.equals("aicardsinexile")) aiCardTexts.put(ZoneType.Exile, categoryValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
catch (final FileNotFoundException fnfe) {
|
catch (final FileNotFoundException fnfe) {
|
||||||
SOptionPane.showErrorDialog("File not found: " + filename);
|
SOptionPane.showErrorDialog("File not found: " + filename);
|
||||||
@@ -1487,104 +1447,11 @@ public class PlayerControllerHuman extends PlayerController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setupGameState(humanLife, computerLife, humanCardTexts, aiCardTexts, tChangePlayer, tChangePhase);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupGameState(final int humanLife, final int computerLife, final Map<ZoneType, String> humanCardTexts,
|
|
||||||
final Map<ZoneType, String> aiCardTexts, final String tChangePlayer, final String tChangePhase) {
|
|
||||||
|
|
||||||
Player pPriority = game.getPhaseHandler().getPriorityPlayer();
|
Player pPriority = game.getPhaseHandler().getPriorityPlayer();
|
||||||
if (pPriority == null) {
|
if (pPriority == null) {
|
||||||
SGuiDialog.message("No player has priority at the moment, so game state cannot be setup.");
|
SGuiDialog.message("No player has priority at the moment, so game state cannot be setup.");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
game.getAction().invoke(new Runnable() {
|
state.applyToGame(game);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
final Player human = game.getPlayers().get(0);
|
|
||||||
final Player ai = game.getPlayers().get(1);
|
|
||||||
|
|
||||||
Player newPlayerTurn = tChangePlayer.equals("human") ? newPlayerTurn = human : tChangePlayer.equals("ai") ? newPlayerTurn = ai : null;
|
|
||||||
PhaseType newPhase = tChangePhase.trim().equalsIgnoreCase("none") ? null : PhaseType.smartValueOf(tChangePhase);
|
|
||||||
|
|
||||||
game.getPhaseHandler().devModeSet(newPhase, newPlayerTurn);
|
|
||||||
|
|
||||||
game.getTriggerHandler().suppressMode(TriggerType.ChangesZone);
|
|
||||||
|
|
||||||
setupPlayerState(humanLife, humanCardTexts, human);
|
|
||||||
setupPlayerState(computerLife, aiCardTexts, ai);
|
|
||||||
|
|
||||||
game.getTriggerHandler().clearSuppression(TriggerType.ChangesZone);
|
|
||||||
|
|
||||||
game.getAction().checkStateEffects(true); //ensure state based effects and triggers are updated
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupPlayerState(int life, Map<ZoneType, String> cardTexts, final Player p) {
|
|
||||||
Map<ZoneType, CardCollectionView> humanCards = new EnumMap<ZoneType, CardCollectionView>(ZoneType.class);
|
|
||||||
for(Entry<ZoneType, String> kv : cardTexts.entrySet()) {
|
|
||||||
humanCards.put(kv.getKey(), processCardsForZone(kv.getValue().split(";"), p));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (life > 0) p.setLife(life, null);
|
|
||||||
for (Entry<ZoneType, CardCollectionView> kv : humanCards.entrySet()) {
|
|
||||||
if (kv.getKey() == ZoneType.Battlefield) {
|
|
||||||
for (final Card c : kv.getValue()) {
|
|
||||||
p.getZone(ZoneType.Hand).add(c);
|
|
||||||
p.getGame().getAction().moveToPlay(c);
|
|
||||||
c.setSickness(false);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
p.getZone(kv.getKey()).setCards(kv.getValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>
|
|
||||||
* processCardsForZone.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param data
|
|
||||||
* an array of {@link java.lang.String} objects.
|
|
||||||
* @param player
|
|
||||||
* a {@link forge.game.player.Player} object.
|
|
||||||
* @return a {@link forge.CardList} object.
|
|
||||||
*/
|
|
||||||
private CardCollectionView processCardsForZone(final String[] data, final Player player) {
|
|
||||||
final CardCollection cl = new CardCollection();
|
|
||||||
for (final String element : data) {
|
|
||||||
final String[] cardinfo = element.trim().split("\\|");
|
|
||||||
|
|
||||||
final Card c = Card.fromPaperCard(FModel.getMagicDb().getCommonCards().getCard(cardinfo[0]), player);
|
|
||||||
|
|
||||||
boolean hasSetCurSet = false;
|
|
||||||
for (final String info : cardinfo) {
|
|
||||||
if (info.startsWith("Set:")) {
|
|
||||||
c.setSetCode(info.substring(info.indexOf(':') + 1));
|
|
||||||
hasSetCurSet = true;
|
|
||||||
} else if (info.equalsIgnoreCase("Tapped:True")) {
|
|
||||||
c.tap();
|
|
||||||
} else if (info.startsWith("Counters:")) {
|
|
||||||
final String[] counterStrings = info.substring(info.indexOf(':') + 1).split(",");
|
|
||||||
for (final String counter : counterStrings) {
|
|
||||||
c.addCounter(CounterType.valueOf(counter), 1, true);
|
|
||||||
}
|
|
||||||
} else if (info.equalsIgnoreCase("SummonSick:True")) {
|
|
||||||
c.setSickness(true);
|
|
||||||
} else if (info.equalsIgnoreCase("FaceDown:True")) {
|
|
||||||
c.setState(CardStateName.FaceDown, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hasSetCurSet) {
|
|
||||||
c.setSetCode(c.getMostRecentSet());
|
|
||||||
}
|
|
||||||
|
|
||||||
cl.add(c);
|
|
||||||
}
|
|
||||||
return cl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tutorForCard() {
|
public void tutorForCard() {
|
||||||
|
|||||||
Reference in New Issue
Block a user