[Server] Add game lifecycle strategy hook (#7130)
Some checks are pending
CodeQL / Analyze (cpp) (push) Waiting to run
CodeQL / Analyze (actions) (push) Waiting to run
Build Desktop / Configure (push) Waiting to run
Build Desktop / Debian 13 (push) Blocked by required conditions
Build Desktop / Debian 12 (push) Blocked by required conditions
Build Desktop / Fedora 44 (push) Blocked by required conditions
Build Desktop / Fedora 43 (push) Blocked by required conditions
Build Desktop / Servatrice_Debian 12 (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 (push) Blocked by required conditions
Build Desktop / Ubuntu 24.04 (push) Blocked by required conditions
Build Desktop / Arch (push) Blocked by required conditions
Build Desktop / macOS 15 (push) Blocked by required conditions
Build Desktop / macOS 13 Intel (push) Blocked by required conditions
Build Desktop / macOS 14 (push) Blocked by required conditions
Build Desktop / macOS 15 Debug (push) Blocked by required conditions
Build Desktop / Windows 10 (push) Blocked by required conditions
Build Docker Image / amd64 & arm64 (push) Waiting to run

Took 30 minutes

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-08-23 10:26:14 +02:00 committed by GitHub
parent 6b5105eecc
commit da924c2bd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 59 additions and 1 deletions

View file

@ -13,6 +13,7 @@ set(HEADERS
game/game_config.h game/game_config.h
game/server_deck_validation_strategy.h game/server_deck_validation_strategy.h
game/server_game.h game/server_game.h
game/server_game_lifecycle_strategy.h
game/server_player.h game/server_player.h
game/server_spectator.h game/server_spectator.h
server.h server.h

View file

@ -63,7 +63,7 @@ Server_Game::Server_Game(const GameConfig &config, Server_Room *_room)
startingLifeTotal(config.startingLifeTotal), shareDecklistsOnLoad(config.shareDecklistsOnLoad), startingLifeTotal(config.startingLifeTotal), shareDecklistsOnLoad(config.shareDecklistsOnLoad),
inactivityCounter(0), startTimeOfThisGame(0), secondsElapsed(0), firstGameStarted(false), inactivityCounter(0), startTimeOfThisGame(0), secondsElapsed(0), firstGameStarted(false),
turnOrderReversed(false), startTime(QDateTime::currentDateTime()), pingClock(nullptr), turnOrderReversed(false), startTime(QDateTime::currentDateTime()), pingClock(nullptr),
deckValidationStrategy(new Server_DefaultDeckValidationStrategy), gameMutex() lifecycleStrategy(new Server_DefaultLifecycleStrategy), gameMutex()
{ {
currentReplay = new GameReplay; currentReplay = new GameReplay;
currentReplay->set_replay_id(room->getServer()->getDatabaseInterface()->getNextReplayId()); currentReplay->set_replay_id(room->getServer()->getDatabaseInterface()->getNextReplayId());
@ -329,6 +329,11 @@ void Server_Game::doStartGameIfReady(bool forceStartGame)
} }
players = getPlayers(); // players could have been kicked, get new list of players players = getPlayers(); // players could have been kicked, get new list of players
if (lifecycleStrategy->onGameStarting(this) == Server_GameLifecycleStrategy::StartAction::Handled) {
locker.unlock();
return;
}
for (Server_AbstractPlayer *player : players.values()) { for (Server_AbstractPlayer *player : players.values()) {
player->setupZones(); player->setupZones();
} }

View file

@ -23,6 +23,7 @@
#include "../server_response_containers.h" #include "../server_response_containers.h"
#include "game_config.h" #include "game_config.h"
#include "server_deck_validation_strategy.h" #include "server_deck_validation_strategy.h"
#include "server_game_lifecycle_strategy.h"
#include <QDateTime> #include <QDateTime>
#include <QMap> #include <QMap>
@ -83,6 +84,8 @@ private:
QScopedPointer<Server_DeckValidationStrategy> deckValidationStrategy; QScopedPointer<Server_DeckValidationStrategy> deckValidationStrategy;
QScopedPointer<Server_GameLifecycleStrategy> lifecycleStrategy;
void createGameStateChangedEvent(Event_GameStateChanged *event, void createGameStateChangedEvent(Event_GameStateChanged *event,
Server_AbstractParticipant *recipient, Server_AbstractParticipant *recipient,
bool omniscient, bool omniscient,
@ -220,6 +223,12 @@ public:
} }
/** @brief Replace the deck validation strategy; takes ownership of @p strategy. */ /** @brief Replace the deck validation strategy; takes ownership of @p strategy. */
void setDeckValidationStrategy(Server_DeckValidationStrategy *strategy); void setDeckValidationStrategy(Server_DeckValidationStrategy *strategy);
/** @brief Get the current game lifecycle strategy (non-owning). */
Server_GameLifecycleStrategy *getLifecycleStrategy() const
{
return lifecycleStrategy.data();
}
}; };
#endif #endif

View file

@ -0,0 +1,43 @@
#ifndef SERVER_GAME_LIFECYCLE_STRATEGY_H
#define SERVER_GAME_LIFECYCLE_STRATEGY_H
class Server_Game;
/**
* @brief Strategy hook invoked around a game's lifecycle transitions.
*
* Subclasses can intercept the game start to perform custom setup (e.g. draft or
* tournament initialization); the default implementation never intercepts.
*/
class Server_GameLifecycleStrategy
{
public:
virtual ~Server_GameLifecycleStrategy() = default;
/** @brief How the game start should proceed after this hook returns. */
enum class StartAction
{
ProceedNormal, ///< Continue with the normal game start flow.
Handled, ///< The strategy handled the start; abort the normal flow.
};
/**
* @brief Called when a game is about to start.
* @return How the start flow should proceed.
*/
virtual StartAction onGameStarting(Server_Game *game) = 0;
};
/**
* @brief Default lifecycle strategy that never intercepts the game start.
*/
class Server_DefaultLifecycleStrategy : public Server_GameLifecycleStrategy
{
public:
StartAction onGameStarting(Server_Game *) override
{
return StartAction::ProceedNormal;
}
};
#endif