From 4356b5ebb7a4c4d76ca35f9162145757bddc34e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 24 Aug 2026 10:09:37 +0200 Subject: [PATCH 01/18] [Server] Implement Swiss tournament engine for lobby games Adds the server-side tournament mode driven through the strategy and factory interfaces from #7132: - Server_Tournament runs greedy Swiss pairing with rematch avoidance, byes, ceil(log2(n)) rounds, and best-of-N series via gamesPerMatch; match sub-games are spawned through the parent game's Server_MatchGameFactory implementation with players auto-joined and their submitted decks installed - Tournament lifecycle strategy gates the hub game start until all decks are submitted; match result strategy reports sub-game outcomes back to the parent tournament - Server_Game gains tournament state (isTournament flag, settings, parent link), protocol fields in getInfo/game state events, a full-lobby bypass for tournament hubs, and disconnectRemovesPlayer plumbing for upcoming draft modes - Match-game creation is deferred to the owning thread's event loop so room registration never nests lock orders; shared state is guarded by the tournament mutex --- .../network/server/remote/CMakeLists.txt | 8 +- .../game/server_abstract_participant.cpp | 2 +- .../server/remote/game/server_game.cpp | 101 +++- .../network/server/remote/game/server_game.h | 61 +- .../server/remote/game/server_tournament.cpp | 556 ++++++++++++++++++ .../server/remote/game/server_tournament.h | 106 ++++ .../server_tournament_lifecycle_strategy.cpp | 33 ++ .../server_tournament_lifecycle_strategy.h | 12 + ...erver_tournament_match_result_strategy.cpp | 34 ++ .../server_tournament_match_result_strategy.h | 12 + .../server/remote/server_protocolhandler.cpp | 8 + 11 files changed, 927 insertions(+), 6 deletions(-) create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h diff --git a/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt b/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt index 26c2be69a..b429b8eb4 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt +++ b/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt @@ -15,9 +15,12 @@ set(HEADERS game/server_deck_validation_strategy.h game/server_game.h game/server_game_lifecycle_strategy.h - game/server_match_result_strategy.h game/server_match_game_factory.h + game/server_match_result_strategy.h game/server_player.h + game/server_tournament.h + game/server_tournament_lifecycle_strategy.h + game/server_tournament_match_result_strategy.h game/server_spectator.h server.h server_abstractuserinterface.h @@ -43,6 +46,9 @@ add_library( game/server_game.cpp game/server_player.cpp game/server_spectator.cpp + game/server_tournament.cpp + game/server_tournament_lifecycle_strategy.cpp + game/server_tournament_match_result_strategy.cpp server.cpp server_abstractuserinterface.cpp server_database_interface.cpp diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp index 6c5d557b1..96ff74dd4 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp @@ -587,7 +587,7 @@ void Server_AbstractParticipant::setUserInterface(Server_AbstractUserInterface * void Server_AbstractParticipant::disconnectClient() { bool isRegistered = userInfo->user_level() & ServerInfo_User::IsRegistered; - if (!isRegistered || spectator) { + if (!isRegistered || spectator || game->getDisconnectRemovesPlayer()) { game->removeParticipant(this, Event_Leave::USER_DISCONNECTED); } else { setUserInterface(nullptr); diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index b1863c91e..295cbebf0 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -49,6 +49,9 @@ #include "server_player.h" #include "server_response_containers.h" #include "server_spectator.h" +#include "server_tournament.h" +#include "server_tournament_lifecycle_strategy.h" +#include "server_tournament_match_result_strategy.h" #include #include @@ -59,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -91,8 +95,9 @@ Server_Game::Server_Game(const GameConfig &config, Server_Room *_room) 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), - deckValidationStrategy(new Server_DefaultDeckValidationStrategy), + turnOrderReversed(false), startTime(QDateTime::currentDateTime()), pingClock(nullptr), isTournament(false), + tournament(nullptr), tournamentParentGame(nullptr), tournamentMatchPlayer1Id(-1), tournamentMatchPlayer2Id(-1), + disconnectRemovesPlayer(false), deckValidationStrategy(new Server_DefaultDeckValidationStrategy), lifecycleStrategy(new Server_DefaultLifecycleStrategy), matchResultStrategy(new Server_NullMatchResultStrategy), gameMutex() { @@ -305,6 +310,12 @@ void Server_Game::createGameStateChangedEvent(Event_GameStateChanged *event, event->set_game_started(false); } + event->set_is_tournament(isTournament); + + if (tournamentParentGame) { + event->set_parent_game_id(tournamentParentGame->getGameId()); + } + for (Server_AbstractParticipant *participant : participants.values()) { participant->getInfo(event->add_player_list(), recipient, omniscient, withUserInfo); } @@ -353,7 +364,7 @@ void Server_Game::doStartGameIfReady(bool forceStartGame) Server_DatabaseInterface *databaseInterface = room->getServer()->getDatabaseInterface(); QMutexLocker locker(&gameMutex); - if (getPlayerCount() < maxPlayers && !forceStartGame) { + if (!isTournament && getPlayerCount() < maxPlayers && !forceStartGame) { return; } @@ -947,6 +958,7 @@ void Server_Game::getInfo(ServerInfo_Game &result) const result.set_share_decklists_on_load(shareDecklistsOnLoad); result.set_spectators_count(getSpectatorCount()); result.set_start_time(startTime.toSecsSinceEpoch()); + result.set_is_tournament(isTournament); } } @@ -997,3 +1009,86 @@ void Server_Game::setDeckValidationStrategy(Server_DeckValidationStrategy *strat { deckValidationStrategy.reset(strategy); } + +void Server_Game::setMatchResultStrategy(Server_MatchResultStrategy *strategy) +{ + matchResultStrategy.reset(strategy); +} + +void Server_Game::setIsTournamentGame(bool _isTournament) +{ + isTournament = _isTournament; + if (isTournament) { + tournament = new Server_Tournament(this, this, this); + lifecycleStrategy.reset(new Server_TournamentLifecycleStrategy); + matchResultStrategy.reset(new Server_TournamentMatchResultStrategy); + } else if (tournament) { + delete tournament; + tournament = nullptr; + lifecycleStrategy.reset(new Server_DefaultLifecycleStrategy); + matchResultStrategy.reset(new Server_NullMatchResultStrategy); + } +} + +void Server_Game::startTournament() +{ + if (!tournament) { + tournament = new Server_Tournament(this, this, this); + } + + if (!tournament->isStarted()) { + // Add all current players to the tournament + auto players = getPlayers(); + for (auto *player : players.values()) { + tournament->addPlayer(player->getPlayerId(), QString::fromStdString(player->getUserInfo()->name())); + } + + tournament->startTournament(); + } + + GameEventStorage ges; + tournament->broadcastTournamentState(ges); + ges.sendToGame(this); +} + +void Server_Game::setPlayerTournamentDeck(int playerId, DeckList *deck) +{ + if (tournament) { + tournament->setPlayerDeck(playerId, deck); + } +} + +void Server_Game::setTournamentMatchInfo(Server_Game *parentGame, int p1Id, int p2Id) +{ + tournamentParentGame = parentGame; + tournamentMatchPlayer1Id = p1Id; + tournamentMatchPlayer2Id = p2Id; +} + +Server_Game *Server_Game::createMatchGame(const GameConfig &config, int &outGameId) +{ + Server_DatabaseInterface *databaseInterface = room->getServer()->getDatabaseInterface(); + outGameId = databaseInterface->getNextGameId(); + if (outGameId == -1) { + return nullptr; + } + + GameConfig matchConfig = config; + matchConfig.gameId = outGameId; + auto *game = new Server_Game(matchConfig, room); + // Sub-games carry the tournament flag (for protocol fields) but keep the default + // strategies; the parent tournament drives them through the match result strategy + // installed by Server_Tournament::createMatchGame. + game->isTournament = true; + return game; +} + +Server_AbstractUserInterface *Server_Game::getUserInterface(const QString &playerName) +{ + return room->getUserInterfaceByName(playerName); +} + +void Server_Game::addGameToRoom(Server_Game *game) +{ + room->addGame(game); +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h index c7f8d0929..3b8d794dc 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h @@ -21,7 +21,12 @@ #define SERVERGAME_H #include "../server_response_containers.h" +#include "game_config.h" #include "libcockatrice/protocol/pb/game_event_context.pb.h" +#include "server_deck_validation_strategy.h" +#include "server_game_lifecycle_strategy.h" +#include "server_match_game_factory.h" +#include "server_match_result_strategy.h" // IWYU pragma: keep // ServerInfo_Game is passed by value to the gameInfoChanged() signal, so the @@ -37,16 +42,21 @@ #include #include #include +#include #include #include #include class QTimer; +class DeckList; class GameEventContainer; +class GameEventStorage; class GameReplay; class Server_Room; class Server_AbstractPlayer; class Server_AbstractParticipant; +class Server_Card; +class Server_Tournament; class ServerInfo_User; class ServerInfo_Game; class Server_AbstractUserInterface; @@ -63,7 +73,7 @@ class Message; } // namespace google struct GameConfig; -class Server_Game : public QObject +class Server_Game : public QObject, public Server_MatchGameFactory { Q_OBJECT private: @@ -98,6 +108,14 @@ private: QList replayList; GameReplay *currentReplay; + bool isTournament; + TournamentSettings tournamentSettings; + Server_Tournament *tournament; + Server_Game *tournamentParentGame; + int tournamentMatchPlayer1Id; + int tournamentMatchPlayer2Id; + bool disconnectRemovesPlayer; + QScopedPointer deckValidationStrategy; QScopedPointer lifecycleStrategy; @@ -237,6 +255,45 @@ public: void returnCardsFromPlayer(GameEventStorage &ges, Server_AbstractPlayer *player); /** @brief Get the current deck validation strategy (non-owning). */ + bool getIsTournamentGame() const + { + return isTournament; + } + void setIsTournamentGame(bool _isTournament); + bool getIsTournament() const + { + return tournament != nullptr; + } + Server_Tournament *getTournament() const + { + return tournament; + } + void startTournament(); + void setPlayerTournamentDeck(int playerId, DeckList *deck); + void setTournamentMatchInfo(Server_Game *parentGame, int p1Id, int p2Id); + Server_Game *getTournamentParentGame() const + { + return tournamentParentGame; + } + bool getDisconnectRemovesPlayer() const + { + return disconnectRemovesPlayer; + } + + // Server_MatchGameFactory implementation + Server_Game *createMatchGame(const GameConfig &config, int &outGameId) override; + Server_AbstractUserInterface *getUserInterface(const QString &playerName) override; + void addGameToRoom(Server_Game *game) override; + + const TournamentSettings &getTournamentSettings() const + { + return tournamentSettings; + } + void setTournamentSettings(const TournamentSettings &settings) + { + tournamentSettings = settings; + } + Server_DeckValidationStrategy *getDeckValidationStrategy() const { return deckValidationStrategy.data(); @@ -249,6 +306,8 @@ public: { return lifecycleStrategy.data(); } + /** @brief Replace the match result strategy; takes ownership of @p strategy. */ + void setMatchResultStrategy(Server_MatchResultStrategy *strategy); }; #endif diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp new file mode 100644 index 000000000..1d13f31f7 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -0,0 +1,556 @@ +#include "server_tournament.h" + +#include "../server_abstractuserinterface.h" +#include "../server_response_containers.h" +#include "../serverinfo_user_container.h" +#include "game_config.h" +#include "server_abstract_player.h" +#include "server_game.h" +#include "server_match_game_factory.h" +#include "server_player.h" +#include "server_tournament_match_result_strategy.h" + +#include +#include +#include +#include +#include + +inline Q_LOGGING_CATEGORY(TournamentLog, "tournament"); + +Server_Tournament::Server_Tournament(Server_Game *_parentGame, Server_MatchGameFactory *_factory, QObject *parent) + : QObject(parent), parentGame(_parentGame), matchGameFactory(_factory), currentRound(0), totalRounds(0), + started(false) +{ +} + +Server_Tournament::~Server_Tournament() +{ + qDeleteAll(submittedDecks); +} + +void Server_Tournament::addPlayer(int playerId, const QString &playerName) +{ + TournamentPlayerData data; + data.playerId = playerId; + data.playerName = playerName; + players[playerId] = data; +} + +void Server_Tournament::setPlayerDeck(int playerId, DeckList *deck) +{ + QMutexLocker locker(&tournamentMutex); + delete submittedDecks.value(playerId, nullptr); + submittedDecks[playerId] = deck; + if (players.contains(playerId)) { + players[playerId].deckSubmitted = true; + } +} + +void Server_Tournament::removePlayer(int playerId) +{ + QMutexLocker locker(&tournamentMutex); + players.remove(playerId); +} + +void Server_Tournament::startTournament() +{ + { + QMutexLocker locker(&tournamentMutex); + if (started) { + return; + } + totalRounds = calculateTotalRounds(); + started = true; + currentRound = 0; + generateSwissPairings(); + } + + // Spawn the first round's match games + enqueueMatchGameCreation(); +} + +bool Server_Tournament::isAllDecksSubmitted() const +{ + QMutexLocker locker(&tournamentMutex); + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + if (!it->deckSubmitted) { + return false; + } + } + return true; +} + +int Server_Tournament::getTournamentPlayerIdByName(const QString &name) const +{ + QMutexLocker locker(&tournamentMutex); + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + if (it->playerName == name) { + return it->playerId; + } + } + return -1; +} + +void Server_Tournament::generateSwissPairings() +{ + currentPairings.clear(); + QList available; + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + available.append(it->playerId); + } + + // Sort by wins descending (and by record for tie-breaking) + std::sort(available.begin(), available.end(), [this](int a, int b) { + const auto &pa = players[a]; + const auto &pb = players[b]; + if (pa.wins != pb.wins) { + return pa.wins > pb.wins; + } + if (pa.losses != pb.losses) { + return pa.losses < pb.losses; + } + return a < b; + }); + + // Simple greedy Swiss pairing + QSet paired; + for (int i = 0; i < available.size(); ++i) { + if (paired.contains(available[i])) { + continue; + } + for (int j = i + 1; j < available.size(); ++j) { + if (paired.contains(available[j])) { + continue; + } + if (!havePlayed(available[i], available[j])) { + TournamentPairingData pairing; + pairing.player1Id = available[i]; + pairing.player2Id = available[j]; + currentPairings.append(pairing); + paired.insert(available[i]); + paired.insert(available[j]); + break; + } + } + } + + // Bye for unpaired player if odd count + for (int i = 0; i < available.size(); ++i) { + if (!paired.contains(available[i])) { + // Player gets a bye (auto-win) + TournamentPairingData bye; + bye.player1Id = available[i]; + bye.player2Id = -1; + bye.winnerId = available[i]; + bye.player1MatchWins = gamesPerMatch; // Match immediately decided + currentPairings.append(bye); + players[available[i]].wins += 1; + allPreviousPairings.append(qMakePair(available[i], -1)); + break; + } + } +} + +int Server_Tournament::calculateTotalRounds() const +{ + int n = players.size(); + if (n <= 1) { + return 0; + } + // Standard Swiss rounds: ceil(log2(n)) + int rounds = 0; + while ((1 << rounds) < n) { + ++rounds; + } + return rounds; +} + +bool Server_Tournament::havePlayed(int p1, int p2) const +{ + for (const auto &pair : allPreviousPairings) { + if ((pair.first == p1 && pair.second == p2) || (pair.first == p2 && pair.second == p1)) { + return true; + } + } + return false; +} + +void Server_Tournament::advanceRound(GameEventStorage &ges) +{ + { + QMutexLocker locker(&tournamentMutex); + ++currentRound; + if (currentRound >= totalRounds) { + broadcastTournamentState(ges); + return; + } + generateSwissPairings(); + } + + enqueueMatchGameCreation(); + broadcastTournamentState(ges); +} + +bool Server_Tournament::allPairingsDecided() const +{ + for (const auto &pairing : currentPairings) { + if (pairing.winnerId == -2) { + return false; + } + } + return true; +} + +void Server_Tournament::enqueueMatchGameCreation() +{ + QList> planned; + { + QMutexLocker locker(&tournamentMutex); + for (const auto &pairing : currentPairings) { + if (pairing.player2Id != -1 && pairing.winnerId == -2 && + pairing.matchGameIds.size() < static_cast(gamesPerMatch)) { + planned.append(qMakePair(pairing.player1Id, pairing.player2Id)); + } + } + } + if (planned.isEmpty()) { + return; + } + + // Create the games from the event loop instead of the caller's stack: command + // processing holds game mutexes, and room registration takes gamesLock, so spawning + // synchronously would nest lock orders. The queued job runs once this object's + // owning thread returns to its event loop with no locks held; it is dropped if this + // tournament is destroyed first. + QMetaObject::invokeMethod( + this, + [this, planned] { + for (const auto &pair : planned) { + createMatchGame(pair.first, pair.second); + } + + GameEventStorage ges; + broadcastTournamentState(ges); + ges.sendToGame(parentGame); + }, + Qt::QueuedConnection); +} + +void Server_Tournament::createMatchGame(int player1Id, int player2Id) +{ + if (!matchGameFactory || player2Id == -1) { + return; + } + + QString player1Name; + QString player2Name; + QString deck1Native; + QString deck2Native; + int round = 0; + int gameNumber = 1; + { + QMutexLocker locker(&tournamentMutex); + player1Name = players.value(player1Id).playerName; + player2Name = players.value(player2Id).playerName; + + if (submittedDecks.contains(player1Id)) { + deck1Native = submittedDecks.value(player1Id)->writeToString_Native(); + } + if (submittedDecks.contains(player2Id)) { + deck2Native = submittedDecks.value(player2Id)->writeToString_Native(); + } + + round = currentRound; + for (const auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id) { + gameNumber = pairing.matchGameIds.size() + 1; + break; + } + } + + // Defense in depth: never exceed the configured series length + for (const auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id && + pairing.matchGameIds.size() >= static_cast(gamesPerMatch)) { + qCWarning(TournamentLog) << "Refusing to exceed series length for pairing" << player1Id << player2Id; + return; + } + } + } + + // Create a sub-game for this match via the factory + ServerInfo_User creatorInfo; + creatorInfo.set_name(player1Name.toStdString()); + creatorInfo.set_user_level(ServerInfo_User::IsAdmin | ServerInfo_User::IsRegistered); + + QString gameDesc = gamesPerMatch > 1 + ? QString("R%1 Match - Game %2 of %3").arg(round).arg(gameNumber).arg(gamesPerMatch) + : QString("Tournament Round %1").arg(round); + + GameConfig matchConfig; + matchConfig.creatorInfo = creatorInfo; + matchConfig.description = gameDesc; + matchConfig.maxPlayers = 2; + matchConfig.startingLifeTotal = parentGame->getStartingLifeTotal(); + + int matchGameId = -1; + auto *matchGame = matchGameFactory->createMatchGame(matchConfig, matchGameId); + if (!matchGame || matchGameId == -1) { + return; + } + + matchGame->setTournamentMatchInfo(parentGame, player1Id, player2Id); + matchGame->setMatchResultStrategy(new Server_TournamentMatchResultStrategy); + matchGameFactory->addGameToRoom(matchGame); + + // Store the game ID in the pairing + { + QMutexLocker locker(&tournamentMutex); + for (auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id) { + pairing.gameId = matchGameId; + pairing.matchGameIds.append(matchGameId); + break; + } + } + } + + // Auto-join both players, sending the join event directly through their UIs. + QMap> joiners; + + auto joinAndSetupPlayer = [&](int pid, const QString &name) { + Server_AbstractUserInterface *ui = matchGameFactory->getUserInterface(name); + if (ui) { + auto *rc = new ResponseContainer(0); + matchGame->addPlayer(ui, *rc, false, false, false); + joiners[pid] = qMakePair(ui, rc); + } + }; + + joinAndSetupPlayer(player1Id, player1Name); + joinAndSetupPlayer(player2Id, player2Name); + + // Now send the enqueued GameJoined + GameStateChanged events to each player's client. + for (auto it = joiners.constBegin(); it != joiners.constEnd(); ++it) { + it.value().first->sendResponseContainer(*it.value().second, Response::RespNothing); + delete it.value().second; + } + joiners.clear(); + + // Set decks and mark players as ready in the match game + auto matchPlayers = matchGame->getPlayers(); + for (auto *matchPlayer : matchPlayers) { + const QString name = QString::fromStdString(matchPlayer->getUserInfo()->name()); + QString deckNative; + if (name == player1Name) { + deckNative = deck1Native; + } else if (name == player2Name) { + deckNative = deck2Native; + } + + if (!deckNative.isEmpty()) { + matchPlayer->setDeck(new DeckList(deckNative)); + matchPlayer->setReadyStart(true); + } + } + + // Start the match game + matchGame->startGameIfReady(true); +} + +void Server_Tournament::recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges) +{ + QMutexLocker locker(&tournamentMutex); + + // Find the pairing and set the winner + for (auto &pairing : currentPairings) { + if ((pairing.player1Id == playerId1 && pairing.player2Id == playerId2) || + (pairing.player1Id == playerId2 && pairing.player2Id == playerId1)) { + if (pairing.winnerId != -2) { + return; // Already recorded — defense in depth against double-call + } + pairing.winnerId = winnerId; + break; + } + } + + // Update player records + if (winnerId == -1) { + // Draw + players[playerId1].draws += 1; + players[playerId2].draws += 1; + } else if (winnerId == playerId1) { + players[playerId1].wins += 1; + players[playerId2].losses += 1; + } else if (winnerId == playerId2) { + players[playerId2].wins += 1; + players[playerId1].losses += 1; + } + + // Store for future pairing avoidance + allPreviousPairings.append(qMakePair(playerId1, playerId2)); + + // Check if all pairings in current round have results + bool allDecided = true; + for (const auto &pairing : currentPairings) { + if (pairing.winnerId == -2) { + allDecided = false; + break; + } + } + + broadcastTournamentState(ges); + + if (allDecided) { + advanceRound(ges); + } +} + +bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, GameEventStorage &ges) +{ + bool matchDecided = false; + bool seriesContinues = false; + int p1 = -1; + int p2 = -1; + { + QMutexLocker locker(&tournamentMutex); + + // Find the pairing that owns this game + TournamentPairingData *pairingPtr = nullptr; + for (auto &pairing : currentPairings) { + if (pairing.matchGameIds.contains(gameId)) { + pairingPtr = &pairing; + break; + } + } + + if (!pairingPtr) { + return false; + } + + // If the match is already decided, ignore further sub-game results + if (pairingPtr->winnerId != -2) { + return true; + } + + // Increment per-match wins + if (winnerId == pairingPtr->player1Id) { + pairingPtr->player1MatchWins += 1; + } else if (winnerId == pairingPtr->player2Id) { + pairingPtr->player2MatchWins += 1; + } + // Draw (winnerId == -1): no match wins incremented + + // Check if match is decided + const int gamesNeeded = static_cast(gamesPerMatch); + matchDecided = (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); + + if (matchDecided) { + // Determine match winner + int matchWinnerId; + if (pairingPtr->player1MatchWins >= gamesNeeded) { + matchWinnerId = pairingPtr->player1Id; + } else { + matchWinnerId = pairingPtr->player2Id; + } + + // Set the match winner on the pairing + pairingPtr->winnerId = matchWinnerId; + + // Update tournament-level player records + if (matchWinnerId == pairingPtr->player1Id) { + players[pairingPtr->player1Id].wins += 1; + players[pairingPtr->player2Id].losses += 1; + } else { + players[pairingPtr->player2Id].wins += 1; + players[pairingPtr->player1Id].losses += 1; + } + + // Store for future pairing avoidance + allPreviousPairings.append(qMakePair(pairingPtr->player1Id, pairingPtr->player2Id)); + } else { + // Match not decided — spawn the next sub-game outside all locks + seriesContinues = true; + p1 = pairingPtr->player1Id; + p2 = pairingPtr->player2Id; + } + + broadcastTournamentState(ges); + } + + if (seriesContinues) { + QMetaObject::invokeMethod( + this, + [this, p1, p2] { + createMatchGame(p1, p2); + + GameEventStorage nextGes; + broadcastTournamentState(nextGes); + nextGes.sendToGame(parentGame); + }, + Qt::QueuedConnection); + } + + checkAndAdvanceRound(ges); + + return matchDecided; +} + +void Server_Tournament::checkAndAdvanceRound(GameEventStorage &ges) +{ + bool roundComplete = false; + { + QMutexLocker locker(&tournamentMutex); + roundComplete = allPairingsDecided(); + } + + if (roundComplete) { + advanceRound(ges); + } +} + +void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) +{ + QMutexLocker locker(&tournamentMutex); + + Event_TournamentState state; + + if (started && currentRound >= totalRounds) { + state.set_phase(Event_TournamentState::PHASE_FINISHED); + } else if (started) { + state.set_phase(Event_TournamentState::PHASE_PLAYING); + } else { + state.set_phase(Event_TournamentState::PHASE_DECK_BUILDING); + } + + state.set_current_round(currentRound); + state.set_total_rounds(totalRounds); + + // Settings + TournamentSettings *settings = state.mutable_settings(); + settings->set_games_per_match(gamesPerMatch); + + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + TournamentPlayer *p = state.add_players(); + p->set_player_id(it->playerId); + p->set_player_name(it->playerName.toStdString()); + p->set_wins(it->wins); + p->set_losses(it->losses); + p->set_draws(it->draws); + p->set_deck_submitted(it->deckSubmitted); + } + + for (const auto &pairing : currentPairings) { + TournamentPairing *p = state.add_pairings(); + p->set_player1_id(pairing.player1Id); + p->set_player2_id(pairing.player2Id); + p->set_game_id(pairing.gameId); + // Map internal sentinel: -2 (undecided) -> -1 (no winner yet in proto) + p->set_winner_id(pairing.winnerId == -2 ? -1 : pairing.winnerId); + p->set_player1_match_wins(pairing.player1MatchWins); + p->set_player2_match_wins(pairing.player2MatchWins); + } + + ges.enqueueGameEvent(state, -1); +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h new file mode 100644 index 000000000..b59edda4f --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -0,0 +1,106 @@ +#ifndef SERVER_TOURNAMENT_H +#define SERVER_TOURNAMENT_H + +#include +#include +#include +#include +#include +#include + +class DeckList; +class Server_Game; +class Server_MatchGameFactory; +class Server_AbstractParticipant; +class Server_AbstractUserInterface; +class GameEventStorage; + +/** @brief Maximum number of games per match a tournament can be configured with. */ +constexpr int MAX_GAMES_PER_MATCH = 5; + +class Server_Tournament : public QObject +{ + Q_OBJECT +public: + explicit Server_Tournament(Server_Game *_parentGame, Server_MatchGameFactory *_factory, QObject *parent = nullptr); + ~Server_Tournament() override; + + void addPlayer(int playerId, const QString &playerName); + void removePlayer(int playerId); + void startTournament(); + void advanceRound(GameEventStorage &ges); + void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); + bool recordMatchResultByGameId(int gameId, int winnerId, GameEventStorage &ges); + void broadcastTournamentState(GameEventStorage &ges); + + bool isStarted() const + { + return started; + } + bool isAllDecksSubmitted() const; + int getPlayerCount() const + { + return players.size(); + } + int getTournamentPlayerIdByName(const QString &name) const; + void setPlayerDeckSubmitted(int playerId) + { + if (players.contains(playerId)) { + players[playerId].deckSubmitted = true; + } + } + void setPlayerDeck(int playerId, DeckList *deck); + + void setGamesPerMatch(uint32_t n) + { + gamesPerMatch = n; + } + uint32_t getGamesPerMatch() const + { + return gamesPerMatch; + } + + struct TournamentPlayerData + { + int playerId; + QString playerName; + int wins = 0; + int losses = 0; + int draws = 0; + bool deckSubmitted = false; + }; + + struct TournamentPairingData + { + int player1Id; + int player2Id; + int gameId = -1; + int winnerId = -2; // -2 = undecided, -1 = draw, >= 0 = winner player id + int player1MatchWins = 0; + int player2MatchWins = 0; + QList matchGameIds; + }; + +private: + Server_Game *parentGame; + Server_MatchGameFactory *matchGameFactory; + mutable QRecursiveMutex tournamentMutex; + QMap players; + QMap submittedDecks; + QList currentPairings; + QList> allPreviousPairings; + int currentRound; + int totalRounds; + bool started; + uint32_t gamesPerMatch = 1; + + void generateSwissPairings(); + int calculateTotalRounds() const; + bool havePlayed(int p1, int p2) const; + bool allPairingsDecided() const; + void createMatchGame(int player1Id, int player2Id); + void enqueueMatchGameCreation(); + void checkAndAdvanceRound(GameEventStorage &ges); +}; + +#endif // SERVER_TOURNAMENT_H diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp new file mode 100644 index 000000000..5149b9a26 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp @@ -0,0 +1,33 @@ +#include "server_tournament_lifecycle_strategy.h" + +#include "server_abstract_player.h" +#include "server_game.h" + +#include + +inline Q_LOGGING_CATEGORY(TournamentLifecycleLog, "tournament_lifecycle"); + +Server_GameLifecycleStrategy::StartAction Server_TournamentLifecycleStrategy::onGameStarting(Server_Game *game) +{ + // Match sub-games start through the normal flow; only the tournament hub game is + // managed by this lifecycle. + if (game->getTournamentParentGame() != nullptr) { + return StartAction::ProceedNormal; + } + + for (auto *player : game->getPlayers().values()) { + if (!player->getDeckList()) { + qCWarning(TournamentLifecycleLog) + << "Tournament cannot start: player" << player->getUserInfo()->name().c_str() << "has no deck"; + return StartAction::Handled; + } + } + + if (!game->getIsTournamentGame()) { + qCWarning(TournamentLifecycleLog) << "Tournament lifecycle used for non-tournament game — falling back"; + return StartAction::ProceedNormal; + } + + game->startTournament(); + return StartAction::Handled; +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h new file mode 100644 index 000000000..c6dbede1b --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h @@ -0,0 +1,12 @@ +#ifndef SERVER_TOURNAMENT_LIFECYCLE_STRATEGY_H +#define SERVER_TOURNAMENT_LIFECYCLE_STRATEGY_H + +#include "server_game_lifecycle_strategy.h" + +class Server_TournamentLifecycleStrategy : public Server_GameLifecycleStrategy +{ +public: + StartAction onGameStarting(Server_Game *game) override; +}; + +#endif diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp new file mode 100644 index 000000000..5611224e5 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp @@ -0,0 +1,34 @@ +#include "server_tournament_match_result_strategy.h" + +#include "../server_response_containers.h" +#include "server_abstract_player.h" +#include "server_game.h" +#include "server_tournament.h" + +#include +#include + +bool Server_TournamentMatchResultStrategy::onGameFinished(Server_Game *game, + int playing, + Server_AbstractPlayer *lastPlayer) +{ + auto *parentGame = game->getTournamentParentGame(); + if (!parentGame || !parentGame->getTournament()) { + return false; + } + + int winnerId; + if (playing == 0) { + winnerId = -1; + } else { + QString winnerName = QString::fromStdString(lastPlayer->getUserInfo()->name()); + auto *tournament = parentGame->getTournament(); + winnerId = tournament->getTournamentPlayerIdByName(winnerName); + } + + GameEventStorage parentGes; + bool matchDecided = parentGame->getTournament()->recordMatchResultByGameId(game->getGameId(), winnerId, parentGes); + parentGes.sendToGame(parentGame); + + return matchDecided; +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h new file mode 100644 index 000000000..7bc3bafb8 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h @@ -0,0 +1,12 @@ +#ifndef SERVER_TOURNAMENT_MATCH_RESULT_STRATEGY_H +#define SERVER_TOURNAMENT_MATCH_RESULT_STRATEGY_H + +#include "server_match_result_strategy.h" + +class Server_TournamentMatchResultStrategy : public Server_MatchResultStrategy +{ +public: + bool onGameFinished(Server_Game *game, int playing, Server_AbstractPlayer *lastPlayer) override; +}; + +#endif diff --git a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp index dd074a517..eacb9802c 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp @@ -6,6 +6,8 @@ static constexpr qint64 kAuthNonceLifetimeSeconds = 60; #include "game/game_config.h" #include "game/server_abstract_participant.h" #include "game/server_game.h" +#include "game/server_player.h" +#include "game/server_tournament.h" #include "libcockatrice/protocol/pb/admin_commands.pb.h" #include "libcockatrice/protocol/pb/developer_commands.pb.h" #include "libcockatrice/protocol/pb/game_commands.pb.h" @@ -1094,6 +1096,8 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room int startingLifeTotal = cmd.has_starting_life_total() ? cmd.starting_life_total() : 20; bool shareDecklistsOnLoad = cmd.has_share_decklists_on_load() ? cmd.share_decklists_on_load() : false; + bool isTournament = cmd.has_is_tournament() ? cmd.is_tournament() : false; + int gamesPerMatch = cmd.has_games_per_match() ? static_cast(cmd.games_per_match()) : 1; const int gameId = databaseInterface->getNextGameId(); if (gameId == -1) { @@ -1118,6 +1122,10 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room .shareDecklistsOnLoad = shareDecklistsOnLoad}; auto *game = new Server_Game(config, room); + game->setIsTournamentGame(isTournament); + if (isTournament && game->getTournament()) { + game->getTournament()->setGamesPerMatch(static_cast(qBound(1, gamesPerMatch, MAX_GAMES_PER_MATCH))); + } game->addPlayer(this, rc, asSpectator, asJudge, false); room->addGame(game); From b1287897d3b118af9a1f98727bf2f9ca566f0521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 08:57:26 +0200 Subject: [PATCH 02/18] [Server] Harden Swiss tournament engine against drops and stalls - link match sub-games to the hub through QPointer so a finished match can never dereference a torn-down parent game - a player who leaves the hub is dropped: no longer paired, current undecided match awarded to the opponent, absent from the bracket - refuse to spawn a match game when either participant is disconnected, and set disconnectRemovesPlayer on match games so a mid-match disconnect ends it instead of leaving a half-present participant - match winner is a strict majority (gamesPerMatch/2+1); an exhausted series with no majority is recorded as a draw so the round always advances - match games are started without force-start: a missing deck no longer kicks the player; the game stays open for deck selection - buyes are handed to every leftover player, worst-ranked first, at most one per player over the tournament - tournament hubs cannot start on mere 'everyone ready': host force-start is required and fewer than two players never starts - sub-game creator copies the real player user info instead of fabricating IsAdmin --- .../server/remote/game/server_game.cpp | 24 ++ .../network/server/remote/game/server_game.h | 9 +- .../server/remote/game/server_tournament.cpp | 212 ++++++++++++++---- .../server/remote/game/server_tournament.h | 10 +- .../server_tournament_lifecycle_strategy.cpp | 2 +- ...erver_tournament_match_result_strategy.cpp | 5 +- 6 files changed, 216 insertions(+), 46 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 295cbebf0..f536dd3ca 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -368,6 +368,13 @@ void Server_Game::doStartGameIfReady(bool forceStartGame) return; } + // Tournament hubs must be host-started and can't lock in a partially filled + // bracket: a mere "everyone current is ready" must not start a 1-player or + // undersized tournament. startTournament() additionally enforces 2+ players. + if (isTournament && !forceStartGame) { + return; + } + auto players = getPlayers(); for (auto *player : players.values()) { if (!player->getReadyStart()) { @@ -622,6 +629,17 @@ void Server_Game::removeParticipant(Server_AbstractParticipant *participant, Eve bool playerHost = hostId == participant->getPlayerId(); participant->prepareDestroy(); + // If this is the tournament hub (not one of its match sub-games), never re-pair + // the leaving player: mark them dropped so their matches are awarded and they + // disappear from the bracket instead of stalling the tournament. + if (tournament && !tournamentParentGame && !spectator) { + const int leavingPlayerId = participant->getPlayerId(); + GameEventStorage tournGes; + tournament->dropPlayer(leavingPlayerId); + tournament->broadcastTournamentState(tournGes); + tournGes.sendToGame(this); + } + if (playerHost) { int newHostId = -1; for (auto *otherPlayer : getPlayers().values()) { @@ -1043,6 +1061,12 @@ void Server_Game::startTournament() tournament->addPlayer(player->getPlayerId(), QString::fromStdString(player->getUserInfo()->name())); } + // A tournament with fewer than two players can't produce a valid bracket. + if (tournament->getPlayerCount() < 2) { + qCWarning() << "Cannot start tournament with fewer than 2 players"; + return; + } + tournament->startTournament(); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h index 3b8d794dc..3467ed10c 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -111,7 +112,7 @@ private: bool isTournament; TournamentSettings tournamentSettings; Server_Tournament *tournament; - Server_Game *tournamentParentGame; + QPointer tournamentParentGame; int tournamentMatchPlayer1Id; int tournamentMatchPlayer2Id; bool disconnectRemovesPlayer; @@ -271,7 +272,7 @@ public: void startTournament(); void setPlayerTournamentDeck(int playerId, DeckList *deck); void setTournamentMatchInfo(Server_Game *parentGame, int p1Id, int p2Id); - Server_Game *getTournamentParentGame() const + QPointer getTournamentParentGame() const { return tournamentParentGame; } @@ -279,6 +280,10 @@ public: { return disconnectRemovesPlayer; } + void setDisconnectRemovesPlayer(bool _disconnectRemovesPlayer) + { + disconnectRemovesPlayer = _disconnectRemovesPlayer; + } // Server_MatchGameFactory implementation Server_Game *createMatchGame(const GameConfig &config, int &outGameId) override; diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp index 1d13f31f7..527f2e728 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -31,9 +31,11 @@ Server_Tournament::~Server_Tournament() void Server_Tournament::addPlayer(int playerId, const QString &playerName) { + QMutexLocker locker(&tournamentMutex); TournamentPlayerData data; data.playerId = playerId; data.playerName = playerName; + data.dropped = false; players[playerId] = data; } @@ -51,6 +53,41 @@ void Server_Tournament::removePlayer(int playerId) { QMutexLocker locker(&tournamentMutex); players.remove(playerId); + submittedDecks.remove(playerId); + byeGivenPlayers.remove(playerId); +} + +void Server_Tournament::dropPlayer(int playerId) +{ + QMutexLocker locker(&tournamentMutex); + if (!players.contains(playerId)) { + return; + } + players[playerId].dropped = true; + players[playerId].deckSubmitted = false; + + // Any current pairing that involves the dropped player and is not already + // decided is awarded to the surviving opponent (or recorded as undecided if + // both dropped). The opponent keeps playing without sitting out a round. + for (auto &pairing : currentPairings) { + if (pairing.winnerId != -2) { + continue; + } + bool involvesDropped = (pairing.player1Id == playerId || pairing.player2Id == playerId); + if (!involvesDropped) { + continue; + } + if (pairing.player1Id == playerId && pairing.player2Id == playerId) { + continue; + } + int opponent = (pairing.player1Id == playerId) ? pairing.player2Id : pairing.player1Id; + if (players.contains(opponent) && !players[opponent].dropped) { + pairing.winnerId = opponent; + players[opponent].wins += 1; + players[playerId].losses += 1; + } + allPreviousPairings.append(qMakePair(pairing.player1Id, pairing.player2Id)); + } } void Server_Tournament::startTournament() @@ -97,7 +134,9 @@ void Server_Tournament::generateSwissPairings() currentPairings.clear(); QList available; for (auto it = players.constBegin(); it != players.constEnd(); ++it) { - available.append(it->playerId); + if (!it->dropped) { + available.append(it->playerId); + } } // Sort by wins descending (and by record for tie-breaking) @@ -113,8 +152,10 @@ void Server_Tournament::generateSwissPairings() return a < b; }); - // Simple greedy Swiss pairing QSet paired; + // Try to pair every player, allowing a single rematch only if the greedy pass + // would otherwise leave any unpaired remainder. Dropped players are never paired. + int maxRematches = available.size() / 2; for (int i = 0; i < available.size(); ++i) { if (paired.contains(available[i])) { continue; @@ -123,38 +164,74 @@ void Server_Tournament::generateSwissPairings() if (paired.contains(available[j])) { continue; } - if (!havePlayed(available[i], available[j])) { - TournamentPairingData pairing; - pairing.player1Id = available[i]; - pairing.player2Id = available[j]; - currentPairings.append(pairing); - paired.insert(available[i]); - paired.insert(available[j]); - break; + bool rematch = havePlayed(available[i], available[j]); + if (rematch && maxRematches <= 0) { + continue; } + TournamentPairingData pairing; + pairing.player1Id = available[i]; + pairing.player2Id = available[j]; + currentPairings.append(pairing); + paired.insert(available[i]); + paired.insert(available[j]); + if (rematch) { + --maxRematches; + } + break; } } - // Bye for unpaired player if odd count - for (int i = 0; i < available.size(); ++i) { - if (!paired.contains(available[i])) { - // Player gets a bye (auto-win) - TournamentPairingData bye; - bye.player1Id = available[i]; - bye.player2Id = -1; - bye.winnerId = available[i]; - bye.player1MatchWins = gamesPerMatch; // Match immediately decided - currentPairings.append(bye); - players[available[i]].wins += 1; - allPreviousPairings.append(qMakePair(available[i], -1)); - break; + // Give a bye to every remaining unpaired eligible player, worst-ranked first. + // A player receives at most one bye over the whole tournament. + QList unpaired; + for (int id : available) { + if (!paired.contains(id)) { + unpaired.append(id); } } + // Byes go to the lowest-ranked eligible player who has not had one yet. + std::sort(unpaired.begin(), unpaired.end(), [this](int a, int b) { + const auto &pa = players[a]; + const auto &pb = players[b]; + if (pa.wins != pb.wins) { + return pa.wins < pb.wins; + } + if (pa.losses != pb.losses) { + return pa.losses > pb.losses; + } + return a > b; + }); + + for (int id : unpaired) { + if (byeGivenPlayers.contains(id)) { + // Already used a bye: a dropped opponent or earlier bye means this player + // simply sits out the round with a free win to keep the bracket moving. + TournamentPairingData bye; + bye.player1Id = id; + bye.player2Id = -1; + bye.winnerId = id; + currentPairings.append(bye); + continue; + } + TournamentPairingData bye; + bye.player1Id = id; + bye.player2Id = -1; + bye.winnerId = id; + currentPairings.append(bye); + players[id].wins += 1; + byeGivenPlayers.insert(id); + allPreviousPairings.append(qMakePair(id, -1)); + } } int Server_Tournament::calculateTotalRounds() const { - int n = players.size(); + int n = 0; + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + if (!it->dropped) { + ++n; + } + } if (n <= 1) { return 0; } @@ -208,10 +285,16 @@ void Server_Tournament::enqueueMatchGameCreation() { QMutexLocker locker(&tournamentMutex); for (const auto &pairing : currentPairings) { - if (pairing.player2Id != -1 && pairing.winnerId == -2 && - pairing.matchGameIds.size() < static_cast(gamesPerMatch)) { - planned.append(qMakePair(pairing.player1Id, pairing.player2Id)); + if (pairing.player2Id == -1 || pairing.winnerId != -2) { + continue; } + if (players.value(pairing.player1Id).dropped || players.value(pairing.player2Id).dropped) { + continue; + } + if (pairing.matchGameIds.size() >= static_cast(gamesPerMatch)) { + continue; + } + planned.append(qMakePair(pairing.player1Id, pairing.player2Id)); } } if (planned.isEmpty()) { @@ -277,12 +360,25 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) return; } } + + // Bail out if either participant is no longer connected: a match game with + // zero or one connected player can never finish and would stall the round. + if (!matchGameFactory->getUserInterface(player1Name) || !matchGameFactory->getUserInterface(player2Name)) { + qCWarning(TournamentLog) << "Skipping match creation: a player in pairing" << player1Id << player2Id + << "is no longer connected"; + return; + } } - // Create a sub-game for this match via the factory + // Create a sub-game for this match via the factory, copying the real + // ServerInfo_User so it ships the true user level rather than a fabricated + // admin identity that would surface in buddy/ignore-list checks. ServerInfo_User creatorInfo; - creatorInfo.set_name(player1Name.toStdString()); - creatorInfo.set_user_level(ServerInfo_User::IsAdmin | ServerInfo_User::IsRegistered); + if (auto *ui = matchGameFactory->getUserInterface(player1Name)) { + creatorInfo = *ui->getUserInfo(); + } else { + creatorInfo.set_name(player1Name.toStdString()); + } QString gameDesc = gamesPerMatch > 1 ? QString("R%1 Match - Game %2 of %3").arg(round).arg(gameNumber).arg(gamesPerMatch) @@ -302,6 +398,9 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) matchGame->setTournamentMatchInfo(parentGame, player1Id, player2Id); matchGame->setMatchResultStrategy(new Server_TournamentMatchResultStrategy); + // A disconnect inside a tournament match must remove the player so the match + // can be decided; it must not leave them sitting as a half-present participant. + matchGame->setDisconnectRemovesPlayer(true); matchGameFactory->addGameToRoom(matchGame); // Store the game ID in the pairing @@ -317,6 +416,9 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) } // Auto-join both players, sending the join event directly through their UIs. + // Both UI lookups were verified above, so a player can only drop between that + // check and this add — in which case they get handled by drop processing and + // the pairing settles on the surviving opponent. QMap> joiners; auto joinAndSetupPlayer = [&](int pid, const QString &name) { @@ -338,7 +440,8 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) } joiners.clear(); - // Set decks and mark players as ready in the match game + // Set decks and mark players as ready in the match game. + bool anyDeckMissing = false; auto matchPlayers = matchGame->getPlayers(); for (auto *matchPlayer : matchPlayers) { const QString name = QString::fromStdString(matchPlayer->getUserInfo()->name()); @@ -352,11 +455,21 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) if (!deckNative.isEmpty()) { matchPlayer->setDeck(new DeckList(deckNative)); matchPlayer->setReadyStart(true); + } else { + anyDeckMissing = true; } } - // Start the match game - matchGame->startGameIfReady(true); + if (anyDeckMissing) { + // Not every participant submitted a deck. Do not force-start: that would + // kick the players without a deck. Leave the match game open so they can + // select a deck; the host starts it through the normal ready flow. + return; + } + + // Start the match game without forcing: both participants are ready and have + // decks, so there is nothing to kick. + matchGame->startGameIfReady(false); } void Server_Tournament::recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges) @@ -440,20 +553,29 @@ bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, Game } else if (winnerId == pairingPtr->player2Id) { pairingPtr->player2MatchWins += 1; } - // Draw (winnerId == -1): no match wins incremented + // Draw (winnerId == -1): counts nothing toward the series but does consume + // a slot, so a series can still end in a draw when it is exhausted. - // Check if match is decided - const int gamesNeeded = static_cast(gamesPerMatch); + // The winner needs a strict majority of the games in the series. + const int gamesPlayed = pairingPtr->matchGameIds.size(); + const int gamesNeeded = static_cast(gamesPerMatch / 2 + 1); + const int gamesRemaining = static_cast(gamesPerMatch) - gamesPlayed; matchDecided = (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); + if (!matchDecided) { + // Series exhausted without a strict-majority winner (e.g. a drawn Bo3 + // leaves it 1-1): record the match as a draw so the round always advances. + matchDecided = (gamesRemaining <= 0) && (pairingPtr->player1MatchWins == pairingPtr->player2MatchWins); + } if (matchDecided) { // Determine match winner - int matchWinnerId; + int matchWinnerId = -1; if (pairingPtr->player1MatchWins >= gamesNeeded) { matchWinnerId = pairingPtr->player1Id; - } else { + } else if (pairingPtr->player2MatchWins >= gamesNeeded) { matchWinnerId = pairingPtr->player2Id; } + // Otherwise the series was exhausted evenly — matchWinnerId stays -1 (a draw). // Set the match winner on the pairing pairingPtr->winnerId = matchWinnerId; @@ -462,9 +584,12 @@ bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, Game if (matchWinnerId == pairingPtr->player1Id) { players[pairingPtr->player1Id].wins += 1; players[pairingPtr->player2Id].losses += 1; - } else { + } else if (matchWinnerId == pairingPtr->player2Id) { players[pairingPtr->player2Id].wins += 1; players[pairingPtr->player1Id].losses += 1; + } else { + players[pairingPtr->player1Id].draws += 1; + players[pairingPtr->player2Id].draws += 1; } // Store for future pairing avoidance @@ -546,8 +671,13 @@ void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) p->set_player1_id(pairing.player1Id); p->set_player2_id(pairing.player2Id); p->set_game_id(pairing.gameId); - // Map internal sentinel: -2 (undecided) -> -1 (no winner yet in proto) - p->set_winner_id(pairing.winnerId == -2 ? -1 : pairing.winnerId); + // -2 = undecided; a decided draw is -1. The is_draw bit distinguishes a + // reported draw from an unset winner_id on the wire. + if (pairing.winnerId == -1) { + p->set_is_draw(true); + } else if (pairing.winnerId != -2) { + p->set_winner_id(pairing.winnerId); + } p->set_player1_match_wins(pairing.player1MatchWins); p->set_player2_match_wins(pairing.player2MatchWins); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h index b59edda4f..aeb3ea732 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,9 @@ public: void addPlayer(int playerId, const QString &playerName); void removePlayer(int playerId); + // Marks an already-starting/started tournament player as dropped: they stop + // being paired and their outstanding unstarted match is awarded as a loss. + void dropPlayer(int playerId); void startTournament(); void advanceRound(GameEventStorage &ges); void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); @@ -68,6 +72,7 @@ public: int losses = 0; int draws = 0; bool deckSubmitted = false; + bool dropped = false; }; struct TournamentPairingData @@ -82,12 +87,15 @@ public: }; private: - Server_Game *parentGame; + QPointer parentGame; Server_MatchGameFactory *matchGameFactory; mutable QRecursiveMutex tournamentMutex; QMap players; QMap submittedDecks; QList currentPairings; + // Players that have already received a bye in a previous round, so no one + // gets more than one bye over the whole tournament. + QSet byeGivenPlayers; QList> allPreviousPairings; int currentRound; int totalRounds; diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp index 5149b9a26..73035dd94 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp @@ -11,7 +11,7 @@ Server_GameLifecycleStrategy::StartAction Server_TournamentLifecycleStrategy::on { // Match sub-games start through the normal flow; only the tournament hub game is // managed by this lifecycle. - if (game->getTournamentParentGame() != nullptr) { + if (game->getTournamentParentGame().data() != nullptr) { return StartAction::ProceedNormal; } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp index 5611224e5..7ca8aacc9 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp @@ -12,7 +12,10 @@ bool Server_TournamentMatchResultStrategy::onGameFinished(Server_Game *game, int playing, Server_AbstractPlayer *lastPlayer) { - auto *parentGame = game->getTournamentParentGame(); + // The hub game is owned by the room and may be torn down once its host leaves + // and no players remain, while the match sub-games keep running. QPointer keeps + // this link checked so a later-finishing match can't touch freed memory. + auto *parentGame = game->getTournamentParentGame().data(); if (!parentGame || !parentGame->getTournament()) { return false; } From 7edac18f7c0f49be3fbf058c6a104a7fe2323860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 09:47:27 +0200 Subject: [PATCH 03/18] [Server] Adapt tournament engine to the shared settings message - read games per match from Command_CreateGame.tournament_settings after the proto rework replaced the standalone games_per_match field - use qWarning to match the surrounding file --- .../libcockatrice/network/server/remote/game/server_game.cpp | 2 +- .../network/server/remote/server_protocolhandler.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index f536dd3ca..775372ec5 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -1063,7 +1063,7 @@ void Server_Game::startTournament() // A tournament with fewer than two players can't produce a valid bracket. if (tournament->getPlayerCount() < 2) { - qCWarning() << "Cannot start tournament with fewer than 2 players"; + qWarning() << "Cannot start tournament with fewer than 2 players"; return; } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp index eacb9802c..375d73e0d 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp @@ -1097,7 +1097,8 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room bool shareDecklistsOnLoad = cmd.has_share_decklists_on_load() ? cmd.share_decklists_on_load() : false; bool isTournament = cmd.has_is_tournament() ? cmd.is_tournament() : false; - int gamesPerMatch = cmd.has_games_per_match() ? static_cast(cmd.games_per_match()) : 1; + int gamesPerMatch = + cmd.has_tournament_settings() ? static_cast(cmd.tournament_settings().games_per_match()) : 1; const int gameId = databaseInterface->getNextGameId(); if (gameId == -1) { From b8229bfba637e9ab9f5b882e5df43dab407d8cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 09:49:53 +0200 Subject: [PATCH 04/18] [Server] Replay tournament state to late joiners The bracket, phase and standings live in Event_TournamentState, which only flows on mutation. Without a copy a player or spectator joining after round one would sit on an empty bracket until the next advance, so the current state is now enqueued as part of the join snapshot. Extracts Event_TournamentState building into buildStateEvent() so both the broadcast path and the join path share one source of truth. --- .../network/server/remote/game/server_game.cpp | 9 +++++++++ .../network/server/remote/game/server_tournament.cpp | 9 +++++++-- .../network/server/remote/game/server_tournament.h | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 775372ec5..65b439f93 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -898,6 +898,15 @@ void Server_Game::createGameJoinedEvent(Server_AbstractParticipant *joiningParti prepareGameEvent(event, deckOwner->getPlayerId(), &context)); } } + + // A tournament's bracket/phase/standings live in Event_TournamentState, which + // normally only flows on mutation. Without a copy here a late joiner would sit + // on an empty bracket until the next round advances, so replay the current + // state as part of the join snapshot. + if (tournament) { + rc.enqueuePostResponseItem(ServerMessage::GAME_EVENT_CONTAINER, + prepareGameEvent(tournament->buildStateEvent(), -1)); + } } void Server_Game::sendGameEventContainer(GameEventContainer *cont, diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp index 527f2e728..65529072f 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -635,7 +635,7 @@ void Server_Tournament::checkAndAdvanceRound(GameEventStorage &ges) } } -void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) +Event_TournamentState Server_Tournament::buildStateEvent() const { QMutexLocker locker(&tournamentMutex); @@ -682,5 +682,10 @@ void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) p->set_player2_match_wins(pairing.player2MatchWins); } - ges.enqueueGameEvent(state, -1); + return state; +} + +void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) +{ + ges.enqueueGameEvent(buildStateEvent(), -1); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h index aeb3ea732..438490e84 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -36,6 +36,8 @@ public: void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); bool recordMatchResultByGameId(int gameId, int winnerId, GameEventStorage &ges); void broadcastTournamentState(GameEventStorage &ges); + // Current tournament state message, for replaying to a participant joining late. + Event_TournamentState buildStateEvent() const; bool isStarted() const { From a89e1a89d09223f45de8c7f2d3398600e4256cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 10:28:12 +0200 Subject: [PATCH 05/18] [Server] Stop mirroring is_tournament on the game-state event The client learns tournament status from Event_GameJoined's ServerInfo_Game; mirroring it on every Event_GameStateChanged was dead wire data. The field was removed from the proto in [Protocol]; drop the now-invalidated setter. --- .../libcockatrice/network/server/remote/game/server_game.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 65b439f93..008af02d2 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -310,8 +310,6 @@ void Server_Game::createGameStateChangedEvent(Event_GameStateChanged *event, event->set_game_started(false); } - event->set_is_tournament(isTournament); - if (tournamentParentGame) { event->set_parent_game_id(tournamentParentGame->getGameId()); } From e4ad97847d155078073ded069cb653a9864c6b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sun, 11 Oct 2026 14:58:06 +0200 Subject: [PATCH 06/18] [Tournament] Fix GameConfig forward declaration and annotate command handlers Forward-declare GameConfig as struct to match its definition in game_config.h; the class/struct mismatch trips -Werror -Wmismatched-tags on macOS. Also add the missing /// Server: annotations on the tournament GameCommandType entries now that the Server_AbstractParticipant handlers they point at exist in the tree. --- .../network/server/remote/game/server_match_game_factory.h | 2 +- .../libcockatrice/protocol/pb/game_commands.proto | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h index d5cf81cb6..3963b99d2 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h @@ -3,7 +3,7 @@ #include -class GameConfig; +struct GameConfig; class Server_Game; class Server_AbstractUserInterface; diff --git a/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto b/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto index 68a79eb6b..21560278c 100644 --- a/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto +++ b/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto @@ -182,12 +182,18 @@ message GameCommand { SET_PLAYMAT = 1035; /// Report the result of a tournament match sub-game. + /// Server: Server_AbstractParticipant::cmdReportMatchResult + /// Client: reflected via tournament state events REPORT_MATCH_RESULT = 1037; /// Advance the tournament to the next round. + /// Server: Server_AbstractParticipant::cmdAdvanceTournament + /// Client: reflected via tournament state events ADVANCE_TOURNAMENT = 1038; /// Select tournament settings. + /// Server: Server_AbstractParticipant::cmdTournamentSettingsSelect + /// Client: reflected via tournament state events TOURNAMENT_SETTINGS_SELECT = 1039; } From b80a097a94afb937193c19730f70398a9cdd90c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sun, 11 Oct 2026 17:37:30 +0200 Subject: [PATCH 07/18] [Server] Fix tournament stuck-round and sub-game start paths Gate the host-only start on the hub object so match sub-games can start, advance the round when a drop decides the last pairing (and on late results), settle disconnected pairings instead of stalling, and decide exhausted series by game wins. --- .../server/remote/game/server_game.cpp | 5 +- .../server/remote/game/server_tournament.cpp | 219 +++++++++++------- .../server/remote/game/server_tournament.h | 2 +- 3 files changed, 138 insertions(+), 88 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 008af02d2..46dd84db7 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -369,7 +369,8 @@ void Server_Game::doStartGameIfReady(bool forceStartGame) // Tournament hubs must be host-started and can't lock in a partially filled // bracket: a mere "everyone current is ready" must not start a 1-player or // undersized tournament. startTournament() additionally enforces 2+ players. - if (isTournament && !forceStartGame) { + // Match sub-games carry isTournament too, so gate on the hub object only. + if (tournament && !forceStartGame) { return; } @@ -633,7 +634,7 @@ void Server_Game::removeParticipant(Server_AbstractParticipant *participant, Eve if (tournament && !tournamentParentGame && !spectator) { const int leavingPlayerId = participant->getPlayerId(); GameEventStorage tournGes; - tournament->dropPlayer(leavingPlayerId); + tournament->dropPlayer(leavingPlayerId, tournGes); tournament->broadcastTournamentState(tournGes); tournGes.sendToGame(this); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp index 65529072f..f8345c96d 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -57,37 +57,41 @@ void Server_Tournament::removePlayer(int playerId) byeGivenPlayers.remove(playerId); } -void Server_Tournament::dropPlayer(int playerId) +void Server_Tournament::dropPlayer(int playerId, GameEventStorage &ges) { - QMutexLocker locker(&tournamentMutex); - if (!players.contains(playerId)) { - return; - } - players[playerId].dropped = true; - players[playerId].deckSubmitted = false; + { + QMutexLocker locker(&tournamentMutex); + if (!players.contains(playerId)) { + return; + } + players[playerId].dropped = true; + players[playerId].deckSubmitted = false; - // Any current pairing that involves the dropped player and is not already - // decided is awarded to the surviving opponent (or recorded as undecided if - // both dropped). The opponent keeps playing without sitting out a round. - for (auto &pairing : currentPairings) { - if (pairing.winnerId != -2) { - continue; + // Any current pairing that involves the dropped player and is not already + // decided is awarded to the surviving opponent (or recorded as undecided if + // both dropped). The opponent keeps playing without sitting out a round. + for (auto &pairing : currentPairings) { + if (pairing.winnerId != -2) { + continue; + } + bool involvesDropped = (pairing.player1Id == playerId || pairing.player2Id == playerId); + if (!involvesDropped) { + continue; + } + if (pairing.player1Id == playerId && pairing.player2Id == playerId) { + continue; + } + int opponent = (pairing.player1Id == playerId) ? pairing.player2Id : pairing.player1Id; + if (players.contains(opponent) && !players[opponent].dropped) { + pairing.winnerId = opponent; + players[opponent].wins += 1; + players[playerId].losses += 1; + } + allPreviousPairings.append(qMakePair(pairing.player1Id, pairing.player2Id)); } - bool involvesDropped = (pairing.player1Id == playerId || pairing.player2Id == playerId); - if (!involvesDropped) { - continue; - } - if (pairing.player1Id == playerId && pairing.player2Id == playerId) { - continue; - } - int opponent = (pairing.player1Id == playerId) ? pairing.player2Id : pairing.player1Id; - if (players.contains(opponent) && !players[opponent].dropped) { - pairing.winnerId = opponent; - players[opponent].wins += 1; - players[playerId].losses += 1; - } - allPreviousPairings.append(qMakePair(pairing.player1Id, pairing.player2Id)); } + // A drop can decide the last open pairing of the round; advance when complete. + checkAndAdvanceRound(ges); } void Server_Tournament::startTournament() @@ -332,6 +336,7 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) QString deck2Native; int round = 0; int gameNumber = 1; + bool settledWithoutGame = false; { QMutexLocker locker(&tournamentMutex); player1Name = players.value(player1Id).playerName; @@ -363,13 +368,44 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) // Bail out if either participant is no longer connected: a match game with // zero or one connected player can never finish and would stall the round. - if (!matchGameFactory->getUserInterface(player1Name) || !matchGameFactory->getUserInterface(player2Name)) { + // Settle the pairing like dropPlayer does so the round can still advance. + auto *ui1 = matchGameFactory->getUserInterface(player1Name); + auto *ui2 = matchGameFactory->getUserInterface(player2Name); + if (!ui1 || !ui2) { qCWarning(TournamentLog) << "Skipping match creation: a player in pairing" << player1Id << player2Id << "is no longer connected"; - return; + for (auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id && pairing.winnerId == -2) { + if (ui1 && !ui2) { + pairing.winnerId = player1Id; + players[player1Id].wins += 1; + players[player2Id].losses += 1; + } else if (ui2 && !ui1) { + pairing.winnerId = player2Id; + players[player2Id].wins += 1; + players[player1Id].losses += 1; + } else { + pairing.winnerId = -1; + players[player1Id].draws += 1; + players[player2Id].draws += 1; + } + allPreviousPairings.append(qMakePair(player1Id, player2Id)); + break; + } + } + settledWithoutGame = true; } } + if (settledWithoutGame) { + GameEventStorage ges; + broadcastTournamentState(ges); + checkAndAdvanceRound(ges); + broadcastTournamentState(ges); + ges.sendToGame(parentGame); + return; + } + // Create a sub-game for this match via the factory, copying the real // ServerInfo_User so it ships the true user level rather than a fabricated // admin identity that would surface in buddy/ignore-list checks. @@ -543,65 +579,78 @@ bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, Game } // If the match is already decided, ignore further sub-game results + // but still check whether the round can advance (e.g. a drop decided + // the last pairing and this late result is the first chance to notice). if (pairingPtr->winnerId != -2) { - return true; - } - - // Increment per-match wins - if (winnerId == pairingPtr->player1Id) { - pairingPtr->player1MatchWins += 1; - } else if (winnerId == pairingPtr->player2Id) { - pairingPtr->player2MatchWins += 1; - } - // Draw (winnerId == -1): counts nothing toward the series but does consume - // a slot, so a series can still end in a draw when it is exhausted. - - // The winner needs a strict majority of the games in the series. - const int gamesPlayed = pairingPtr->matchGameIds.size(); - const int gamesNeeded = static_cast(gamesPerMatch / 2 + 1); - const int gamesRemaining = static_cast(gamesPerMatch) - gamesPlayed; - matchDecided = (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); - if (!matchDecided) { - // Series exhausted without a strict-majority winner (e.g. a drawn Bo3 - // leaves it 1-1): record the match as a draw so the round always advances. - matchDecided = (gamesRemaining <= 0) && (pairingPtr->player1MatchWins == pairingPtr->player2MatchWins); - } - - if (matchDecided) { - // Determine match winner - int matchWinnerId = -1; - if (pairingPtr->player1MatchWins >= gamesNeeded) { - matchWinnerId = pairingPtr->player1Id; - } else if (pairingPtr->player2MatchWins >= gamesNeeded) { - matchWinnerId = pairingPtr->player2Id; - } - // Otherwise the series was exhausted evenly — matchWinnerId stays -1 (a draw). - - // Set the match winner on the pairing - pairingPtr->winnerId = matchWinnerId; - - // Update tournament-level player records - if (matchWinnerId == pairingPtr->player1Id) { - players[pairingPtr->player1Id].wins += 1; - players[pairingPtr->player2Id].losses += 1; - } else if (matchWinnerId == pairingPtr->player2Id) { - players[pairingPtr->player2Id].wins += 1; - players[pairingPtr->player1Id].losses += 1; - } else { - players[pairingPtr->player1Id].draws += 1; - players[pairingPtr->player2Id].draws += 1; - } - - // Store for future pairing avoidance - allPreviousPairings.append(qMakePair(pairingPtr->player1Id, pairingPtr->player2Id)); + broadcastTournamentState(ges); + // Fall through to checkAndAdvanceRound below. + matchDecided = true; + seriesContinues = false; } else { - // Match not decided — spawn the next sub-game outside all locks - seriesContinues = true; - p1 = pairingPtr->player1Id; - p2 = pairingPtr->player2Id; - } + // Increment per-match wins + if (winnerId == pairingPtr->player1Id) { + pairingPtr->player1MatchWins += 1; + } else if (winnerId == pairingPtr->player2Id) { + pairingPtr->player2MatchWins += 1; + } + // Draw (winnerId == -1): counts nothing toward the series but does consume + // a slot, so a series can still end in a draw when it is exhausted. - broadcastTournamentState(ges); + // The winner needs a strict majority of the games in the series. + const int gamesPlayed = pairingPtr->matchGameIds.size(); + const int gamesNeeded = static_cast(gamesPerMatch / 2 + 1); + const int gamesRemaining = static_cast(gamesPerMatch) - gamesPlayed; + matchDecided = + (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); + if (!matchDecided && gamesRemaining <= 0) { + // Series exhausted: draws can leave the wins unequal without either + // side reaching a majority (e.g. Bo3 going win/draw/draw for 1-0). + // Decide by game wins, draw only on a tie, so the round advances. + matchDecided = true; + } + + if (matchDecided) { + // Determine match winner + int matchWinnerId = -1; + if (pairingPtr->player1MatchWins >= gamesNeeded) { + matchWinnerId = pairingPtr->player1Id; + } else if (pairingPtr->player2MatchWins >= gamesNeeded) { + matchWinnerId = pairingPtr->player2Id; + } else if (gamesRemaining <= 0) { + if (pairingPtr->player1MatchWins > pairingPtr->player2MatchWins) { + matchWinnerId = pairingPtr->player1Id; + } else if (pairingPtr->player2MatchWins > pairingPtr->player1MatchWins) { + matchWinnerId = pairingPtr->player2Id; + } + } + // Otherwise the series was exhausted evenly — matchWinnerId stays -1 (a draw). + + // Set the match winner on the pairing + pairingPtr->winnerId = matchWinnerId; + + // Update tournament-level player records + if (matchWinnerId == pairingPtr->player1Id) { + players[pairingPtr->player1Id].wins += 1; + players[pairingPtr->player2Id].losses += 1; + } else if (matchWinnerId == pairingPtr->player2Id) { + players[pairingPtr->player2Id].wins += 1; + players[pairingPtr->player1Id].losses += 1; + } else { + players[pairingPtr->player1Id].draws += 1; + players[pairingPtr->player2Id].draws += 1; + } + + // Store for future pairing avoidance + allPreviousPairings.append(qMakePair(pairingPtr->player1Id, pairingPtr->player2Id)); + } else { + // Match not decided — spawn the next sub-game outside all locks + seriesContinues = true; + p1 = pairingPtr->player1Id; + p2 = pairingPtr->player2Id; + } + + broadcastTournamentState(ges); + } // else (undecided pairing) } if (seriesContinues) { diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h index 438490e84..3d976da7b 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -30,7 +30,7 @@ public: void removePlayer(int playerId); // Marks an already-starting/started tournament player as dropped: they stop // being paired and their outstanding unstarted match is awarded as a loss. - void dropPlayer(int playerId); + void dropPlayer(int playerId, GameEventStorage &ges); void startTournament(); void advanceRound(GameEventStorage &ges); void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); From 5add13a9c4923dd16a28563faeff1629bff007d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 24 Aug 2026 09:07:11 +0200 Subject: [PATCH 08/18] [Client] Add tournament event plumbing and player extension points Wires the client game layer for tournament state without touching existing behavior: - GameEventHandler dispatches Event_TournamentState (2027) to a new tournamentStateChanged signal - GameMetaInfo exposes isTournament and parentGameId over the new ServerInfo_Game field - PlayerEventHandler::processGameEvent is virtual and player is protected; PlayerLogic gains a protected constructor accepting a custom handler, so mode-specific subclasses can intercept events --- .../libcockatrice/game/game_event_handler.cpp | 10 ++++++++ .../libcockatrice/game/game_event_handler.h | 2 ++ .../libcockatrice/game/game_meta_info.h | 21 ++++++++++++++++ .../game/player/player_event_handler.h | 10 ++++---- .../game/player/player_logic.cpp | 13 ++++++++++ .../libcockatrice/game/player/player_logic.h | 25 ++++++++++++++++++- 6 files changed, 75 insertions(+), 6 deletions(-) diff --git a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp index 856475dfa..27526a361 100644 --- a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp +++ b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -189,6 +190,9 @@ void GameEventHandler::processGameEventContainer(const GameEventContainer &cont, case GameEvent::REVERSE_TURN: eventReverseTurn(event.GetExtension(Event_ReverseTurn::ext), playerId, context); break; + case GameEvent::TOURNAMENT_STATE: + emit tournamentStateChanged(event.GetExtension(Event_TournamentState::ext)); + break; default: { PlayerLogic *player = game->getPlayerManager()->getPlayers().value(playerId, 0); @@ -375,6 +379,12 @@ void GameEventHandler::eventGameStateChanged(const Event_GameStateChanged &event int /*eventPlayerId*/, const GameEventContext & /*context*/) { + // Sub-games of a tournament report their parent hub game so the client can + // route "close game" back to the parent tab. + if (event.parent_game_id() != -1) { + game->getGameMetaInfo()->setParentGameId(event.parent_game_id()); + } + const int playerListSize = event.player_list_size(); QVector>> opponentDecksToDisplay; diff --git a/libcockatrice_game/libcockatrice/game/game_event_handler.h b/libcockatrice_game/libcockatrice/game/game_event_handler.h index f7b11780b..1320d053b 100644 --- a/libcockatrice_game/libcockatrice/game/game_event_handler.h +++ b/libcockatrice_game/libcockatrice/game/game_event_handler.h @@ -50,6 +50,7 @@ class Event_SetActivePhase; class Event_GameSay; class Event_Kicked; class Event_ReverseTurn; +class Event_TournamentState; class Event_Ping; class ServerInfo_PlayerProperties; namespace google @@ -415,6 +416,7 @@ signals: void gameStopped(); void gameClosed(); + void tournamentStateChanged(const Event_TournamentState &state); void playerPropertiesChanged(const ServerInfo_PlayerProperties &prop, int playerId); void playerJoined(const ServerInfo_PlayerProperties &playerInfo); void playerLeft(int leavingPlayerId); diff --git a/libcockatrice_game/libcockatrice/game/game_meta_info.h b/libcockatrice_game/libcockatrice/game/game_meta_info.h index ef8f0c08e..0ea5cd7cc 100644 --- a/libcockatrice_game/libcockatrice/game/game_meta_info.h +++ b/libcockatrice_game/libcockatrice/game/game_meta_info.h @@ -87,6 +87,26 @@ public: return roomGameTypes.find(gameInfo_.game_types(index)).value(); } + bool isTournament() const + { + return gameInfo_.is_tournament(); + } + + void setIsTournament(bool t) + { + gameInfo_.set_is_tournament(t); + } + + int parentGameId() const + { + return parentGameId_; + } + + void setParentGameId(int id) + { + parentGameId_ = id; + } + public slots: void setStarted(bool s) { @@ -111,6 +131,7 @@ signals: private: ServerInfo_Game gameInfo_; + int parentGameId_ = -1; }; #endif // GAME_META_INFO_H diff --git a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h index d253deef0..59e448d7a 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h +++ b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h @@ -93,10 +93,10 @@ public: * @param context Additional context (undo, judge, etc.). * @param options Processing options (UI suppression, reveal behavior). */ - void processGameEvent(GameEvent::GameEventType type, - const GameEvent &event, - const GameEventContext &context, - EventProcessingOptions options); + virtual void processGameEvent(GameEvent::GameEventType type, + const GameEvent &event, + const GameEventContext &context, + EventProcessingOptions options); /** @} */ @@ -277,7 +277,7 @@ signals: */ void cardViewRefreshRequested(CardState *card, bool zoneChanged, PlayerLogic *newOwner); -private: +protected: /** Owning player instance. */ PlayerLogic *player; diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp index 96bd07164..9098729eb 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp @@ -49,6 +49,19 @@ PlayerLogic::PlayerLogic(const ServerInfo_User &info, initializeZones(); } +PlayerLogic::PlayerLogic(const ServerInfo_User &info, + int _id, + bool _local, + bool _judge, + AbstractGame *_parent, + PlayerEventHandler *customEventHandler) + : QObject(_parent), game(_parent), playerInfo(new PlayerInfo(info, _id, _local, _judge)), + playerEventHandler(customEventHandler), playerActions(new PlayerActions(this)), active(false), conceded(false), + zoneId(0), dialogSemaphore(false) +{ + initializeZones(); +} + void PlayerLogic::initializeZones() { addZone(new PileZoneLogic(this, ZoneNames::DECK, false, true, false, this)); diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.h b/libcockatrice_game/libcockatrice/game/player/player_logic.h index d6c4f5187..168d5fd5f 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.h +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.h @@ -103,7 +103,30 @@ public: ICardPreferenceProvider *cardPreferences = nullptr); ~PlayerLogic() override; - void initializeZones(); +protected: + /** + * @brief Constructor for subclasses that need a custom event handler (e.g. DraftPlayerLogic). + * + * @p customEventHandler must be non-null and either QObject-parented to this PlayerLogic + * or deleted externally; it is not owned by PlayerLogic. The handler connects to @c player + * during its own constructor, so passing a freshly built subclass handler from an + * initializer list is safe. + */ + PlayerLogic(const ServerInfo_User &info, + int _id, + bool _local, + bool _judge, + AbstractGame *_parent, + PlayerEventHandler *customEventHandler); + +public: + /** + * @brief Creates the standard zone set. + * + * Not virtually dispatched from constructors — subclasses overriding this must add + * their extra zones in their own constructor body. + */ + virtual void initializeZones(); void updateZones(); void clear(); From e003ac0a45eea6410a86ab5c1e8e0146db71e2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 10:27:44 +0200 Subject: [PATCH 09/18] [Client] Drop dead player event handler extension points MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The second PlayerLogic constructor taking a custom PlayerEventHandler was UB (converting this to PlayerLogic* in the base initializer, and connecting a freshly-built handler against a not-yet-constructed object), and its abstract extension surface — virtual initializeZones, virtual getPlayerEventHandler, virtual processGameEvent, protected player member — has no subclass consumer in the stack. Revert to the single primary constructor. Also: - Drop the unused GameMetaInfo::setIsTournament setter. Tournament status is authoritative from Event_GameJoined's ServerInfo_Game; the redundant is_tournament field on Event_GameStateChanged is removed in [Protocol]. - Gate the parent-game routing on has_parent_game_id() instead of comparing the proto default, so a future drop of the default keeps working. --- .../libcockatrice/game/game_event_handler.cpp | 5 ++-- .../libcockatrice/game/game_meta_info.h | 5 ---- .../game/player/player_event_handler.h | 10 ++++---- .../game/player/player_logic.cpp | 13 ---------- .../libcockatrice/game/player/player_logic.h | 25 +------------------ 5 files changed, 9 insertions(+), 49 deletions(-) diff --git a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp index 27526a361..931e6425c 100644 --- a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp +++ b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp @@ -380,8 +380,9 @@ void GameEventHandler::eventGameStateChanged(const Event_GameStateChanged &event const GameEventContext & /*context*/) { // Sub-games of a tournament report their parent hub game so the client can - // route "close game" back to the parent tab. - if (event.parent_game_id() != -1) { + // route "close game" back to the parent tab. Use has_parent_game_id rather + // than comparing the default so a future drop of the proto default still works. + if (event.has_parent_game_id()) { game->getGameMetaInfo()->setParentGameId(event.parent_game_id()); } diff --git a/libcockatrice_game/libcockatrice/game/game_meta_info.h b/libcockatrice_game/libcockatrice/game/game_meta_info.h index 0ea5cd7cc..b408ef061 100644 --- a/libcockatrice_game/libcockatrice/game/game_meta_info.h +++ b/libcockatrice_game/libcockatrice/game/game_meta_info.h @@ -92,11 +92,6 @@ public: return gameInfo_.is_tournament(); } - void setIsTournament(bool t) - { - gameInfo_.set_is_tournament(t); - } - int parentGameId() const { return parentGameId_; diff --git a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h index 59e448d7a..d253deef0 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h +++ b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h @@ -93,10 +93,10 @@ public: * @param context Additional context (undo, judge, etc.). * @param options Processing options (UI suppression, reveal behavior). */ - virtual void processGameEvent(GameEvent::GameEventType type, - const GameEvent &event, - const GameEventContext &context, - EventProcessingOptions options); + void processGameEvent(GameEvent::GameEventType type, + const GameEvent &event, + const GameEventContext &context, + EventProcessingOptions options); /** @} */ @@ -277,7 +277,7 @@ signals: */ void cardViewRefreshRequested(CardState *card, bool zoneChanged, PlayerLogic *newOwner); -protected: +private: /** Owning player instance. */ PlayerLogic *player; diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp index 9098729eb..96bd07164 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp @@ -49,19 +49,6 @@ PlayerLogic::PlayerLogic(const ServerInfo_User &info, initializeZones(); } -PlayerLogic::PlayerLogic(const ServerInfo_User &info, - int _id, - bool _local, - bool _judge, - AbstractGame *_parent, - PlayerEventHandler *customEventHandler) - : QObject(_parent), game(_parent), playerInfo(new PlayerInfo(info, _id, _local, _judge)), - playerEventHandler(customEventHandler), playerActions(new PlayerActions(this)), active(false), conceded(false), - zoneId(0), dialogSemaphore(false) -{ - initializeZones(); -} - void PlayerLogic::initializeZones() { addZone(new PileZoneLogic(this, ZoneNames::DECK, false, true, false, this)); diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.h b/libcockatrice_game/libcockatrice/game/player/player_logic.h index 168d5fd5f..d6c4f5187 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.h +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.h @@ -103,30 +103,7 @@ public: ICardPreferenceProvider *cardPreferences = nullptr); ~PlayerLogic() override; -protected: - /** - * @brief Constructor for subclasses that need a custom event handler (e.g. DraftPlayerLogic). - * - * @p customEventHandler must be non-null and either QObject-parented to this PlayerLogic - * or deleted externally; it is not owned by PlayerLogic. The handler connects to @c player - * during its own constructor, so passing a freshly built subclass handler from an - * initializer list is safe. - */ - PlayerLogic(const ServerInfo_User &info, - int _id, - bool _local, - bool _judge, - AbstractGame *_parent, - PlayerEventHandler *customEventHandler); - -public: - /** - * @brief Creates the standard zone set. - * - * Not virtually dispatched from constructors — subclasses overriding this must add - * their extra zones in their own constructor body. - */ - virtual void initializeZones(); + void initializeZones(); void updateZones(); void clear(); From fe6d4e9e5f2fd7c3c010c150377ce7b3c5e5f42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 24 Aug 2026 10:09:37 +0200 Subject: [PATCH 10/18] [Server] Implement Swiss tournament engine for lobby games Adds the server-side tournament mode driven through the strategy and factory interfaces from #7132: - Server_Tournament runs greedy Swiss pairing with rematch avoidance, byes, ceil(log2(n)) rounds, and best-of-N series via gamesPerMatch; match sub-games are spawned through the parent game's Server_MatchGameFactory implementation with players auto-joined and their submitted decks installed - Tournament lifecycle strategy gates the hub game start until all decks are submitted; match result strategy reports sub-game outcomes back to the parent tournament - Server_Game gains tournament state (isTournament flag, settings, parent link), protocol fields in getInfo/game state events, a full-lobby bypass for tournament hubs, and disconnectRemovesPlayer plumbing for upcoming draft modes - Match-game creation is deferred to the owning thread's event loop so room registration never nests lock orders; shared state is guarded by the tournament mutex --- .../network/server/remote/CMakeLists.txt | 8 +- .../game/server_abstract_participant.cpp | 2 +- .../server/remote/game/server_game.cpp | 101 +++- .../network/server/remote/game/server_game.h | 61 +- .../server/remote/game/server_tournament.cpp | 556 ++++++++++++++++++ .../server/remote/game/server_tournament.h | 106 ++++ .../server_tournament_lifecycle_strategy.cpp | 33 ++ .../server_tournament_lifecycle_strategy.h | 12 + ...erver_tournament_match_result_strategy.cpp | 34 ++ .../server_tournament_match_result_strategy.h | 12 + .../server/remote/server_protocolhandler.cpp | 8 + 11 files changed, 927 insertions(+), 6 deletions(-) create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp create mode 100644 libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h diff --git a/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt b/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt index 26c2be69a..b429b8eb4 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt +++ b/libcockatrice_network/libcockatrice/network/server/remote/CMakeLists.txt @@ -15,9 +15,12 @@ set(HEADERS game/server_deck_validation_strategy.h game/server_game.h game/server_game_lifecycle_strategy.h - game/server_match_result_strategy.h game/server_match_game_factory.h + game/server_match_result_strategy.h game/server_player.h + game/server_tournament.h + game/server_tournament_lifecycle_strategy.h + game/server_tournament_match_result_strategy.h game/server_spectator.h server.h server_abstractuserinterface.h @@ -43,6 +46,9 @@ add_library( game/server_game.cpp game/server_player.cpp game/server_spectator.cpp + game/server_tournament.cpp + game/server_tournament_lifecycle_strategy.cpp + game/server_tournament_match_result_strategy.cpp server.cpp server_abstractuserinterface.cpp server_database_interface.cpp diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp index 6c5d557b1..96ff74dd4 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_abstract_participant.cpp @@ -587,7 +587,7 @@ void Server_AbstractParticipant::setUserInterface(Server_AbstractUserInterface * void Server_AbstractParticipant::disconnectClient() { bool isRegistered = userInfo->user_level() & ServerInfo_User::IsRegistered; - if (!isRegistered || spectator) { + if (!isRegistered || spectator || game->getDisconnectRemovesPlayer()) { game->removeParticipant(this, Event_Leave::USER_DISCONNECTED); } else { setUserInterface(nullptr); diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index b1863c91e..295cbebf0 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -49,6 +49,9 @@ #include "server_player.h" #include "server_response_containers.h" #include "server_spectator.h" +#include "server_tournament.h" +#include "server_tournament_lifecycle_strategy.h" +#include "server_tournament_match_result_strategy.h" #include #include @@ -59,6 +62,7 @@ #include #include #include +#include #include #include #include @@ -91,8 +95,9 @@ Server_Game::Server_Game(const GameConfig &config, Server_Room *_room) 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), - deckValidationStrategy(new Server_DefaultDeckValidationStrategy), + turnOrderReversed(false), startTime(QDateTime::currentDateTime()), pingClock(nullptr), isTournament(false), + tournament(nullptr), tournamentParentGame(nullptr), tournamentMatchPlayer1Id(-1), tournamentMatchPlayer2Id(-1), + disconnectRemovesPlayer(false), deckValidationStrategy(new Server_DefaultDeckValidationStrategy), lifecycleStrategy(new Server_DefaultLifecycleStrategy), matchResultStrategy(new Server_NullMatchResultStrategy), gameMutex() { @@ -305,6 +310,12 @@ void Server_Game::createGameStateChangedEvent(Event_GameStateChanged *event, event->set_game_started(false); } + event->set_is_tournament(isTournament); + + if (tournamentParentGame) { + event->set_parent_game_id(tournamentParentGame->getGameId()); + } + for (Server_AbstractParticipant *participant : participants.values()) { participant->getInfo(event->add_player_list(), recipient, omniscient, withUserInfo); } @@ -353,7 +364,7 @@ void Server_Game::doStartGameIfReady(bool forceStartGame) Server_DatabaseInterface *databaseInterface = room->getServer()->getDatabaseInterface(); QMutexLocker locker(&gameMutex); - if (getPlayerCount() < maxPlayers && !forceStartGame) { + if (!isTournament && getPlayerCount() < maxPlayers && !forceStartGame) { return; } @@ -947,6 +958,7 @@ void Server_Game::getInfo(ServerInfo_Game &result) const result.set_share_decklists_on_load(shareDecklistsOnLoad); result.set_spectators_count(getSpectatorCount()); result.set_start_time(startTime.toSecsSinceEpoch()); + result.set_is_tournament(isTournament); } } @@ -997,3 +1009,86 @@ void Server_Game::setDeckValidationStrategy(Server_DeckValidationStrategy *strat { deckValidationStrategy.reset(strategy); } + +void Server_Game::setMatchResultStrategy(Server_MatchResultStrategy *strategy) +{ + matchResultStrategy.reset(strategy); +} + +void Server_Game::setIsTournamentGame(bool _isTournament) +{ + isTournament = _isTournament; + if (isTournament) { + tournament = new Server_Tournament(this, this, this); + lifecycleStrategy.reset(new Server_TournamentLifecycleStrategy); + matchResultStrategy.reset(new Server_TournamentMatchResultStrategy); + } else if (tournament) { + delete tournament; + tournament = nullptr; + lifecycleStrategy.reset(new Server_DefaultLifecycleStrategy); + matchResultStrategy.reset(new Server_NullMatchResultStrategy); + } +} + +void Server_Game::startTournament() +{ + if (!tournament) { + tournament = new Server_Tournament(this, this, this); + } + + if (!tournament->isStarted()) { + // Add all current players to the tournament + auto players = getPlayers(); + for (auto *player : players.values()) { + tournament->addPlayer(player->getPlayerId(), QString::fromStdString(player->getUserInfo()->name())); + } + + tournament->startTournament(); + } + + GameEventStorage ges; + tournament->broadcastTournamentState(ges); + ges.sendToGame(this); +} + +void Server_Game::setPlayerTournamentDeck(int playerId, DeckList *deck) +{ + if (tournament) { + tournament->setPlayerDeck(playerId, deck); + } +} + +void Server_Game::setTournamentMatchInfo(Server_Game *parentGame, int p1Id, int p2Id) +{ + tournamentParentGame = parentGame; + tournamentMatchPlayer1Id = p1Id; + tournamentMatchPlayer2Id = p2Id; +} + +Server_Game *Server_Game::createMatchGame(const GameConfig &config, int &outGameId) +{ + Server_DatabaseInterface *databaseInterface = room->getServer()->getDatabaseInterface(); + outGameId = databaseInterface->getNextGameId(); + if (outGameId == -1) { + return nullptr; + } + + GameConfig matchConfig = config; + matchConfig.gameId = outGameId; + auto *game = new Server_Game(matchConfig, room); + // Sub-games carry the tournament flag (for protocol fields) but keep the default + // strategies; the parent tournament drives them through the match result strategy + // installed by Server_Tournament::createMatchGame. + game->isTournament = true; + return game; +} + +Server_AbstractUserInterface *Server_Game::getUserInterface(const QString &playerName) +{ + return room->getUserInterfaceByName(playerName); +} + +void Server_Game::addGameToRoom(Server_Game *game) +{ + room->addGame(game); +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h index c7f8d0929..3b8d794dc 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h @@ -21,7 +21,12 @@ #define SERVERGAME_H #include "../server_response_containers.h" +#include "game_config.h" #include "libcockatrice/protocol/pb/game_event_context.pb.h" +#include "server_deck_validation_strategy.h" +#include "server_game_lifecycle_strategy.h" +#include "server_match_game_factory.h" +#include "server_match_result_strategy.h" // IWYU pragma: keep // ServerInfo_Game is passed by value to the gameInfoChanged() signal, so the @@ -37,16 +42,21 @@ #include #include #include +#include #include #include #include class QTimer; +class DeckList; class GameEventContainer; +class GameEventStorage; class GameReplay; class Server_Room; class Server_AbstractPlayer; class Server_AbstractParticipant; +class Server_Card; +class Server_Tournament; class ServerInfo_User; class ServerInfo_Game; class Server_AbstractUserInterface; @@ -63,7 +73,7 @@ class Message; } // namespace google struct GameConfig; -class Server_Game : public QObject +class Server_Game : public QObject, public Server_MatchGameFactory { Q_OBJECT private: @@ -98,6 +108,14 @@ private: QList replayList; GameReplay *currentReplay; + bool isTournament; + TournamentSettings tournamentSettings; + Server_Tournament *tournament; + Server_Game *tournamentParentGame; + int tournamentMatchPlayer1Id; + int tournamentMatchPlayer2Id; + bool disconnectRemovesPlayer; + QScopedPointer deckValidationStrategy; QScopedPointer lifecycleStrategy; @@ -237,6 +255,45 @@ public: void returnCardsFromPlayer(GameEventStorage &ges, Server_AbstractPlayer *player); /** @brief Get the current deck validation strategy (non-owning). */ + bool getIsTournamentGame() const + { + return isTournament; + } + void setIsTournamentGame(bool _isTournament); + bool getIsTournament() const + { + return tournament != nullptr; + } + Server_Tournament *getTournament() const + { + return tournament; + } + void startTournament(); + void setPlayerTournamentDeck(int playerId, DeckList *deck); + void setTournamentMatchInfo(Server_Game *parentGame, int p1Id, int p2Id); + Server_Game *getTournamentParentGame() const + { + return tournamentParentGame; + } + bool getDisconnectRemovesPlayer() const + { + return disconnectRemovesPlayer; + } + + // Server_MatchGameFactory implementation + Server_Game *createMatchGame(const GameConfig &config, int &outGameId) override; + Server_AbstractUserInterface *getUserInterface(const QString &playerName) override; + void addGameToRoom(Server_Game *game) override; + + const TournamentSettings &getTournamentSettings() const + { + return tournamentSettings; + } + void setTournamentSettings(const TournamentSettings &settings) + { + tournamentSettings = settings; + } + Server_DeckValidationStrategy *getDeckValidationStrategy() const { return deckValidationStrategy.data(); @@ -249,6 +306,8 @@ public: { return lifecycleStrategy.data(); } + /** @brief Replace the match result strategy; takes ownership of @p strategy. */ + void setMatchResultStrategy(Server_MatchResultStrategy *strategy); }; #endif diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp new file mode 100644 index 000000000..1d13f31f7 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -0,0 +1,556 @@ +#include "server_tournament.h" + +#include "../server_abstractuserinterface.h" +#include "../server_response_containers.h" +#include "../serverinfo_user_container.h" +#include "game_config.h" +#include "server_abstract_player.h" +#include "server_game.h" +#include "server_match_game_factory.h" +#include "server_player.h" +#include "server_tournament_match_result_strategy.h" + +#include +#include +#include +#include +#include + +inline Q_LOGGING_CATEGORY(TournamentLog, "tournament"); + +Server_Tournament::Server_Tournament(Server_Game *_parentGame, Server_MatchGameFactory *_factory, QObject *parent) + : QObject(parent), parentGame(_parentGame), matchGameFactory(_factory), currentRound(0), totalRounds(0), + started(false) +{ +} + +Server_Tournament::~Server_Tournament() +{ + qDeleteAll(submittedDecks); +} + +void Server_Tournament::addPlayer(int playerId, const QString &playerName) +{ + TournamentPlayerData data; + data.playerId = playerId; + data.playerName = playerName; + players[playerId] = data; +} + +void Server_Tournament::setPlayerDeck(int playerId, DeckList *deck) +{ + QMutexLocker locker(&tournamentMutex); + delete submittedDecks.value(playerId, nullptr); + submittedDecks[playerId] = deck; + if (players.contains(playerId)) { + players[playerId].deckSubmitted = true; + } +} + +void Server_Tournament::removePlayer(int playerId) +{ + QMutexLocker locker(&tournamentMutex); + players.remove(playerId); +} + +void Server_Tournament::startTournament() +{ + { + QMutexLocker locker(&tournamentMutex); + if (started) { + return; + } + totalRounds = calculateTotalRounds(); + started = true; + currentRound = 0; + generateSwissPairings(); + } + + // Spawn the first round's match games + enqueueMatchGameCreation(); +} + +bool Server_Tournament::isAllDecksSubmitted() const +{ + QMutexLocker locker(&tournamentMutex); + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + if (!it->deckSubmitted) { + return false; + } + } + return true; +} + +int Server_Tournament::getTournamentPlayerIdByName(const QString &name) const +{ + QMutexLocker locker(&tournamentMutex); + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + if (it->playerName == name) { + return it->playerId; + } + } + return -1; +} + +void Server_Tournament::generateSwissPairings() +{ + currentPairings.clear(); + QList available; + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + available.append(it->playerId); + } + + // Sort by wins descending (and by record for tie-breaking) + std::sort(available.begin(), available.end(), [this](int a, int b) { + const auto &pa = players[a]; + const auto &pb = players[b]; + if (pa.wins != pb.wins) { + return pa.wins > pb.wins; + } + if (pa.losses != pb.losses) { + return pa.losses < pb.losses; + } + return a < b; + }); + + // Simple greedy Swiss pairing + QSet paired; + for (int i = 0; i < available.size(); ++i) { + if (paired.contains(available[i])) { + continue; + } + for (int j = i + 1; j < available.size(); ++j) { + if (paired.contains(available[j])) { + continue; + } + if (!havePlayed(available[i], available[j])) { + TournamentPairingData pairing; + pairing.player1Id = available[i]; + pairing.player2Id = available[j]; + currentPairings.append(pairing); + paired.insert(available[i]); + paired.insert(available[j]); + break; + } + } + } + + // Bye for unpaired player if odd count + for (int i = 0; i < available.size(); ++i) { + if (!paired.contains(available[i])) { + // Player gets a bye (auto-win) + TournamentPairingData bye; + bye.player1Id = available[i]; + bye.player2Id = -1; + bye.winnerId = available[i]; + bye.player1MatchWins = gamesPerMatch; // Match immediately decided + currentPairings.append(bye); + players[available[i]].wins += 1; + allPreviousPairings.append(qMakePair(available[i], -1)); + break; + } + } +} + +int Server_Tournament::calculateTotalRounds() const +{ + int n = players.size(); + if (n <= 1) { + return 0; + } + // Standard Swiss rounds: ceil(log2(n)) + int rounds = 0; + while ((1 << rounds) < n) { + ++rounds; + } + return rounds; +} + +bool Server_Tournament::havePlayed(int p1, int p2) const +{ + for (const auto &pair : allPreviousPairings) { + if ((pair.first == p1 && pair.second == p2) || (pair.first == p2 && pair.second == p1)) { + return true; + } + } + return false; +} + +void Server_Tournament::advanceRound(GameEventStorage &ges) +{ + { + QMutexLocker locker(&tournamentMutex); + ++currentRound; + if (currentRound >= totalRounds) { + broadcastTournamentState(ges); + return; + } + generateSwissPairings(); + } + + enqueueMatchGameCreation(); + broadcastTournamentState(ges); +} + +bool Server_Tournament::allPairingsDecided() const +{ + for (const auto &pairing : currentPairings) { + if (pairing.winnerId == -2) { + return false; + } + } + return true; +} + +void Server_Tournament::enqueueMatchGameCreation() +{ + QList> planned; + { + QMutexLocker locker(&tournamentMutex); + for (const auto &pairing : currentPairings) { + if (pairing.player2Id != -1 && pairing.winnerId == -2 && + pairing.matchGameIds.size() < static_cast(gamesPerMatch)) { + planned.append(qMakePair(pairing.player1Id, pairing.player2Id)); + } + } + } + if (planned.isEmpty()) { + return; + } + + // Create the games from the event loop instead of the caller's stack: command + // processing holds game mutexes, and room registration takes gamesLock, so spawning + // synchronously would nest lock orders. The queued job runs once this object's + // owning thread returns to its event loop with no locks held; it is dropped if this + // tournament is destroyed first. + QMetaObject::invokeMethod( + this, + [this, planned] { + for (const auto &pair : planned) { + createMatchGame(pair.first, pair.second); + } + + GameEventStorage ges; + broadcastTournamentState(ges); + ges.sendToGame(parentGame); + }, + Qt::QueuedConnection); +} + +void Server_Tournament::createMatchGame(int player1Id, int player2Id) +{ + if (!matchGameFactory || player2Id == -1) { + return; + } + + QString player1Name; + QString player2Name; + QString deck1Native; + QString deck2Native; + int round = 0; + int gameNumber = 1; + { + QMutexLocker locker(&tournamentMutex); + player1Name = players.value(player1Id).playerName; + player2Name = players.value(player2Id).playerName; + + if (submittedDecks.contains(player1Id)) { + deck1Native = submittedDecks.value(player1Id)->writeToString_Native(); + } + if (submittedDecks.contains(player2Id)) { + deck2Native = submittedDecks.value(player2Id)->writeToString_Native(); + } + + round = currentRound; + for (const auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id) { + gameNumber = pairing.matchGameIds.size() + 1; + break; + } + } + + // Defense in depth: never exceed the configured series length + for (const auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id && + pairing.matchGameIds.size() >= static_cast(gamesPerMatch)) { + qCWarning(TournamentLog) << "Refusing to exceed series length for pairing" << player1Id << player2Id; + return; + } + } + } + + // Create a sub-game for this match via the factory + ServerInfo_User creatorInfo; + creatorInfo.set_name(player1Name.toStdString()); + creatorInfo.set_user_level(ServerInfo_User::IsAdmin | ServerInfo_User::IsRegistered); + + QString gameDesc = gamesPerMatch > 1 + ? QString("R%1 Match - Game %2 of %3").arg(round).arg(gameNumber).arg(gamesPerMatch) + : QString("Tournament Round %1").arg(round); + + GameConfig matchConfig; + matchConfig.creatorInfo = creatorInfo; + matchConfig.description = gameDesc; + matchConfig.maxPlayers = 2; + matchConfig.startingLifeTotal = parentGame->getStartingLifeTotal(); + + int matchGameId = -1; + auto *matchGame = matchGameFactory->createMatchGame(matchConfig, matchGameId); + if (!matchGame || matchGameId == -1) { + return; + } + + matchGame->setTournamentMatchInfo(parentGame, player1Id, player2Id); + matchGame->setMatchResultStrategy(new Server_TournamentMatchResultStrategy); + matchGameFactory->addGameToRoom(matchGame); + + // Store the game ID in the pairing + { + QMutexLocker locker(&tournamentMutex); + for (auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id) { + pairing.gameId = matchGameId; + pairing.matchGameIds.append(matchGameId); + break; + } + } + } + + // Auto-join both players, sending the join event directly through their UIs. + QMap> joiners; + + auto joinAndSetupPlayer = [&](int pid, const QString &name) { + Server_AbstractUserInterface *ui = matchGameFactory->getUserInterface(name); + if (ui) { + auto *rc = new ResponseContainer(0); + matchGame->addPlayer(ui, *rc, false, false, false); + joiners[pid] = qMakePair(ui, rc); + } + }; + + joinAndSetupPlayer(player1Id, player1Name); + joinAndSetupPlayer(player2Id, player2Name); + + // Now send the enqueued GameJoined + GameStateChanged events to each player's client. + for (auto it = joiners.constBegin(); it != joiners.constEnd(); ++it) { + it.value().first->sendResponseContainer(*it.value().second, Response::RespNothing); + delete it.value().second; + } + joiners.clear(); + + // Set decks and mark players as ready in the match game + auto matchPlayers = matchGame->getPlayers(); + for (auto *matchPlayer : matchPlayers) { + const QString name = QString::fromStdString(matchPlayer->getUserInfo()->name()); + QString deckNative; + if (name == player1Name) { + deckNative = deck1Native; + } else if (name == player2Name) { + deckNative = deck2Native; + } + + if (!deckNative.isEmpty()) { + matchPlayer->setDeck(new DeckList(deckNative)); + matchPlayer->setReadyStart(true); + } + } + + // Start the match game + matchGame->startGameIfReady(true); +} + +void Server_Tournament::recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges) +{ + QMutexLocker locker(&tournamentMutex); + + // Find the pairing and set the winner + for (auto &pairing : currentPairings) { + if ((pairing.player1Id == playerId1 && pairing.player2Id == playerId2) || + (pairing.player1Id == playerId2 && pairing.player2Id == playerId1)) { + if (pairing.winnerId != -2) { + return; // Already recorded — defense in depth against double-call + } + pairing.winnerId = winnerId; + break; + } + } + + // Update player records + if (winnerId == -1) { + // Draw + players[playerId1].draws += 1; + players[playerId2].draws += 1; + } else if (winnerId == playerId1) { + players[playerId1].wins += 1; + players[playerId2].losses += 1; + } else if (winnerId == playerId2) { + players[playerId2].wins += 1; + players[playerId1].losses += 1; + } + + // Store for future pairing avoidance + allPreviousPairings.append(qMakePair(playerId1, playerId2)); + + // Check if all pairings in current round have results + bool allDecided = true; + for (const auto &pairing : currentPairings) { + if (pairing.winnerId == -2) { + allDecided = false; + break; + } + } + + broadcastTournamentState(ges); + + if (allDecided) { + advanceRound(ges); + } +} + +bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, GameEventStorage &ges) +{ + bool matchDecided = false; + bool seriesContinues = false; + int p1 = -1; + int p2 = -1; + { + QMutexLocker locker(&tournamentMutex); + + // Find the pairing that owns this game + TournamentPairingData *pairingPtr = nullptr; + for (auto &pairing : currentPairings) { + if (pairing.matchGameIds.contains(gameId)) { + pairingPtr = &pairing; + break; + } + } + + if (!pairingPtr) { + return false; + } + + // If the match is already decided, ignore further sub-game results + if (pairingPtr->winnerId != -2) { + return true; + } + + // Increment per-match wins + if (winnerId == pairingPtr->player1Id) { + pairingPtr->player1MatchWins += 1; + } else if (winnerId == pairingPtr->player2Id) { + pairingPtr->player2MatchWins += 1; + } + // Draw (winnerId == -1): no match wins incremented + + // Check if match is decided + const int gamesNeeded = static_cast(gamesPerMatch); + matchDecided = (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); + + if (matchDecided) { + // Determine match winner + int matchWinnerId; + if (pairingPtr->player1MatchWins >= gamesNeeded) { + matchWinnerId = pairingPtr->player1Id; + } else { + matchWinnerId = pairingPtr->player2Id; + } + + // Set the match winner on the pairing + pairingPtr->winnerId = matchWinnerId; + + // Update tournament-level player records + if (matchWinnerId == pairingPtr->player1Id) { + players[pairingPtr->player1Id].wins += 1; + players[pairingPtr->player2Id].losses += 1; + } else { + players[pairingPtr->player2Id].wins += 1; + players[pairingPtr->player1Id].losses += 1; + } + + // Store for future pairing avoidance + allPreviousPairings.append(qMakePair(pairingPtr->player1Id, pairingPtr->player2Id)); + } else { + // Match not decided — spawn the next sub-game outside all locks + seriesContinues = true; + p1 = pairingPtr->player1Id; + p2 = pairingPtr->player2Id; + } + + broadcastTournamentState(ges); + } + + if (seriesContinues) { + QMetaObject::invokeMethod( + this, + [this, p1, p2] { + createMatchGame(p1, p2); + + GameEventStorage nextGes; + broadcastTournamentState(nextGes); + nextGes.sendToGame(parentGame); + }, + Qt::QueuedConnection); + } + + checkAndAdvanceRound(ges); + + return matchDecided; +} + +void Server_Tournament::checkAndAdvanceRound(GameEventStorage &ges) +{ + bool roundComplete = false; + { + QMutexLocker locker(&tournamentMutex); + roundComplete = allPairingsDecided(); + } + + if (roundComplete) { + advanceRound(ges); + } +} + +void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) +{ + QMutexLocker locker(&tournamentMutex); + + Event_TournamentState state; + + if (started && currentRound >= totalRounds) { + state.set_phase(Event_TournamentState::PHASE_FINISHED); + } else if (started) { + state.set_phase(Event_TournamentState::PHASE_PLAYING); + } else { + state.set_phase(Event_TournamentState::PHASE_DECK_BUILDING); + } + + state.set_current_round(currentRound); + state.set_total_rounds(totalRounds); + + // Settings + TournamentSettings *settings = state.mutable_settings(); + settings->set_games_per_match(gamesPerMatch); + + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + TournamentPlayer *p = state.add_players(); + p->set_player_id(it->playerId); + p->set_player_name(it->playerName.toStdString()); + p->set_wins(it->wins); + p->set_losses(it->losses); + p->set_draws(it->draws); + p->set_deck_submitted(it->deckSubmitted); + } + + for (const auto &pairing : currentPairings) { + TournamentPairing *p = state.add_pairings(); + p->set_player1_id(pairing.player1Id); + p->set_player2_id(pairing.player2Id); + p->set_game_id(pairing.gameId); + // Map internal sentinel: -2 (undecided) -> -1 (no winner yet in proto) + p->set_winner_id(pairing.winnerId == -2 ? -1 : pairing.winnerId); + p->set_player1_match_wins(pairing.player1MatchWins); + p->set_player2_match_wins(pairing.player2MatchWins); + } + + ges.enqueueGameEvent(state, -1); +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h new file mode 100644 index 000000000..b59edda4f --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -0,0 +1,106 @@ +#ifndef SERVER_TOURNAMENT_H +#define SERVER_TOURNAMENT_H + +#include +#include +#include +#include +#include +#include + +class DeckList; +class Server_Game; +class Server_MatchGameFactory; +class Server_AbstractParticipant; +class Server_AbstractUserInterface; +class GameEventStorage; + +/** @brief Maximum number of games per match a tournament can be configured with. */ +constexpr int MAX_GAMES_PER_MATCH = 5; + +class Server_Tournament : public QObject +{ + Q_OBJECT +public: + explicit Server_Tournament(Server_Game *_parentGame, Server_MatchGameFactory *_factory, QObject *parent = nullptr); + ~Server_Tournament() override; + + void addPlayer(int playerId, const QString &playerName); + void removePlayer(int playerId); + void startTournament(); + void advanceRound(GameEventStorage &ges); + void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); + bool recordMatchResultByGameId(int gameId, int winnerId, GameEventStorage &ges); + void broadcastTournamentState(GameEventStorage &ges); + + bool isStarted() const + { + return started; + } + bool isAllDecksSubmitted() const; + int getPlayerCount() const + { + return players.size(); + } + int getTournamentPlayerIdByName(const QString &name) const; + void setPlayerDeckSubmitted(int playerId) + { + if (players.contains(playerId)) { + players[playerId].deckSubmitted = true; + } + } + void setPlayerDeck(int playerId, DeckList *deck); + + void setGamesPerMatch(uint32_t n) + { + gamesPerMatch = n; + } + uint32_t getGamesPerMatch() const + { + return gamesPerMatch; + } + + struct TournamentPlayerData + { + int playerId; + QString playerName; + int wins = 0; + int losses = 0; + int draws = 0; + bool deckSubmitted = false; + }; + + struct TournamentPairingData + { + int player1Id; + int player2Id; + int gameId = -1; + int winnerId = -2; // -2 = undecided, -1 = draw, >= 0 = winner player id + int player1MatchWins = 0; + int player2MatchWins = 0; + QList matchGameIds; + }; + +private: + Server_Game *parentGame; + Server_MatchGameFactory *matchGameFactory; + mutable QRecursiveMutex tournamentMutex; + QMap players; + QMap submittedDecks; + QList currentPairings; + QList> allPreviousPairings; + int currentRound; + int totalRounds; + bool started; + uint32_t gamesPerMatch = 1; + + void generateSwissPairings(); + int calculateTotalRounds() const; + bool havePlayed(int p1, int p2) const; + bool allPairingsDecided() const; + void createMatchGame(int player1Id, int player2Id); + void enqueueMatchGameCreation(); + void checkAndAdvanceRound(GameEventStorage &ges); +}; + +#endif // SERVER_TOURNAMENT_H diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp new file mode 100644 index 000000000..5149b9a26 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp @@ -0,0 +1,33 @@ +#include "server_tournament_lifecycle_strategy.h" + +#include "server_abstract_player.h" +#include "server_game.h" + +#include + +inline Q_LOGGING_CATEGORY(TournamentLifecycleLog, "tournament_lifecycle"); + +Server_GameLifecycleStrategy::StartAction Server_TournamentLifecycleStrategy::onGameStarting(Server_Game *game) +{ + // Match sub-games start through the normal flow; only the tournament hub game is + // managed by this lifecycle. + if (game->getTournamentParentGame() != nullptr) { + return StartAction::ProceedNormal; + } + + for (auto *player : game->getPlayers().values()) { + if (!player->getDeckList()) { + qCWarning(TournamentLifecycleLog) + << "Tournament cannot start: player" << player->getUserInfo()->name().c_str() << "has no deck"; + return StartAction::Handled; + } + } + + if (!game->getIsTournamentGame()) { + qCWarning(TournamentLifecycleLog) << "Tournament lifecycle used for non-tournament game — falling back"; + return StartAction::ProceedNormal; + } + + game->startTournament(); + return StartAction::Handled; +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h new file mode 100644 index 000000000..c6dbede1b --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.h @@ -0,0 +1,12 @@ +#ifndef SERVER_TOURNAMENT_LIFECYCLE_STRATEGY_H +#define SERVER_TOURNAMENT_LIFECYCLE_STRATEGY_H + +#include "server_game_lifecycle_strategy.h" + +class Server_TournamentLifecycleStrategy : public Server_GameLifecycleStrategy +{ +public: + StartAction onGameStarting(Server_Game *game) override; +}; + +#endif diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp new file mode 100644 index 000000000..5611224e5 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp @@ -0,0 +1,34 @@ +#include "server_tournament_match_result_strategy.h" + +#include "../server_response_containers.h" +#include "server_abstract_player.h" +#include "server_game.h" +#include "server_tournament.h" + +#include +#include + +bool Server_TournamentMatchResultStrategy::onGameFinished(Server_Game *game, + int playing, + Server_AbstractPlayer *lastPlayer) +{ + auto *parentGame = game->getTournamentParentGame(); + if (!parentGame || !parentGame->getTournament()) { + return false; + } + + int winnerId; + if (playing == 0) { + winnerId = -1; + } else { + QString winnerName = QString::fromStdString(lastPlayer->getUserInfo()->name()); + auto *tournament = parentGame->getTournament(); + winnerId = tournament->getTournamentPlayerIdByName(winnerName); + } + + GameEventStorage parentGes; + bool matchDecided = parentGame->getTournament()->recordMatchResultByGameId(game->getGameId(), winnerId, parentGes); + parentGes.sendToGame(parentGame); + + return matchDecided; +} diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h new file mode 100644 index 000000000..7bc3bafb8 --- /dev/null +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.h @@ -0,0 +1,12 @@ +#ifndef SERVER_TOURNAMENT_MATCH_RESULT_STRATEGY_H +#define SERVER_TOURNAMENT_MATCH_RESULT_STRATEGY_H + +#include "server_match_result_strategy.h" + +class Server_TournamentMatchResultStrategy : public Server_MatchResultStrategy +{ +public: + bool onGameFinished(Server_Game *game, int playing, Server_AbstractPlayer *lastPlayer) override; +}; + +#endif diff --git a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp index dd074a517..eacb9802c 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp @@ -6,6 +6,8 @@ static constexpr qint64 kAuthNonceLifetimeSeconds = 60; #include "game/game_config.h" #include "game/server_abstract_participant.h" #include "game/server_game.h" +#include "game/server_player.h" +#include "game/server_tournament.h" #include "libcockatrice/protocol/pb/admin_commands.pb.h" #include "libcockatrice/protocol/pb/developer_commands.pb.h" #include "libcockatrice/protocol/pb/game_commands.pb.h" @@ -1094,6 +1096,8 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room int startingLifeTotal = cmd.has_starting_life_total() ? cmd.starting_life_total() : 20; bool shareDecklistsOnLoad = cmd.has_share_decklists_on_load() ? cmd.share_decklists_on_load() : false; + bool isTournament = cmd.has_is_tournament() ? cmd.is_tournament() : false; + int gamesPerMatch = cmd.has_games_per_match() ? static_cast(cmd.games_per_match()) : 1; const int gameId = databaseInterface->getNextGameId(); if (gameId == -1) { @@ -1118,6 +1122,10 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room .shareDecklistsOnLoad = shareDecklistsOnLoad}; auto *game = new Server_Game(config, room); + game->setIsTournamentGame(isTournament); + if (isTournament && game->getTournament()) { + game->getTournament()->setGamesPerMatch(static_cast(qBound(1, gamesPerMatch, MAX_GAMES_PER_MATCH))); + } game->addPlayer(this, rc, asSpectator, asJudge, false); room->addGame(game); From e44ddc1138c30f554c45bc8cec000dcf1cf9c00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 08:57:26 +0200 Subject: [PATCH 11/18] [Server] Harden Swiss tournament engine against drops and stalls - link match sub-games to the hub through QPointer so a finished match can never dereference a torn-down parent game - a player who leaves the hub is dropped: no longer paired, current undecided match awarded to the opponent, absent from the bracket - refuse to spawn a match game when either participant is disconnected, and set disconnectRemovesPlayer on match games so a mid-match disconnect ends it instead of leaving a half-present participant - match winner is a strict majority (gamesPerMatch/2+1); an exhausted series with no majority is recorded as a draw so the round always advances - match games are started without force-start: a missing deck no longer kicks the player; the game stays open for deck selection - buyes are handed to every leftover player, worst-ranked first, at most one per player over the tournament - tournament hubs cannot start on mere 'everyone ready': host force-start is required and fewer than two players never starts - sub-game creator copies the real player user info instead of fabricating IsAdmin --- .../server/remote/game/server_game.cpp | 24 ++ .../network/server/remote/game/server_game.h | 9 +- .../server/remote/game/server_tournament.cpp | 212 ++++++++++++++---- .../server/remote/game/server_tournament.h | 10 +- .../server_tournament_lifecycle_strategy.cpp | 2 +- ...erver_tournament_match_result_strategy.cpp | 5 +- 6 files changed, 216 insertions(+), 46 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 295cbebf0..f536dd3ca 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -368,6 +368,13 @@ void Server_Game::doStartGameIfReady(bool forceStartGame) return; } + // Tournament hubs must be host-started and can't lock in a partially filled + // bracket: a mere "everyone current is ready" must not start a 1-player or + // undersized tournament. startTournament() additionally enforces 2+ players. + if (isTournament && !forceStartGame) { + return; + } + auto players = getPlayers(); for (auto *player : players.values()) { if (!player->getReadyStart()) { @@ -622,6 +629,17 @@ void Server_Game::removeParticipant(Server_AbstractParticipant *participant, Eve bool playerHost = hostId == participant->getPlayerId(); participant->prepareDestroy(); + // If this is the tournament hub (not one of its match sub-games), never re-pair + // the leaving player: mark them dropped so their matches are awarded and they + // disappear from the bracket instead of stalling the tournament. + if (tournament && !tournamentParentGame && !spectator) { + const int leavingPlayerId = participant->getPlayerId(); + GameEventStorage tournGes; + tournament->dropPlayer(leavingPlayerId); + tournament->broadcastTournamentState(tournGes); + tournGes.sendToGame(this); + } + if (playerHost) { int newHostId = -1; for (auto *otherPlayer : getPlayers().values()) { @@ -1043,6 +1061,12 @@ void Server_Game::startTournament() tournament->addPlayer(player->getPlayerId(), QString::fromStdString(player->getUserInfo()->name())); } + // A tournament with fewer than two players can't produce a valid bracket. + if (tournament->getPlayerCount() < 2) { + qCWarning() << "Cannot start tournament with fewer than 2 players"; + return; + } + tournament->startTournament(); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h index 3b8d794dc..3467ed10c 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -111,7 +112,7 @@ private: bool isTournament; TournamentSettings tournamentSettings; Server_Tournament *tournament; - Server_Game *tournamentParentGame; + QPointer tournamentParentGame; int tournamentMatchPlayer1Id; int tournamentMatchPlayer2Id; bool disconnectRemovesPlayer; @@ -271,7 +272,7 @@ public: void startTournament(); void setPlayerTournamentDeck(int playerId, DeckList *deck); void setTournamentMatchInfo(Server_Game *parentGame, int p1Id, int p2Id); - Server_Game *getTournamentParentGame() const + QPointer getTournamentParentGame() const { return tournamentParentGame; } @@ -279,6 +280,10 @@ public: { return disconnectRemovesPlayer; } + void setDisconnectRemovesPlayer(bool _disconnectRemovesPlayer) + { + disconnectRemovesPlayer = _disconnectRemovesPlayer; + } // Server_MatchGameFactory implementation Server_Game *createMatchGame(const GameConfig &config, int &outGameId) override; diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp index 1d13f31f7..527f2e728 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -31,9 +31,11 @@ Server_Tournament::~Server_Tournament() void Server_Tournament::addPlayer(int playerId, const QString &playerName) { + QMutexLocker locker(&tournamentMutex); TournamentPlayerData data; data.playerId = playerId; data.playerName = playerName; + data.dropped = false; players[playerId] = data; } @@ -51,6 +53,41 @@ void Server_Tournament::removePlayer(int playerId) { QMutexLocker locker(&tournamentMutex); players.remove(playerId); + submittedDecks.remove(playerId); + byeGivenPlayers.remove(playerId); +} + +void Server_Tournament::dropPlayer(int playerId) +{ + QMutexLocker locker(&tournamentMutex); + if (!players.contains(playerId)) { + return; + } + players[playerId].dropped = true; + players[playerId].deckSubmitted = false; + + // Any current pairing that involves the dropped player and is not already + // decided is awarded to the surviving opponent (or recorded as undecided if + // both dropped). The opponent keeps playing without sitting out a round. + for (auto &pairing : currentPairings) { + if (pairing.winnerId != -2) { + continue; + } + bool involvesDropped = (pairing.player1Id == playerId || pairing.player2Id == playerId); + if (!involvesDropped) { + continue; + } + if (pairing.player1Id == playerId && pairing.player2Id == playerId) { + continue; + } + int opponent = (pairing.player1Id == playerId) ? pairing.player2Id : pairing.player1Id; + if (players.contains(opponent) && !players[opponent].dropped) { + pairing.winnerId = opponent; + players[opponent].wins += 1; + players[playerId].losses += 1; + } + allPreviousPairings.append(qMakePair(pairing.player1Id, pairing.player2Id)); + } } void Server_Tournament::startTournament() @@ -97,7 +134,9 @@ void Server_Tournament::generateSwissPairings() currentPairings.clear(); QList available; for (auto it = players.constBegin(); it != players.constEnd(); ++it) { - available.append(it->playerId); + if (!it->dropped) { + available.append(it->playerId); + } } // Sort by wins descending (and by record for tie-breaking) @@ -113,8 +152,10 @@ void Server_Tournament::generateSwissPairings() return a < b; }); - // Simple greedy Swiss pairing QSet paired; + // Try to pair every player, allowing a single rematch only if the greedy pass + // would otherwise leave any unpaired remainder. Dropped players are never paired. + int maxRematches = available.size() / 2; for (int i = 0; i < available.size(); ++i) { if (paired.contains(available[i])) { continue; @@ -123,38 +164,74 @@ void Server_Tournament::generateSwissPairings() if (paired.contains(available[j])) { continue; } - if (!havePlayed(available[i], available[j])) { - TournamentPairingData pairing; - pairing.player1Id = available[i]; - pairing.player2Id = available[j]; - currentPairings.append(pairing); - paired.insert(available[i]); - paired.insert(available[j]); - break; + bool rematch = havePlayed(available[i], available[j]); + if (rematch && maxRematches <= 0) { + continue; } + TournamentPairingData pairing; + pairing.player1Id = available[i]; + pairing.player2Id = available[j]; + currentPairings.append(pairing); + paired.insert(available[i]); + paired.insert(available[j]); + if (rematch) { + --maxRematches; + } + break; } } - // Bye for unpaired player if odd count - for (int i = 0; i < available.size(); ++i) { - if (!paired.contains(available[i])) { - // Player gets a bye (auto-win) - TournamentPairingData bye; - bye.player1Id = available[i]; - bye.player2Id = -1; - bye.winnerId = available[i]; - bye.player1MatchWins = gamesPerMatch; // Match immediately decided - currentPairings.append(bye); - players[available[i]].wins += 1; - allPreviousPairings.append(qMakePair(available[i], -1)); - break; + // Give a bye to every remaining unpaired eligible player, worst-ranked first. + // A player receives at most one bye over the whole tournament. + QList unpaired; + for (int id : available) { + if (!paired.contains(id)) { + unpaired.append(id); } } + // Byes go to the lowest-ranked eligible player who has not had one yet. + std::sort(unpaired.begin(), unpaired.end(), [this](int a, int b) { + const auto &pa = players[a]; + const auto &pb = players[b]; + if (pa.wins != pb.wins) { + return pa.wins < pb.wins; + } + if (pa.losses != pb.losses) { + return pa.losses > pb.losses; + } + return a > b; + }); + + for (int id : unpaired) { + if (byeGivenPlayers.contains(id)) { + // Already used a bye: a dropped opponent or earlier bye means this player + // simply sits out the round with a free win to keep the bracket moving. + TournamentPairingData bye; + bye.player1Id = id; + bye.player2Id = -1; + bye.winnerId = id; + currentPairings.append(bye); + continue; + } + TournamentPairingData bye; + bye.player1Id = id; + bye.player2Id = -1; + bye.winnerId = id; + currentPairings.append(bye); + players[id].wins += 1; + byeGivenPlayers.insert(id); + allPreviousPairings.append(qMakePair(id, -1)); + } } int Server_Tournament::calculateTotalRounds() const { - int n = players.size(); + int n = 0; + for (auto it = players.constBegin(); it != players.constEnd(); ++it) { + if (!it->dropped) { + ++n; + } + } if (n <= 1) { return 0; } @@ -208,10 +285,16 @@ void Server_Tournament::enqueueMatchGameCreation() { QMutexLocker locker(&tournamentMutex); for (const auto &pairing : currentPairings) { - if (pairing.player2Id != -1 && pairing.winnerId == -2 && - pairing.matchGameIds.size() < static_cast(gamesPerMatch)) { - planned.append(qMakePair(pairing.player1Id, pairing.player2Id)); + if (pairing.player2Id == -1 || pairing.winnerId != -2) { + continue; } + if (players.value(pairing.player1Id).dropped || players.value(pairing.player2Id).dropped) { + continue; + } + if (pairing.matchGameIds.size() >= static_cast(gamesPerMatch)) { + continue; + } + planned.append(qMakePair(pairing.player1Id, pairing.player2Id)); } } if (planned.isEmpty()) { @@ -277,12 +360,25 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) return; } } + + // Bail out if either participant is no longer connected: a match game with + // zero or one connected player can never finish and would stall the round. + if (!matchGameFactory->getUserInterface(player1Name) || !matchGameFactory->getUserInterface(player2Name)) { + qCWarning(TournamentLog) << "Skipping match creation: a player in pairing" << player1Id << player2Id + << "is no longer connected"; + return; + } } - // Create a sub-game for this match via the factory + // Create a sub-game for this match via the factory, copying the real + // ServerInfo_User so it ships the true user level rather than a fabricated + // admin identity that would surface in buddy/ignore-list checks. ServerInfo_User creatorInfo; - creatorInfo.set_name(player1Name.toStdString()); - creatorInfo.set_user_level(ServerInfo_User::IsAdmin | ServerInfo_User::IsRegistered); + if (auto *ui = matchGameFactory->getUserInterface(player1Name)) { + creatorInfo = *ui->getUserInfo(); + } else { + creatorInfo.set_name(player1Name.toStdString()); + } QString gameDesc = gamesPerMatch > 1 ? QString("R%1 Match - Game %2 of %3").arg(round).arg(gameNumber).arg(gamesPerMatch) @@ -302,6 +398,9 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) matchGame->setTournamentMatchInfo(parentGame, player1Id, player2Id); matchGame->setMatchResultStrategy(new Server_TournamentMatchResultStrategy); + // A disconnect inside a tournament match must remove the player so the match + // can be decided; it must not leave them sitting as a half-present participant. + matchGame->setDisconnectRemovesPlayer(true); matchGameFactory->addGameToRoom(matchGame); // Store the game ID in the pairing @@ -317,6 +416,9 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) } // Auto-join both players, sending the join event directly through their UIs. + // Both UI lookups were verified above, so a player can only drop between that + // check and this add — in which case they get handled by drop processing and + // the pairing settles on the surviving opponent. QMap> joiners; auto joinAndSetupPlayer = [&](int pid, const QString &name) { @@ -338,7 +440,8 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) } joiners.clear(); - // Set decks and mark players as ready in the match game + // Set decks and mark players as ready in the match game. + bool anyDeckMissing = false; auto matchPlayers = matchGame->getPlayers(); for (auto *matchPlayer : matchPlayers) { const QString name = QString::fromStdString(matchPlayer->getUserInfo()->name()); @@ -352,11 +455,21 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) if (!deckNative.isEmpty()) { matchPlayer->setDeck(new DeckList(deckNative)); matchPlayer->setReadyStart(true); + } else { + anyDeckMissing = true; } } - // Start the match game - matchGame->startGameIfReady(true); + if (anyDeckMissing) { + // Not every participant submitted a deck. Do not force-start: that would + // kick the players without a deck. Leave the match game open so they can + // select a deck; the host starts it through the normal ready flow. + return; + } + + // Start the match game without forcing: both participants are ready and have + // decks, so there is nothing to kick. + matchGame->startGameIfReady(false); } void Server_Tournament::recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges) @@ -440,20 +553,29 @@ bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, Game } else if (winnerId == pairingPtr->player2Id) { pairingPtr->player2MatchWins += 1; } - // Draw (winnerId == -1): no match wins incremented + // Draw (winnerId == -1): counts nothing toward the series but does consume + // a slot, so a series can still end in a draw when it is exhausted. - // Check if match is decided - const int gamesNeeded = static_cast(gamesPerMatch); + // The winner needs a strict majority of the games in the series. + const int gamesPlayed = pairingPtr->matchGameIds.size(); + const int gamesNeeded = static_cast(gamesPerMatch / 2 + 1); + const int gamesRemaining = static_cast(gamesPerMatch) - gamesPlayed; matchDecided = (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); + if (!matchDecided) { + // Series exhausted without a strict-majority winner (e.g. a drawn Bo3 + // leaves it 1-1): record the match as a draw so the round always advances. + matchDecided = (gamesRemaining <= 0) && (pairingPtr->player1MatchWins == pairingPtr->player2MatchWins); + } if (matchDecided) { // Determine match winner - int matchWinnerId; + int matchWinnerId = -1; if (pairingPtr->player1MatchWins >= gamesNeeded) { matchWinnerId = pairingPtr->player1Id; - } else { + } else if (pairingPtr->player2MatchWins >= gamesNeeded) { matchWinnerId = pairingPtr->player2Id; } + // Otherwise the series was exhausted evenly — matchWinnerId stays -1 (a draw). // Set the match winner on the pairing pairingPtr->winnerId = matchWinnerId; @@ -462,9 +584,12 @@ bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, Game if (matchWinnerId == pairingPtr->player1Id) { players[pairingPtr->player1Id].wins += 1; players[pairingPtr->player2Id].losses += 1; - } else { + } else if (matchWinnerId == pairingPtr->player2Id) { players[pairingPtr->player2Id].wins += 1; players[pairingPtr->player1Id].losses += 1; + } else { + players[pairingPtr->player1Id].draws += 1; + players[pairingPtr->player2Id].draws += 1; } // Store for future pairing avoidance @@ -546,8 +671,13 @@ void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) p->set_player1_id(pairing.player1Id); p->set_player2_id(pairing.player2Id); p->set_game_id(pairing.gameId); - // Map internal sentinel: -2 (undecided) -> -1 (no winner yet in proto) - p->set_winner_id(pairing.winnerId == -2 ? -1 : pairing.winnerId); + // -2 = undecided; a decided draw is -1. The is_draw bit distinguishes a + // reported draw from an unset winner_id on the wire. + if (pairing.winnerId == -1) { + p->set_is_draw(true); + } else if (pairing.winnerId != -2) { + p->set_winner_id(pairing.winnerId); + } p->set_player1_match_wins(pairing.player1MatchWins); p->set_player2_match_wins(pairing.player2MatchWins); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h index b59edda4f..aeb3ea732 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,9 @@ public: void addPlayer(int playerId, const QString &playerName); void removePlayer(int playerId); + // Marks an already-starting/started tournament player as dropped: they stop + // being paired and their outstanding unstarted match is awarded as a loss. + void dropPlayer(int playerId); void startTournament(); void advanceRound(GameEventStorage &ges); void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); @@ -68,6 +72,7 @@ public: int losses = 0; int draws = 0; bool deckSubmitted = false; + bool dropped = false; }; struct TournamentPairingData @@ -82,12 +87,15 @@ public: }; private: - Server_Game *parentGame; + QPointer parentGame; Server_MatchGameFactory *matchGameFactory; mutable QRecursiveMutex tournamentMutex; QMap players; QMap submittedDecks; QList currentPairings; + // Players that have already received a bye in a previous round, so no one + // gets more than one bye over the whole tournament. + QSet byeGivenPlayers; QList> allPreviousPairings; int currentRound; int totalRounds; diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp index 5149b9a26..73035dd94 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_lifecycle_strategy.cpp @@ -11,7 +11,7 @@ Server_GameLifecycleStrategy::StartAction Server_TournamentLifecycleStrategy::on { // Match sub-games start through the normal flow; only the tournament hub game is // managed by this lifecycle. - if (game->getTournamentParentGame() != nullptr) { + if (game->getTournamentParentGame().data() != nullptr) { return StartAction::ProceedNormal; } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp index 5611224e5..7ca8aacc9 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament_match_result_strategy.cpp @@ -12,7 +12,10 @@ bool Server_TournamentMatchResultStrategy::onGameFinished(Server_Game *game, int playing, Server_AbstractPlayer *lastPlayer) { - auto *parentGame = game->getTournamentParentGame(); + // The hub game is owned by the room and may be torn down once its host leaves + // and no players remain, while the match sub-games keep running. QPointer keeps + // this link checked so a later-finishing match can't touch freed memory. + auto *parentGame = game->getTournamentParentGame().data(); if (!parentGame || !parentGame->getTournament()) { return false; } From f25990ff034dfab73863bd0858d5f00c8c5df880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 09:47:27 +0200 Subject: [PATCH 12/18] [Server] Adapt tournament engine to the shared settings message - read games per match from Command_CreateGame.tournament_settings after the proto rework replaced the standalone games_per_match field - use qWarning to match the surrounding file --- .../libcockatrice/network/server/remote/game/server_game.cpp | 2 +- .../network/server/remote/server_protocolhandler.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index f536dd3ca..775372ec5 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -1063,7 +1063,7 @@ void Server_Game::startTournament() // A tournament with fewer than two players can't produce a valid bracket. if (tournament->getPlayerCount() < 2) { - qCWarning() << "Cannot start tournament with fewer than 2 players"; + qWarning() << "Cannot start tournament with fewer than 2 players"; return; } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp index eacb9802c..375d73e0d 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/server_protocolhandler.cpp @@ -1097,7 +1097,8 @@ Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room bool shareDecklistsOnLoad = cmd.has_share_decklists_on_load() ? cmd.share_decklists_on_load() : false; bool isTournament = cmd.has_is_tournament() ? cmd.is_tournament() : false; - int gamesPerMatch = cmd.has_games_per_match() ? static_cast(cmd.games_per_match()) : 1; + int gamesPerMatch = + cmd.has_tournament_settings() ? static_cast(cmd.tournament_settings().games_per_match()) : 1; const int gameId = databaseInterface->getNextGameId(); if (gameId == -1) { From 5c25f33ca8fba95c1bd12c8556697701333f8b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 09:49:53 +0200 Subject: [PATCH 13/18] [Server] Replay tournament state to late joiners The bracket, phase and standings live in Event_TournamentState, which only flows on mutation. Without a copy a player or spectator joining after round one would sit on an empty bracket until the next advance, so the current state is now enqueued as part of the join snapshot. Extracts Event_TournamentState building into buildStateEvent() so both the broadcast path and the join path share one source of truth. --- .../network/server/remote/game/server_game.cpp | 9 +++++++++ .../network/server/remote/game/server_tournament.cpp | 9 +++++++-- .../network/server/remote/game/server_tournament.h | 2 ++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 775372ec5..65b439f93 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -898,6 +898,15 @@ void Server_Game::createGameJoinedEvent(Server_AbstractParticipant *joiningParti prepareGameEvent(event, deckOwner->getPlayerId(), &context)); } } + + // A tournament's bracket/phase/standings live in Event_TournamentState, which + // normally only flows on mutation. Without a copy here a late joiner would sit + // on an empty bracket until the next round advances, so replay the current + // state as part of the join snapshot. + if (tournament) { + rc.enqueuePostResponseItem(ServerMessage::GAME_EVENT_CONTAINER, + prepareGameEvent(tournament->buildStateEvent(), -1)); + } } void Server_Game::sendGameEventContainer(GameEventContainer *cont, diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp index 527f2e728..65529072f 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -635,7 +635,7 @@ void Server_Tournament::checkAndAdvanceRound(GameEventStorage &ges) } } -void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) +Event_TournamentState Server_Tournament::buildStateEvent() const { QMutexLocker locker(&tournamentMutex); @@ -682,5 +682,10 @@ void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) p->set_player2_match_wins(pairing.player2MatchWins); } - ges.enqueueGameEvent(state, -1); + return state; +} + +void Server_Tournament::broadcastTournamentState(GameEventStorage &ges) +{ + ges.enqueueGameEvent(buildStateEvent(), -1); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h index aeb3ea732..438490e84 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -36,6 +36,8 @@ public: void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); bool recordMatchResultByGameId(int gameId, int winnerId, GameEventStorage &ges); void broadcastTournamentState(GameEventStorage &ges); + // Current tournament state message, for replaying to a participant joining late. + Event_TournamentState buildStateEvent() const; bool isStarted() const { From 1c6af5eaba1fe5b25aaa55d896d1aed346bc0936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 10:28:12 +0200 Subject: [PATCH 14/18] [Server] Stop mirroring is_tournament on the game-state event The client learns tournament status from Event_GameJoined's ServerInfo_Game; mirroring it on every Event_GameStateChanged was dead wire data. The field was removed from the proto in [Protocol]; drop the now-invalidated setter. --- .../libcockatrice/network/server/remote/game/server_game.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 65b439f93..008af02d2 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -310,8 +310,6 @@ void Server_Game::createGameStateChangedEvent(Event_GameStateChanged *event, event->set_game_started(false); } - event->set_is_tournament(isTournament); - if (tournamentParentGame) { event->set_parent_game_id(tournamentParentGame->getGameId()); } From 84a8b751d34a66e57f13142f7c964289b9d6440d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sun, 11 Oct 2026 14:58:06 +0200 Subject: [PATCH 15/18] [Tournament] Fix GameConfig forward declaration and annotate command handlers Forward-declare GameConfig as struct to match its definition in game_config.h; the class/struct mismatch trips -Werror -Wmismatched-tags on macOS. Also add the missing /// Server: annotations on the tournament GameCommandType entries now that the Server_AbstractParticipant handlers they point at exist in the tree. --- .../network/server/remote/game/server_match_game_factory.h | 2 +- .../libcockatrice/protocol/pb/game_commands.proto | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h index d5cf81cb6..3963b99d2 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_match_game_factory.h @@ -3,7 +3,7 @@ #include -class GameConfig; +struct GameConfig; class Server_Game; class Server_AbstractUserInterface; diff --git a/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto b/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto index 68a79eb6b..21560278c 100644 --- a/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto +++ b/libcockatrice_protocol/libcockatrice/protocol/pb/game_commands.proto @@ -182,12 +182,18 @@ message GameCommand { SET_PLAYMAT = 1035; /// Report the result of a tournament match sub-game. + /// Server: Server_AbstractParticipant::cmdReportMatchResult + /// Client: reflected via tournament state events REPORT_MATCH_RESULT = 1037; /// Advance the tournament to the next round. + /// Server: Server_AbstractParticipant::cmdAdvanceTournament + /// Client: reflected via tournament state events ADVANCE_TOURNAMENT = 1038; /// Select tournament settings. + /// Server: Server_AbstractParticipant::cmdTournamentSettingsSelect + /// Client: reflected via tournament state events TOURNAMENT_SETTINGS_SELECT = 1039; } From d2c93aee7bbff55e0a660808ceac1eabbc9e504b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sun, 11 Oct 2026 17:37:30 +0200 Subject: [PATCH 16/18] [Server] Fix tournament stuck-round and sub-game start paths Gate the host-only start on the hub object so match sub-games can start, advance the round when a drop decides the last pairing (and on late results), settle disconnected pairings instead of stalling, and decide exhausted series by game wins. --- .../server/remote/game/server_game.cpp | 5 +- .../server/remote/game/server_tournament.cpp | 219 +++++++++++------- .../server/remote/game/server_tournament.h | 2 +- 3 files changed, 138 insertions(+), 88 deletions(-) diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp index 008af02d2..46dd84db7 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_game.cpp @@ -369,7 +369,8 @@ void Server_Game::doStartGameIfReady(bool forceStartGame) // Tournament hubs must be host-started and can't lock in a partially filled // bracket: a mere "everyone current is ready" must not start a 1-player or // undersized tournament. startTournament() additionally enforces 2+ players. - if (isTournament && !forceStartGame) { + // Match sub-games carry isTournament too, so gate on the hub object only. + if (tournament && !forceStartGame) { return; } @@ -633,7 +634,7 @@ void Server_Game::removeParticipant(Server_AbstractParticipant *participant, Eve if (tournament && !tournamentParentGame && !spectator) { const int leavingPlayerId = participant->getPlayerId(); GameEventStorage tournGes; - tournament->dropPlayer(leavingPlayerId); + tournament->dropPlayer(leavingPlayerId, tournGes); tournament->broadcastTournamentState(tournGes); tournGes.sendToGame(this); } diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp index 65529072f..f8345c96d 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.cpp @@ -57,37 +57,41 @@ void Server_Tournament::removePlayer(int playerId) byeGivenPlayers.remove(playerId); } -void Server_Tournament::dropPlayer(int playerId) +void Server_Tournament::dropPlayer(int playerId, GameEventStorage &ges) { - QMutexLocker locker(&tournamentMutex); - if (!players.contains(playerId)) { - return; - } - players[playerId].dropped = true; - players[playerId].deckSubmitted = false; + { + QMutexLocker locker(&tournamentMutex); + if (!players.contains(playerId)) { + return; + } + players[playerId].dropped = true; + players[playerId].deckSubmitted = false; - // Any current pairing that involves the dropped player and is not already - // decided is awarded to the surviving opponent (or recorded as undecided if - // both dropped). The opponent keeps playing without sitting out a round. - for (auto &pairing : currentPairings) { - if (pairing.winnerId != -2) { - continue; + // Any current pairing that involves the dropped player and is not already + // decided is awarded to the surviving opponent (or recorded as undecided if + // both dropped). The opponent keeps playing without sitting out a round. + for (auto &pairing : currentPairings) { + if (pairing.winnerId != -2) { + continue; + } + bool involvesDropped = (pairing.player1Id == playerId || pairing.player2Id == playerId); + if (!involvesDropped) { + continue; + } + if (pairing.player1Id == playerId && pairing.player2Id == playerId) { + continue; + } + int opponent = (pairing.player1Id == playerId) ? pairing.player2Id : pairing.player1Id; + if (players.contains(opponent) && !players[opponent].dropped) { + pairing.winnerId = opponent; + players[opponent].wins += 1; + players[playerId].losses += 1; + } + allPreviousPairings.append(qMakePair(pairing.player1Id, pairing.player2Id)); } - bool involvesDropped = (pairing.player1Id == playerId || pairing.player2Id == playerId); - if (!involvesDropped) { - continue; - } - if (pairing.player1Id == playerId && pairing.player2Id == playerId) { - continue; - } - int opponent = (pairing.player1Id == playerId) ? pairing.player2Id : pairing.player1Id; - if (players.contains(opponent) && !players[opponent].dropped) { - pairing.winnerId = opponent; - players[opponent].wins += 1; - players[playerId].losses += 1; - } - allPreviousPairings.append(qMakePair(pairing.player1Id, pairing.player2Id)); } + // A drop can decide the last open pairing of the round; advance when complete. + checkAndAdvanceRound(ges); } void Server_Tournament::startTournament() @@ -332,6 +336,7 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) QString deck2Native; int round = 0; int gameNumber = 1; + bool settledWithoutGame = false; { QMutexLocker locker(&tournamentMutex); player1Name = players.value(player1Id).playerName; @@ -363,13 +368,44 @@ void Server_Tournament::createMatchGame(int player1Id, int player2Id) // Bail out if either participant is no longer connected: a match game with // zero or one connected player can never finish and would stall the round. - if (!matchGameFactory->getUserInterface(player1Name) || !matchGameFactory->getUserInterface(player2Name)) { + // Settle the pairing like dropPlayer does so the round can still advance. + auto *ui1 = matchGameFactory->getUserInterface(player1Name); + auto *ui2 = matchGameFactory->getUserInterface(player2Name); + if (!ui1 || !ui2) { qCWarning(TournamentLog) << "Skipping match creation: a player in pairing" << player1Id << player2Id << "is no longer connected"; - return; + for (auto &pairing : currentPairings) { + if (pairing.player1Id == player1Id && pairing.player2Id == player2Id && pairing.winnerId == -2) { + if (ui1 && !ui2) { + pairing.winnerId = player1Id; + players[player1Id].wins += 1; + players[player2Id].losses += 1; + } else if (ui2 && !ui1) { + pairing.winnerId = player2Id; + players[player2Id].wins += 1; + players[player1Id].losses += 1; + } else { + pairing.winnerId = -1; + players[player1Id].draws += 1; + players[player2Id].draws += 1; + } + allPreviousPairings.append(qMakePair(player1Id, player2Id)); + break; + } + } + settledWithoutGame = true; } } + if (settledWithoutGame) { + GameEventStorage ges; + broadcastTournamentState(ges); + checkAndAdvanceRound(ges); + broadcastTournamentState(ges); + ges.sendToGame(parentGame); + return; + } + // Create a sub-game for this match via the factory, copying the real // ServerInfo_User so it ships the true user level rather than a fabricated // admin identity that would surface in buddy/ignore-list checks. @@ -543,65 +579,78 @@ bool Server_Tournament::recordMatchResultByGameId(int gameId, int winnerId, Game } // If the match is already decided, ignore further sub-game results + // but still check whether the round can advance (e.g. a drop decided + // the last pairing and this late result is the first chance to notice). if (pairingPtr->winnerId != -2) { - return true; - } - - // Increment per-match wins - if (winnerId == pairingPtr->player1Id) { - pairingPtr->player1MatchWins += 1; - } else if (winnerId == pairingPtr->player2Id) { - pairingPtr->player2MatchWins += 1; - } - // Draw (winnerId == -1): counts nothing toward the series but does consume - // a slot, so a series can still end in a draw when it is exhausted. - - // The winner needs a strict majority of the games in the series. - const int gamesPlayed = pairingPtr->matchGameIds.size(); - const int gamesNeeded = static_cast(gamesPerMatch / 2 + 1); - const int gamesRemaining = static_cast(gamesPerMatch) - gamesPlayed; - matchDecided = (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); - if (!matchDecided) { - // Series exhausted without a strict-majority winner (e.g. a drawn Bo3 - // leaves it 1-1): record the match as a draw so the round always advances. - matchDecided = (gamesRemaining <= 0) && (pairingPtr->player1MatchWins == pairingPtr->player2MatchWins); - } - - if (matchDecided) { - // Determine match winner - int matchWinnerId = -1; - if (pairingPtr->player1MatchWins >= gamesNeeded) { - matchWinnerId = pairingPtr->player1Id; - } else if (pairingPtr->player2MatchWins >= gamesNeeded) { - matchWinnerId = pairingPtr->player2Id; - } - // Otherwise the series was exhausted evenly — matchWinnerId stays -1 (a draw). - - // Set the match winner on the pairing - pairingPtr->winnerId = matchWinnerId; - - // Update tournament-level player records - if (matchWinnerId == pairingPtr->player1Id) { - players[pairingPtr->player1Id].wins += 1; - players[pairingPtr->player2Id].losses += 1; - } else if (matchWinnerId == pairingPtr->player2Id) { - players[pairingPtr->player2Id].wins += 1; - players[pairingPtr->player1Id].losses += 1; - } else { - players[pairingPtr->player1Id].draws += 1; - players[pairingPtr->player2Id].draws += 1; - } - - // Store for future pairing avoidance - allPreviousPairings.append(qMakePair(pairingPtr->player1Id, pairingPtr->player2Id)); + broadcastTournamentState(ges); + // Fall through to checkAndAdvanceRound below. + matchDecided = true; + seriesContinues = false; } else { - // Match not decided — spawn the next sub-game outside all locks - seriesContinues = true; - p1 = pairingPtr->player1Id; - p2 = pairingPtr->player2Id; - } + // Increment per-match wins + if (winnerId == pairingPtr->player1Id) { + pairingPtr->player1MatchWins += 1; + } else if (winnerId == pairingPtr->player2Id) { + pairingPtr->player2MatchWins += 1; + } + // Draw (winnerId == -1): counts nothing toward the series but does consume + // a slot, so a series can still end in a draw when it is exhausted. - broadcastTournamentState(ges); + // The winner needs a strict majority of the games in the series. + const int gamesPlayed = pairingPtr->matchGameIds.size(); + const int gamesNeeded = static_cast(gamesPerMatch / 2 + 1); + const int gamesRemaining = static_cast(gamesPerMatch) - gamesPlayed; + matchDecided = + (pairingPtr->player1MatchWins >= gamesNeeded) || (pairingPtr->player2MatchWins >= gamesNeeded); + if (!matchDecided && gamesRemaining <= 0) { + // Series exhausted: draws can leave the wins unequal without either + // side reaching a majority (e.g. Bo3 going win/draw/draw for 1-0). + // Decide by game wins, draw only on a tie, so the round advances. + matchDecided = true; + } + + if (matchDecided) { + // Determine match winner + int matchWinnerId = -1; + if (pairingPtr->player1MatchWins >= gamesNeeded) { + matchWinnerId = pairingPtr->player1Id; + } else if (pairingPtr->player2MatchWins >= gamesNeeded) { + matchWinnerId = pairingPtr->player2Id; + } else if (gamesRemaining <= 0) { + if (pairingPtr->player1MatchWins > pairingPtr->player2MatchWins) { + matchWinnerId = pairingPtr->player1Id; + } else if (pairingPtr->player2MatchWins > pairingPtr->player1MatchWins) { + matchWinnerId = pairingPtr->player2Id; + } + } + // Otherwise the series was exhausted evenly — matchWinnerId stays -1 (a draw). + + // Set the match winner on the pairing + pairingPtr->winnerId = matchWinnerId; + + // Update tournament-level player records + if (matchWinnerId == pairingPtr->player1Id) { + players[pairingPtr->player1Id].wins += 1; + players[pairingPtr->player2Id].losses += 1; + } else if (matchWinnerId == pairingPtr->player2Id) { + players[pairingPtr->player2Id].wins += 1; + players[pairingPtr->player1Id].losses += 1; + } else { + players[pairingPtr->player1Id].draws += 1; + players[pairingPtr->player2Id].draws += 1; + } + + // Store for future pairing avoidance + allPreviousPairings.append(qMakePair(pairingPtr->player1Id, pairingPtr->player2Id)); + } else { + // Match not decided — spawn the next sub-game outside all locks + seriesContinues = true; + p1 = pairingPtr->player1Id; + p2 = pairingPtr->player2Id; + } + + broadcastTournamentState(ges); + } // else (undecided pairing) } if (seriesContinues) { diff --git a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h index 438490e84..3d976da7b 100644 --- a/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h +++ b/libcockatrice_network/libcockatrice/network/server/remote/game/server_tournament.h @@ -30,7 +30,7 @@ public: void removePlayer(int playerId); // Marks an already-starting/started tournament player as dropped: they stop // being paired and their outstanding unstarted match is awarded as a loss. - void dropPlayer(int playerId); + void dropPlayer(int playerId, GameEventStorage &ges); void startTournament(); void advanceRound(GameEventStorage &ges); void recordMatchResult(int playerId1, int playerId2, int winnerId, GameEventStorage &ges); From 36e7f533bcad13953e56217c84c569a8f26f60f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 24 Aug 2026 09:07:11 +0200 Subject: [PATCH 17/18] [Client] Add tournament event plumbing and player extension points Wires the client game layer for tournament state without touching existing behavior: - GameEventHandler dispatches Event_TournamentState (2027) to a new tournamentStateChanged signal - GameMetaInfo exposes isTournament and parentGameId over the new ServerInfo_Game field - PlayerEventHandler::processGameEvent is virtual and player is protected; PlayerLogic gains a protected constructor accepting a custom handler, so mode-specific subclasses can intercept events --- .../libcockatrice/game/game_event_handler.cpp | 10 ++++++++ .../libcockatrice/game/game_event_handler.h | 2 ++ .../libcockatrice/game/game_meta_info.h | 21 ++++++++++++++++ .../game/player/player_event_handler.h | 10 ++++---- .../game/player/player_logic.cpp | 13 ++++++++++ .../libcockatrice/game/player/player_logic.h | 25 ++++++++++++++++++- 6 files changed, 75 insertions(+), 6 deletions(-) diff --git a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp index 856475dfa..27526a361 100644 --- a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp +++ b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -189,6 +190,9 @@ void GameEventHandler::processGameEventContainer(const GameEventContainer &cont, case GameEvent::REVERSE_TURN: eventReverseTurn(event.GetExtension(Event_ReverseTurn::ext), playerId, context); break; + case GameEvent::TOURNAMENT_STATE: + emit tournamentStateChanged(event.GetExtension(Event_TournamentState::ext)); + break; default: { PlayerLogic *player = game->getPlayerManager()->getPlayers().value(playerId, 0); @@ -375,6 +379,12 @@ void GameEventHandler::eventGameStateChanged(const Event_GameStateChanged &event int /*eventPlayerId*/, const GameEventContext & /*context*/) { + // Sub-games of a tournament report their parent hub game so the client can + // route "close game" back to the parent tab. + if (event.parent_game_id() != -1) { + game->getGameMetaInfo()->setParentGameId(event.parent_game_id()); + } + const int playerListSize = event.player_list_size(); QVector>> opponentDecksToDisplay; diff --git a/libcockatrice_game/libcockatrice/game/game_event_handler.h b/libcockatrice_game/libcockatrice/game/game_event_handler.h index f7b11780b..1320d053b 100644 --- a/libcockatrice_game/libcockatrice/game/game_event_handler.h +++ b/libcockatrice_game/libcockatrice/game/game_event_handler.h @@ -50,6 +50,7 @@ class Event_SetActivePhase; class Event_GameSay; class Event_Kicked; class Event_ReverseTurn; +class Event_TournamentState; class Event_Ping; class ServerInfo_PlayerProperties; namespace google @@ -415,6 +416,7 @@ signals: void gameStopped(); void gameClosed(); + void tournamentStateChanged(const Event_TournamentState &state); void playerPropertiesChanged(const ServerInfo_PlayerProperties &prop, int playerId); void playerJoined(const ServerInfo_PlayerProperties &playerInfo); void playerLeft(int leavingPlayerId); diff --git a/libcockatrice_game/libcockatrice/game/game_meta_info.h b/libcockatrice_game/libcockatrice/game/game_meta_info.h index ef8f0c08e..0ea5cd7cc 100644 --- a/libcockatrice_game/libcockatrice/game/game_meta_info.h +++ b/libcockatrice_game/libcockatrice/game/game_meta_info.h @@ -87,6 +87,26 @@ public: return roomGameTypes.find(gameInfo_.game_types(index)).value(); } + bool isTournament() const + { + return gameInfo_.is_tournament(); + } + + void setIsTournament(bool t) + { + gameInfo_.set_is_tournament(t); + } + + int parentGameId() const + { + return parentGameId_; + } + + void setParentGameId(int id) + { + parentGameId_ = id; + } + public slots: void setStarted(bool s) { @@ -111,6 +131,7 @@ signals: private: ServerInfo_Game gameInfo_; + int parentGameId_ = -1; }; #endif // GAME_META_INFO_H diff --git a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h index d253deef0..59e448d7a 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h +++ b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h @@ -93,10 +93,10 @@ public: * @param context Additional context (undo, judge, etc.). * @param options Processing options (UI suppression, reveal behavior). */ - void processGameEvent(GameEvent::GameEventType type, - const GameEvent &event, - const GameEventContext &context, - EventProcessingOptions options); + virtual void processGameEvent(GameEvent::GameEventType type, + const GameEvent &event, + const GameEventContext &context, + EventProcessingOptions options); /** @} */ @@ -277,7 +277,7 @@ signals: */ void cardViewRefreshRequested(CardState *card, bool zoneChanged, PlayerLogic *newOwner); -private: +protected: /** Owning player instance. */ PlayerLogic *player; diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp index 96bd07164..9098729eb 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp @@ -49,6 +49,19 @@ PlayerLogic::PlayerLogic(const ServerInfo_User &info, initializeZones(); } +PlayerLogic::PlayerLogic(const ServerInfo_User &info, + int _id, + bool _local, + bool _judge, + AbstractGame *_parent, + PlayerEventHandler *customEventHandler) + : QObject(_parent), game(_parent), playerInfo(new PlayerInfo(info, _id, _local, _judge)), + playerEventHandler(customEventHandler), playerActions(new PlayerActions(this)), active(false), conceded(false), + zoneId(0), dialogSemaphore(false) +{ + initializeZones(); +} + void PlayerLogic::initializeZones() { addZone(new PileZoneLogic(this, ZoneNames::DECK, false, true, false, this)); diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.h b/libcockatrice_game/libcockatrice/game/player/player_logic.h index d6c4f5187..168d5fd5f 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.h +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.h @@ -103,7 +103,30 @@ public: ICardPreferenceProvider *cardPreferences = nullptr); ~PlayerLogic() override; - void initializeZones(); +protected: + /** + * @brief Constructor for subclasses that need a custom event handler (e.g. DraftPlayerLogic). + * + * @p customEventHandler must be non-null and either QObject-parented to this PlayerLogic + * or deleted externally; it is not owned by PlayerLogic. The handler connects to @c player + * during its own constructor, so passing a freshly built subclass handler from an + * initializer list is safe. + */ + PlayerLogic(const ServerInfo_User &info, + int _id, + bool _local, + bool _judge, + AbstractGame *_parent, + PlayerEventHandler *customEventHandler); + +public: + /** + * @brief Creates the standard zone set. + * + * Not virtually dispatched from constructors — subclasses overriding this must add + * their extra zones in their own constructor body. + */ + virtual void initializeZones(); void updateZones(); void clear(); From dfa0edfcdb85d612f5c7ab833b26b725a3716c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Wed, 2 Sep 2026 10:27:44 +0200 Subject: [PATCH 18/18] [Client] Drop dead player event handler extension points MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The second PlayerLogic constructor taking a custom PlayerEventHandler was UB (converting this to PlayerLogic* in the base initializer, and connecting a freshly-built handler against a not-yet-constructed object), and its abstract extension surface — virtual initializeZones, virtual getPlayerEventHandler, virtual processGameEvent, protected player member — has no subclass consumer in the stack. Revert to the single primary constructor. Also: - Drop the unused GameMetaInfo::setIsTournament setter. Tournament status is authoritative from Event_GameJoined's ServerInfo_Game; the redundant is_tournament field on Event_GameStateChanged is removed in [Protocol]. - Gate the parent-game routing on has_parent_game_id() instead of comparing the proto default, so a future drop of the default keeps working. --- .../libcockatrice/game/game_event_handler.cpp | 5 ++-- .../libcockatrice/game/game_meta_info.h | 5 ---- .../game/player/player_event_handler.h | 10 ++++---- .../game/player/player_logic.cpp | 13 ---------- .../libcockatrice/game/player/player_logic.h | 25 +------------------ 5 files changed, 9 insertions(+), 49 deletions(-) diff --git a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp index 27526a361..931e6425c 100644 --- a/libcockatrice_game/libcockatrice/game/game_event_handler.cpp +++ b/libcockatrice_game/libcockatrice/game/game_event_handler.cpp @@ -380,8 +380,9 @@ void GameEventHandler::eventGameStateChanged(const Event_GameStateChanged &event const GameEventContext & /*context*/) { // Sub-games of a tournament report their parent hub game so the client can - // route "close game" back to the parent tab. - if (event.parent_game_id() != -1) { + // route "close game" back to the parent tab. Use has_parent_game_id rather + // than comparing the default so a future drop of the proto default still works. + if (event.has_parent_game_id()) { game->getGameMetaInfo()->setParentGameId(event.parent_game_id()); } diff --git a/libcockatrice_game/libcockatrice/game/game_meta_info.h b/libcockatrice_game/libcockatrice/game/game_meta_info.h index 0ea5cd7cc..b408ef061 100644 --- a/libcockatrice_game/libcockatrice/game/game_meta_info.h +++ b/libcockatrice_game/libcockatrice/game/game_meta_info.h @@ -92,11 +92,6 @@ public: return gameInfo_.is_tournament(); } - void setIsTournament(bool t) - { - gameInfo_.set_is_tournament(t); - } - int parentGameId() const { return parentGameId_; diff --git a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h index 59e448d7a..d253deef0 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_event_handler.h +++ b/libcockatrice_game/libcockatrice/game/player/player_event_handler.h @@ -93,10 +93,10 @@ public: * @param context Additional context (undo, judge, etc.). * @param options Processing options (UI suppression, reveal behavior). */ - virtual void processGameEvent(GameEvent::GameEventType type, - const GameEvent &event, - const GameEventContext &context, - EventProcessingOptions options); + void processGameEvent(GameEvent::GameEventType type, + const GameEvent &event, + const GameEventContext &context, + EventProcessingOptions options); /** @} */ @@ -277,7 +277,7 @@ signals: */ void cardViewRefreshRequested(CardState *card, bool zoneChanged, PlayerLogic *newOwner); -protected: +private: /** Owning player instance. */ PlayerLogic *player; diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp index 9098729eb..96bd07164 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.cpp +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.cpp @@ -49,19 +49,6 @@ PlayerLogic::PlayerLogic(const ServerInfo_User &info, initializeZones(); } -PlayerLogic::PlayerLogic(const ServerInfo_User &info, - int _id, - bool _local, - bool _judge, - AbstractGame *_parent, - PlayerEventHandler *customEventHandler) - : QObject(_parent), game(_parent), playerInfo(new PlayerInfo(info, _id, _local, _judge)), - playerEventHandler(customEventHandler), playerActions(new PlayerActions(this)), active(false), conceded(false), - zoneId(0), dialogSemaphore(false) -{ - initializeZones(); -} - void PlayerLogic::initializeZones() { addZone(new PileZoneLogic(this, ZoneNames::DECK, false, true, false, this)); diff --git a/libcockatrice_game/libcockatrice/game/player/player_logic.h b/libcockatrice_game/libcockatrice/game/player/player_logic.h index 168d5fd5f..d6c4f5187 100644 --- a/libcockatrice_game/libcockatrice/game/player/player_logic.h +++ b/libcockatrice_game/libcockatrice/game/player/player_logic.h @@ -103,30 +103,7 @@ public: ICardPreferenceProvider *cardPreferences = nullptr); ~PlayerLogic() override; -protected: - /** - * @brief Constructor for subclasses that need a custom event handler (e.g. DraftPlayerLogic). - * - * @p customEventHandler must be non-null and either QObject-parented to this PlayerLogic - * or deleted externally; it is not owned by PlayerLogic. The handler connects to @c player - * during its own constructor, so passing a freshly built subclass handler from an - * initializer list is safe. - */ - PlayerLogic(const ServerInfo_User &info, - int _id, - bool _local, - bool _judge, - AbstractGame *_parent, - PlayerEventHandler *customEventHandler); - -public: - /** - * @brief Creates the standard zone set. - * - * Not virtually dispatched from constructors — subclasses overriding this must add - * their extra zones in their own constructor body. - */ - virtual void initializeZones(); + void initializeZones(); void updateZones(); void clear();