From cf6120628dcf79ed8549016a5ee69935c7159aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Sun, 7 Sep 2025 18:20:04 +0200 Subject: [PATCH] Hook up playerListWidget. Took 1 hour 2 minutes Took 10 seconds --- cockatrice/src/client/tabs/tab_game.cpp | 20 ++++++++++++------- cockatrice/src/client/tabs/tab_game.h | 1 + cockatrice/src/game/game_event_handler.cpp | 10 +++++++--- cockatrice/src/game/game_event_handler.h | 6 +++++- .../src/game/player/player_list_widget.cpp | 19 +++++++++++++++--- .../src/game/player/player_list_widget.h | 11 +++++++--- 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/cockatrice/src/client/tabs/tab_game.cpp b/cockatrice/src/client/tabs/tab_game.cpp index 7d2368457..9305d0c77 100644 --- a/cockatrice/src/client/tabs/tab_game.cpp +++ b/cockatrice/src/client/tabs/tab_game.cpp @@ -120,6 +120,7 @@ TabGame::TabGame(TabSupervisor *_tabSupervisor, createCardInfoDock(); createPlayerListDock(); + connectPlayerListToGameEventHandler(); createMessageDock(); connectMessageLogToGameEventHandler(); createPlayAreaWidget(); @@ -189,6 +190,16 @@ void TabGame::connectMessageLogToGameEventHandler() connect(gameEventHandler, &GameEventHandler::logGameClosed, messageLog, &MessageLogWidget::logGameClosed); } +void TabGame::connectPlayerListToGameEventHandler() +{ + connect(gameState, &GameState::playerAdded, playerListWidget, &PlayerListWidget::addPlayer); + connect(gameEventHandler, &GameEventHandler::playerLeft, playerListWidget, &PlayerListWidget::removePlayer); + connect(gameEventHandler, &GameEventHandler::spectatorJoined, playerListWidget, &PlayerListWidget::addSpectator); + connect(gameEventHandler, &GameEventHandler::spectatorLeft, playerListWidget, &PlayerListWidget::removePlayer); + connect(gameEventHandler, &GameEventHandler::playerPropertiesChanged, playerListWidget, + &PlayerListWidget::updatePlayerProperties); +} + void TabGame::loadReplay(GameReplay *replay) { gameMetaInfo->setFromProto(replay->game_info()); @@ -526,7 +537,7 @@ void TabGame::removePlayerFromAutoCompleteList(QString playerName) void TabGame::addSpectator(ServerInfo_PlayerProperties prop) { - playerListWidget->addPlayer(prop); + playerListWidget->addSpectator(prop); } void TabGame::removeSpectator(int spectatorId, ServerInfo_User spectator) @@ -534,8 +545,6 @@ void TabGame::removeSpectator(int spectatorId, ServerInfo_User spectator) Q_UNUSED(spectator); QString playerName = "@" + gameState->getSpectatorName(spectatorId); removePlayerFromAutoCompleteList(playerName); - - playerListWidget->removePlayer(spectatorId); } void TabGame::actPhaseAction() @@ -623,7 +632,7 @@ void TabGame::processPlayerLeave(Player *leavingPlayer) { QString playerName = "@" + leavingPlayer->getName(); removePlayerFromAutoCompleteList(playerName); - playerListWidget->removePlayer(leavingPlayer->getId()); + scene->removePlayer(leavingPlayer); } @@ -632,9 +641,6 @@ Player *TabGame::addPlayer(Player *newPlayer) QString newPlayerName = "@" + newPlayer->getName(); addPlayerToAutoCompleteList(newPlayerName); - // TODO - // playerListWidget->addPlayer(newPlayer); - scene->addPlayer(newPlayer); connect(newPlayer, &Player::newCardAdded, this, &TabGame::newCardAdded); diff --git a/cockatrice/src/client/tabs/tab_game.h b/cockatrice/src/client/tabs/tab_game.h index 318943343..39632938d 100644 --- a/cockatrice/src/client/tabs/tab_game.h +++ b/cockatrice/src/client/tabs/tab_game.h @@ -173,6 +173,7 @@ public: const Event_GameJoined &event, const QMap &_roomGameTypes); void connectMessageLogToGameEventHandler(); + void connectPlayerListToGameEventHandler(); void loadReplay(GameReplay *replay); TabGame(TabSupervisor *_tabSupervisor, GameReplay *replay); ~TabGame() override; diff --git a/cockatrice/src/game/game_event_handler.cpp b/cockatrice/src/game/game_event_handler.cpp index a64487a2f..59d0cd11b 100644 --- a/cockatrice/src/game/game_event_handler.cpp +++ b/cockatrice/src/game/game_event_handler.cpp @@ -237,6 +237,8 @@ void GameEventHandler::eventSpectatorLeave(const Event_Leave &event, { emit logSpectatorLeave(gameState->getSpectatorName(eventPlayerId), getLeaveReason(event.reason())); + emit spectatorLeft(eventPlayerId); + gameState->removeSpectator(eventPlayerId); game->emitUserEvent(); @@ -322,7 +324,7 @@ void GameEventHandler::eventPlayerPropertiesChanged(const Event_PlayerProperties if (!player) return; const ServerInfo_PlayerProperties &prop = event.player_properties(); - // playerListWidget->updatePlayerProperties(prop, eventPlayerId); + emit playerPropertiesChanged(prop, eventPlayerId); const auto contextType = static_cast(getPbExtension(context)); switch (contextType) { @@ -395,11 +397,13 @@ void GameEventHandler::eventJoin(const Event_Join &event, int /*eventPlayerId*/, if (playerInfo.spectator()) { gameState->addSpectator(playerId, playerInfo); emit logJoinSpectator(playerName); + emit spectatorJoined(playerInfo); } else { Player *newPlayer = gameState->addPlayer(playerId, playerInfo.user_info(), game); emit logJoinPlayer(newPlayer); + emit playerJoined(playerInfo); } - // playerListWidget->addPlayer(playerInfo); + game->emitUserEvent(); } @@ -427,7 +431,7 @@ void GameEventHandler::eventLeave(const Event_Leave &event, int eventPlayerId, c if (!player) return; - emit playerLeft(player); + emit playerLeft(eventPlayerId); emit logLeave(player, getLeaveReason(event.reason())); diff --git a/cockatrice/src/game/game_event_handler.h b/cockatrice/src/game/game_event_handler.h index e1ef72166..3ce036e12 100644 --- a/cockatrice/src/game/game_event_handler.h +++ b/cockatrice/src/game/game_event_handler.h @@ -92,8 +92,12 @@ signals: void localPlayerReadyStateChanged(int playerId, bool ready); void gameStopped(); void gameClosed(); - void playerLeft(Player *leavingPlayer); + void playerPropertiesChanged(const ServerInfo_PlayerProperties &prop, int playerId); + void playerJoined(const ServerInfo_PlayerProperties &playerInfo); + void playerLeft(int leavingPlayerId); void playerKicked(); + void spectatorJoined(const ServerInfo_PlayerProperties &spectatorInfo); + void spectatorLeft(int leavingSpectatorId); void gameFlooded(); void containerProcessingStarted(GameEventContext context); void setContextJudgeName(QString judgeName); diff --git a/cockatrice/src/game/player/player_list_widget.cpp b/cockatrice/src/game/player/player_list_widget.cpp index dc7489dce..4e901ab41 100644 --- a/cockatrice/src/game/player/player_list_widget.cpp +++ b/cockatrice/src/game/player/player_list_widget.cpp @@ -97,11 +97,24 @@ void PlayerListWidget::retranslateUi() { } -void PlayerListWidget::addPlayer(const ServerInfo_PlayerProperties &player) +void PlayerListWidget::addPlayer(const Player *player) { + QTreeWidgetItem *newPlayer = new PlayerListTWI; - players.insert(player.player_id(), newPlayer); - updatePlayerProperties(player); + players.insert(player->getId(), newPlayer); + // updatePlayerProperties(player->getUserInfo()); + addTopLevelItem(newPlayer); + sortItems(1, Qt::AscendingOrder); + resizeColumnToContents(4); + resizeColumnToContents(5); +} + +void PlayerListWidget::addSpectator(const ServerInfo_PlayerProperties &spectator) +{ + + QTreeWidgetItem *newPlayer = new PlayerListTWI; + players.insert(spectator.player_id(), newPlayer); + updatePlayerProperties(spectator); addTopLevelItem(newPlayer); sortItems(1, Qt::AscendingOrder); resizeColumnToContents(4); diff --git a/cockatrice/src/game/player/player_list_widget.h b/cockatrice/src/game/player/player_list_widget.h index a0714bc4d..7caf3db8a 100644 --- a/cockatrice/src/game/player/player_list_widget.h +++ b/cockatrice/src/game/player/player_list_widget.h @@ -1,6 +1,8 @@ #ifndef PLAYERLISTWIDGET_H #define PLAYERLISTWIDGET_H +#include "player.h" + #include #include #include @@ -47,12 +49,15 @@ signals: public: PlayerListWidget(TabSupervisor *_tabSupervisor, AbstractClient *_client, TabGame *_game, QWidget *parent = nullptr); void retranslateUi(); - void addPlayer(const ServerInfo_PlayerProperties &player); - void removePlayer(int playerId); void setActivePlayer(int playerId); - void updatePlayerProperties(const ServerInfo_PlayerProperties &prop, int playerId = -1); void setGameStarted(bool _gameStarted, bool resuming); void showContextMenu(const QPoint &pos, const QModelIndex &index); + +public slots: + void addPlayer(const Player *player); + void addSpectator(const ServerInfo_PlayerProperties &spectator); + void removePlayer(int playerId); + void updatePlayerProperties(const ServerInfo_PlayerProperties &prop, int playerId = -1); }; #endif