Compare commits

...

14 commits

Author SHA1 Message Date
Lukas Brübach
c1bce86888 [Client] Fix tournament UI includes after the game logic move
The game logic now lives in libcockatrice_game, so the tab's relative
includes into cockatrice/src/game no longer resolve. Include the library
headers instead and pull in the complete types the tab dereferences.
2026-11-03 00:33:11 +01:00
Lukas Brübach
cf76d42791 [Client] Skip decided pairings when resolving live tournament game 2026-11-03 00:29:38 +01:00
Lukas Brübach
c433b165e5 [Tournament] Fold tournament settings hint into the spinbox tooltip
The standalone hint label sat in the form layout's field column with an
empty label cell, which misaligned it against the row above. Per review,
drop the label and carry its text in the spinbox tooltip instead.
2026-11-03 00:29:38 +01:00
Lukas Brübach
a700f8d00d [Client] Address tournament UI review feedback
- Drop the in-game tournament-settings button: Command_TournamentSettingsSelect
  has no server handler (RespContextError), the host-ness decision was evaluated
  once at tab construction, and the button never received a label on first show.
  The dialog can be re-added together with a working Server_Player override.
- Pass PlayerManager::isSpectator() to TournamentWidget instead of relying on
  localPlayerId == -1, which never holds for spectators.
- Report games per match as the total series length (Best of %1) to match the
  DlgTournamentSettings hint and the server's use of the value; the previous
  gamesPerMatch * 2 - 1 mislabeled Bo3 as Best of 5.
- Block signals around read-only tournamentCheckBox setChecked so the disabled
  settings button is not re-enabled, and seed Command_CreateGame through
  mutable_tournament_settings() (field 15 became the settings message).
- return after closing a sub-game that routed to its parent tab.
2026-11-03 00:29:38 +01:00
Lukas Brübach
de983ed01b [Client] Add tournament UI for creating and following lobby tournaments
Wires the tournament protocol into the client:
- DlgCreateGame gains a tournament checkbox with a games-per-match
  settings sub-dialog; both values are sent with Command_CreateGame and
  mirrored in the join-info variant of the dialog
- TournamentWidget renders live standings (with deck-submission state)
  and pairings for the local player's perspective, and asks to open the
  current match game via openMatchGameRequested()
- TournamentTabGameExtension attaches an overview page to TabGame's
  stacked views, mirrors phase and submission progress onto a deck-view
  status strip, and provides round-trip navigation between game view and
  standings; hosts get an in-game settings dialog during deck building
- Open-match falls back to joining as spectator through TabSupervisor;
  sub-games now return to their parent tab on close, falling back to
  the normal leave-game flow when the parent is gone
