[Server] Consolidate Server_Game construction parameters into a GameConfig struct (#7128)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2026-08-16 19:13:03 +02:00 committed by GitHub
parent d99798111e
commit 7c134efd29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 72 additions and 45 deletions

View file

@ -10,6 +10,7 @@ set(HEADERS
game/server_card.h
game/server_cardzone.h
game/server_counter.h
game/game_config.h
game/server_game.h
game/server_player.h
game/server_spectator.h

View file

@ -0,0 +1,26 @@
#ifndef GAME_CONFIG_H
#define GAME_CONFIG_H
#include <QList>
#include <QString>
#include <libcockatrice/protocol/pb/serverinfo_user.pb.h>
struct GameConfig
{
ServerInfo_User creatorInfo;
int gameId = -1;
QString description;
QString password;
int maxPlayers = 2;
QList<int> gameTypes;
bool onlyBuddies = false;
bool onlyRegistered = false;
bool spectatorsAllowed = false;
bool spectatorsNeedPassword = false;
bool spectatorsCanTalk = true;
bool spectatorsSeeEverything = true;
int startingLifeTotal = 20;
bool shareDecklistsOnLoad = true;
};
#endif

View file

@ -53,34 +53,19 @@
#include <libcockatrice/protocol/pb/game_replay.pb.h>
#include <libcockatrice/utility/zone_names.h>
Server_Game::Server_Game(const ServerInfo_User &_creatorInfo,
int _gameId,
const QString &_description,
const QString &_password,
int _maxPlayers,
const QList<int> &_gameTypes,
bool _onlyBuddies,
bool _onlyRegistered,
bool _spectatorsAllowed,
bool _spectatorsNeedPassword,
bool _spectatorsCanTalk,
bool _spectatorsSeeEverything,
int _startingLifeTotal,
bool _shareDecklistsOnLoad,
Server_Room *_room)
: QObject(), room(_room), nextPlayerId(0), hostId(0), creatorInfo(new ServerInfo_User(_creatorInfo)),
gameStarted(false), gameClosed(false), gameId(_gameId), password(_password), maxPlayers(_maxPlayers),
gameTypes(_gameTypes), activePlayer(-1), activePhase(-1), onlyBuddies(_onlyBuddies),
onlyRegistered(_onlyRegistered), spectatorsAllowed(_spectatorsAllowed),
spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk),
spectatorsSeeEverything(_spectatorsSeeEverything), startingLifeTotal(_startingLifeTotal),
shareDecklistsOnLoad(_shareDecklistsOnLoad), inactivityCounter(0), startTimeOfThisGame(0), secondsElapsed(0),
firstGameStarted(false), turnOrderReversed(false), startTime(QDateTime::currentDateTime()), pingClock(nullptr),
gameMutex()
Server_Game::Server_Game(const GameConfig &config, Server_Room *_room)
: QObject(), room(_room), nextPlayerId(0), hostId(0), creatorInfo(new ServerInfo_User(config.creatorInfo)),
gameStarted(false), gameClosed(false), gameId(config.gameId), description(config.description.simplified()),
password(config.password), maxPlayers(config.maxPlayers), gameTypes(config.gameTypes), activePlayer(-1),
activePhase(-1), onlyBuddies(config.onlyBuddies), onlyRegistered(config.onlyRegistered),
spectatorsAllowed(config.spectatorsAllowed), spectatorsNeedPassword(config.spectatorsNeedPassword),
spectatorsCanTalk(config.spectatorsCanTalk), spectatorsSeeEverything(config.spectatorsSeeEverything),
startingLifeTotal(config.startingLifeTotal), shareDecklistsOnLoad(config.shareDecklistsOnLoad),
inactivityCounter(0), startTimeOfThisGame(0), secondsElapsed(0), firstGameStarted(false),
turnOrderReversed(false), startTime(QDateTime::currentDateTime()), pingClock(nullptr), gameMutex()
{
currentReplay = new GameReplay;
currentReplay->set_replay_id(room->getServer()->getDatabaseInterface()->getNextReplayId());
description = _description.simplified();
connect(this, &Server_Game::sigStartGameIfReady, this, &Server_Game::doStartGameIfReady, Qt::QueuedConnection);

View file

@ -21,6 +21,7 @@
#define SERVERGAME_H
#include "../server_response_containers.h"
#include "game_config.h"
#include <QDateTime>
#include <QMap>
@ -92,21 +93,7 @@ private slots:
public:
mutable QRecursiveMutex gameMutex;
Server_Game(const ServerInfo_User &_creatorInfo,
int _gameId,
const QString &_description,
const QString &_password,
int _maxPlayers,
const QList<int> &_gameTypes,
bool _onlyBuddies,
bool _onlyRegistered,
bool _spectatorsAllowed,
bool _spectatorsNeedPassword,
bool _spectatorsCanTalk,
bool _spectatorsSeeEverything,
int _startingLifeTotal,
bool _shareDecklistsOnLoad,
Server_Room *parent);
Server_Game(const GameConfig &config, Server_Room *parent);
~Server_Game() override;
Server_Room *getRoom() const
{

View file

@ -1,5 +1,6 @@
#include "server_protocolhandler.h"
#include "game/game_config.h"
#include "game/server_game.h"
#include "game/server_player.h"
#include "server_database_interface.h"
@ -920,10 +921,22 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room
// When server doesn't permit registered users to exist, do not honor only-reg setting
bool onlyRegisteredUsers = cmd.only_registered() && (server->permitUnregisteredUsers());
auto *game = new Server_Game(copyUserInfo(false), gameId, description, QString::fromStdString(cmd.password()),
cmd.max_players(), gameTypes, cmd.only_buddies(), onlyRegisteredUsers,
cmd.spectators_allowed(), cmd.spectators_need_password(), cmd.spectators_can_talk(),
cmd.spectators_see_everything(), startingLifeTotal, shareDecklistsOnLoad, room);
GameConfig config{.creatorInfo = copyUserInfo(false),
.gameId = gameId,
.description = description,
.password = QString::fromStdString(cmd.password()),
.maxPlayers = static_cast<int>(cmd.max_players()),
.gameTypes = gameTypes,
.onlyBuddies = cmd.only_buddies(),
.onlyRegistered = onlyRegisteredUsers,
.spectatorsAllowed = cmd.spectators_allowed(),
.spectatorsNeedPassword = cmd.spectators_need_password(),
.spectatorsCanTalk = cmd.spectators_can_talk(),
.spectatorsSeeEverything = cmd.spectators_see_everything(),
.startingLifeTotal = startingLifeTotal,
.shareDecklistsOnLoad = shareDecklistsOnLoad};
auto *game = new Server_Game(config, room);
game->addPlayer(this, rc, asSpectator, asJudge, false);
room->addGame(game);

View file

@ -1,3 +1,4 @@
#include "game/game_config.h"
#include "game/server_abstract_player.h"
#include "game/server_card.h"
#include "game/server_cardzone.h"
@ -22,7 +23,21 @@ TEST(ReverseCardMoveTest, MoveCardFromBottomTest)
// instantiate a fake server instance
FakeServer server;
Server_Room room(0, 0, "", "", "", "", false, "", {}, &server);
Server_Game game(user, 1, "", "", 2, QList<int>(), false, false, false, false, false, false, 20, false, &room);
GameConfig config{.creatorInfo = user,
.gameId = 1,
.description = QString(),
.password = QString(),
.maxPlayers = 2,
.gameTypes = QList<int>(),
.onlyBuddies = false,
.onlyRegistered = false,
.spectatorsAllowed = false,
.spectatorsNeedPassword = false,
.spectatorsCanTalk = false,
.spectatorsSeeEverything = false,
.startingLifeTotal = 20,
.shareDecklistsOnLoad = false};
Server_Game game(config, &room);
Server_AbstractPlayer player(&game, 1, user, false, nullptr);
Server_CardZone deckZone(&player, ZoneNames::DECK, true, ServerInfo_Zone::PublicZone);
Server_CardZone exileZone(&player, ZoneNames::EXILE, true, ServerInfo_Zone::PublicZone);