Package net.sf.freecol.client
Class FreeColClient
- java.lang.Object
-
- net.sf.freecol.client.FreeColClient
-
public final class FreeColClient extends java.lang.ObjectThe main control class for the FreeCol client. This class both starts and keeps references to the GUI and the control objects.
-
-
Field Summary
Fields Modifier and Type Field Description private ActionManageractionManagerThe encapsulation of the actions.private FreeColServer.ServerStatecachedServerStateCached value of server state.private java.util.List<java.lang.String>cachedVacantPlayerNamesCached list of vacant players.private ClientOptionsclientOptionsThe client options specific to this player.private ConnectControllerconnectControllerprivate FreeColServerfreeColServerThe server that has been started from the client-GUI.private GamegameThe game itself.private GUIguiThe GUI encapsulation.private booleaninGameIndicates if the game has started, has nothing to do with whether or not the client is logged in.private InGameControllerinGameControllerprivate ClientInputHandlerinputHandlerprivate booleanloggedInIndicated whether or not there is an open connection to the server.private static java.util.logging.Loggerloggerprivate booleanmapEditorAre we using the map editor?private MapEditorControllermapEditorControllerprivate PlayerplayerThe player that `owns' this client.private PreGameControllerpreGameControllerprivate ServerAPIserverAPIEncapsulation of the server API.private booleansinglePlayerIs this a single player game?private SoundControllersoundController
-
Constructor Summary
Constructors Constructor Description FreeColClient(SplashScreen splashScreen, java.lang.String fontName, java.awt.Dimension windowSize, java.lang.String userMsg, boolean sound, boolean showOpeningVideo, java.io.File savedGame, Specification spec)Creates a newFreeColClient.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSpecificationActions(Specification specification)Sets the actions derived from the specification.ServerAPIaskServer()Meaningfully named access to the ServerAPI.voidaskToQuit()Quits the application.booleancanSaveCurrentGame()Verifies if this client can save the current game Clients that do not have the server running, or that have not the required permissions cannot save and should have the menu entry disabledvoidchangeClientState(boolean inGame)Change the client in-game state (either in or pre-game).voidcontinuePlaying()Continue playing after winning the game.booleancurrentPlayerIsMyPlayer()Is the current player the client owner player?private FreeColServerfailToMain(java.lang.Exception ex, java.lang.String key)Fail to start a server due to an exception.private FreeColServerfailToMain(java.lang.Exception ex, StringTemplate template)Fail to start a server due to an exception.ActionManagergetActionManager()Gets the action manager.intgetAnimationSpeed(Player player)Common utility routine to retrieve animation speed.ClientOptionsgetClientOptions()Gets the object keeping the current client options.ConnectControllergetConnectController()Gets the controller responsible for starting a server and connecting to it.FreeColServergetFreeColServer()Gets theFreeColServerstarted by the client.GamegetGame()Gets theGamethat we are currently playing.GUIgetGUI()Gets the GUI attached to this client.InGameControllergetInGameController()Gets the controller that will be used when the game has been started.MapEditorControllergetMapEditorController()Gets the controller for the map editor, if we are in the map editor.PlayergetMyPlayer()Gets thePlayerthat uses this client.PreGameControllergetPreGameController()Gets the controller that will be used before the game has been started.FreeColServer.ServerStategetServerState()Get the server state, or at least our most recently cached value.booleangetSinglePlayer()Is the user playing in single player mode?SoundControllergetSoundController()Gets the controller for the sound.java.util.List<java.lang.String>getVacantPlayerNames()Get the cached list of vacant player names.booleanisAdmin()Checks if this client is the game admin.booleanisInGame()Has the game started?booleanisLoggedIn()Is this client logged in to a server?booleanisMapEditor()Are we using the map editor?booleanisReadyToStart()Is the client ready to switch to in-game mode?private ClientOptionsloadClientOptions(java.io.File savedGameFile)Loads the client options.voidlogin(boolean inGame, Game game, Player player, boolean single)Log in to a new game.voidlogout(boolean inGame)Log this client out.voidquit()Quits the application without any questions.voidrestoreGUI(Player player)Set up the GUI after the game starts or a player reconnects.voidretire()Retire from the game.voidsetFreeColServer(FreeColServer freeColServer)Sets theFreeColServerwhich has been started by the client gui.voidsetGame(Game game)Sets theGamethat we are currently playing.voidsetMapEditor(boolean mapEditor)Sets the map editor state.voidsetMessageHandler(MessageHandler messageHandler)Set a message handler to handle messages from the server.voidsetMyPlayer(Player player)Sets thePlayerthat uses this client.voidsetServerState(FreeColServer.ServerState state)Set the cached server state.voidsetSinglePlayer(boolean singlePlayer)Sets whether or not this game is a single player game.voidsetVacantPlayerNames(java.util.List<java.lang.String> names)Set the cached list of vacant player names.voidskipTurns(int turns)Start the game skipping turns.private voidstartFirstTaskInGui(java.lang.String userMsg, boolean showOpeningVideo, java.io.File savedGame, Specification spec)FreeColServerstartServer(boolean publicServer, boolean singlePlayer, java.io.File saveFile, java.net.InetAddress address, int port, java.lang.String name)Start a server with a saved game.FreeColServerstartServer(boolean publicServer, boolean singlePlayer, Specification spec, java.net.InetAddress address, int port)Start a server.voidstopServer()Stop a server if present.voidtoggleClientOption(java.lang.String op)Toggle the value of a boolean client option.booleantutorialMode()Convenience accessor for checking whether to display tutorial messages.booleanunblockServer(int port)Shut down an existing server on a given port.voidupdateActions()Updates the game actions.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
connectController
private final ConnectController connectController
-
preGameController
private final PreGameController preGameController
-
inGameController
private final InGameController inGameController
-
inputHandler
private final ClientInputHandler inputHandler
-
mapEditorController
private final MapEditorController mapEditorController
-
soundController
private SoundController soundController
-
freeColServer
private FreeColServer freeColServer
The server that has been started from the client-GUI.
-
serverAPI
private final ServerAPI serverAPI
Encapsulation of the server API.
-
gui
private GUI gui
The GUI encapsulation.
-
actionManager
private final ActionManager actionManager
The encapsulation of the actions.
-
game
private Game game
The game itself.
-
player
private Player player
The player that `owns' this client.
-
clientOptions
private ClientOptions clientOptions
The client options specific to this player.
-
inGame
private boolean inGame
Indicates if the game has started, has nothing to do with whether or not the client is logged in.
-
mapEditor
private boolean mapEditor
Are we using the map editor?
-
singlePlayer
private boolean singlePlayer
Is this a single player game?
-
loggedIn
private boolean loggedIn
Indicated whether or not there is an open connection to the server. This is not an indication of the existence of a Connection Object, but instead it is an indication of an approved login to a server.
-
cachedServerState
private FreeColServer.ServerState cachedServerState
Cached value of server state.
-
cachedVacantPlayerNames
private java.util.List<java.lang.String> cachedVacantPlayerNames
Cached list of vacant players.
-
-
Constructor Detail
-
FreeColClient
public FreeColClient(SplashScreen splashScreen, java.lang.String fontName, java.awt.Dimension windowSize, java.lang.String userMsg, boolean sound, boolean showOpeningVideo, java.io.File savedGame, Specification spec)
Creates a newFreeColClient. Creates the control objects.- Parameters:
splashScreen- An optional splash screen to display.fontName- An optional override of the main font.windowSize- An optional window size.userMsg- An optional message key to be displayed early.sound- True if sounds should be playedshowOpeningVideo- Display the opening video.savedGame- An optional saved game.spec- If non-null, aSpecificationto use to start a new game immediately.
-
-
Method Detail
-
startFirstTaskInGui
private void startFirstTaskInGui(java.lang.String userMsg, boolean showOpeningVideo, java.io.File savedGame, Specification spec)
-
loadClientOptions
private ClientOptions loadClientOptions(java.io.File savedGameFile)
Loads the client options. There are several sources: 1) Base options (data/base/client-options.xml) 2) Standard action manager actions 3) Saved game 4) User options The base and action manager options are definitive, so they can just be added/loaded. The others are from sources that may be out of date (i.e. options can be in the wrong group, or no longer exist), so they must be merged cautiously.- Parameters:
savedGameFile- An optional saved gameFileto load options from.- Returns:
- The loaded
ClientOptions.
-
getConnectController
public ConnectController getConnectController()
Gets the controller responsible for starting a server and connecting to it.- Returns:
- The
ConnectController.
-
getPreGameController
public PreGameController getPreGameController()
Gets the controller that will be used before the game has been started.- Returns:
- The
PreGameController.
-
getInGameController
public InGameController getInGameController()
Gets the controller that will be used when the game has been started.- Returns:
- The
InGameController.
-
getMapEditorController
public MapEditorController getMapEditorController()
Gets the controller for the map editor, if we are in the map editor.- Returns:
- The map editor controller, if any.
-
getSoundController
public SoundController getSoundController()
Gets the controller for the sound.- Returns:
- The sound controller, if any.
-
getFreeColServer
public FreeColServer getFreeColServer()
Gets theFreeColServerstarted by the client.- Returns:
- The
FreeColServerornullif no server has been started.
-
setFreeColServer
public void setFreeColServer(FreeColServer freeColServer)
Sets theFreeColServerwhich has been started by the client gui.- Parameters:
freeColServer- TheFreeColServer.- See Also:
getFreeColServer()
-
askServer
public ServerAPI askServer()
Meaningfully named access to the ServerAPI.- Returns:
- The user wrapper for the
ServerAPI.
-
getGUI
public GUI getGUI()
Gets the GUI attached to this client.- Returns:
- The current
GUI.
-
getActionManager
public ActionManager getActionManager()
Gets the action manager.- Returns:
- The action manager.
-
getGame
public Game getGame()
Gets theGamethat we are currently playing.- Returns:
- The current
Game. - See Also:
setGame(net.sf.freecol.common.model.Game)
-
setGame
public void setGame(Game game)
Sets theGamethat we are currently playing.- Parameters:
game- The newGame.- See Also:
getGame()
-
getMyPlayer
public Player getMyPlayer()
Gets thePlayerthat uses this client.- Returns:
- The
Playermade to represent this clients user. - See Also:
setMyPlayer(Player)
-
setMyPlayer
public void setMyPlayer(Player player)
Sets thePlayerthat uses this client.- Parameters:
player- ThePlayermade to represent this clients user.- See Also:
getMyPlayer()
-
getClientOptions
public ClientOptions getClientOptions()
Gets the object keeping the current client options.- Returns:
- The
ClientOptionsattached to thisFreeColClient.
-
toggleClientOption
public void toggleClientOption(java.lang.String op)
Toggle the value of a boolean client option.- Parameters:
op- The name of the option to toggle.
-
tutorialMode
public boolean tutorialMode()
Convenience accessor for checking whether to display tutorial messages.- Returns:
- True if tutorial messages should be displayed.
-
isInGame
public boolean isInGame()
Has the game started?- Returns:
- True if the game has started.
-
changeClientState
public void changeClientState(boolean inGame)
Change the client in-game state (either in or pre-game).- Parameters:
inGame- If true, change to in-game state.
-
isReadyToStart
public boolean isReadyToStart()
Is the client ready to switch to in-game mode?- Returns:
- True if in pre-game mode, the game is present, and it has a map.
-
isMapEditor
public boolean isMapEditor()
Are we using the map editor?- Returns:
- True if the map editor is enabled.
-
setMapEditor
public void setMapEditor(boolean mapEditor)
Sets the map editor state.- Parameters:
mapEditor- True if the map editor is enabled.
-
getSinglePlayer
public boolean getSinglePlayer()
Is the user playing in single player mode?- Returns:
- True if the user is playing in single player mode.
- See Also:
setSinglePlayer(boolean)
-
setSinglePlayer
public void setSinglePlayer(boolean singlePlayer)
Sets whether or not this game is a single player game.- Parameters:
singlePlayer- Whether or not this game is a single player game.
-
isLoggedIn
public boolean isLoggedIn()
Is this client logged in to a server?- Returns:
- True if this client is logged in to a server.
-
getServerState
public FreeColServer.ServerState getServerState()
Get the server state, or at least our most recently cached value.- Returns:
- A server state.
-
setServerState
public void setServerState(FreeColServer.ServerState state)
Set the cached server state.- Parameters:
state- The newServerState.
-
getVacantPlayerNames
public java.util.List<java.lang.String> getVacantPlayerNames()
Get the cached list of vacant player names.- Returns:
- A list of available player names.
-
setVacantPlayerNames
public void setVacantPlayerNames(java.util.List<java.lang.String> names)
Set the cached list of vacant player names.- Parameters:
names- The new vacant player names.
-
setMessageHandler
public void setMessageHandler(MessageHandler messageHandler)
Set a message handler to handle messages from the server. Used when switching from pre-game to in-game.- Parameters:
messageHandler- The newMessageHandler.
-
updateActions
public void updateActions()
Updates the game actions. Generally useful when menu actions should change due to the current game context.
-
addSpecificationActions
public void addSpecificationActions(Specification specification)
Sets the actions derived from the specification.- Parameters:
specification- TheSpecificationto use.
-
isAdmin
public boolean isAdmin()
Checks if this client is the game admin.- Returns:
- True if the client is the game admin and the game has begun.
-
canSaveCurrentGame
public boolean canSaveCurrentGame()
Verifies if this client can save the current game Clients that do not have the server running, or that have not the required permissions cannot save and should have the menu entry disabled- Returns:
- True if this client can save the game in progress.
-
currentPlayerIsMyPlayer
public boolean currentPlayerIsMyPlayer()
Is the current player the client owner player?- Returns:
- True if the current player is owned by this client.
-
getAnimationSpeed
public int getAnimationSpeed(Player player)
Common utility routine to retrieve animation speed.- Parameters:
player- ThePlayerto be animated.- Returns:
- The animation speed.
-
restoreGUI
public void restoreGUI(Player player)
Set up the GUI after the game starts or a player reconnects.- Parameters:
player- The clientPlayer.
-
failToMain
private FreeColServer failToMain(java.lang.Exception ex, StringTemplate template)
Fail to start a server due to an exception. Complain and fail back to the main panel.- Parameters:
ex- TheExceptionthat causes the trouble.template- AStringTemplatewith the error message.- Returns:
- Null.
-
failToMain
private FreeColServer failToMain(java.lang.Exception ex, java.lang.String key)
Fail to start a server due to an exception. Complain and fail back to the main panel.- Parameters:
ex- TheExceptionthat causes the trouble.key- A message key with the error.- Returns:
- Null.
-
unblockServer
public boolean unblockServer(int port)
Shut down an existing server on a given port.- Parameters:
port- The port to unblock.- Returns:
- True if there should be no blocking server remaining.
-
stopServer
public void stopServer()
Stop a server if present. Public for FreeColClient.quit and showMain.
-
startServer
public FreeColServer startServer(boolean publicServer, boolean singlePlayer, Specification spec, java.net.InetAddress address, int port)
Start a server.- Parameters:
publicServer- If true, add to the meta-server.singlePlayer- True if this is a single player game.spec- TheSpecificationto use in this game.address- The address to use for the public socket.port- The TCP port to use for the public socket. If null, try ports until- Returns:
- A new
FreeColServeror null on error.
-
startServer
public FreeColServer startServer(boolean publicServer, boolean singlePlayer, java.io.File saveFile, java.net.InetAddress address, int port, java.lang.String name)
Start a server with a saved game.- Parameters:
publicServer- If true, add to the meta-server.singlePlayer- True if this is a single player game.saveFile- The saved gameFile.address- The TCP address to use for the public socket.port- The TCP port to use for the public socket.name- An optional name for the server.- Returns:
- A new
FreeColServer, or null on error.
-
login
public void login(boolean inGame, Game game, Player player, boolean single)Log in to a new game. Called when the ConnectController processes a login.- Parameters:
inGame- Whether the server is in-game.game- The newGame.player- The new clientPlayer.single- True if this is a single player game.
-
logout
public void logout(boolean inGame)
Log this client out. Called when the ConnectController processes a logout.- Parameters:
inGame- Whether the server is in-game.
-
continuePlaying
public void continuePlaying()
Continue playing after winning the game.
-
skipTurns
public void skipTurns(int turns)
Start the game skipping turns.- Parameters:
turns- The number of turns to skip.
-
askToQuit
public void askToQuit()
Quits the application.
-
retire
public void retire()
Retire from the game.
-
quit
public void quit()
Quits the application without any questions.
-
-