Search in sources :

Example 1 with SOCScenario

use of soc.game.SOCScenario in project JSettlers2 by jdmonin.

the class NewGameOptionsFrame method clickScenarioInfo.

/**
 * The "Scenario Info" button was clicked.
 * Reads the current scenario, if any, from {@link #scenDropdown}.
 * Calls {@link #showScenarioInfoDialog(SOCScenario, Map, int, SOCPlayerClient.GameAwtDisplay, Frame)}.
 * @since 2.0.00
 */
private void clickScenarioInfo() {
    if (scenDropdown == null)
        // should not happen, scenDropdown is created before scenInfo
        return;
    final Object scObj = scenDropdown.getSelectedItem();
    if ((scObj == null) || !(scObj instanceof SOCScenario))
        // "(none)" item is a String, not a scenario
        return;
    final SOCScenario scen = (SOCScenario) scObj;
    // find game's vp_winner
    int vpWinner = SOCGame.VP_WINNER_STANDARD;
    boolean vpKnown = false;
    if (opts != null) {
        SOCGameOption vp = opts.get("VP");
        if (vp.getBoolValue()) {
            vpWinner = vp.getIntValue();
            vpKnown = true;
        }
    }
    if (forNewGame && (!vpKnown) && scen.scOpts.contains("VP=")) {
        final Map<String, SOCGameOption> scenOpts = SOCGameOption.parseOptionsToMap(scen.scOpts);
        final SOCGameOption scOptVP = (scenOpts != null) ? scenOpts.get("VP") : null;
        if (scOptVP != null)
            vpWinner = scOptVP.getIntValue();
    }
    showScenarioInfoDialog(scen, null, vpWinner, gameDisplay, this);
}
Also used : SOCGameOption(soc.game.SOCGameOption) SOCScenario(soc.game.SOCScenario)

Example 2 with SOCScenario

use of soc.game.SOCScenario in project JSettlers2 by jdmonin.

the class NewGameOptionsFrame method initInterface_OptLine.

/**
 * Set up one game option in one line of the panel.
 * Based on the option type, create the appropriate AWT component and call
 * {@link #initInterface_Opt1(SOCGameOption, Component, boolean, boolean, JPanel, GridBagLayout, GridBagConstraints)}.
 *<P>
 * Special handling: Scenario (option {@code "SC"}) gets a checkbox, label, dropdown, and a second line with
 * an Info button. (Sets {@link #scenDropdown}, {@link #scenInfo}).
 *
 * @param op  Option data
 * @param bp  Add to this panel
 * @param gbl Use this layout
 * @param gbc Use these constraints
 */
