use of soc.game.SOCGameOption 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;
}
use of soc.game.SOCGameOption in project JSettlers2 by jdmonin.
the class SOCBotJoinGameRequest method parseDataStr.
/**
* Parse the command String into a BotJoinGameRequest message
*
* @param s the String to parse
* @return a BotJoinGameRequest message, or null if the data is garbled
*/
public static SOCBotJoinGameRequest parseDataStr(String s) {
// the game name
String ga;
// the seat number
int pn;
String optstr;
StringTokenizer st = new StringTokenizer(s, sep2);
try {
ga = st.nextToken();
pn = Integer.parseInt(st.nextToken());
// NOT sep2: options may contain ","
optstr = st.nextToken(sep);
} catch (Exception e) {
return null;
}
Map<String, SOCGameOption> opts = SOCGameOption.parseOptionsToMap(optstr);
return new SOCBotJoinGameRequest(ga, pn, opts);
}
use of soc.game.SOCGameOption in project JSettlers2 by jdmonin.
the class ServerGametypeInfo method receiveInfo.
/**
* After calling receiveDefaults, call this as each GAMEOPTIONGETINFO is received.
* Updates allOptionsReceived.
*
* @param gi Message from server with info on one parameter
* @return true if all are known, false if more are unknown after this one
*/
public boolean receiveInfo(SOCGameOptionInfo gi) {
String oKey = gi.getOptionNameKey();
SOCGameOption oinfo = gi.getOptionInfo();
SOCGameOption oldcopy = optionSet.get(oKey);
if ((oinfo.key.equals("-")) && (oinfo.optType == SOCGameOption.OTYPE_UNKNOWN)) {
// end-of-list marker: no more options from server.
// That is end of srv's response to cli sending GAMEOPTIONGETINFOS("-").
noMoreOptions(false);
return true;
} else {
// remove old, replace with new from server (if any)
if (oldcopy != null) {
optionSet.remove(oKey);
final SOCGameOption.ChangeListener cl = oldcopy.getChangeListener();
if (cl != null)
oinfo.addChangeListener(cl);
}
SOCGameOption.addKnownOption(oinfo);
if (oinfo.optType != SOCGameOption.OTYPE_UNKNOWN)
optionSet.put(oKey, oinfo);
return false;
}
}
use of soc.game.SOCGameOption in project JSettlers2 by jdmonin.
the class SOCBoardAtServer method getBoardSize.
/**
* Get the board size for
* {@link BoardFactoryAtServer#createBoard(Map, boolean, int) BoardFactoryAtServer.createBoard}:
* The default size {@link SOCBoardLarge#BOARDHEIGHT_LARGE BOARDHEIGHT_LARGE} by
* {@link SOCBoardLarge#BOARDWIDTH_LARGE BOARDWIDTH_LARGE},
* unless <tt>gameOpts</tt> contains a scenario (<tt>"SC"</tt>) whose layout has a custom height/width.
* The fallback 6-player layout size is taller,
* {@link SOCBoardLarge#BOARDHEIGHT_LARGE BOARDHEIGHT_LARGE} + 3 by {@link SOCBoardLarge#BOARDWIDTH_LARGE BOARDWIDTH_LARGE}.
* @param gameOpts Game options, or null
* @param maxPlayers Maximum players; must be 4 or 6 (from game option {@code "PL"} > 4 or {@code "PLB"}).
* If {@code maxPlayers} == 4 and {@code gameOpts} contains {@code "PL"},
* that overrides {@code maxPlayers} using the same logic as in {@link #makeNewBoard(Map)}.
* @return encoded size (0xRRCC), the same format as game option {@code "_BHW"}
* @see SOCBoardLarge#getBoardSize(Map, int)
*/
private static int getBoardSize(final Map<String, SOCGameOption> gameOpts, final int maxPlayers) {
int heightWidth = 0;
// Always test maxPl for ==6 or < 4 ; actual value may be 6, 4, 3, or 2.
// Same maxPl initialization as in getBoardShift(opts) and makeNewBoard(..).
final int maxPl;
SOCGameOption opt = (gameOpts != null ? gameOpts.get("PL") : null);
if ((opt == null) || (maxPlayers == 6))
maxPl = maxPlayers;
else if (opt.getIntValue() > 4)
maxPl = 6;
else
maxPl = opt.getIntValue();
SOCGameOption scOpt = null;
if (gameOpts != null)
scOpt = gameOpts.get("SC");
if (scOpt != null) {
// Check scenario name; not all scenarios have a custom board size.
final String sc = scOpt.getStringValue();
if (sc.equals(SOCScenario.K_SC_4ISL)) {
// 3, 4-player boards have same board size
heightWidth = FOUR_ISL_BOARDSIZE[(maxPl == 6) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_FOG)) {
// 3, 4-player boards have same board size
heightWidth = FOG_ISL_BOARDSIZE[(maxPl == 6) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_TTD)) {
heightWidth = TTDESERT_BOARDSIZE[(maxPl == 6) ? 2 : (maxPl == 4) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_PIRI)) {
heightWidth = PIR_ISL_BOARDSIZE[(maxPl == 6) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_FTRI)) {
heightWidth = FOR_TRI_BOARDSIZE[(maxPl == 6) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_CLVI)) {
heightWidth = CLVI_BOARDSIZE[(maxPl == 6) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_WOND)) {
heightWidth = WOND_BOARDSIZE[(maxPl == 6) ? 1 : 0];
} else if (sc.equals(SOCScenario.K_SC_NSHO)) {
heightWidth = NSHO_BOARDSIZE[(maxPl == 6) ? 2 : (maxPl == 4) ? 1 : 0];
}
}
if (heightWidth == 0) {
// No recognized scenario, so use the fallback board.
heightWidth = FALLBACK_BOARDSIZE[(maxPl == 6) ? 1 : 0];
}
return heightWidth;
}
use of soc.game.SOCGameOption in project JSettlers2 by jdmonin.
the class SOCBoardAtServer method getBoardShift.
/**
* Given max players and scenario from {@code gameOpts},
* get this board's Visual Shift amount if any (layout part "VS").
* See {@link #getAddedLayoutPart(String)} javadoc for details on "VS".
* @param gameOpts Game options, or null.
* Looks for {@code "PL"} for max players and {@code "SC"} for scenario name key.
* @return array with vsDown, vsRight, or {@code null}
*/
private static int[] getBoardShift(final Map<String, SOCGameOption> gameOpts) {
SOCGameOption opt;
// Use 3-player or 4-player or 6-player layout?
// Always test maxPl for ==6 or < 4 ; actual value may be 6, 4, 3, or 2.
// Same maxPl initialization as in makeNewBoard(opts).
final int maxPl;
opt = (gameOpts != null ? gameOpts.get("PLB") : null);
if ((opt != null) && opt.getBoolValue()) {
maxPl = 6;
} else {
opt = (gameOpts != null ? gameOpts.get("PL") : null);
if (opt == null)
maxPl = 4;
else if (opt.getIntValue() > 4)
maxPl = 6;
else
maxPl = opt.getIntValue();
}
// scenario key, such as SOCScenario.K_SC_4ISL, or empty string
final String scen;
opt = (gameOpts != null ? gameOpts.get("SC") : null);
if (opt != null) {
final String ostr = opt.getStringValue();
scen = (ostr != null) ? ostr : "";
} else {
scen = "";
}
// when 1 choice (same VS for all player count): return it directly.
// otherwise set boardVS and choose below based on maxPl.
int[][] boardVS = null;
if (scen.length() == 0)
boardVS = FALLBACK_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_NSHO))
boardVS = NSHO_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_4ISL))
return FOUR_ISL_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_PIRI))
return PIR_ISL_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_TTD))
return TTDESERT_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_FTRI))
boardVS = FOR_TRI_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_CLVI))
boardVS = CLVI_VIS_SHIFT;
else if (scen.equals(SOCScenario.K_SC_WOND))
boardVS = WOND_VIS_SHIFT;
else if (gameOpts != null) {
opt = gameOpts.get(SOCGameOption.K_SC_FOG);
if ((opt != null) && opt.getBoolValue())
boardVS = FOG_ISL_VIS_SHIFT;
}
if (boardVS == null)
return null;
final int idx = (boardVS.length == 2) ? // 6-player or 4-player board layout
((maxPl > 4) ? 1 : 0) : // 6-player, 4, or 3-player board layout
((maxPl > 4) ? 2 : (maxPl > 3) ? 1 : 0);
// may return null
return boardVS[idx];
}
Aggregations