Class SimpleGame<T extends QuizGame>

  • Type Parameters:
    T - the type of QuizGame the server gate works with.
    All Implemented Interfaces:
    QuizGame
    Direct Known Subclasses:
    DefaultSimpleGame

    public class SimpleGame<T extends QuizGame>
    extends java.lang.Object
    implements QuizGame
    Main implementation of the logic of a QuizGame.

    An instance of SimpleGame holds a queue of game rounds that will be played one after the other, as well as a mutable player list representing the players who participate in the game.

    Since the class was designed to be extended by custom implementations, the ServerGate is not created internally, it must be provided either by the implementation, or externally via constructor parameter or the method setGate(ServerGate). For a completely 'finished' implementation, check out DefaultSimpleGame.

    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleGame​(java.util.Queue<? extends GameRound> rounds, java.util.Collection<? extends Player<?>> players)  
      SimpleGame​(java.util.Queue<? extends GameRound> rounds, java.util.Collection<? extends Player<?>> players, ServerGate<T> gate)  
    • Method Summary

      Modifier and Type Method Description
      void checkEndOfRound​(GameRound current)
      Require the game to check whether the current round is over.
      boolean containsPlayerId​(java.lang.Object id)
      Retrieve whether the game contains the player with the given ID.
      void forceStop()
      Force-stop the game, shutting down all communications with players.
      protected java.util.Collection<? extends Player<?>> getPlayers()  
      boolean isCurrentRoundFinished()
      Determine whether the round currently in action is finished and should thus be skipped.
      boolean isWaitingForNextRound()  
      boolean nextRound()
      Skip the current round (usually when over) and start the following one.
      void onInputReceived​(java.lang.Object playerId, java.lang.String message)
      Main communication gate between the players and the game logic.
      void pause()
      Pause the game.
      void removePlayer​(java.lang.Object playerId)
      Remove a player of the player list, if present
      void resume()
      Resume the game after it was paused.
      void sendInputToServer​(java.util.function.Consumer<GameServer<?>> action)
      Main communication gate between the game object and a GameServer.
      void setGate​(ServerGate<T> gate)
      Set the server gate for the game object.
      void start()
      Start the game.
      void update()
      Update the game object, allowing the latter to perform periodic actions such as even triggering, if needed.
      • Methods inherited from class java.lang.Object

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

      • SimpleGame

        public SimpleGame​(java.util.Queue<? extends GameRound> rounds,
                          java.util.Collection<? extends Player<?>> players)
      • SimpleGame

        public SimpleGame​(java.util.Queue<? extends GameRound> rounds,
                          java.util.Collection<? extends Player<?>> players,
                          ServerGate<T> gate)
    • Method Detail

      • setGate

        public void setGate​(ServerGate<T> gate)
        Set the server gate for the game object.
        Parameters:
        gate - the gate object that will allow interactions with the server
      • start

        public void start()
        Description copied from interface: QuizGame
        Start the game. No interaction with the game object should be allowed unless a unique call to `start` was previously made.
        Specified by:
        start in interface QuizGame
      • isCurrentRoundFinished

        public boolean isCurrentRoundFinished()
        Description copied from interface: QuizGame
        Determine whether the round currently in action is finished and should thus be skipped.
        Specified by:
        isCurrentRoundFinished in interface QuizGame
        Returns:
        whether the current round is over
      • nextRound

        public boolean nextRound()
        Description copied from interface: QuizGame
        Skip the current round (usually when over) and start the following one.
        Specified by:
        nextRound in interface QuizGame
        Returns:
        whether the operation was a success
      • onInputReceived

        public void onInputReceived​(java.lang.Object playerId,
                                    java.lang.String message)
        Description copied from interface: QuizGame
        Main communication gate between the players and the game logic. The game handler should call this method when a player sends whatever input that must be transmitted to the game. Whether the player was eligible for sending a message should be decided by the game itself, not beforehand.
        Specified by:
        onInputReceived in interface QuizGame
        Parameters:
        playerId - the ID of the player who sent a message
        message - the message provided
      • checkEndOfRound

        public void checkEndOfRound​(GameRound current)
        Description copied from interface: QuizGame
        Require the game to check whether the current round is over. Even though usually called internally, the method can also be called when an exterior factor may make the round terminate. Timers are a good example, since they should usually be handled externally.
        Specified by:
        checkEndOfRound in interface QuizGame
        Parameters:
        current - the current round
      • sendInputToServer

        public void sendInputToServer​(java.util.function.Consumer<GameServer<?>> action)
        Description copied from interface: QuizGame
        Main communication gate between the game object and a GameServer. This method is often used by the game rounds, who would like to notify the server that a particular event should be triggered. The default implementation of QuizGame uses `ServerGate`s, as an additional layer for game-server communication.
        Specified by:
        sendInputToServer in interface QuizGame
        Parameters:
        action - the action to perform from a `GameServer` object
        See Also:
        ServerGate
      • pause

        public void pause()
        Description copied from interface: QuizGame
        Pause the game. The common behavior of this method should be to ignore all communications from players until QuizGame.resume() is called.
        Specified by:
        pause in interface QuizGame
      • resume

        public void resume()
        Description copied from interface: QuizGame
        Resume the game after it was paused. If the game wasn't paused, the method has no effect
        Specified by:
        resume in interface QuizGame
      • forceStop

        public void forceStop()
        Description copied from interface: QuizGame
        Force-stop the game, shutting down all communications with players. Assuming the default game system, quiz games should naturally stop when all rounds are played
        Specified by:
        forceStop in interface QuizGame
      • update

        public void update()
        Description copied from interface: QuizGame
        Update the game object, allowing the latter to perform periodic actions such as even triggering, if needed. The call frequency of update is up to the game object handler
        Specified by:
        update in interface QuizGame
      • containsPlayerId

        public boolean containsPlayerId​(java.lang.Object id)
        Description copied from interface: QuizGame
        Retrieve whether the game contains the player with the given ID.
        Specified by:
        containsPlayerId in interface QuizGame
        Parameters:
        id - an object representing a player ID
        Returns:
        whether the object is present in the player list of the game
      • removePlayer

        public void removePlayer​(java.lang.Object playerId)
        Description copied from interface: QuizGame
        Remove a player of the player list, if present
        Specified by:
        removePlayer in interface QuizGame
        Parameters:
        playerId - the ID of the player that is to be removed
      • getPlayers

        protected java.util.Collection<? extends Player<?>> getPlayers()
      • isWaitingForNextRound

        public boolean isWaitingForNextRound()