private void initInterface_OptLine(SOCGameOption op, JPanel bp, GridBagLayout gbl, GridBagConstraints gbc) {
    if (op.key.equals("SC")) {
        // special handling: Scenario
        if ((allSc == null) || allSc.isEmpty())
            return;
        int i = 0, sel = 0;
        JComboBox jcb = new JComboBox();
        // "(none)" is item 0 in dropdown
        jcb.addItem(strings.get("base.none.parens"));
        Collection<SOCScenario> scens = allSc.values();
        if (!readOnly) {
            // Sort by description.
            // Don't sort if readOnly and thus dropdown not enabled, probably not browsable.
            ArrayList<SOCScenario> sl = new ArrayList<SOCScenario>(scens);
            Collections.sort(sl, new Comparator<SOCScenario>() {

                // This method isn't part of SOCScenario because that class already has
                // equals and compareTo methods comparing keys, not descriptions
                public int compare(SOCScenario a, SOCScenario b) {
                    return a.getDesc().compareTo(b.getDesc());
                }
            });
            scens = sl;
        }
        for (final SOCScenario sc : scens) {
            ++i;
            // sc.toString() == sc.desc
            jcb.addItem(sc);
            if (sc.key.equals(op.getStringValue()))
                sel = i;
        }
        if (sel != 0) {
            jcb.setSelectedIndex(sel);
            op.setBoolValue(true);
        }
        scenDropdown = jcb;
        initInterface_Opt1(op, jcb, true, true, bp, gbl, gbc);
        if ((!readOnly) || opts.containsKey("SC")) {
            // 2nd line: right-justified "Scenario Info..." button
            Label blank = new Label();
            gbc.gridwidth = 1;
            gbc.weightx = 0;
            gbl.setConstraints(blank, gbc);
            bp.add(blank);
            // "Scenario Info..."
            scenInfo = new Button(strings.get("game.options.scenario.info_btn"));
            scenInfo.addActionListener(this);
            scenInfo.addKeyListener(this);
            // disable if "(none)" is selected scenario option
            scenInfo.setEnabled(sel != 0);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            final int oldAnchor = gbc.anchor, oldFill = gbc.fill;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.EAST;
            gbl.setConstraints(scenInfo, gbc);
            bp.add(scenInfo);
            gbc.fill = oldFill;
            gbc.anchor = oldAnchor;
        }
        return;
    }
    switch(// OTYPE_*
    op.optType) {
        case SOCGameOption.OTYPE_BOOL:
            {
                Checkbox cb = new Checkbox();
                initInterface_Opt1(op, cb, true, false, bp, gbl, gbc);
                cb.addItemListener(this);
            }
            break;
        case SOCGameOption.OTYPE_INT:
        case SOCGameOption.OTYPE_INTBOOL:
            {
                final boolean hasCheckbox = (op.optType == SOCGameOption.OTYPE_INTBOOL);
                initInterface_Opt1(op, initOption_int(op), hasCheckbox, true, bp, gbl, gbc);
            }
            break;
        case SOCGameOption.OTYPE_ENUM:
        case SOCGameOption.OTYPE_ENUMBOOL:
            // Choice (popup menu)
            {
                final boolean hasCheckbox = (op.optType == SOCGameOption.OTYPE_ENUMBOOL);
                initInterface_Opt1(op, initOption_enum(op), hasCheckbox, true, bp, gbl, gbc);
            }
            break;
        case SOCGameOption.OTYPE_STR:
        case SOCGameOption.OTYPE_STRHIDE:
            {
                // used as max length
                int txtwid = op.maxIntValue;
                if (txtwid > 20)
                    txtwid = 20;
                final boolean doHide = (op.optType == SOCGameOption.OTYPE_STRHIDE);
                String txtcontent = (doHide ? "" : op.getStringValue());
                TextField txtc = new TextField(txtcontent, txtwid);
                if (doHide) {
                    if (SOCPlayerClient.isJavaOnOSX)
                        // round bullet (option-8)
                        txtc.setEchoChar('\u2022');
                    else
                        txtc.setEchoChar('*');
                }
                if (!readOnly) {
                    // for ESC/ENTER
                    txtc.addKeyListener(this);
                    // for gameopt.ChangeListener and userChanged
                    txtc.addTextListener(this);
                }
                initInterface_Opt1(op, txtc, false, false, bp, gbl, gbc);
            }
            break;
    }
}
Also used : JComboBox(javax.swing.JComboBox) Button(java.awt.Button) Checkbox(java.awt.Checkbox) ArrayList(java.util.ArrayList) Label(java.awt.Label) TextField(java.awt.TextField) SOCScenario(soc.game.SOCScenario)

Example 3 with SOCScenario

use of soc.game.SOCScenario in project JSettlers2 by jdmonin.

the class NewGameOptionsFrame method showScenarioInfoDialog.

/**
 * Show a popup window with this game's scenario's description, special rules, and number of victory points to win.
 * Calls {@link EventQueue#invokeLater(Runnable)}.
 * @param ga  Game to display scenario info for; if game option {@code "SC"} missing or blank, does nothing.
 * @param cli     Player client interface, for {@link NotifyDialog} call
 * @param parent  Current game's player interface, or another Frame for our parent window,
 *                or null to look for {@code cli}'s Frame as parent
 * @since 2.0.00
 */
public static void showScenarioInfoDialog(final SOCGame ga, final GameAwtDisplay cli, final Frame parent) {
    final String scKey = ga.getGameOptionStringValue("SC");
    if (scKey == null)
        return;
    SOCScenario sc = SOCScenario.getScenario(scKey);
    if (sc == null)
        return;
    showScenarioInfoDialog(sc, ga.getGameOptions(), ga.vp_winner, cli, parent);
}
Also used : SOCScenario(soc.game.SOCScenario)

Example 4 with SOCScenario

use of soc.game.SOCScenario in project JSettlers2 by jdmonin.

the class SOCPlayerClient method localizeGameScenarios.

/**
 * Localize {@link SOCScenario} names and descriptions with strings from the server.
 * Updates scenario data in {@link #practiceServGameOpts} or {@link #tcpServGameOpts}.
 *
 * @param scStrs  Scenario localized strings, same format as {@link SOCLocalizedStrings} params.
 * @param skipFirst  If true skip the first element of {@code scStrs}, it isn't a scenario keyname.
 * @param sentAll  True if no further strings will be received; is {@link SOCLocalizedStrings#FLAG_SENT_ALL} set?
 *     If true, sets {@link ServerGametypeInfo#allScenStringsReceived}.
 * @param isPractice  Is the server {@link ClientNetwork#practiceServer}, not remote?
 * @since 2.0.00
 */
protected void localizeGameScenarios(final List<String> scStrs, final boolean skipFirst, final boolean sentAll, final boolean isPractice) {
    ServerGametypeInfo opts = (isPractice ? practiceServGameOpts : tcpServGameOpts);
    final int L = scStrs.size();
    int i = (skipFirst) ? 1 : 0;
    while (i < L) {
        final String scKey = scStrs.get(i);
        ++i;
        opts.scenKeys.add(scKey);
        final String nm = scStrs.get(i);
        ++i;
        if (nm.equals(SOCLocalizedStrings.MARKER_KEY_UNKNOWN))
            continue;
        String desc = scStrs.get(i);
        ++i;
        SOCScenario sc = SOCScenario.getScenario(scKey);
        if ((sc != null) && (nm.length() > 0)) {
            if ((desc != null) && (desc.length() == 0))
                desc = null;
            sc.setDesc(nm, desc);
        }
    }
    if (sentAll)
        opts.allScenStringsReceived = true;
}
Also used : SOCScenario(soc.game.SOCScenario)

