File indexing completed on 2024-10-06 03:45:27

0001 /*
0002     This file is part of the KDE games kwin4 program
0003     SPDX-FileCopyrightText: 2001 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kwin4player.h"
0009 
0010 // own
0011 #include "kfourinline_debug.h"
0012 // KDEGames
0013 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
0014 #include <libkdegamesprivate/kgame/kgamepropertyhandler.h>
0015 
0016 // Construct a player object
0017 KWin4Player::KWin4Player()
0018     : KPlayer()
0019 {
0020     // Register KGameProperties in KGame framework
0021     mStatus = nullptr;
0022     mWin.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mWin"));
0023     mRemis.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mRemis"));
0024     mLost.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mLost"));
0025     mBrk.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mBrk"));
0026     mAllWin.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mAllWin"));
0027     mAllRemis.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mAllRemis"));
0028     mAllLost.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mAllLost"));
0029     mAllBrk.registerData(dataHandler(), KGamePropertyBase::PolicyDirty, QStringLiteral("mAllBrk"));
0030 
0031     dataHandler()->setPolicy(KGamePropertyBase::PolicyDirty, false);
0032 
0033     // Clear all stats (they will be loaded from the config file later on)
0034     resetStats();
0035     connect(this, &KWin4Player::signalPropertyChanged, this, &KWin4Player::slotPlayerPropertyChanged);
0036 
0037     mWin.setValue(0);
0038     mLost.setValue(0);
0039     mBrk.setValue(0);
0040     mRemis.setValue(0);
0041 }
0042 
0043 // Destructor
0044 KWin4Player::~KWin4Player()
0045 {
0046     qCDebug(KFOURINLINE_LOG) << "~KPlayer()";
0047     qCDebug(KFOURINLINE_LOG) << "~KPlayer() done";
0048 }
0049 
0050 // A registered player property changed (KGame framework function). Store this property
0051 // in the score object for the GUI.
0052 void KWin4Player::slotPlayerPropertyChanged(KGamePropertyBase *prop, KPlayer * /*player*/)
0053 {
0054     if (!mStatus)
0055         return;
0056     if (!isActive())
0057         return;
0058     if (prop->id() == KGamePropertyBase::IdName) {
0059         mStatus->setPlayerName(name(), userId());
0060     } else if (prop->id() == mAllWin.id()) {
0061         mStatus->setWins(mAllWin, userId());
0062     } else if (prop->id() == mAllRemis.id()) {
0063         mStatus->setRemis(mAllRemis, userId());
0064     } else if (prop->id() == mAllLost.id()) {
0065         mStatus->setLosses(mAllLost, userId());
0066     } else if (prop->id() == mAllBrk.id()) {
0067         mStatus->setBreaks(mAllBrk, userId());
0068     }
0069 }
0070 
0071 // Read the player all time score from the config file
0072 void KWin4Player::readConfig(KConfigGroup &config)
0073 {
0074     mAllWin.setValue(config.readEntry("win", 0));
0075     mAllRemis.setValue(config.readEntry("remis", 0));
0076     mAllLost.setValue(config.readEntry("lost", 0));
0077     mAllBrk.setValue(config.readEntry("brk", 0));
0078 }
0079 
0080 // Write the player all time score to the config file
0081 void KWin4Player::writeConfig(KConfigGroup &config)
0082 {
0083     config.writeEntry("win", mAllWin.value());
0084     config.writeEntry("remis", mAllRemis.value());
0085     config.writeEntry("lost", mAllLost.value());
0086     config.writeEntry("brk", mAllBrk.value());
0087 }
0088 
0089 // Increase the number of wins
0090 void KWin4Player::incWin()
0091 {
0092     mWin.setValue(mWin.value() + 1);
0093     mAllWin.setValue(mAllWin.value() + 1);
0094 }
0095 
0096 // Increase the number of losses
0097 void KWin4Player::incLost()
0098 {
0099     mLost.setValue(mLost.value() + 1);
0100     mAllLost.setValue(mAllLost.value() + 1);
0101 }
0102 
0103 // Increase the number of draws
0104 void KWin4Player::incRemis()
0105 {
0106     mRemis.setValue(mRemis.value() + 1);
0107     mAllRemis.setValue(mAllRemis.value() + 1);
0108 }
0109 
0110 // Increase the number of aborted games
0111 void KWin4Player::incBrk()
0112 {
0113     mBrk.setValue(mBrk.value() + 1);
0114     mAllBrk.setValue(mAllBrk.value() + 1);
0115 }
0116 
0117 // Clear player status: For the argument true the long time
0118 // statistics in the config file is cleared as well. For the
0119 // argument false only the current session is cleared.
0120 void KWin4Player::resetStats(bool all)
0121 {
0122     mWin = 0;
0123     mLost = 0;
0124     mBrk = 0;
0125     mRemis = 0;
0126     if (all) {
0127         mAllWin = 0;
0128         mAllLost = 0;
0129         mAllBrk = 0;
0130         mAllRemis = 0;
0131     }
0132 }
0133 
0134 #include "moc_kwin4player.cpp"