use of games.strategy.engine.data.GameParseException in project triplea by triplea-game.
the class RulesAttachment method setTechs.
private void setTechs(final String newTechs) throws GameParseException {
if (newTechs == null) {
m_techs = null;
return;
}
final String[] s = newTechs.split(":");
if (s.length < 1) {
throw new GameParseException("Empty tech list" + thisErrorMsg());
}
int count = -1;
try {
count = getInt(s[0]);
m_techCount = count;
} catch (final Exception e) {
m_techCount = 0;
}
if (s.length < 1 || (s.length == 1 && count != -1)) {
throw new GameParseException("Empty tech list" + thisErrorMsg());
}
m_techs = new ArrayList<>();
for (int i = count == -1 ? 0 : 1; i < s.length; i++) {
TechAdvance ta = getData().getTechnologyFrontier().getAdvanceByProperty(s[i]);
if (ta == null) {
ta = getData().getTechnologyFrontier().getAdvanceByName(s[i]);
}
if (ta == null) {
throw new GameParseException("Technology not found :" + Arrays.toString(s) + thisErrorMsg());
}
m_techs.add(ta);
}
}
use of games.strategy.engine.data.GameParseException in project triplea by triplea-game.
the class RulesAttachment method setBattle.
private void setBattle(final String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length < 5) {
throw new GameParseException("battle must have at least 5 fields, attacker:defender:resultType:round:territory1..." + thisErrorMsg());
}
final PlayerID attacker = getData().getPlayerList().getPlayerId(s[0]);
if (attacker == null && !s[0].equalsIgnoreCase("any")) {
throw new GameParseException("no player named: " + s[0] + thisErrorMsg());
}
final PlayerID defender = getData().getPlayerList().getPlayerId(s[1]);
if (defender == null && !s[1].equalsIgnoreCase("any")) {
throw new GameParseException("no player named: " + s[1] + thisErrorMsg());
}
if (!s[2].equalsIgnoreCase("any")) {
throw new GameParseException("battle allows the following for resultType: any" + thisErrorMsg());
}
if (!s[3].equalsIgnoreCase("currentRound")) {
try {
getInt(s[3].split("-")[0]);
getInt(s[3].split("-")[1]);
} catch (final Exception e) {
throw new GameParseException("round must either be currentRound or two numbers like: 2-4" + thisErrorMsg());
}
}
final ArrayList<Territory> terrs = new ArrayList<>();
final GameMap map = getData().getMap();
// this loop starts on 4, so do not replace with an enhanced for loop
for (int i = 4; i < s.length; i++) {
final Territory t = map.getTerritory(s[i]);
if (t == null) {
throw new GameParseException("no such territory called: " + s[i] + thisErrorMsg());
}
terrs.add(t);
}
m_battle.add(Tuple.of((s[0] + ":" + s[1] + ":" + s[2] + ":" + s[3]), terrs));
}
use of games.strategy.engine.data.GameParseException in project triplea by triplea-game.
the class RulesAttachment method setUnitPresence.
private void setUnitPresence(String value) throws GameParseException {
final String[] s = value.split(":");
if (s.length <= 1) {
throw new GameParseException("unitPresence must have at least 2 fields. Format value=unit1 count=number, or " + "value=unit1:unit2:unit3 count=number" + thisErrorMsg());
}
final int n = getInt(s[0]);
if (n < 0) {
throw new GameParseException("unitPresence must be a positive integer" + thisErrorMsg());
}
for (int i = 1; i < s.length; i++) {
final String unitTypeToProduce = s[i];
// validate that this unit exists in the xml
final UnitType ut = getData().getUnitTypeList().getUnitType(unitTypeToProduce);
if (ut == null && !(unitTypeToProduce.equals("any") || unitTypeToProduce.equals("ANY"))) {
throw new GameParseException("No unit called: " + unitTypeToProduce + thisErrorMsg());
}
}
value = value.replaceFirst(s[0] + ":", "");
m_unitPresence.put(value, n);
}
use of games.strategy.engine.data.GameParseException in project triplea by triplea-game.
the class GameSelectorPanel method selectGameFile.
private void selectGameFile(final boolean saved) {
// is to use an AWT FileDialog instead of a Swing JDialog
if (saved) {
final File file = selectGameFile();
if (file == null || !file.exists()) {
return;
}
Interruptibles.await(() -> GameRunner.newBackgroundTaskRunner().runInBackground("Loading savegame...", () -> {
model.load(file, this);
setOriginalPropertiesMap(model.getGameData());
}));
} else {
try {
final GameChooserEntry entry = GameChooser.chooseGame(JOptionPane.getFrameForComponent(this), model.getGameName());
if (entry != null) {
GameRunner.newBackgroundTaskRunner().runInBackground("Loading map...", () -> {
if (!entry.isGameDataLoaded()) {
try {
entry.fullyParseGameData();
} catch (final GameParseException e) {
// TODO remove bad entries from the underlying model
return;
}
}
model.load(entry);
});
// we'll have a null game data.
if (model.getGameData() != null) {
setOriginalPropertiesMap(model.getGameData());
// only for new games, not saved games, we set the default options, and set them only once
// (the first time it is loaded)
gamePropertiesCache.loadCachedGamePropertiesInto(model.getGameData());
}
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
use of games.strategy.engine.data.GameParseException in project triplea by triplea-game.
the class TerritoryAttachment method setCapital.
public void setCapital(final String value) throws GameParseException {
if (value == null) {
m_capital = null;
return;
}
final PlayerID p = getData().getPlayerList().getPlayerId(value);
if (p == null) {
throw new GameParseException("No Player named: " + value + thisErrorMsg());
}
m_capital = value;
}
Aggregations