2026-11-03 00:29:38 +01:00
Lukas Brübach
dfa0edfcdb [Client] Drop dead player event handler extension points
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.
2026-11-03 00:25:34 +01:00
Lukas Brübach
36e7f533bc [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
2026-11-03 00:25:34 +01:00
Lukas Brübach
d2c93aee7b [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.
2026-11-03 00:25:20 +01:00
Lukas Brübach
84a8b751d3 [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.
2026-11-03 00:25:20 +01:00
Lukas Brübach
1c6af5eaba [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.
2026-11-03 00:25:20 +01:00
Lukas Brübach
5c25f33ca8 [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.
2026-11-03 00:25:20 +01:00
Lukas Brübach
f25990ff03 [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
2026-11-03 00:25:20 +01:00
Lukas Brübach
e44ddc1138 [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
2026-11-03 00:25:20 +01:00
Lukas Brübach
fe6d4e9e5f [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
2026-11-03 00:25:20 +01:00
28 changed files with 1869 additions and 11 deletions

View file

@ -56,6 +56,7 @@ set(cockatrice_SOURCES
src/interface/widgets/dialogs/dlg_settings.cpp
src/interface/widgets/dialogs/dlg_startup_card_check.cpp
src/interface/widgets/dialogs/dlg_tip_of_the_day.cpp
src/interface/widgets/dialogs/dlg_tournament_settings.cpp
src/interface/widgets/dialogs/dlg_update.cpp
src/interface/widgets/dialogs/dlg_view_log.cpp
src/interface/widgets/dialogs/override_printing_warning.cpp
@ -211,6 +212,7 @@ set(cockatrice_SOURCES
src/interface/widgets/deck_editor/deck_state_manager.cpp
src/interface/widgets/deck_editor/deck_zone_dialog.cpp
src/interface/widgets/deck_editor/printing_disabled_info_widget.cpp
src/interface/widgets/draft/tournament_widget.cpp
src/interface/widgets/general/background_sources.cpp
src/interface/widgets/general/display/background_plate_widget.cpp
src/interface/widgets/general/display/banner_widget.cpp
@ -392,6 +394,7 @@ set(cockatrice_SOURCES
src/interface/widgets/tabs/tab_server.cpp
src/interface/widgets/tabs/tab_supervisor.cpp
src/interface/widgets/tabs/tab_visual_database_display.cpp
src/interface/widgets/tabs/tournament_tab_game_extension.cpp
src/interface/widgets/tabs/visual_deck_editor/tab_deck_editor_visual.cpp
src/interface/widgets/tabs/visual_deck_editor/tab_deck_editor_visual_tab_widget.cpp
src/interface/widgets/tabs/visual_deck_storage/tab_deck_storage_visual.cpp

View file

@ -2,6 +2,7 @@
#include "../../../client/settings/cache_settings.h"
#include "../interface/widgets/tabs/tab_room.h"
#include "dlg_tournament_settings.h"
#include "libcockatrice/protocol/pb/response.pb.h"
#include "libcockatrice/protocol/pb/room_commands.pb.h"
#include "libcockatrice/protocol/pb/serverinfo_user.pb.h"
@ -20,6 +21,7 @@
#include <QRadioButton>
#include <QSet>
#include <QSharedPointer>
#include <QSignalBlocker>
#include <QSize>
#include <QSpinBox>
#include <QVBoxLayout>
@ -116,14 +118,29 @@ void DlgCreateGame::sharedCtor()
shareDecklistsOnLoadCheckBox = new QCheckBox(tr("Open decklists in lobby"));
tournamentCheckBox = new QCheckBox(tr("Tournament mode"));
tournamentCheckBox->setToolTip(tr("All players submit a deck up front and rounds are paired automatically"));
tournamentSettingsButton = new QPushButton(tr("Settings..."));
tournamentSettingsButton->setEnabled(false);
connect(tournamentCheckBox, &QCheckBox::toggled, tournamentSettingsButton, &QPushButton::setEnabled);
connect(tournamentSettingsButton, &QPushButton::clicked, this, [this] {
DlgTournamentSettings dlg(this);
dlg.setCurrentGamesPerMatch(tournamentSettings.gamesPerMatch);
if (dlg.exec() == QDialog::Accepted) {
tournamentSettings = dlg.getResult();
}
});
createGameAsJudgeCheckBox = new QCheckBox(tr("Create game as judge"));
auto *gameSetupOptionsLayout = new QGridLayout;
gameSetupOptionsLayout->addWidget(startingLifeTotalLabel, 0, 0);
gameSetupOptionsLayout->addWidget(startingLifeTotalEdit, 0, 1);
gameSetupOptionsLayout->addWidget(shareDecklistsOnLoadCheckBox, 1, 0);
gameSetupOptionsLayout->addWidget(tournamentCheckBox, 2, 0);
gameSetupOptionsLayout->addWidget(tournamentSettingsButton, 2, 1);
if (room && room->getUserInfo()->user_level() & ServerInfo_User::IsJudge) {
gameSetupOptionsLayout->addWidget(createGameAsJudgeCheckBox, 2, 0);
gameSetupOptionsLayout->addWidget(createGameAsJudgeCheckBox, 3, 0);
} else {
createGameAsJudgeCheckBox->setChecked(false);
createGameAsJudgeCheckBox->setHidden(true);
@ -200,7 +217,7 @@ DlgCreateGame::DlgCreateGame(TabRoom *_room, const QMap<int, QString> &_gameType
}
DlgCreateGame::DlgCreateGame(const ServerInfo_Game &gameInfo, const QMap<int, QString> &_gameTypes, QWidget *parent)
: QDialog(parent), room(0), gameTypes(_gameTypes)
: QDialog(parent), room(nullptr), gameTypes(_gameTypes)
{
sharedCtor();
@ -217,6 +234,8 @@ DlgCreateGame::DlgCreateGame(const ServerInfo_Game &gameInfo, const QMap<int, QS
createGameAsSpectatorCheckBox->setEnabled(false);
startingLifeTotalEdit->setEnabled(false);
shareDecklistsOnLoadCheckBox->setEnabled(false);
tournamentCheckBox->setEnabled(false);
tournamentSettingsButton->setEnabled(false);
descriptionEdit->setText(QString::fromStdString(gameInfo.description()));
maxPlayersEdit->setValue(gameInfo.max_players());
@ -227,6 +246,10 @@ DlgCreateGame::DlgCreateGame(const ServerInfo_Game &gameInfo, const QMap<int, QS
spectatorsCanTalkCheckBox->setChecked(gameInfo.spectators_can_chat());
spectatorsSeeEverythingCheckBox->setChecked(gameInfo.spectators_omniscient());
shareDecklistsOnLoadCheckBox->setChecked(gameInfo.share_decklists_on_load());
{
const QSignalBlocker blocker(tournamentCheckBox);
tournamentCheckBox->setChecked(gameInfo.is_tournament());
}
QSet<int> types;
for (int i = 0; i < gameInfo.game_types_size(); ++i) {
@ -264,6 +287,8 @@ void DlgCreateGame::actReset()
startingLifeTotalEdit->setValue(20);
shareDecklistsOnLoadCheckBox->setChecked(false);
tournamentCheckBox->setChecked(false);
tournamentSettings = DlgTournamentSettingsResult{};
createGameAsJudgeCheckBox->setChecked(false);
QMapIterator<int, QRadioButton *> gameTypeCheckBoxIterator(gameTypeCheckBoxes);
@ -294,6 +319,10 @@ void DlgCreateGame::actOK()
cmd.set_join_as_spectator(createGameAsSpectatorCheckBox->isChecked());
cmd.set_starting_life_total(startingLifeTotalEdit->value());
cmd.set_share_decklists_on_load(shareDecklistsOnLoadCheckBox->isChecked());
cmd.set_is_tournament(tournamentCheckBox->isChecked());
if (tournamentCheckBox->isChecked()) {
cmd.mutable_tournament_settings()->set_games_per_match(tournamentSettings.gamesPerMatch);
}
auto _gameTypes = QString();
QMapIterator<int, QRadioButton *> gameTypeCheckBoxIterator(gameTypeCheckBoxes);

View file

@ -7,6 +7,8 @@
#ifndef DLG_CREATEGAME_H
#define DLG_CREATEGAME_H
#include "dlg_tournament_settings.h"
#include <QDialog>
#include <QMap>
#include <QString>
@ -51,6 +53,9 @@ private:
QCheckBox *spectatorsAllowedCheckBox, *spectatorsNeedPasswordCheckBox, *spectatorsCanTalkCheckBox,
*spectatorsSeeEverythingCheckBox, *createGameAsJudgeCheckBox, *createGameAsSpectatorCheckBox;
QCheckBox *shareDecklistsOnLoadCheckBox;
QCheckBox *tournamentCheckBox;
QPushButton *tournamentSettingsButton;
DlgTournamentSettingsResult tournamentSettings;
QDialogButtonBox *buttonBox;
QPushButton *clearButton;
QCheckBox *rememberGameSettings;

View file

@ -0,0 +1,43 @@
#include "dlg_tournament_settings.h"
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QPushButton>
#include <QSpinBox>
DlgTournamentSettings::DlgTournamentSettings(QWidget *parent) : QDialog(parent)
{
setWindowTitle(tr("Tournament Settings"));
auto *mainLayout = new QFormLayout(this);
gamesPerMatchSpin = new QSpinBox(this);
gamesPerMatchSpin->setRange(1, 5);
gamesPerMatchSpin->setValue(1);
gamesPerMatchSpin->setToolTip(tr("Number of games per match (e.g., 3 for Best of 3, 5 for Best of 5)"));
mainLayout->addRow(tr("Games per match:"), gamesPerMatchSpin);
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
connect(buttonBox, &QDialogButtonBox::accepted, this, &DlgTournamentSettings::actOK);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
mainLayout->addRow(buttonBox);
setFixedHeight(sizeHint().height());
}
DlgTournamentSettingsResult DlgTournamentSettings::getResult() const
{
DlgTournamentSettingsResult result;
result.gamesPerMatch = gamesPerMatchSpin->value();
return result;
}
void DlgTournamentSettings::setCurrentGamesPerMatch(int n)
{
gamesPerMatchSpin->setValue(n);
}
void DlgTournamentSettings::actOK()
{
accept();
}

View file

@ -0,0 +1,30 @@
#ifndef DLG_TOURNAMENT_SETTINGS_H
#define DLG_TOURNAMENT_SETTINGS_H
#include <QDialog>
class QDialogButtonBox;
class QSpinBox;
struct DlgTournamentSettingsResult
{
int gamesPerMatch = 1;
};
class DlgTournamentSettings : public QDialog
{
Q_OBJECT
public:
explicit DlgTournamentSettings(QWidget *parent = nullptr);
DlgTournamentSettingsResult getResult() const;
void setCurrentGamesPerMatch(int n);
private slots:
void actOK();
private:
QSpinBox *gamesPerMatchSpin;
QDialogButtonBox *buttonBox;
};
#endif

View file

@ -0,0 +1,232 @@
#include "tournament_widget.h"
#include <QHeaderView>
#include <QLabel>
#include <QPushButton>
#include <QTableWidgetItem>
#include <QVBoxLayout>
namespace
{
const int PLAYER_COLUMN = 0;
const int DECK_COLUMN = 1;
const int WINS_COLUMN = 2;
const int LOSSES_COLUMN = 3;
const int DRAWS_COLUMN = 4;
const int PLAYER1_COLUMN = 0;
const int SCORE_COLUMN = 1;
const int STATUS_COLUMN = 2;
const int PLAYER2_COLUMN = 3;
} // namespace
TournamentWidget::TournamentWidget(QWidget *parent) : QWidget(parent)
{
auto *mainLayout = new QVBoxLayout(this);
statusLabel = new QLabel(this);
statusLabel->setStyleSheet("font-weight: bold;");
mainLayout->addWidget(statusLabel);
roundLabel = new QLabel(this);
mainLayout->addWidget(roundLabel);
standingsTable = new QTableWidget(this);
standingsTable->setColumnCount(5);
standingsTable->horizontalHeader()->setStretchLastSection(true);
standingsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
standingsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
standingsTable->verticalHeader()->setVisible(false);
mainLayout->addWidget(standingsTable);
pairingsTable = new QTableWidget(this);
pairingsTable->setColumnCount(4);
pairingsTable->horizontalHeader()->setStretchLastSection(true);
pairingsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
pairingsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
pairingsTable->verticalHeader()->setVisible(false);
mainLayout->addWidget(pairingsTable);
openMatchButton = new QPushButton(this);
openMatchButton->setEnabled(false);
connect(openMatchButton, &QPushButton::clicked, this, [this]() { emit openMatchGameRequested(currentGameId); });
mainLayout->addWidget(openMatchButton);
retranslateUi();
}
void TournamentWidget::retranslateUi()
{
standingsTable->setHorizontalHeaderLabels({tr("Player"), tr("Deck"), tr("W"), tr("L"), tr("D")});
pairingsTable->setHorizontalHeaderLabels({tr("Player 1"), tr("Score"), tr("Status"), tr("Player 2")});
openMatchButton->setText(tr("Open match game"));
if (lastState.has_phase()) {
updateTournamentState(lastState);
} else {
statusLabel->setText(tr("Tournament"));
roundLabel->setVisible(true);
roundLabel->setText(tr("Waiting for the tournament to start"));
}
}
void TournamentWidget::setLocalPlayerId(int playerId)
{
localPlayerId = playerId;
}
void TournamentWidget::setIsSpectator(bool spectator)
{
isSpectator = spectator;
}
QString TournamentWidget::getPlayerName(const Event_TournamentState &state, int playerId) const
{
for (int i = 0; i < state.players_size(); ++i) {
if (state.players(i).player_id() == playerId) {
return QString::fromStdString(state.players(i).player_name());
}
}
return tr("Player %1").arg(playerId);
}
void TournamentWidget::updateTournamentState(const Event_TournamentState &state)
{
lastState = state;
int gamesPerMatch = getGamesPerMatch(state);
QString statusText;
switch (state.phase()) {
case Event_TournamentState::PHASE_DECK_BUILDING:
statusText = gamesPerMatch > 1 ? tr("Tournament - Deck building (Best of %1)").arg(gamesPerMatch)
: tr("Tournament - Deck building");
break;
case Event_TournamentState::PHASE_PLAYING:
statusText = gamesPerMatch > 1 ? tr("Tournament - Playing (Best of %1)").arg(gamesPerMatch)
: tr("Tournament - Playing");
break;
case Event_TournamentState::PHASE_FINISHED:
statusText = gamesPerMatch > 1 ? tr("Tournament - Finished (Best of %1)").arg(gamesPerMatch)
: tr("Tournament - Finished");
break;
default:
statusText = tr("Tournament");
break;
}
statusLabel->setText(statusText);
// The server does not report rounds before pairing starts; avoid showing "Round 0 of 0".
bool roundStarted = state.current_round() > 0 || state.total_rounds() > 0;
roundLabel->setVisible(roundStarted);
if (roundStarted) {
roundLabel->setText(tr("Round %1 of %2").arg(state.current_round()).arg(state.total_rounds()));
}
rebuildTables(state);
resolveCurrentGameId(state);
updateOpenMatchButton();
}
void TournamentWidget::updateOpenMatchButton()
{
bool playingPhase = lastState.phase() == Event_TournamentState::PHASE_PLAYING;
openMatchButton->setVisible(playingPhase);
if (!playingPhase) {
return;
}
if (hasOwnLivePairing) {
openMatchButton->setEnabled(true);
openMatchButton->setText(tr("Open match game"));
openMatchButton->setToolTip(tr("Switch to your current match game"));
} else if (isSpectator && hasAnyLivePairing) {
// Spectators have no pairing of their own but may watch any running match.
openMatchButton->setEnabled(true);
openMatchButton->setText(tr("Spectate live match"));
openMatchButton->setToolTip(tr("Watch a match that is currently running"));
} else {
openMatchButton->setEnabled(false);
openMatchButton->setText(tr("Open match game"));
openMatchButton->setToolTip(tr("No match game is running for you right now"));
}
}
void TournamentWidget::rebuildTables(const Event_TournamentState &state)
{
standingsTable->setRowCount(state.players_size());
for (int i = 0; i < state.players_size(); ++i) {
const auto &player = state.players(i);
auto *nameItem = new QTableWidgetItem(QString::fromStdString(player.player_name()));
nameItem->setToolTip(QString::fromStdString(player.player_name()));
standingsTable->setItem(i, PLAYER_COLUMN, nameItem);
auto *deckItem = new QTableWidgetItem(player.deck_submitted() ? tr("Submitted") : tr("Pending"));
deckItem->setToolTip(player.deck_submitted() ? tr("Deck submitted") : tr("Still choosing a deck"));
standingsTable->setItem(i, DECK_COLUMN, deckItem);
standingsTable->setItem(i, WINS_COLUMN, new QTableWidgetItem(QString::number(player.wins())));
standingsTable->setItem(i, LOSSES_COLUMN, new QTableWidgetItem(QString::number(player.losses())));
standingsTable->setItem(i, DRAWS_COLUMN, new QTableWidgetItem(QString::number(player.draws())));
}
pairingsTable->setRowCount(state.pairings_size());
for (int i = 0; i < state.pairings_size(); ++i) {
const auto &pairing = state.pairings(i);
QString name1 = getPlayerName(state, pairing.player1_id());
QString name2 = pairing.player2_id() == -1 ? tr("BYE") : getPlayerName(state, pairing.player2_id());
QString scoreStr;
if (getGamesPerMatch(state) > 1 && pairing.player2_id() != -1) {
scoreStr = tr("%1 - %2").arg(pairing.player1_match_wins()).arg(pairing.player2_match_wins());
}
QString statusStr;
if (pairing.player2_id() == -1) {
statusStr = tr("BYE");
} else if (pairing.has_winner_id() || pairing.is_draw()) {
statusStr = tr("Finished");
} else {
statusStr = tr("vs");
}
pairingsTable->setItem(i, PLAYER1_COLUMN, new QTableWidgetItem(name1));
pairingsTable->setItem(i, SCORE_COLUMN, new QTableWidgetItem(scoreStr));
pairingsTable->setItem(i, STATUS_COLUMN, new QTableWidgetItem(statusStr));
pairingsTable->setItem(i, PLAYER2_COLUMN, new QTableWidgetItem(name2));
}
}
int TournamentWidget::getGamesPerMatch(const Event_TournamentState &state) const
{
return state.has_settings() ? state.settings().games_per_match() : 1;
}
void TournamentWidget::resolveCurrentGameId(const Event_TournamentState &state)
{
currentGameId = -1;
hasOwnLivePairing = false;
hasAnyLivePairing = false;
for (int i = 0; i < state.pairings_size(); ++i) {
const auto &pairing = state.pairings(i);
if (pairing.game_id() == -1 || pairing.has_winner_id() || pairing.is_draw()) {
continue;
}
if (!hasOwnLivePairing && localPlayerId != -1 &&
(pairing.player1_id() == localPlayerId || pairing.player2_id() == localPlayerId)) {
currentGameId = pairing.game_id();
hasOwnLivePairing = true;
}
if (!hasAnyLivePairing) {
hasAnyLivePairing = true;
if (!hasOwnLivePairing) {
currentGameId = pairing.game_id();
}
}
if (hasOwnLivePairing && hasAnyLivePairing) {
return;
}
}
}

View file

@ -0,0 +1,58 @@
#ifndef TOURNAMENT_WIDGET_H
#define TOURNAMENT_WIDGET_H
#include <QWidget>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
class QLabel;
class QPushButton;
class QTableWidget;
class QTableWidgetItem;
/**
* @class TournamentWidget
* @ingroup GameViews
* @brief Displays live standings and pairings for a tournament game.
*
* Updated exclusively via updateTournamentState(); the widget holds no
* logic of its own beyond mapping protocol state to table rows. Navigation
* decisions (which pairing belongs to the local player) are resolved here so
* callers only need to react to openMatchGameRequested().
*/
class TournamentWidget : public QWidget
{
Q_OBJECT
public:
explicit TournamentWidget(QWidget *parent = nullptr);
void updateTournamentState(const Event_TournamentState &state);
void setLocalPlayerId(int playerId);
void setIsSpectator(bool spectator);
void retranslateUi();
signals:
/*! Emitted when the user asks to open the match game of their current pairing. */
void openMatchGameRequested(int gameId);
private:
void rebuildTables(const Event_TournamentState &state);
void resolveCurrentGameId(const Event_TournamentState &state);
void updateOpenMatchButton();
[[nodiscard]] QString getPlayerName(const Event_TournamentState &state, int playerId) const;
[[nodiscard]] int getGamesPerMatch(const Event_TournamentState &state) const;
QLabel *statusLabel;
QLabel *roundLabel;
QTableWidget *standingsTable;
QTableWidget *pairingsTable;
QPushButton *openMatchButton;
int localPlayerId = -1;
bool isSpectator = false;
int currentGameId = -1;
bool hasOwnLivePairing = false;
bool hasAnyLivePairing = false;
Event_TournamentState lastState;
};
#endif // TOURNAMENT_WIDGET_H

View file

@ -39,6 +39,7 @@
#include "libcockatrice/protocol/pb/serverinfo_playerproperties.pb.h"
#include "libcockatrice/utility/card_ref.h"
#include "tab_supervisor.h"
#include "tournament_tab_game_extension.h"
#include <QAction>
#include <QApplication>
@ -168,6 +169,10 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor,
createMenuItems();
createViewMenuItems();
if (game->getGameMetaInfo()->isTournament()) {
tournamentExtension = new TournamentTabGameExtension(this);
}
connectToGameState();
connectToPlayerManager();
connectToGameEventHandler();
@ -184,6 +189,10 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor,
gameTypes.append(game->getGameMetaInfo()->findRoomGameType(i));
}
if (tournamentExtension) {
tournamentExtension->initializeTournamentMode();
}
QTimer::singleShot(0, this, &TabGame::loadLayout);
}
@ -441,6 +450,10 @@ void TabGame::retranslateUi()
}
scene->retranslateUi();
if (tournamentExtension) {
tournamentExtension->retranslateUi();
}
}
void TabGame::refreshShortcuts()
@ -968,8 +981,19 @@ void TabGame::stopGame()
}
}
bool TabGame::switchToGameTab(int gameId)
{
return tabSupervisor->switchToGameTabIfAlreadyExists(gameId);
}
void TabGame::closeGame()
{
int parentId = game->getGameMetaInfo()->parentGameId();
if (parentId >= 0 && switchToGameTab(parentId)) {
close();
return;
}
gameMenu->clear();
gameMenu->addAction(aLeaveGame);
}

View file

@ -15,9 +15,11 @@
#include <QLoggingCategory>
#include <QMap>
#include <QPair>
#include <QPointer>
#include <QSize>
#include <QString>
#include <QStringList>
#include <QStringListModel>
#include <QVector>
#include <qtmetamacros.h>
// IWYU pragma: keep
@ -34,12 +36,12 @@ class MessageLogWidget;
class PlayerLogic;
class QAction;
class QCompleter;
class QStringListModel;
class QWidget;
class ServerInfo_Player;
class ServerInfo_User;
class TabSupervisor;
class TearOffMenu;
class TournamentTabGameExtension;
struct CardRef;
struct LoadedDeck;
@ -100,6 +102,8 @@ private:
QList<QAction *> phaseActions;
QAction *aCardMenu;
QPointer<TournamentTabGameExtension> tournamentExtension;
/**
* @brief The actions associated with managing a QDockWidget
*/
@ -197,6 +201,8 @@ public:
void connectToGameEventHandler();
void connectMessageLogToGameEventHandler();
void connectPlayerListToGameEventHandler();
/*! Brings the tab of the given game to front if it is open in this session. */
bool switchToGameTab(int gameId);
TabGame(TabSupervisor *_tabSupervisor, GameReplay *replay);
~TabGame() override;
void retranslateUi() override;
@ -210,6 +216,19 @@ public:
return game;
}
[[nodiscard]] QStackedWidget *getMainWidget() const
{
return mainWidget;
}
[[nodiscard]] QVBoxLayout *getDeckViewContainerLayout() const
{
return deckViewContainerLayout;
}
[[nodiscard]] QWidget *getDeckViewContainerWidget() const
{
return deckViewContainerWidget;
}
public slots:
void viewCardInfo(const CardRef &cardRef = {}) const;
void resetForRewind();

View file

@ -0,0 +1,170 @@
#include "tournament_tab_game_extension.h"
#include "../../widgets/draft/tournament_widget.h"
#include "tab_game.h"
#include "tab_supervisor.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <libcockatrice/game/abstract_game.h>
#include <libcockatrice/game/game_event_handler.h>
#include <libcockatrice/game/game_meta_info.h>
#include <libcockatrice/game/player/player_manager.h>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
TournamentTabGameExtension::TournamentTabGameExtension(TabGame *parent) : QObject(parent), tabGame(parent)
{
tournamentOverviewWidget = new QWidget(parent);
auto *overviewLayout = new QVBoxLayout(tournamentOverviewWidget);
auto *headerLayout = new QHBoxLayout;
backToGameButton = new QPushButton(tournamentOverviewWidget);
connect(backToGameButton, &QPushButton::clicked, this, &TournamentTabGameExtension::showDeckViewPage);
headerLayout->addWidget(backToGameButton);
headerLayout->addStretch();
overviewLayout->addLayout(headerLayout);
tournamentWidget = new TournamentWidget(tournamentOverviewWidget);
tournamentWidget->setLocalPlayerId(parent->getGame()->getPlayerManager()->getLocalPlayerId());
tournamentWidget->setIsSpectator(parent->getGame()->getPlayerManager()->isSpectator());
overviewLayout->addWidget(tournamentWidget);
parent->getMainWidget()->addWidget(tournamentOverviewWidget);
deckViewStatusLabel = new QLabel(parent->getDeckViewContainerWidget());
deckViewStatusLabel->setStyleSheet("color: palette(placeholderText);");
deckViewStatusLabel->setVisible(false);
standingsButton = new QPushButton(parent->getDeckViewContainerWidget());
connect(standingsButton, &QPushButton::clicked, this, &TournamentTabGameExtension::showOverviewPage);
standingsButton->setVisible(false);
connectSignals();
}
void TournamentTabGameExtension::connectSignals()
{
auto *handler = tabGame->getGame()->getGameEventHandler();
connect(handler, &GameEventHandler::tournamentStateChanged, this,
&TournamentTabGameExtension::onTournamentStateChanged);
connect(tournamentWidget, &TournamentWidget::openMatchGameRequested, this,
&TournamentTabGameExtension::openMatchGame);
}
void TournamentTabGameExtension::initializeTournamentMode()
{
if (!tabGame) {
return;
}
auto *deckLayout = tabGame->getDeckViewContainerLayout();
int index = 0;
deckLayout->insertWidget(index++, deckViewStatusLabel);
deckLayout->insertWidget(index++, standingsButton);
deckLayout->insertSpacing(index, 4);
}
void TournamentTabGameExtension::retranslateUi()
{
backToGameButton->setText(tr("Back to game view"));
standingsButton->setText(tr("Tournament standings"));
tournamentWidget->retranslateUi();
}
void TournamentTabGameExtension::updateDeckViewStrip(const Event_TournamentState &state)
{
int submitted = 0;
for (int i = 0; i < state.players_size(); ++i) {
if (state.players(i).deck_submitted()) {
++submitted;
}
}
QString phaseText;
switch (state.phase()) {
case Event_TournamentState::PHASE_DECK_BUILDING:
phaseText = tr("Deck building");
break;
case Event_TournamentState::PHASE_PLAYING:
phaseText = tr("Round in progress");
break;
case Event_TournamentState::PHASE_FINISHED:
phaseText = tr("Finished");
break;
default:
phaseText = tr("Unknown phase");
break;
}
// The submission count only matters while players are still picking decks.
QString text =
state.phase() == Event_TournamentState::PHASE_DECK_BUILDING
? tr("Tournament: %1 - %2/%3 decks submitted").arg(phaseText).arg(submitted).arg(state.players_size())
: tr("Tournament: %1").arg(phaseText);
deckViewStatusLabel->setText(text);
deckViewStatusLabel->setVisible(true);
}
void TournamentTabGameExtension::updateNavigationButtons(const Event_TournamentState &state)
{
bool showStandings =
state.phase() == Event_TournamentState::PHASE_PLAYING || state.phase() == Event_TournamentState::PHASE_FINISHED;
standingsButton->setVisible(showStandings);
}
void TournamentTabGameExtension::onTournamentStateChanged(const Event_TournamentState &state)
{
if (!tabGame) {
return;
}
updateDeckViewStrip(state);
updateNavigationButtons(state);
tournamentWidget->updateTournamentState(state);
// Switch to the standings page only when the phase itself changes, so score
// updates never yank the user away while they are looking at their deck.
// Instant switches are the app-wide baseline today; motion and sound cues
// for this transition are deferred to the Game dressing phase.
bool overviewPhase =
state.phase() == Event_TournamentState::PHASE_PLAYING || state.phase() == Event_TournamentState::PHASE_FINISHED;
if (overviewPhase && (!hasLastKnownPhase || state.phase() != lastKnownPhase)) {
tabGame->getMainWidget()->setCurrentWidget(tournamentOverviewWidget);
}
lastKnownPhase = state.phase();
hasLastKnownPhase = true;
}
void TournamentTabGameExtension::showOverviewPage()
{
if (tabGame) {
tabGame->getMainWidget()->setCurrentWidget(tournamentOverviewWidget);
}
}
void TournamentTabGameExtension::showDeckViewPage()
{
if (tabGame) {
tabGame->getMainWidget()->setCurrentWidget(tabGame->getDeckViewContainerWidget());
}
}
void TournamentTabGameExtension::openMatchGame(int gameId)
{
if (!tabGame || gameId <= 0) {
return;
}
// Players are auto-joined into their match game by the server, so the tab
// usually already exists and switching is enough. Otherwise (e.g. after
// leaving the match) fall back to joining as spectator.
if (tabGame->switchToGameTab(gameId)) {
return;
}
int roomId = tabGame->getGame()->getGameMetaInfo()->proto().room_id();
tabGame->getTabSupervisor()->joinGameFromUserCard(gameId, roomId, true);
}

View file

@ -0,0 +1,54 @@
#ifndef TOURNAMENT_TAB_GAME_EXTENSION_H
#define TOURNAMENT_TAB_GAME_EXTENSION_H
#include <QObject>
#include <QPointer>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
class QLabel;
class QPushButton;
class QWidget;
class TournamentWidget;
class TabGame;
/**
* @class TournamentTabGameExtension
* @ingroup GameViews
* @brief Attaches tournament behavior to a TabGame hosting a tournament hub game.
*
* Owns the tournament overview page inside the tab's stacked main widget,
* mirrors tournament state onto the always-visible deck-view status strip,
* and routes user intents (open match game, change settings, switch pages)
* between the two views.
*/
class TournamentTabGameExtension : public QObject
{
Q_OBJECT
public:
explicit TournamentTabGameExtension(TabGame *parent);
void initializeTournamentMode();
void retranslateUi();
private slots:
void onTournamentStateChanged(const Event_TournamentState &state);
void showOverviewPage();
void showDeckViewPage();
void openMatchGame(int gameId);
private:
void connectSignals();
void updateDeckViewStrip(const Event_TournamentState &state);
void updateNavigationButtons(const Event_TournamentState &state);
QPointer<TabGame> tabGame;
TournamentWidget *tournamentWidget = nullptr;
QWidget *tournamentOverviewWidget = nullptr;
QPushButton *backToGameButton = nullptr;
QPushButton *standingsButton = nullptr;
QLabel *deckViewStatusLabel = nullptr;
Event_TournamentState::TournamentPhase lastKnownPhase = Event_TournamentState::PHASE_DECK_BUILDING;
bool hasLastKnownPhase = false;
};
#endif // TOURNAMENT_TAB_GAME_EXTENSION_H

View file

@ -54,6 +54,7 @@
#include <libcockatrice/protocol/pb/event_reverse_turn.pb.h>
#include <libcockatrice/protocol/pb/event_set_active_phase.pb.h>
#include <libcockatrice/protocol/pb/event_set_active_player.pb.h>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
#include <libcockatrice/protocol/pb/game_event_container.pb.h>
#include <libcockatrice/protocol/pending_command.h>
#include <libcockatrice/utility/zone_names.h>
@ -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,13 @@ 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. 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());
}
const int playerListSize = event.player_list_size();
QVector<QPair<int, QPair<QString, QString>>> opponentDecksToDisplay;

View file

@ -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);

View file

@ -87,6 +87,21 @@ public:
return roomGameTypes.find(gameInfo_.game_types(index)).value();
}
bool isTournament() const
{
return gameInfo_.is_tournament();
}
int parentGameId() const
{
return parentGameId_;
}
void setParentGameId(int id)
{
parentGameId_ = id;
}
public slots:
void setStarted(bool s)
{
@ -111,6 +126,7 @@ signals:
private:
ServerInfo_Game gameInfo_;
int parentGameId_ = -1;
};
#endif // GAME_META_INFO_H

View file

@ -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

View file

@ -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);

View file

@ -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 <QDebug>
#include <QElapsedTimer>
@ -59,6 +62,7 @@
#include <QSharedPointer>
#include <QStringList>
#include <QTimer>
#include <algorithm>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include <libcockatrice/deck_list/deck_list.h>
@ -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,10 @@ void Server_Game::createGameStateChangedEvent(Event_GameStateChanged *event,
event->set_game_started(false);
}
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 +362,15 @@ 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;
}
// 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.
// Match sub-games carry isTournament too, so gate on the hub object only.
if (tournament && !forceStartGame) {
return;
}
@ -611,6 +628,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, tournGes);
tournament->broadcastTournamentState(tournGes);
tournGes.sendToGame(this);
}
if (playerHost) {
int newHostId = -1;
for (auto *otherPlayer : getPlayers().values()) {
@ -869,6 +897,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,
@ -947,6 +984,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 +1035,92 @@ 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()));
}
// A tournament with fewer than two players can't produce a valid bracket.
if (tournament->getPlayerCount() < 2) {
qWarning() << "Cannot start tournament with fewer than 2 players";
return;
}
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);
}

