File indexing completed on 2024-07-21 03:49:35

0001 /*
0002     This file is part of the KDE games lskat program
0003     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "player.h"
0009 
0010 // Qt includes
0011 
0012 // KF includes
0013 #include <KConfigGroup>
0014 
0015 // Local includes
0016 #include "abstractinput.h"
0017 
0018 // Constructor for the player
0019 Player::Player(int id, QObject *parent)
0020     : QObject(parent)
0021 {
0022     mId     = id;
0023     mCards.clear();
0024     mPoints = 0;
0025     mDeck   = nullptr;
0026     mInput  = nullptr;
0027     mTrump  = Club;
0028 
0029     // Reset internal variables - they are set by 'load'
0030     setName(QLatin1String(""));
0031     mScore         = 0;
0032     mNumberOfGames = 0;
0033     mGamesWon      = 0;
0034 }
0035 
0036 // Destructor
0037 Player::~Player()
0038 {
0039     delete mInput;
0040 }
0041 
0042 // Save properties
0043 void Player::save(KConfigGroup &config)
0044 {
0045     config.writeEntry("name", mName);
0046     config.writeEntry("gameswon", mGamesWon);
0047     config.writeEntry("score", mScore);
0048     config.writeEntry("noofgames", mNumberOfGames);
0049 }
0050 
0051 // Load properties
0052 void Player::load(KConfigGroup &config)
0053 {
0054     mName          = config.readEntry("name", mName);
0055     mGamesWon      = config.readEntry("gameswon", mGamesWon);
0056     mScore         = config.readEntry("score", mScore);
0057     mNumberOfGames = config.readEntry("noofgames", mNumberOfGames);
0058 
0059     // Emit signals
0060     refresh();
0061 }
0062 
0063 // Set the deck for drawing cards.
0064 void Player::setDeck(Deck *deck)
0065 {
0066     mDeck = deck;
0067 }
0068 
0069 // Clear the all time statistics of this player.
0070 void Player::clear()
0071 {
0072     mNumberOfGames = 0;
0073     mScore = 0;
0074     mGamesWon = 0;
0075     refresh();
0076 }
0077 
0078 // Deal a number of cards to this player
0079 void Player::deal(int amount)
0080 {
0081     if (!mDeck)
0082     {
0083         qCCritical(LSKAT_LOG) << "No deck set to player.";
0084         return;
0085     }
0086     mCards.clear();
0087     mCards.resize(amount);
0088     for (int i = 0; i < amount; i++)
0089     {
0090         int card = mDeck->drawCard();
0091         mCards[i] = card;
0092     }
0093 
0094     // Reset moves and points
0095     mMovesWon = 0;
0096     setPoints(0);
0097     mWonCards.clear();
0098 
0099     refresh();
0100 }
0101 
0102 // Retrieve the input device of the player
0103 AbstractInput *Player::input()
0104 {
0105     return mInput;
0106 }
0107 
0108 // Set the input device of the player
0109 void Player::setInput(AbstractInput *input)
0110 {
0111     // Try to set the same turn status after changing input
0112     bool oldTurnAllowed = false;
0113     // Get rid of old input device if existing
0114     if (mInput)
0115     {
0116         oldTurnAllowed = mInput->inputAllowed();
0117         mInput->setInputAllowed(false);
0118         delete mInput;
0119     }
0120     // Store new input
0121     mInput = input;
0122     // Store player
0123     mInput->setId(mId);
0124     // Restore turn status
0125     mInput->setInputAllowed(oldTurnAllowed);
0126 
0127     refresh();
0128 }
0129 
0130 // Set this player to start a turn
0131 void Player::startTurn()
0132 {
0133     mInput->setInputAllowed(true);
0134 }
0135 
0136 // Set this player to stop a turn
0137 void Player::stopTurn()
0138 {
0139     mInput->setInputAllowed(false);
0140 }
0141 
0142 // Remove a card from the given position. Typically if the card was played.
0143 void Player::deleteCard(int cardPosition)
0144 {
0145     if (cardPosition >= mCards.size())
0146     {
0147         qCCritical(LSKAT_LOG) << "Player " << mId << " tries to delete non existing card position "
0148                 << cardPosition << " >=" << mCards.size();
0149     }
0150     mCards[cardPosition] = -1;
0151 }
0152 
0153 // Add a card to the player
0154 void Player::addCard(int cardPosition, int card)
0155 {
0156     if (cardPosition >= mCards.size())
0157     {
0158         qCCritical(LSKAT_LOG) << "Player " << mId << " tries to add to existing card position "
0159                 << cardPosition << " >=" << mCards.size();
0160     }
0161     mCards[cardPosition] = card;
0162 }
0163 
0164 // Retrieve card value at given logical position
0165 int Player::getCard(int playerNumber)
0166 {
0167     if (playerNumber >= mCards.size())
0168     {
0169         qCCritical(LSKAT_LOG) << "Player " << mId << " tries to get non existing card "
0170                 << playerNumber << " >=" << mCards.size();
0171     }
0172 
0173     int card = mCards[playerNumber];
0174     return card;
0175 }
0176 
0177 // Increases the number of moves one for this player
0178 void Player::increaseMovesWon(int amount)
0179 {
0180     mMovesWon += amount;
0181 }
0182 
0183 // Retrieve the number of won moves for this player
0184 int Player::noOfMovesWon()
0185 {
0186     return mMovesWon;
0187 }
0188 
0189 // Adds a card which is one in a move to this player.
0190 void Player::addWonCard(int card)
0191 {
0192     // Store card
0193     mWonCards.append(card);
0194 
0195     // Add points
0196     int value = mDeck->getCardValue(card);
0197     setPoints(points() + value);
0198 }
0199 
0200 // Retrieve a card won by this player.
0201 int Player::getWonCard(int no)
0202 {
0203     if (no >= mWonCards.size())
0204     {
0205         qCCritical(LSKAT_LOG) << "Player::getWonCard This card " << no << " is not available. "
0206                 << "Only " << mWonCards.size() << " cards stored.";
0207         return -1;
0208     }
0209     return mWonCards[no];
0210 }
0211 
0212 // Retrieve the amount of points this player has
0213 int Player::points()
0214 {
0215     return mPoints;
0216 }
0217 
0218 // Set the points of the player.
0219 void Player::setPoints(int points)
0220 {
0221     mPoints = points;
0222     refresh();
0223 }
0224 
0225 // Retrieve the player's name
0226 QString Player::name()
0227 {
0228     return mName;
0229 }
0230 
0231 // Set the name of the player.
0232 void Player::setName(const QString &name)
0233 {
0234     mName = name;
0235     refresh();
0236 }
0237 
0238 // Add a number of won games to the overall statistic
0239 void Player::addWonGame(int amount)
0240 {
0241     mGamesWon += amount;
0242     refresh();
0243 }
0244 
0245 // Get amount of won games
0246 int Player::wonGames()
0247 {
0248     return mGamesWon;
0249 }
0250 
0251 // Add a number of games to the overall statistic
0252 void Player::addGame(int amount)
0253 {
0254     mNumberOfGames += amount;
0255     refresh();
0256 }
0257 
0258 // Get number of games
0259 int Player::games()
0260 {
0261     return mNumberOfGames;
0262 }
0263 
0264 // Add a score to the overall statistic
0265 void Player::addScore(int amount)
0266 {
0267     mScore += amount;
0268     refresh();
0269 }
0270 
0271 // Get score
0272 int Player::score()
0273 {
0274     return mScore;
0275 }
0276 
0277 // Set trump
0278 void Player::setTrump(Suite trump)
0279 {
0280     mTrump = trump;
0281     refresh();
0282 }
0283 
0284 // Get trump
0285 Suite Player::trump()
0286 {
0287     return mTrump;
0288 }
0289 
0290 // Emit all signals for GUI
0291 void Player::refresh()
0292 {
0293     Q_EMIT signalUpdate(this);
0294 }
0295 
0296 #include "moc_player.cpp"