Class 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 Detail

      • logger

        private static final java.util.logging.Logger logger
      • 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.
      • 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 new FreeColClient. 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 played
        showOpeningVideo - Display the opening video.
        savedGame - An optional saved game.
        spec - If non-null, a Specification 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 game File 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 the FreeColServer started by the client.
        Returns:
        The FreeColServer or null if no server has been started.
      • setFreeColServer

        public void setFreeColServer​(FreeColServer freeColServer)
        Sets the FreeColServer which has been started by the client gui.
        Parameters:
        freeColServer - The FreeColServer.
        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.
      • setGame

        public void setGame​(Game game)
        Sets the Game that we are currently playing.
        Parameters:
        game - The new Game.
        See Also:
        getGame()
      • getMyPlayer

        public Player getMyPlayer()
        Gets the Player that uses this client.
        Returns:
        The Player made to represent this clients user.
        See Also:
        setMyPlayer(Player)
      • setMyPlayer

        public void setMyPlayer​(Player player)
        Sets the Player that uses this client.
        Parameters:
        player - The Player 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 this FreeColClient.
      • 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 new ServerState.
      • 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 new MessageHandler.
      • 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 - The Specification 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 - The Player 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 client Player.
      • 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 - The Exception that causes the trouble.
        template - A StringTemplate 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 - The Exception 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 - The Specification 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 game File.
        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 new Game.
        player - The new client Player.
        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.