use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCBuildingPanel method updateButtonStatus.
/**
* Update the status of the buttons. Each piece type's button is labeled "Buy" or disabled ("---")
* depending on game state and resources available, unless we're currently placing a bought piece.
* In that case the bought piece type's button is labeled "Cancel", and the others are disabled with
* their current labels until placement is complete.
*/
public void updateButtonStatus() {
SOCGame game = pi.getGame();
// If placing a piece, if-statements here will set the right state
pieceButtonsState = 0;
if (player != null) {
final int pnum = player.getPlayerNumber();
final boolean isDebugFreePlacement = game.isDebugFreePlacement();
final boolean isCurrent = (!isDebugFreePlacement) && (game.getCurrentPlayerNumber() == pnum);
final int gstate = game.getGameState();
boolean currentCanBuy = (!isDebugFreePlacement) && game.canBuyOrAskSpecialBuild(pnum);
if (isCurrent && ((gstate == SOCGame.PLACING_ROAD) || ((gstate == SOCGame.PLACING_FREE_ROAD2) && (game.isPractice || pi.getClient().sVersion >= SOCGame.VERSION_FOR_CANCEL_FREE_ROAD2)))) {
roadBut.setEnabled(true);
// "Cancel"
roadBut.setLabel(strings.get("base.cancel"));
pieceButtonsState = (gstate == SOCGame.PLACING_FREE_ROAD2) ? gstate : SOCGame.PLACING_ROAD;
} else if (game.couldBuildRoad(pnum)) {
roadBut.setEnabled(currentCanBuy);
// "Buy"
roadBut.setLabel(strings.get("build.buy"));
} else {
roadBut.setEnabled(false);
roadBut.setLabel("---");
}
if (isCurrent && ((gstate == SOCGame.PLACING_SETTLEMENT) || (gstate == SOCGame.START1B) || (gstate == SOCGame.START2B) || (gstate == SOCGame.START3B))) {
settlementBut.setEnabled(true);
settlementBut.setLabel(strings.get("base.cancel"));
pieceButtonsState = SOCGame.PLACING_SETTLEMENT;
} else if (game.couldBuildSettlement(pnum)) {
settlementBut.setEnabled(currentCanBuy);
settlementBut.setLabel(strings.get("build.buy"));
} else {
settlementBut.setEnabled(false);
settlementBut.setLabel("---");
}
if (isCurrent && (gstate == SOCGame.PLACING_CITY)) {
cityBut.setEnabled(true);
cityBut.setLabel(strings.get("base.cancel"));
pieceButtonsState = SOCGame.PLACING_CITY;
} else if (game.couldBuildCity(pnum)) {
cityBut.setEnabled(currentCanBuy);
cityBut.setLabel(strings.get("build.buy"));
} else {
cityBut.setEnabled(false);
cityBut.setLabel("---");
}
if (game.couldBuyDevCard(pnum)) {
cardBut.setEnabled(currentCanBuy);
cardBut.setLabel(strings.get("build.buy"));
} else {
cardBut.setEnabled(false);
cardBut.setLabel("---");
}
if (shipBut != null) {
if (isCurrent && ((gstate == SOCGame.PLACING_SHIP) || (gstate == SOCGame.PLACING_FREE_ROAD2))) {
shipBut.setEnabled(true);
shipBut.setLabel(strings.get("base.cancel"));
// PLACING_SHIP or PLACING_FREE_ROAD2
pieceButtonsState = gstate;
// ships were added after VERSION_FOR_CANCEL_FREE_ROAD2, so no need to check server version
// to make sure the server supports canceling.
} else if (game.couldBuildShip(pnum)) {
shipBut.setEnabled(currentCanBuy);
shipBut.setLabel(strings.get("build.buy"));
} else {
shipBut.setEnabled(false);
shipBut.setLabel("---");
}
}
if ((sbBut != null) && (player != null)) {
final boolean askedSB = player.hasAskedSpecialBuild();
if (askedSB != sbIsHilight) {
final Color want = (askedSB) ? ColorSquare.WARN_LEVEL_COLOR_BG_FROMGREY : ColorSquare.GREY;
sbPanel.setBackground(want);
if (sbLab != null)
sbLab.setBackground(want);
sbIsHilight = askedSB;
}
sbBut.setEnabled(game.canAskSpecialBuild(pnum, false) && !askedSB);
}
}
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCBuildingPanel method setPlayer.
/**
* Set our game and player data based on client's nickname,
* via game.getPlayer(client.getNickname()).
*
* @throws IllegalStateException If the player data has already been set,
* and this isn't a new game (a board reset).
*/
public void setPlayer() throws IllegalStateException {
SOCGame game = pi.getGame();
if ((player != null) && !game.isBoardReset())
throw new IllegalStateException("Player data is already set");
player = game.getPlayer(pi.getClient().getNickname());
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCBuildingPanel method actionPerformed.
/**
* Handle button clicks in this panel.
*
* @param e button click event
*/
public void actionPerformed(ActionEvent e) {
try {
String target = e.getActionCommand();
SOCGame game = pi.getGame();
if (e.getSource() == gameOptsBut) {
if ((ngof != null) && ngof.isVisible()) {
// method override also requests topmost/focus
ngof.setVisible(true);
} else {
ngof = NewGameOptionsFrame.createAndShow(pi, pi.getGameDisplay(), game.getName(), game.getGameOptions(), false, true);
// drop ngof reference when window is closed
ngof.addWindowListener(this);
}
return;
}
if (e.getSource() == statsBut) {
if (statsFrame != null)
statsFrame.dispose();
GameStatisticsFrame f = new GameStatisticsFrame(pi);
f.register(pi.getGameStats());
f.setLocation(this.getLocationOnScreen());
f.setVisible(true);
statsFrame = f;
return;
} else if (e.getSource() == wondersBut) {
final SOCSpecialItemDialog dia = new SOCSpecialItemDialog(pi, SOCGameOption.K_SC_WOND);
dia.setNonBlockingDialogDismissListener(pi);
pi.nbdForEvent = dia;
dia.pack();
// is modal but other players' gameplay can continue (separate threads)
dia.setVisible(true);
return;
}
if (player != null) {
clickBuildingButton(game, target, false);
}
} catch (Throwable th) {
pi.chatPrintStackTrace(th);
}
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCQuitConfirmDialog method createAndShow.
/**
* Creates and shows a new SOCQuitConfirmDialog.
* If the game is over, the "Quit" button is the default;
* otherwise, Continue is default.
*<P>
* Assumes currently running on AWT event thread.
*
* @param cli Player client interface
* @param gamePI Current game's player interface
* @throws IllegalArgumentException If cli or gamePI is null
*/
public static void createAndShow(GameAwtDisplay cli, SOCPlayerInterface gamePI) throws IllegalArgumentException {
if ((cli == null) || (gamePI == null))
throw new IllegalArgumentException("no nulls");
SOCGame ga = gamePI.getGame();
boolean gaOver = (ga.getGameState() >= SOCGame.OVER) || gamePI.gameHasErrorOrDeletion;
SOCQuitConfirmDialog qcd = new SOCQuitConfirmDialog(cli, gamePI, gaOver);
qcd.setVisible(true);
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCServerMessageHandler method handleGAMETEXTMSG.
/**
* Handle game text messages, including debug commands.
* Was part of SOCServer.processCommand before 1.1.07.
*<P>
* Some commands are unprivileged and can be run by any client:
*<UL>
* <LI> *ADDTIME*
* <LI> *CHECKTIME*
* <LI> *VERSION*
* <LI> *STATS*
* <LI> *WHO*
*</UL>
* These commands are processed in this method.
* Others can be run only by certain users or when certain server flags are set.
* Those are processed in {@link SOCServer#processDebugCommand(Connection, String, String, String)}.
*
* @since 1.1.07
*/
void handleGAMETEXTMSG(Connection c, SOCGameTextMsg gameTextMsgMes) {
// createNewGameEventRecord();
// currentGameEventRecord.setMessageIn(new SOCMessageRecord(mes, c.getData(), "SERVER"));
final String gaName = gameTextMsgMes.getGame();
srv.recordGameEvent(gaName, gameTextMsgMes);
SOCGame ga = gameList.getGameData(gaName);
if (ga == null)
// <---- early return: no game by that name ----
return;
final String plName = c.getData();
if (null == ga.getPlayer(plName)) {
// c isn't a seated player in that game; have they joined it?
// To avoid disruptions by game observers, only players can chat after initial placement.
// To help form the game, non-seated members can also participate in the chat until then.
final boolean canChat = (ga.getGameState() < SOCGame.ROLL_OR_CARD) && gameList.isMember(c, gaName);
if (!canChat) {
// "Observers can't chat during the game."
srv.messageToPlayerKeyed(c, gaName, "member.chat.not_observers");
// <---- early return: not a player in that game ----
return;
}
}
// currentGameEventRecord.setSnapshot(ga);
final String cmdText = gameTextMsgMes.getText();
final String cmdTxtUC = cmdText.toUpperCase();
// /
if (cmdTxtUC.startsWith("*ADDTIME*") || cmdTxtUC.startsWith("ADDTIME")) {
if (ga.isPractice) {
// ">>> Practice games never expire."
srv.messageToPlayerKeyed(c, gaName, "reply.addtime.practice.never");
} else if (ga.getGameState() >= SOCGame.OVER) {
// "This game is over, cannot extend its time."
srv.messageToPlayerKeyed(c, gaName, "reply.addtime.game_over");
} else {
// check game time currently remaining: if already more than
// the original GAME_TIME_EXPIRE_MINUTES + GAME_TIME_EXPIRE_ADDTIME_MINUTES,
// don't add more now.
final long now = System.currentTimeMillis();
long exp = ga.getExpiration();
int minRemain = (int) ((exp - now) / (60 * 1000));
final int gameMaxMins = SOCGameListAtServer.GAME_TIME_EXPIRE_MINUTES + SOCServer.GAME_TIME_EXPIRE_ADDTIME_MINUTES;
if (minRemain > gameMaxMins - 4) {
srv.messageToPlayerKeyed(c, gaName, "reply.addtime.not_expire_soon", Integer.valueOf(minRemain));
// "Ask again later: This game does not expire soon, it has {0} minutes remaining."
// This check time subtracts 4 minutes to keep too-frequent addtime requests
// from spamming all game members with announcements
} else {
int minAdd = SOCServer.GAME_TIME_EXPIRE_ADDTIME_MINUTES;
if (minRemain + minAdd > gameMaxMins)
minAdd = gameMaxMins - minRemain;
exp += (minAdd * 60 * 1000);
minRemain += minAdd;
ga.setExpiration(exp);
// ">>> Game time has been extended."
srv.messageToGameKeyed(ga, true, "reply.addtime.extended");
srv.messageToGameKeyed(ga, true, "stats.game.willexpire.urgent", Integer.valueOf(minRemain));
// ">>> This game will expire in 45 minutes."
}
}
} else // /
if (cmdTxtUC.startsWith("*CHECKTIME*")) {
processDebugCommand_gameStats(c, gaName, ga, true);
} else if (cmdTxtUC.startsWith("*VERSION*")) {
srv.messageToPlayer(c, gaName, "Java Settlers Server " + Version.versionNumber() + " (" + Version.version() + ") build " + Version.buildnum());
} else if (cmdTxtUC.startsWith("*STATS*")) {
srv.processDebugCommand_serverStats(c, ga);
} else if (cmdTxtUC.startsWith("*WHO*")) {
processDebugCommand_who(c, ga, cmdText);
} else if (cmdTxtUC.startsWith("*DBSETTINGS*")) {
processDebugCommand_dbSettings(c, ga);
} else //
// check for admin/debugging commands
//
// 1.1.07: all practice games are debug mode, for ease of debugging;
// not much use for a chat window in a practice game anyway.
//
{
final boolean userIsDebug = ((srv.isDebugUserEnabled() && plName.equals("debug")) || (c instanceof StringConnection));
if (cmdTxtUC.startsWith("*HELP")) {
for (int i = 0; i < SOCServer.GENERAL_COMMANDS_HELP.length; ++i) srv.messageToPlayer(c, gaName, SOCServer.GENERAL_COMMANDS_HELP[i]);
if (// no user admins in practice games
(userIsDebug && !(c instanceof StringConnection)) || srv.isUserDBUserAdmin(plName)) {
srv.messageToPlayer(c, gaName, SOCServer.ADMIN_COMMANDS_HEADING);
for (int i = 0; i < SOCServer.ADMIN_USER_COMMANDS_HELP.length; ++i) srv.messageToPlayer(c, gaName, SOCServer.ADMIN_USER_COMMANDS_HELP[i]);
}
if (userIsDebug) {
for (int i = 0; i < SOCServer.DEBUG_COMMANDS_HELP.length; ++i) srv.messageToPlayer(c, gaName, SOCServer.DEBUG_COMMANDS_HELP[i]);
GameHandler hand = gameList.getGameTypeHandler(gaName);
if (hand != null) {
final String[] GAMETYPE_DEBUG_HELP = hand.getDebugCommandsHelp();
if (GAMETYPE_DEBUG_HELP != null)
for (int i = 0; i < GAMETYPE_DEBUG_HELP.length; ++i) srv.messageToPlayer(c, gaName, GAMETYPE_DEBUG_HELP[i]);
}
}
} else {
boolean isCmd = userIsDebug && srv.processDebugCommand(c, ga.getName(), cmdText, cmdTxtUC);
if (!isCmd)
//
// Send the message to the members of the game
//
srv.messageToGame(gaName, new SOCGameTextMsg(gaName, plName, cmdText));
}
}
// saveCurrentGameEventRecord(gameTextMsgMes.getGame());
}
Aggregations