Cute Chess  0.1
Classes | Public Types | Public Slots | Signals | Public Member Functions | List of all members
GameManager Class Reference

A class for managing chess games and players. More...

#include <gamemanager.h>

Inheritance diagram for GameManager:
QObject

Public Types

enum  CleanupMode { DeletePlayers, ReusePlayers }
 
enum  StartMode { StartImmediately, Enqueue }
 

Public Slots

void finish ()
 

Signals

void debugMessage (const QString &data)
 
void finished ()
 
void gameDestroyed (ChessGame *game)
 
void gameStarted (ChessGame *game)
 
void ready ()
 

Public Member Functions

QList< ChessGame * > activeGames () const
 
void cleanupIdleThreads ()
 
int concurrency () const
 
 GameManager (QObject *parent=nullptr)
 
void newGame (ChessGame *game, const PlayerBuilder *white, const PlayerBuilder *black, StartMode startMode=StartImmediately, CleanupMode cleanupMode=DeletePlayers)
 
void setConcurrency (int concurrency)
 

Detailed Description

A class for managing chess games and players.

GameManager can start games in a new thread, run multiple games concurrently, and queue games to be run when a game slot/thread is free.

See also
ChessGame, PlayerBuilder

Member Enumeration Documentation

The mode for cleaning up after deleted games.

Enumerator
DeletePlayers 

The players and their builder objects are deleted when the game object is deleted.

ReusePlayers 

The players are left alive after the game is deleted. If a new game with the same builder objects is started, the players are reused for that game.

The mode for starting games.

Enumerator
StartImmediately 

The game is started immediately.

Enqueue 

The game is added to a queue, and is started when a game slot becomes free. This could be immediately.

Constructor & Destructor Documentation

GameManager::GameManager ( QObject parent = nullptr)

Creates a new game manager.

Member Function Documentation

QList< ChessGame * > GameManager::activeGames ( ) const

Returns the list of active games.

Any game that was started (ie. isn't waiting in the queue) is considered active even if the game has ended. This is because the game could still be used for analysis, etc. The game loses its active status only when it's deleted.

void GameManager::cleanupIdleThreads ( )

Cleans up and deletes all idle game threads

This function cleans up and removes all resources used by game threads that are waiting for new games. The resources include the players and the thread they're living in. The PlayerBuilder objects will not be deleted.

Generally this function should be called after a tournament has ended.

int GameManager::concurrency ( ) const

Returns the maximum allowed number of concurrent games.

The concurrency limit only affects games that are started in Enqueue mode.

See also
setConcurrency()
void GameManager::debugMessage ( const QString data)
signal

This signal redirects the ChessPlayer::debugMessage() signal.

void GameManager::finish ( )
slot

Removes all future games from the queue, waits for ongoing games to end, and deletes all idle players. Emits the finished() signal when done.

void GameManager::finished ( )
signal

This signal is emitted when all games have ended and all idle players have been deleted. Then the manager can be safely deleted.

void GameManager::gameDestroyed ( ChessGame game)
signal

This signal is emitted when a game is destroyed.

Dereferencing the game pointer results in undefined behavior, so this signal should only be used for cleanup.

void GameManager::gameStarted ( ChessGame game)
signal

This signal is emitted when a new game starts.

void GameManager::newGame ( ChessGame game,
const PlayerBuilder white,
const PlayerBuilder black,
StartMode  startMode = StartImmediately,
CleanupMode  cleanupMode = DeletePlayers 
)

Adds a new game to the game manager.

This function gives control of game to the game manager, which moves the game to its own thread and starts it. Because the game is played in a separate thread, it must not have a parent object. When it's not needed anymore, it must be destroyed with the deleteLater() method. Only then will the game manager free the game slot used by the game.

Construction of the players is delayed to the moment when the game starts. If the same builder objects (white and black) were used in a previous game, the players are reused instead of constructing new players.

If mode is StartImmediately, the game starts immediately even if the number of active games is over the concurrency limit. In Enqueue mode the game is started as soon as a free game slot is available.

cleanupMode determines whether the players and their builder objects are destroyed or reused after the game.

If the game cannot be started because one or both of the players can't be initialized, game will emit the startFailed() signal.

Note
If there are still free game slots after starting this game, the ready() signal is emitted immediately.
void GameManager::ready ( )
signal

This signal is emitted after a game has started or after a game has ended, if there are free game slots.

Note
The signal is NOT emitted if a newly freed game slot can be used by a game that was waiting in the queue.
void GameManager::setConcurrency ( int  concurrency)

Sets the concurrency limit to concurrency.

See also
concurrency()

The documentation for this class was generated from the following files: