Cockatrice/libcockatrice_game/libcockatrice/game/player/player_manager.cpp
BruebachL 345b0e8ba9
[Refactor] Move game logic into libcockatrice_game (#7437)
* [Refactor] Move game logic into libcockatrice_game

* [Refactor] Include card_state.h in player_actions.h

* [Game] Inject settings providers into libcockatrice_game, drop Widgets

* [Game] Follow base rename to createCardRequested after rebase

* [Game] Drop unused CardItem forward declaration

* [Game] Fix includes after moving game logic into libcockatrice_game

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-10-31 18:44:15 +01:00

103 lines
No EOL
3 KiB
C++

#include "player_manager.h"
#include "../abstract_game.h"
#include "../game_state.h"
#include "player_info.h"
#include "player_logic.h"
#include <QList>
#include <QMapIterator>
#include <QSharedPointer>
PlayerManager::PlayerManager(AbstractGame *_game,
int _localPlayerId,
bool _localPlayerIsJudge,
bool localPlayerIsSpectator,
IInterfaceSettingsProvider *_interfaceSettings,
ICardPreferenceProvider *_cardPreferences)
: QObject(_game), game(_game), players(QMap<int, PlayerLogic *>()), localPlayerId(_localPlayerId),
localPlayerIsJudge(_localPlayerIsJudge), localPlayerIsSpectator(localPlayerIsSpectator),
interfaceSettings(_interfaceSettings), cardPreferences(_cardPreferences)
{
}
bool PlayerManager::isMainPlayerConceded() const
{
PlayerLogic *player = players.value(localPlayerId, nullptr);
return player && player->getConceded();
}
PlayerLogic *PlayerManager::getActiveLocalPlayer(int activePlayer) const
{
PlayerLogic *active = players.value(activePlayer, 0);
if (active) {
if (active->getPlayerInfo()->getLocal()) {
return active;
}
}
QMapIterator<int, PlayerLogic *> playerIterator(players);
while (playerIterator.hasNext()) {
PlayerLogic *temp = playerIterator.next().value();
if (temp->getPlayerInfo()->getLocal()) {
return temp;
}
}
return nullptr;
}
bool PlayerManager::isLocalPlayer(int playerId)
{
return game->getGameState()->getIsLocalGame() || playerId == localPlayerId;
}
PlayerLogic *PlayerManager::addPlayer(int playerId, const ServerInfo_User &info)
{
auto *newPlayer = new PlayerLogic(info, playerId, isLocalPlayer(playerId) || game->getGameState()->getIsLocalGame(),
isJudge(), getGame(), interfaceSettings, cardPreferences);
connect(newPlayer, &PlayerLogic::concededChanged, this, &PlayerManager::onPlayerConceded);
players.insert(playerId, newPlayer);
emit playerAdded(newPlayer);
emit playerCountChanged();
return newPlayer;
}
void PlayerManager::removePlayer(int playerId)
{
PlayerLogic *player = getPlayer(playerId);
if (!player) {
return;
}
emit playerRemoved(player);
emit playerCountChanged();
players.remove(playerId);
player->deleteLater();
}
PlayerLogic *PlayerManager::getPlayer(int playerId) const
{
PlayerLogic *player = players.value(playerId, 0);
if (!player) {
return nullptr;
}
return player;
}
void PlayerManager::clearSpectators()
{
const QList<int> spectatorIds = spectators.keys();
for (int spectatorId : spectatorIds) {
removeSpectator(spectatorId);
}
}
void PlayerManager::onPlayerConceded(int playerId, bool conceded)
{
// Everything else cares about this
if (conceded) {
emit playerConceded(playerId);
} else {
emit playerUnconceded(playerId);
}
}