File: kwin4player.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (165 lines) | stat: -rw-r--r-- 4,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 2001 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Header includes
#include "kwin4player.h"
#include "kfourinline_debug.h"

#define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
#include <libkdegamesprivate/kgame/kgamepropertyhandler.h>


// Construct a player object
KWin4Player::KWin4Player() : KPlayer()
{
  // Register KGameProperties in KGame framework  
  mStatus = 0;
  mWin.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mWin"));
  mRemis.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mRemis"));
  mLost.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mLost"));
  mBrk.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mBrk"));
  mAllWin.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mAllWin"));
  mAllRemis.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mAllRemis"));
  mAllLost.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mAllLost"));
  mAllBrk.registerData(dataHandler(),KGamePropertyBase::PolicyDirty,QStringLiteral("mAllBrk"));

  dataHandler()->setPolicy(KGamePropertyBase::PolicyDirty,false);

  // Clear all stats (they will be loaded from the config file later on)
  resetStats();
  connect(this, &KWin4Player::signalPropertyChanged, this, &KWin4Player::slotPlayerPropertyChanged);

  mWin.setValue(0);
  mLost.setValue(0);
  mBrk.setValue(0);
  mRemis.setValue(0);
}

// Destructor
KWin4Player::~KWin4Player()
{
  qCDebug(KFOURINLINE_LOG) << "~KPlayer()";
  qCDebug(KFOURINLINE_LOG) << "~KPlayer() done";
}


// A registered player property changed (KGame framework function). Store this property
// in the score object for the GUI.
void KWin4Player::slotPlayerPropertyChanged(KGamePropertyBase *prop, KPlayer * /*player*/)
{
  if (!mStatus) return ;
  if (!isActive()) return ;
  if (prop->id()==KGamePropertyBase::IdName)
  {
    mStatus->setPlayerName(name(), userId());
  }
  else if (prop->id()==mAllWin.id())
  {
    mStatus->setWins(mAllWin, userId());
  }
  else if (prop->id()==mAllRemis.id())
  {
    mStatus->setRemis(mAllRemis, userId());
  }
  else if (prop->id()==mAllLost.id())
  {
    mStatus->setLosses(mAllLost, userId());
  }
  else if (prop->id()==mAllBrk.id())
  {
    mStatus->setBreaks(mAllBrk, userId());
  }
}


// Read the player all time score from the config file
void KWin4Player::readConfig(KConfigGroup& config)
{
  mAllWin.setValue(config.readEntry("win",0));
  mAllRemis.setValue(config.readEntry("remis",0));
  mAllLost.setValue(config.readEntry("lost",0));
  mAllBrk.setValue(config.readEntry("brk",0));
}


// Write the player all time score to the config file
void KWin4Player::writeConfig(KConfigGroup& config)
{
  config.writeEntry("win",mAllWin.value());
  config.writeEntry("remis",mAllRemis.value());
  config.writeEntry("lost",mAllLost.value());
  config.writeEntry("brk",mAllBrk.value());
}


// Increase the number of wins
void KWin4Player::incWin()
{
  mWin.setValue(mWin.value()+1);
  mAllWin.setValue(mAllWin.value()+1);
}


// Increase the number of losses
void KWin4Player::incLost()
{
  mLost.setValue(mLost.value()+1);
  mAllLost.setValue(mAllLost.value()+1);
}


// Increase the number of draws
void KWin4Player::incRemis()
{
  mRemis.setValue(mRemis.value()+1);
  mAllRemis.setValue(mAllRemis.value()+1);
}


// Increase the number of aborted games
void KWin4Player::incBrk()
{
  mBrk.setValue(mBrk.value()+1);
  mAllBrk.setValue(mAllBrk.value()+1);
}


// Clear player status: For the argument true the long time
// statistics in the config file is cleared as well. For the
// argument false only the current session is cleared.
void KWin4Player::resetStats(bool all)
{
  mWin=0;
  mLost=0;
  mBrk=0;
  mRemis=0;
  if (all)
  {
    mAllWin=0;
    mAllLost=0;
    mAllBrk=0;
    mAllRemis=0;
  }
}