Example 5 with SOCScenario

use of soc.game.SOCScenario in project JSettlers2 by jdmonin.

the class TestI18NGameoptScenStrings method testDescriptionsFile.

/**
 * For {@link #testDescriptionsForNet()}, test one string props file's description strings.
 * @param pfile Full filename to open and test
 * @return True if OK, or prints failed strings and return false
 * @throws Exception if {@code pfile} can't be opened and read
 */
private boolean testDescriptionsFile(File pfile) throws Exception {
    final FileInputStream fis = new FileInputStream(pfile);
    final PropertyResourceBundle props = new PropertyResourceBundle(fis);
    try {
        fis.close();
    } catch (IOException e) {
    }
    boolean allOK = true;
    final TreeSet<String> // use TreeSet for sorted results
    optBadChar = new TreeSet<String>(), scenBadChar = new TreeSet<String>();
    final ArrayList<String> optsStr = new ArrayList<String>(), scenStr = new ArrayList<String>();
    for (final SOCGameOption opt : allOpts.values()) {
        // but if present there the description strings do need to be OK.
        try {
            final String smDesc = props.getString("gameopt." + opt.key);
            if (smDesc != null) {
                optsStr.add(smDesc);
                if (!SOCMessage.isSingleLineAndSafe(smDesc))
                    optBadChar.add(opt.key);
            }
        } catch (MissingResourceException e) {
        }
    }
    for (final SOCScenario sc : allScens.values()) {
        String strKey = sc.key + ".n";
        try {
            final String smDesc = props.getString("gamescen." + strKey);
            if (smDesc != null) {
                scenStr.add(smDesc);
                if (!SOCMessage.isSingleLineAndSafe(smDesc))
                    scenBadChar.add(strKey);
            }
        } catch (MissingResourceException e) {
        }
        final String longDesc = sc.getLongDesc();
        if (longDesc != null) {
            strKey = sc.key + ".d";
            try {
                final String smDesc = props.getString("gamescen." + strKey);
                if (smDesc != null) {
                    scenStr.add(smDesc);
                    if (smDesc.contains(SOCMessage.sep) || !SOCMessage.isSingleLineAndSafe(smDesc, true))
                        scenBadChar.add(strKey);
                }
            } catch (MissingResourceException e) {
            }
        }
    }
    if (!optBadChar.isEmpty()) {
        allOK = false;
        System.out.println(pfile.getName() + ": Game opts with gameopt.* strings failing SOCMessage.isSingleLineAndSafe(..): " + optBadChar);
    }
    if (!scenBadChar.isEmpty()) {
        allOK = false;
        System.out.println(pfile.getName() + ": SOCScenario key strings in gamescen.* failing SOCMessage.isSingleLineAndSafe(..): " + scenBadChar);
    }
    // alias for brevity
    final int MAX = Connection.MAX_MESSAGE_SIZE_UTF8;
    String msg = SOCLocalizedStrings.toCmd(SOCLocalizedStrings.TYPE_GAMEOPT, Integer.MAX_VALUE, optsStr);
    int L = msg.getBytes("utf-8").length;
    if (L > MAX) {
        allOK = false;
        System.out.println(pfile.getName() + ": Total length gameopt.* strings too long for SOCLocalizedStrings (" + L + ", max " + MAX + ")");
    }
    msg = SOCLocalizedStrings.toCmd(SOCLocalizedStrings.TYPE_SCENARIO, Integer.MAX_VALUE, scenStr);
    L = msg.getBytes("utf-8").length;
    if (L > MAX) {
        allOK = false;
        System.out.println(pfile.getName() + ": Total length gamescen.* strings too long for SOCLocalizedStrings (" + L + ", max " + MAX + ")");
    }
    return allOK;
}
Also used : MissingResourceException(java.util.MissingResourceException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) SOCScenario(soc.game.SOCScenario) TreeSet(java.util.TreeSet) SOCGameOption(soc.game.SOCGameOption)

Aggregations

SOCScenario (soc.game.SOCScenario)10 TreeSet (java.util.TreeSet)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 SOCGameOption (soc.game.SOCGameOption)3 MissingResourceException (java.util.MissingResourceException)2 Button (java.awt.Button)1 Checkbox (java.awt.Checkbox)1 Label (java.awt.Label)1 TextField (java.awt.TextField)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 PropertyResourceBundle (java.util.PropertyResourceBundle)1 JComboBox (javax.swing.JComboBox)1