mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 03:38:01 +00:00
devSetupGameState - written in less lines, game state is changed from game thread, won't NPE
This commit is contained in:
@@ -24,9 +24,11 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.EnumMap;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import javax.swing.JFileChooser;
|
import javax.swing.JFileChooser;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
@@ -68,18 +70,12 @@ public final class GuiDisplayUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void devSetupGameState() {
|
public static void devSetupGameState() {
|
||||||
String tHumanLife = "-1";
|
int humanLife = -1;
|
||||||
String tComputerLife = "-1";
|
int computerLife = -1;
|
||||||
String tHumanSetupCardsInPlay = "NONE";
|
|
||||||
String tComputerSetupCardsInPlay = "NONE";
|
final Map<ZoneType, String> humanCardTexts = new EnumMap<ZoneType, String>(ZoneType.class);
|
||||||
String tHumanSetupCardsInHand = "NONE";
|
final Map<ZoneType, String> aiCardTexts = new EnumMap<ZoneType, String>(ZoneType.class);
|
||||||
String tComputerSetupCardsInHand = "NONE";
|
|
||||||
String tHumanSetupGraveyard = "NONE";
|
|
||||||
String tComputerSetupGraveyard = "NONE";
|
|
||||||
String tHumanSetupLibrary = "NONE";
|
|
||||||
String tComputerSetupLibrary = "NONE";
|
|
||||||
String tHumanSetupExile = "NONE";
|
|
||||||
String tComputerSetupExile = "NONE";
|
|
||||||
String tChangePlayer = "NONE";
|
String tChangePlayer = "NONE";
|
||||||
String tChangePhase = "NONE";
|
String tChangePhase = "NONE";
|
||||||
|
|
||||||
@@ -89,7 +85,6 @@ public final class GuiDisplayUtil {
|
|||||||
if (rc != JFileChooser.APPROVE_OPTION) {
|
if (rc != JFileChooser.APPROVE_OPTION) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final GameState game = getGame();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final FileInputStream fstream = new FileInputStream(fc.getSelectedFile().getAbsolutePath());
|
final FileInputStream fstream = new FileInputStream(fc.getSelectedFile().getAbsolutePath());
|
||||||
@@ -99,47 +94,32 @@ public final class GuiDisplayUtil {
|
|||||||
String temp = "";
|
String temp = "";
|
||||||
|
|
||||||
while ((temp = br.readLine()) != null) {
|
while ((temp = br.readLine()) != null) {
|
||||||
|
|
||||||
final String[] tempData = temp.split("=");
|
final String[] tempData = temp.split("=");
|
||||||
|
if (tempData.length < 2 || temp.charAt(0) == '#') {
|
||||||
if (tempData.length < 2) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (tempData[0].toCharArray()[0] == '#') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String categoryName = tempData[0];
|
final String categoryName = tempData[0].toLowerCase();
|
||||||
final String categoryValue = tempData[1];
|
final String categoryValue = tempData[1];
|
||||||
|
|
||||||
if (categoryName.toLowerCase().equals("humanlife")) {
|
if (categoryName.equals("humanlife")) humanLife = Integer.parseInt(categoryValue);
|
||||||
tHumanLife = categoryValue;
|
else if (categoryName.equals("ailife")) computerLife = Integer.parseInt(categoryValue);
|
||||||
} else if (categoryName.toLowerCase().equals("ailife")) {
|
|
||||||
tComputerLife = categoryValue;
|
else if (categoryName.equals("activeplayer")) tChangePlayer = categoryValue.trim().toLowerCase();
|
||||||
} else if (categoryName.toLowerCase().equals("humancardsinplay")) {
|
else if (categoryName.equals("activephase")) tChangePhase = categoryValue;
|
||||||
tHumanSetupCardsInPlay = categoryValue;
|
|
||||||
} else if (categoryName.toLowerCase().equals("aicardsinplay")) {
|
else if (categoryName.equals("humancardsinplay")) humanCardTexts.put(ZoneType.Battlefield, categoryValue);
|
||||||
tComputerSetupCardsInPlay = categoryValue;
|
else if (categoryName.equals("aicardsinplay")) aiCardTexts.put(ZoneType.Battlefield, categoryValue);
|
||||||
} else if (categoryName.toLowerCase().equals("humancardsinhand")) {
|
else if (categoryName.equals("humancardsinhand")) humanCardTexts.put(ZoneType.Hand, categoryValue);
|
||||||
tHumanSetupCardsInHand = categoryValue;
|
else if (categoryName.equals("aicardsinhand")) aiCardTexts.put(ZoneType.Hand, categoryValue);
|
||||||
} else if (categoryName.toLowerCase().equals("aicardsinhand")) {
|
else if (categoryName.equals("humancardsingraveyard")) humanCardTexts.put(ZoneType.Graveyard, categoryValue);
|
||||||
tComputerSetupCardsInHand = categoryValue;
|
else if (categoryName.equals("aicardsingraveyard")) aiCardTexts.put(ZoneType.Graveyard, categoryValue);
|
||||||
} else if (categoryName.toLowerCase().equals("humancardsingraveyard")) {
|
else if (categoryName.equals("humancardsinlibrary")) humanCardTexts.put(ZoneType.Library, categoryValue);
|
||||||
tHumanSetupGraveyard = categoryValue;
|
else if (categoryName.equals("aicardsinlibrary")) aiCardTexts.put(ZoneType.Library, categoryValue);
|
||||||
} else if (categoryName.toLowerCase().equals("aicardsingraveyard")) {
|
else if (categoryName.equals("humancardsinexile")) humanCardTexts.put(ZoneType.Exile, categoryValue);
|
||||||
tComputerSetupGraveyard = categoryValue;
|
else if (categoryName.equals("aicardsinexile")) aiCardTexts.put(ZoneType.Exile, categoryValue);
|
||||||
} else if (categoryName.toLowerCase().equals("humancardsinlibrary")) {
|
|
||||||
tHumanSetupLibrary = categoryValue;
|
|
||||||
} else if (categoryName.toLowerCase().equals("aicardsinlibrary")) {
|
|
||||||
tComputerSetupLibrary = categoryValue;
|
|
||||||
} else if (categoryName.toLowerCase().equals("humancardsinexile")) {
|
|
||||||
tHumanSetupExile = categoryValue;
|
|
||||||
} else if (categoryName.toLowerCase().equals("aicardsinexile")) {
|
|
||||||
tComputerSetupExile = categoryValue;
|
|
||||||
} else if (categoryName.toLowerCase().equals("activeplayer")) {
|
|
||||||
tChangePlayer = categoryValue;
|
|
||||||
} else if (categoryName.toLowerCase().equals("activephase")) {
|
|
||||||
tChangePhase = categoryValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
in.close();
|
in.close();
|
||||||
@@ -150,139 +130,37 @@ public final class GuiDisplayUtil {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final int setHumanLife = Integer.parseInt(tHumanLife);
|
setupGameState(humanLife, computerLife, humanCardTexts, aiCardTexts, tChangePlayer, tChangePhase);
|
||||||
final int setComputerLife = Integer.parseInt(tComputerLife);
|
|
||||||
|
|
||||||
final String[] humanSetupCardsInPlay = tHumanSetupCardsInPlay.split(";");
|
|
||||||
final String[] computerSetupCardsInPlay = tComputerSetupCardsInPlay.split(";");
|
|
||||||
final String[] humanSetupCardsInHand = tHumanSetupCardsInHand.split(";");
|
|
||||||
final String[] computerSetupCardsInHand = tComputerSetupCardsInHand.split(";");
|
|
||||||
final String[] humanSetupGraveyard = tHumanSetupGraveyard.split(";");
|
|
||||||
final String[] computerSetupGraveyard = tComputerSetupGraveyard.split(";");
|
|
||||||
final String[] humanSetupLibrary = tHumanSetupLibrary.split(";");
|
|
||||||
final String[] computerSetupLibrary = tComputerSetupLibrary.split(";");
|
|
||||||
final String[] humanSetupExile = tHumanSetupExile.split(";");
|
|
||||||
final String[] computerSetupExile = tComputerSetupExile.split(";");
|
|
||||||
|
|
||||||
List<Card> humanDevSetup = new ArrayList<Card>();
|
|
||||||
List<Card> computerDevSetup = new ArrayList<Card>();
|
|
||||||
List<Card> humanDevHandSetup = new ArrayList<Card>();
|
|
||||||
List<Card> computerDevHandSetup = new ArrayList<Card>();
|
|
||||||
List<Card> humanDevGraveyardSetup = new ArrayList<Card>();
|
|
||||||
List<Card> computerDevGraveyardSetup = new ArrayList<Card>();
|
|
||||||
List<Card> humanDevLibrarySetup = new ArrayList<Card>();
|
|
||||||
List<Card> computerDevLibrarySetup = new ArrayList<Card>();
|
|
||||||
List<Card> humanDevExileSetup = new ArrayList<Card>();
|
|
||||||
List<Card> computerDevExileSetup = new ArrayList<Card>();
|
|
||||||
|
|
||||||
final Player human = getGame().getPlayers().get(0);
|
|
||||||
final Player ai = getGame().getPlayers().get(1);
|
|
||||||
|
|
||||||
if (!tChangePlayer.trim().toLowerCase().equals("none")) {
|
|
||||||
if (tChangePlayer.trim().toLowerCase().equals("human")) {
|
|
||||||
game.getPhaseHandler().setPlayerTurn(human);
|
|
||||||
}
|
}
|
||||||
if (tChangePlayer.trim().toLowerCase().equals("ai")) {
|
|
||||||
|
private static void setupGameState(final int humanLife, final int computerLife, final Map<ZoneType, String> humanCardTexts,
|
||||||
|
final Map<ZoneType, String> aiCardTexts, final String tChangePlayer, final String tChangePhase) {
|
||||||
|
|
||||||
|
FThreads.invokeInNewThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
final GameState game = getGame();
|
||||||
|
final Player human = game.getPlayers().get(0);
|
||||||
|
final Player ai = game.getPlayers().get(1);
|
||||||
|
|
||||||
|
if (tChangePlayer.equals("human")) {
|
||||||
|
game.getPhaseHandler().setPlayerTurn(human);
|
||||||
|
} else if (tChangePlayer.equals("ai")) {
|
||||||
game.getPhaseHandler().setPlayerTurn(ai);
|
game.getPhaseHandler().setPlayerTurn(ai);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!tChangePhase.trim().equalsIgnoreCase("none")) {
|
||||||
|
|
||||||
if (!tChangePhase.trim().toLowerCase().equals("none")) {
|
|
||||||
game.getPhaseHandler().setDevPhaseState(forge.game.phase.PhaseType.smartValueOf(tChangePhase));
|
game.getPhaseHandler().setDevPhaseState(forge.game.phase.PhaseType.smartValueOf(tChangePhase));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tHumanSetupCardsInPlay.trim().toLowerCase().equals("none")) {
|
|
||||||
humanDevSetup = GuiDisplayUtil.devProcessCardsForZone(humanSetupCardsInPlay, human);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tHumanSetupCardsInHand.trim().toLowerCase().equals("none")) {
|
|
||||||
humanDevHandSetup = GuiDisplayUtil.devProcessCardsForZone(humanSetupCardsInHand, human);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tComputerSetupCardsInPlay.trim().toLowerCase().equals("none")) {
|
|
||||||
computerDevSetup = GuiDisplayUtil.devProcessCardsForZone(computerSetupCardsInPlay, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tComputerSetupCardsInHand.trim().toLowerCase().equals("none")) {
|
|
||||||
computerDevHandSetup = GuiDisplayUtil.devProcessCardsForZone(computerSetupCardsInHand, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tComputerSetupGraveyard.trim().toLowerCase().equals("none")) {
|
|
||||||
computerDevGraveyardSetup = GuiDisplayUtil.devProcessCardsForZone(computerSetupGraveyard, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tHumanSetupGraveyard.trim().toLowerCase().equals("none")) {
|
|
||||||
humanDevGraveyardSetup = GuiDisplayUtil.devProcessCardsForZone(humanSetupGraveyard, human);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tHumanSetupLibrary.trim().toLowerCase().equals("none")) {
|
|
||||||
humanDevLibrarySetup = GuiDisplayUtil.devProcessCardsForZone(humanSetupLibrary, human);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tComputerSetupLibrary.trim().toLowerCase().equals("none")) {
|
|
||||||
computerDevLibrarySetup = GuiDisplayUtil.devProcessCardsForZone(computerSetupLibrary, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tHumanSetupExile.trim().toLowerCase().equals("none")) {
|
|
||||||
humanDevExileSetup = GuiDisplayUtil.devProcessCardsForZone(humanSetupExile, human);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tComputerSetupExile.trim().toLowerCase().equals("none")) {
|
|
||||||
computerDevExileSetup = GuiDisplayUtil.devProcessCardsForZone(computerSetupExile, ai);
|
|
||||||
}
|
|
||||||
|
|
||||||
game.getTriggerHandler().suppressMode(TriggerType.ChangesZone);
|
|
||||||
game.getCombat().reset(game.getPhaseHandler().getPlayerTurn());
|
game.getCombat().reset(game.getPhaseHandler().getPlayerTurn());
|
||||||
for (final Card c : humanDevSetup) {
|
game.getTriggerHandler().suppressMode(TriggerType.ChangesZone);
|
||||||
human.getZone(ZoneType.Hand).add(c);
|
|
||||||
game.getAction().moveToPlay(c);
|
|
||||||
c.setSickness(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (final Card c : computerDevSetup) {
|
devSetupPlayerState(humanLife, humanCardTexts, human);
|
||||||
ai.getZone(ZoneType.Hand).add(c);
|
devSetupPlayerState(computerLife, aiCardTexts, ai);
|
||||||
game.getAction().moveToPlay(c);
|
|
||||||
c.setSickness(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (computerDevGraveyardSetup.size() > 0) {
|
|
||||||
ai.getZone(ZoneType.Graveyard).setCards(computerDevGraveyardSetup);
|
|
||||||
}
|
|
||||||
if (humanDevGraveyardSetup.size() > 0) {
|
|
||||||
human.getZone(ZoneType.Graveyard).setCards(humanDevGraveyardSetup);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (computerDevHandSetup.size() > 0) {
|
|
||||||
ai.getZone(ZoneType.Hand).setCards(computerDevHandSetup);
|
|
||||||
}
|
|
||||||
if (humanDevHandSetup.size() > 0) {
|
|
||||||
human.getZone(ZoneType.Hand).setCards(humanDevHandSetup);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (humanDevLibrarySetup.size() > 0) {
|
|
||||||
human.getZone(ZoneType.Library).setCards(humanDevLibrarySetup);
|
|
||||||
}
|
|
||||||
if (computerDevLibrarySetup.size() > 0) {
|
|
||||||
ai.getZone(ZoneType.Library).setCards(computerDevLibrarySetup);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (humanDevExileSetup.size() > 0) {
|
|
||||||
human.getZone(ZoneType.Exile).setCards(humanDevExileSetup);
|
|
||||||
}
|
|
||||||
if (computerDevExileSetup.size() > 0) {
|
|
||||||
ai.getZone(ZoneType.Exile).setCards(computerDevExileSetup);
|
|
||||||
}
|
|
||||||
|
|
||||||
game.getTriggerHandler().clearSuppression(TriggerType.ChangesZone);
|
game.getTriggerHandler().clearSuppression(TriggerType.ChangesZone);
|
||||||
|
|
||||||
if (setComputerLife > 0) {
|
|
||||||
ai.setLife(setComputerLife, null);
|
|
||||||
}
|
|
||||||
if (setHumanLife > 0) {
|
|
||||||
human.setLife(setHumanLife, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
game.getAction().checkStateEffects();
|
game.getAction().checkStateEffects();
|
||||||
game.getPhaseHandler().updateObservers();
|
game.getPhaseHandler().updateObservers();
|
||||||
@@ -290,6 +168,28 @@ public final class GuiDisplayUtil {
|
|||||||
p.getZone(ZoneType.Battlefield).updateObservers();
|
p.getZone(ZoneType.Battlefield).updateObservers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void devSetupPlayerState(int life, Map<ZoneType, String> cardTexts, final Player p) {
|
||||||
|
Map<ZoneType, List<Card>> humanCards = new EnumMap<ZoneType, List<Card>>(ZoneType.class);
|
||||||
|
for(Entry<ZoneType, String> kv : cardTexts.entrySet()) {
|
||||||
|
humanCards.put(kv.getKey(), GuiDisplayUtil.devProcessCardsForZone(kv.getValue().split(";"), p));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (life > 0) p.setLife(life, null);
|
||||||
|
for (Entry<ZoneType, List<Card>> 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>
|
* <p>
|
||||||
|
|||||||
Reference in New Issue
Block a user