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
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 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 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 presentvoid
resume()
Resume the game after it was paused.void
sendInputToServer(java.util.function.Consumer<GameServer<?>> action)
Main communication gate between the game object and aGameServer
.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.
-
-
-
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.
-
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 interfaceQuizGame
- 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.
-
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 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: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 interfaceQuizGame
- 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 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 ofQuizGame
uses `ServerGate`s, as an additional layer for game-server communication.- Specified by:
sendInputToServer
in interfaceQuizGame
- 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 untilQuizGame.resume()
is called.
-
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
-
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
-
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
-
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 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:QuizGame
Remove a player of the player list, if present- Specified by:
removePlayer
in 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()
-
-