mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
game inactivity timeout
This commit is contained in:
parent
a9f590e905
commit
5efb92e2d6
7 changed files with 30 additions and 3 deletions
|
|
@ -31,6 +31,9 @@ public:
|
|||
void closeOldSession(const QString &playerName);
|
||||
virtual QString getLoginMessage() const = 0;
|
||||
Server_Game *createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, Server_ProtocolHandler *creator);
|
||||
|
||||
virtual int getMaxGameInactivityTime() const = 0;
|
||||
virtual int getMaxPlayerInactivityTime() const = 0;
|
||||
private:
|
||||
QMap<int, Server_Game *> games;
|
||||
QList<Server_ProtocolHandler *> clients;
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@
|
|||
#include <QTimer>
|
||||
|
||||
Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, QObject *parent)
|
||||
: QObject(parent), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), activePlayer(-1), activePhase(-1), spectatorsAllowed(_spectatorsAllowed)
|
||||
: QObject(parent), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), activePlayer(-1), activePhase(-1), spectatorsAllowed(_spectatorsAllowed), inactivityCounter(0)
|
||||
{
|
||||
creator = addPlayer(_creator, false, false);
|
||||
|
||||
|
|
@ -55,12 +55,25 @@ void Server_Game::pingClockTimeout()
|
|||
QDateTime now = QDateTime::currentDateTime();
|
||||
QList<ServerInfo_PlayerPing *> pingList;
|
||||
QMapIterator<int, Server_Player *> playerIterator(players);
|
||||
bool allPlayersInactive = true;
|
||||
while (playerIterator.hasNext()) {
|
||||
Server_Player *player = playerIterator.next().value();
|
||||
int pingTime = player->getProtocolHandler() ? player->getProtocolHandler()->getLastCommandTime().secsTo(now) : -1;
|
||||
int pingTime;
|
||||
if (player->getProtocolHandler()) {
|
||||
pingTime = player->getProtocolHandler()->getLastCommandTime().secsTo(now);
|
||||
allPlayersInactive = false;
|
||||
} else
|
||||
pingTime = -1;
|
||||
pingList.append(new ServerInfo_PlayerPing(player->getPlayerId(), pingTime));
|
||||
}
|
||||
sendGameEvent(new Event_Ping(-1, pingList));
|
||||
|
||||
const int maxTime = static_cast<Server *>(parent())->getMaxGameInactivityTime();
|
||||
if (allPlayersInactive) {
|
||||
if ((++inactivityCounter >= maxTime) && (maxTime > 0))
|
||||
deleteLater();
|
||||
} else
|
||||
inactivityCounter = 0;
|
||||
}
|
||||
|
||||
int Server_Game::getPlayerCount() const
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ private:
|
|||
int maxPlayers;
|
||||
int activePlayer, activePhase;
|
||||
bool spectatorsAllowed;
|
||||
int inactivityCounter;
|
||||
QTimer *pingClock;
|
||||
signals:
|
||||
void gameClosing();
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@ void Server_ProtocolHandler::processCommand(Command *command)
|
|||
|
||||
void Server_ProtocolHandler::pingClockTimeout()
|
||||
{
|
||||
if (lastCommandTime.secsTo(QDateTime::currentDateTime()) > 10)
|
||||
if (lastCommandTime.secsTo(QDateTime::currentDateTime()) > server->getMaxPlayerInactivityTime())
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue