Search in sources :

Example 26 with GameParseException

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);
    }
}
Also used : TechAdvance(games.strategy.triplea.delegate.TechAdvance) GameParseException(games.strategy.engine.data.GameParseException) GameParseException(games.strategy.engine.data.GameParseException)

Example 27 with GameParseException

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));
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameMap(games.strategy.engine.data.GameMap) ArrayList(java.util.ArrayList) GameParseException(games.strategy.engine.data.GameParseException) GameParseException(games.strategy.engine.data.GameParseException)

Example 28 with GameParseException

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);
}
Also used : UnitType(games.strategy.engine.data.UnitType) GameParseException(games.strategy.engine.data.GameParseException)

Example 29 with GameParseException

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();
        }
    }
}
Also used : GameChooserEntry(games.strategy.engine.framework.ui.GameChooserEntry) GameParseException(games.strategy.engine.data.GameParseException) File(java.io.File)

Example 30 with GameParseException

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;
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) GameParseException(games.strategy.engine.data.GameParseException)

Aggregations

GameParseException (games.strategy.engine.data.GameParseException)66 UnitType (games.strategy.engine.data.UnitType)29 PlayerID (games.strategy.engine.data.PlayerID)13 Territory (games.strategy.engine.data.Territory)7 Resource (games.strategy.engine.data.Resource)6 GameChooserEntry (games.strategy.engine.framework.ui.GameChooserEntry)3 TechAdvance (games.strategy.triplea.delegate.TechAdvance)3 IntegerMap (games.strategy.util.IntegerMap)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 EngineVersionException (games.strategy.engine.data.EngineVersionException)2 IOException (java.io.IOException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 GameMap (games.strategy.engine.data.GameMap)1 PlayerList (games.strategy.engine.data.PlayerList)1 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)1 RelationshipType (games.strategy.engine.data.RelationshipType)1 ResourceCollection (games.strategy.engine.data.ResourceCollection)1 TerritoryEffect (games.strategy.engine.data.TerritoryEffect)1 InternalDoNotExport (games.strategy.engine.data.annotations.InternalDoNotExport)1