View file

@ -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
@ -30,6 +35,7 @@
#include <QList>
#include <QMap>
#include <QObject>
#include <QPointer>
#include <QRecursiveMutex>
#include <QScopedPointer>
#include <QSet>
@ -37,16 +43,21 @@
#include <QtGlobal>
#include <atomic>
#include <libcockatrice/protocol/pb/event_leave.pb.h>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
#include <libcockatrice/protocol/pb/response.pb.h>
#include <libcockatrice/protocol/pb/serverinfo_game.pb.h>
#include <qtmetamacros.h>
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 +74,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 +109,14 @@ private:
QList<GameReplay *> replayList;
GameReplay *currentReplay;
bool isTournament;
TournamentSettings tournamentSettings;
Server_Tournament *tournament;
QPointer<Server_Game> tournamentParentGame;
int tournamentMatchPlayer1Id;
int tournamentMatchPlayer2Id;
bool disconnectRemovesPlayer;
QScopedPointer<Server_DeckValidationStrategy> deckValidationStrategy;
QScopedPointer<Server_GameLifecycleStrategy> lifecycleStrategy;
@ -237,6 +256,49 @@ 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);
QPointer<Server_Game> getTournamentParentGame() const
{
return tournamentParentGame;
}
bool getDisconnectRemovesPlayer() const
{
return disconnectRemovesPlayer;
}
void setDisconnectRemovesPlayer(bool _disconnectRemovesPlayer)
{
disconnectRemovesPlayer = _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 +311,8 @@ public:
{
return lifecycleStrategy.data();
}
/** @brief Replace the match result strategy; takes ownership of @p strategy. */
void setMatchResultStrategy(Server_MatchResultStrategy *strategy);
};
#endif

