Package net.sf.freecol.client
Class FreeColClient
- java.lang.Object
-
- net.sf.freecol.client.FreeColClient
-
public final class FreeColClient extends java.lang.Object
The 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 ActionManager
actionManager
The encapsulation of the actions.private FreeColServer.ServerState
cachedServerState
Cached value of server state.private java.util.List<java.lang.String>
cachedVacantPlayerNames
Cached list of vacant players.private ClientOptions
clientOptions
The client options specific to this player.private ConnectController
connectController
private FreeColServer
freeColServer
The server that has been started from the client-GUI.private Game
game
The game itself.private GUI
gui
The GUI encapsulation.private boolean
inGame
Indicates if the game has started, has nothing to do with whether or not the client is logged in.private InGameController
inGameController
private ClientInputHandler
inputHandler
private boolean
loggedIn
Indicated whether or not there is an open connection to the server.private static java.util.logging.Logger
logger
private boolean
mapEditor
Are we using the map editor?private MapEditorController
mapEditorController
private Player
player
The player that `owns' this client.private PreGameController
preGameController
private ServerAPI
serverAPI
Encapsulation of the server API.private boolean
singlePlayer
Is this a single player game?private SoundController
soundController
-
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 void
addSpecificationActions(Specification specification)
Sets the actions derived from the specification.ServerAPI
askServer()
Meaningfully named access to the ServerAPI.void
askToQuit()
Quits the application.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 disabledvoid
changeClientState(boolean inGame)
Change the client in-game state (either in or pre-game).void
continuePlaying()
Continue playing after winning the game.boolean
currentPlayerIsMyPlayer()
Is the current player the client owner player?private FreeColServer
failToMain(java.lang.Exception ex, java.lang.String key)
Fail to start a server due to an exception.private FreeColServer
failToMain(java.lang.Exception ex, StringTemplate template)
Fail to start a server due to an exception.ActionManager
getActionManager()
Gets the action manager.int
getAnimationSpeed(Player player)
Common utility routine to retrieve animation speed.ClientOptions
getClientOptions()
Gets the object keeping the current client options.ConnectController
getConnectController()
Gets the controller responsible for starting a server and connecting to it.FreeColServer
getFreeColServer()
Gets theFreeColServer
started by the client.Game
getGame()
Gets theGame
that we are currently playing.GUI
getGUI()
Gets the GUI attached to this client.InGameController
getInGameController()
Gets the controller that will be used when the game has been started.MapEditorController
getMapEditorController()
Gets the controller for the map editor, if we are in the map editor.Player
getMyPlayer()
Gets thePlayer
that uses this client.PreGameController
getPreGameController()
Gets the controller that will be used before the game has been started.FreeColServer.ServerState
getServerState()
Get the server state, or at least our most recently cached value.boolean
getSinglePlayer()
Is the user playing in single player mode?SoundController
getSoundController()
Gets the controller for the sound.java.util.List<java.lang.String>
getVacantPlayerNames()
Get the cached list of vacant player names.boolean
isAdmin()
Checks if this client is the game admin.boolean
isInGame()
Has the game started?boolean
isLoggedIn()
Is this client logged in to a server?boolean
isMapEditor()
Are we using the map editor?boolean
isReadyToStart()
Is the client ready to switch to in-game mode?private ClientOptions
loadClientOptions(java.io.File savedGameFile)
Loads the client options.void
login(boolean inGame, Game game, Player player, boolean single)
Log in to a new game.void
logout(boolean inGame)
Log this client out.void
quit()
Quits the application without any questions.void
restoreGUI(Player player)
Set up the GUI after the game starts or a player reconnects.void
retire()
Retire from the game.void
setFreeColServer(FreeColServer freeColServer)
Sets theFreeColServer
which has been started by the client gui.void
setGame(Game game)
Sets theGame
that we are currently playing.void
setMapEditor(boolean mapEditor)
Sets the map editor state.void
setMessageHandler(MessageHandler messageHandler)
Set a message handler to handle messages from the server.void
setMyPlayer(Player player)
Sets thePlayer
that uses this client.void
setServerState(FreeColServer.ServerState state)
Set the cached server state.void
setSinglePlayer(boolean singlePlayer)
Sets whether or not this game is a single player game.void
setVacantPlayerNames(java.util.List<java.lang.String> names)
Set the cached list of vacant player names.void
skipTurns(int turns)
Start the game skipping turns.private void
startFirstTaskInGui(java.lang.String userMsg, boolean showOpeningVideo, java.io.File savedGame, Specification spec)
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.FreeColServer
startServer(boolean publicServer, boolean singlePlayer, Specification spec, java.net.InetAddress address, int port)
Start a server.void
stopServer()
Stop a server if present.void
toggleClientOption(java.lang.String op)
Toggle the value of a boolean client option.boolean
tutorialMode()
Convenience accessor for checking whether to display tutorial messages.boolean
unblockServer(int port)
Shut down an existing server on a given port.void
updateActions()
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, aSpecification
to 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 gameFile
to 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 theFreeColServer
started by the client.- Returns:
- The
FreeColServer
ornull
if no server has been started.
-
setFreeColServer
public void setFreeColServer(FreeColServer freeColServer)
Sets theFreeColServer
which 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 theGame
that we are currently playing.- Returns:
- The current
Game
. - See Also:
setGame(net.sf.freecol.common.model.Game)
-
setGame
public void setGame(Game game)
Sets theGame
that we are currently playing.- Parameters:
game
- The newGame
.- See Also:
getGame()
-
getMyPlayer
public Player getMyPlayer()
Gets thePlayer
that uses this client.- Returns:
- The
Player
made to represent this clients user. - See Also:
setMyPlayer(Player)
-
setMyPlayer
public void setMyPlayer(Player player)
Sets thePlayer
that uses this client.- Parameters:
player
- ThePlayer
made to represent this clients user.- See Also:
getMyPlayer()
-
getClientOptions
public ClientOptions getClientOptions()
Gets the object keeping the current client options.- Returns:
- The
ClientOptions
attached 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
- TheSpecification
to 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
- ThePlayer
to 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
- TheException
that causes the trouble.template
- AStringTemplate
with 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
- TheException
that 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
- TheSpecification
to 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
FreeColServer
or 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.
-
-