Package net.starype.quiz.api.game
Class SimpleGame<T extends QuizGame>
- java.lang.Object
-
- net.starype.quiz.api.game.SimpleGame<T>
-
- 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 aQuizGame.An instance of
SimpleGameholds 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
ServerGateis not created internally, it must be provided either by the implementation, or externally via constructor parameter or the methodsetGate(ServerGate). For a completely 'finished' implementation, check outDefaultSimpleGame.
-
-
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 voidcheckEndOfRound(GameRound current)Require the game to check whether the current round is over.booleancontainsPlayerId(java.lang.Object id)Retrieve whether the game contains the player with the given ID.voidforceStop()Force-stop the game, shutting down all communications with players.protected java.util.Collection<? extends Player<?>>getPlayers()booleanisCurrentRoundFinished()Determine whether the round currently in action is finished and should thus be skipped.booleanisWaitingForNextRound()booleannextRound()Skip the current round (usually when over) and start the following one.voidonInputReceived(java.lang.Object playerId, java.lang.String message)Main communication gate between the players and the game logic.voidpause()Pause the game.voidremovePlayer(java.lang.Object playerId)Remove a player of the player list, if presentvoidresume()Resume the game after it was paused.voidsendInputToServer(java.util.function.Consumer<GameServer<?>> action)Main communication gate between the game object and aGameServer.voidsetGate(ServerGate<T> gate)Set the server gate for the game object.voidstart()Start the game.voidupdate()Update the game object, allowing the latter to perform periodic actions such as even triggering, if needed.
-
-
-
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:QuizGameStart the game. No interaction with the game object should be allowed unless a unique call to `start` was previously made.
-
isCurrentRoundFinished
public boolean isCurrentRoundFinished()
Description copied from interface:QuizGameDetermine whether the round currently in action is finished and should thus be skipped.- Specified by:
isCurrentRoundFinishedin interfaceQuizGame- Returns:
- whether the current round is over
-
nextRound
public boolean nextRound()
Description copied from interface:QuizGameSkip the current round (usually when over) and start the following one.
-
onInputReceived
public void onInputReceived(java.lang.Object playerId, java.lang.String message)Description copied from interface:QuizGameMain 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:
onInputReceivedin interfaceQuizGame- Parameters:
playerId- the ID of the player who sent a messagemessage- the message provided
-
checkEndOfRound
public void checkEndOfRound(GameRound current)
Description copied from interface:QuizGameRequire 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:
checkEndOfRoundin interfaceQuizGame- Parameters:
current- the current round
-
sendInputToServer
public void sendInputToServer(java.util.function.Consumer<GameServer<?>> action)
Description copied from interface:QuizGameMain communication gate between the game object and aGameServer. 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 ofQuizGameuses `ServerGate`s, as an additional layer for game-server communication.- Specified by:
sendInputToServerin interfaceQuizGame- Parameters:
action- the action to perform from a `GameServer` object- See Also:
ServerGate
-
pause
public void pause()
Description copied from interface:QuizGamePause the game. The common behavior of this method should be to ignore all communications from players untilQuizGame.resume()is called.
-
resume
public void resume()
Description copied from interface:QuizGameResume the game after it was paused. If the game wasn't paused, the method has no effect
-
forceStop
public void forceStop()
Description copied from interface:QuizGameForce-stop the game, shutting down all communications with players. Assuming the default game system, quiz games should naturally stop when all rounds are played
-
update
public void update()
Description copied from interface:QuizGameUpdate 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
-
containsPlayerId
public boolean containsPlayerId(java.lang.Object id)
Description copied from interface:QuizGameRetrieve whether the game contains the player with the given ID.- Specified by:
containsPlayerIdin interfaceQuizGame- 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:QuizGameRemove a player of the player list, if present- Specified by:
removePlayerin interfaceQuizGame- Parameters:
playerId- the ID of the player that is to be removed
-
getPlayers
protected java.util.Collection<? extends Player<?>> getPlayers()
-
isWaitingForNextRound
public boolean isWaitingForNextRound()
-
-