View file

@ -3,7 +3,7 @@
#include <QString>
class GameConfig;
struct GameConfig;
class Server_Game;
class Server_AbstractUserInterface;

View file

@ -0,0 +1,740 @@
#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 <QLoggingCategory>
#include <algorithm>
#include <libcockatrice/deck_list/deck_list.h>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
#include <libcockatrice/protocol/pb/game_event_container.pb.h>
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)
{
QMutexLocker locker(&tournamentMutex);
TournamentPlayerData data;
data.playerId = playerId;
data.playerName = playerName;
data.dropped = false;
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);
submittedDecks.remove(playerId);
byeGivenPlayers.remove(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;
// 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));
}
}
// A drop can decide the last open pairing of the round; advance when complete.
checkAndAdvanceRound(ges);
}
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<int> available;
for (auto it = players.constBegin(); it != players.constEnd(); ++it) {
if (!it->dropped) {
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;
});
QSet<int> 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;
}
for (int j = i + 1; j < available.size(); ++j) {
if (paired.contains(available[j])) {
continue;
}
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;
}
}
// Give a bye to every remaining unpaired eligible player, worst-ranked first.
// A player receives at most one bye over the whole tournament.
QList<int> 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 = 0;
for (auto it = players.constBegin(); it != players.constEnd(); ++it) {
if (!it->dropped) {
++n;
}
}
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<QPair<int, int>> planned;
{
QMutexLocker locker(&tournamentMutex);
for (const auto &pairing : currentPairings) {
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<int>(gamesPerMatch)) {
continue;
}
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;
bool settledWithoutGame = false;
{
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<int>(gamesPerMatch)) {
qCWarning(TournamentLog) << "Refusing to exceed series length for pairing" << player1Id << 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.
// 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";
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.
ServerInfo_User creatorInfo;
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)
: 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);
// 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
{
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.
// 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<int, QPair<Server_AbstractUserInterface *, ResponseContainer *>> 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.
bool anyDeckMissing = false;
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);
} else {
anyDeckMissing = 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)
{
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
// 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) {
broadcastTournamentState(ges);
// Fall through to checkAndAdvanceRound below.
matchDecided = true;
seriesContinues = false;
} else {
// 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<int>(gamesPerMatch / 2 + 1);
const int gamesRemaining = static_cast<int>(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) {
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);
}
}
Event_TournamentState Server_Tournament::buildStateEvent() const
{
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);
// -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);
}
return state;
}
void Server_Tournament::broadcastTournamentState(GameEventStorage &ges)
{
ges.enqueueGameEvent(buildStateEvent(), -1);
}

