Class ServerGate<T extends QuizGame>

  • Type Parameters:
    T - the type of QuizGame the server works with

    public class ServerGate<T extends QuizGame>
    extends java.lang.Object
    An immutable connection between a server and a game object.

    The main purpose of ServerGate is to provide a simple way for any quiz game to update the server when needed. Server gates can be generated using GameServer.createGate(), then configured with withGame(QuizGame).

    Since the QuizGame object is the one containing the gate, in most cases it will configure the server with itself as the reference for the game object that will interact with the server in the future.

    However, it is sometimes useful to configure the gate with a different type of game than the one creating it (typically in an inheritance context), therefore game call backs should always use the lambda parameter provided (which is nothing but the configured object) instead of this. For instance, SimpleGame contains a default implementation for every required method, however, if one wishes to extend it for their own implementation, they might want to work with a GameServer<MySimpleGameExtension> rather than a GameServer<SimpleGame>. In this case, the gate should be created as a ServerGate<MySimpleGameExtension>. So it is important that SimpleGame does not use itself as a reference for game call backs (but rather use the lambda parameter), otherwise working with a ServerGate<MySimpleGameExtension> will not be possible.

    • Constructor Summary

      Constructors 
      Constructor Description
      ServerGate​(GameServer<? super T> server)
      Initialize a gate with the given server and no game.
      ServerGate​(GameServer<? super T> server, T game)
      Initialize a gate with a given server and a given game.
    • Method Summary

      Modifier and Type Method Description
      void callback​(java.util.function.Consumer<GameServer<?>> request)
      Notify the server with a given action that does not require any game object.
      void gameCallback​(java.util.function.BiConsumer<GameServer<? super T>,​T> request)
      Notify the server with a given action.
      ServerGate<T> withGame​(T game)
      Creates a copy of the gate with a specific game object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ServerGate

        public ServerGate​(GameServer<? super T> server)
        Initialize a gate with the given server and no game.

        Since server gates are immutable objects, instances created from this constructor will never used for call backs. Such objects are exclusively used with the idea of calling withGame(QuizGame) as soon as the game object is provided.

        Parameters:
        server - the game server that will receive updates from the game object
      • ServerGate

        public ServerGate​(GameServer<? super T> server,
                          T game)
        Initialize a gate with a given server and a given game.

        All interactions will be made between these two objects.

        Parameters:
        server - the game server object
        game - the game object
    • Method Detail

      • gameCallback

        public void gameCallback​(java.util.function.BiConsumer<GameServer<? super T>,​T> request)
        Notify the server with a given action.

        The action provided depends on a game object and a game server object that are both provided by the gate, since it was previously set up. External instances of ServerGate should never be used in stead of the lambda parameter. For more information, check the class documentation.

        Parameters:
        request - the action to perform depending on the game object and the server
      • callback

        public void callback​(java.util.function.Consumer<GameServer<?>> request)
        Notify the server with a given action that does not require any game object.

        This type of callback is more accessible, since it accepts all actions regardless of which type of game the server works with.

        Typically, when questions are released, the server does not require any game object, all it wants is the data of the question.

        Parameters:
        request - the action to perform depending on the server
      • withGame

        public ServerGate<T> withGame​(T game)
        Creates a copy of the gate with a specific game object.
        Parameters:
        game - the given game object
        Returns:
        the copy created