use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCGameListAtServer method resetBoard.
/**
* Reset the board of this game, create a new game of same name,
* same players, new layout. The new "reset" board takes the place
* of the old game in the game list.
*<P>
* Robots are not copied and
* must re-join the game. (They're removed from the list of game members.)
* If the game had robots, they must leave the old game before any players can
* join the new game; the new game's {@link SOCGame#boardResetOngoingInfo} field
* is set to the object returned by this method, and its gameState will be
* {@link SOCGame#READY_RESET_WAIT_ROBOT_DISMISS} instead of {@link SOCGame#NEW}.
*<P>
* <b>Locking:</b>
* Takes game monitor.
* Copies old game.
* Adds reset-copy to gamelist.
* Destroys old game.
* Releases game monitor.
*
* @param gaName Name of game - If not found, do nothing. No monitor is taken.
* @return New game if gaName was found and copied; null if no game called gaName,
* or if a problem occurs during reset
* @see soc.game.SOCGame#resetAsCopy()
*/
public SOCGameBoardReset resetBoard(String gaName) {
SOCGame oldGame = gameData.get(gaName);
if (oldGame == null)
return null;
takeMonitorForGame(gaName);
// Create reset-copy of game;
// also removes robots from game obj and its member list,
// and sets boardResetOngoingInfo field/gamestate if there are robots.
SOCGameBoardReset reset = null;
try {
reset = new SOCGameBoardReset(oldGame, getMembers(gaName));
SOCGame rgame = reset.newGame;
// As in createGame, set expiration timer to 90 min. from now
rgame.setExpiration(System.currentTimeMillis() + (60 * 1000 * GAME_TIME_EXPIRE_MINUTES));
// Adjust game-list
gameData.remove(gaName);
gameData.put(gaName, rgame);
// Done.
oldGame.destroyGame();
} catch (Throwable e) {
D.ebugPrintStackTrace(e, "ERROR -> gamelist.resetBoard");
} finally {
releaseMonitorForGame(gaName);
}
// null if error during reset
return reset;
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCGameListAtServer method playerGamesMinVersion.
/**
* For the games this player is in, what's the
* minimum required client version?
* Checks {@link SOCGame#getClientVersionMinRequired()}.
*<P>
* This method helps determine if a client's connection can be
* "taken over" after a network problem. It synchronizes on <tt>gameData</tt>.
*
* @param plConn the previous connection of the player, which might be taken over
* @return Minimum version, in same format as {@link SOCGame#getClientVersionMinRequired()},
* or 0 if player isn't in any games.
* @since 1.1.08
*/
public int playerGamesMinVersion(Connection plConn) {
int minVers = 0;
synchronized (gameData) {
for (SOCGame ga : getGamesData()) {
Vector<Connection> members = getMembers(ga.getName());
if ((members == null) || !members.contains(plConn))
continue;
// plConn is a member of this game.
int vers = ga.getClientVersionMinRequired();
if (vers > minVers)
minVers = vers;
}
}
return minVers;
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCGameListAtServer method removeMember.
/**
* remove member from the game.
* Also updates game's client version range, with remaining connected members.
* Please call {@link #takeMonitorForGame(String)} before calling this.
*
* @param gaName the name of the game
* @param conn the member's connection
*/
public synchronized void removeMember(Connection conn, String gaName) {
Vector<Connection> members = getMembers(gaName);
if ((members != null)) {
members.removeElement(conn);
// Check version of remaining members
if (!members.isEmpty()) {
Connection c = members.firstElement();
int lowVers = c.getVersion();
int highVers = lowVers;
for (int i = members.size() - 1; i >= 1; --i) {
c = members.elementAt(i);
int v = c.getVersion();
if (v < lowVers)
lowVers = v;
if (v > highVers)
highVers = v;
}
SOCGame ga = getGameData(gaName);
ga.clientVersionLowest = lowVers;
ga.clientVersionHighest = highVers;
ga.hasOldClients = (lowVers < Version.versionNumber());
}
}
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCServerMessageHandler method handleCHANGEFACE.
// / General messages during a game ///
/**
* handle "change face" message.
*
* @param c the connection
* @param mes the message
* @since 1.0.0
*/
private void handleCHANGEFACE(Connection c, final SOCChangeFace mes) {
final String gaName = mes.getGame();
final SOCGame ga = gameList.getGameData(gaName);
if (ga == null)
return;
SOCPlayer player = ga.getPlayer(c.getData());
if (player == null)
return;
final int id = mes.getFaceId();
if ((id <= 0) && !player.isRobot())
// only bots should use bot icons
return;
player.setFaceId(id);
srv.messageToGame(gaName, new SOCChangeFace(gaName, player.getPlayerNumber(), id));
}
use of soc.game.SOCGame in project JSettlers2 by jdmonin.
the class SOCServerMessageHandler method handleLEAVEGAME_maybeGameReset_oldRobot.
/**
* Handle an unattached robot saying it is leaving the game,
* from {@link #handleLEAVEGAME(Connection, SOCLeaveGame)}.
* Ignore the robot (since it's not a member of the game) unless
* gamestate is {@link SOCGame#READY_RESET_WAIT_ROBOT_DISMISS}.
*
* @since 1.1.07
*/
private void handleLEAVEGAME_maybeGameReset_oldRobot(final String gaName) {
SOCGame cg = gameList.getGameData(gaName);
if (cg.getGameState() != SOCGame.READY_RESET_WAIT_ROBOT_DISMISS)
return;
boolean gameResetRobotsAllDismissed = false;
// TODO locks
SOCGameBoardReset gr = cg.boardResetOngoingInfo;
if (gr != null) {
--gr.oldRobotCount;
if (0 == gr.oldRobotCount)
gameResetRobotsAllDismissed = true;
}
if (gameResetRobotsAllDismissed)
// TODO locks?
srv.resetBoardAndNotify_finish(gr, cg);
}
Aggregations