View file

@ -0,0 +1,116 @@
#ifndef SERVER_TOURNAMENT_H
#define SERVER_TOURNAMENT_H
#include <QList>
#include <QMap>
#include <QObject>
#include <QPointer>
#include <QRecursiveMutex>
#include <QSet>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
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);
// 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, GameEventStorage &ges);
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);
// Current tournament state message, for replaying to a participant joining late.
Event_TournamentState buildStateEvent() const;
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;
bool dropped = 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<int> matchGameIds;
};
private:
QPointer<Server_Game> parentGame;
Server_MatchGameFactory *matchGameFactory;
mutable QRecursiveMutex tournamentMutex;
QMap<int, TournamentPlayerData> players;
QMap<int, DeckList *> submittedDecks;
QList<TournamentPairingData> 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<int> byeGivenPlayers;
QList<QPair<int, int>> 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

View file

@ -0,0 +1,33 @@
#include "server_tournament_lifecycle_strategy.h"
#include "server_abstract_player.h"
#include "server_game.h"
#include <QLoggingCategory>
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().data() != 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;
}

View file

@ -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

View file

@ -0,0 +1,37 @@
#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 <libcockatrice/protocol/pb/event_game_closed.pb.h>
#include <libcockatrice/protocol/pb/event_tournament_state.pb.h>
bool Server_TournamentMatchResultStrategy::onGameFinished(Server_Game *game,
int playing,
Server_AbstractPlayer *lastPlayer)
{
// 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;
}
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;
}

View file

@ -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

View file

@ -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,9 @@ 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_tournament_settings() ? static_cast<int>(cmd.tournament_settings().games_per_match()) : 1;
const int gameId = databaseInterface->getNextGameId();
if (gameId == -1) {
@ -1118,6 +1123,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<uint32_t>(qBound(1, gamesPerMatch, MAX_GAMES_PER_MATCH)));
}
game->addPlayer(this, rc, asSpectator, asJudge, false);
room->addGame(game);

View file

@ -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;
}

2
vcpkg

@ -1 +1 @@
Subproject commit 9eec002df1cd109f8f104cd966a763daf9751489
Subproject commit 9e593bb18ea69cc5095e012465dcd675a822ed0d