• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • highscore
kexthighscore_item.cpp
1/*
2 This file is part of the TDE games library
3 Copyright (C) 2001-2003 Nicolas Hadacek (hadacek@kde.org)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kexthighscore_item.h"
21
22#include <tqlayout.h>
23#include <tdeglobal.h>
24#include <kdialogbase.h>
25#include <kdebug.h>
26
27#include "khighscore.h"
28#include "kexthighscore_internal.h"
29#include "kexthighscore_gui.h"
30
31
32namespace KExtHighscore
33{
34
35//-----------------------------------------------------------------------------
36Item::Item(const TQVariant &def, const TQString &label, int alignment)
37 : _default(def), _label(label), _alignment(alignment),
38 _format(NoFormat), _special(NoSpecial)
39{}
40
41Item::~Item()
42{}
43
44TQVariant Item::read(uint, const TQVariant &value) const
45{
46 return value;
47}
48
49void Item::setPrettyFormat(Format format)
50{
51 bool buint = ( _default.type()==TQVariant::UInt );
52 bool bdouble = ( _default.type()==TQVariant::Double );
53 bool bnum = ( buint || bdouble || _default.type()==TQVariant::Int );
54
55 switch (format) {
56 case OneDecimal:
57 case Percentage:
58 Q_ASSERT(bdouble);
59 break;
60 case MinuteTime:
61 Q_ASSERT(bnum);
62 break;
63 case DateTime:
64 Q_ASSERT( _default.type()==TQVariant::DateTime );
65 break;
66 case NoFormat:
67 break;
68 }
69
70 _format = format;
71}
72
73void Item::setPrettySpecial(Special special)
74{
75 bool buint = ( _default.type()==TQVariant::UInt );
76 bool bnum = ( buint || _default.type()==TQVariant::Double
77 || _default.type()==TQVariant::Int );
78
79 switch (special) {
80 case ZeroNotDefined:
81 Q_ASSERT(bnum);
82 break;
83 case NegativeNotDefined:
84 Q_ASSERT(bnum && !buint);
85 break;
86 case DefaultNotDefined:
87 break;
88 case Anonymous:
89 Q_ASSERT( _default.type()==TQVariant::String );
90 break;
91 case NoSpecial:
92 break;
93 }
94
95 _special = special;
96}
97
98TQString Item::timeFormat(uint n)
99{
100 Q_ASSERT( n<=3600 && n!=0 );
101 n = 3600 - n;
102 return TQString::number(n / 60).rightJustify(2, '0') + ':'
103 + TQString::number(n % 60).rightJustify(2, '0');
104}
105
106TQString Item::pretty(uint, const TQVariant &value) const
107{
108 switch (_special) {
109 case ZeroNotDefined:
110 if ( value.toUInt()==0 ) return "--";
111 break;
112 case NegativeNotDefined:
113 if ( value.toInt()<0 ) return "--";
114 break;
115 case DefaultNotDefined:
116 if ( value==_default ) return "--";
117 break;
118 case Anonymous:
119 if ( value.toString()==ItemContainer::ANONYMOUS )
120 return i18n(ItemContainer::ANONYMOUS_LABEL);
121 break;
122 case NoFormat:
123 break;
124 }
125
126 switch (_format) {
127 case OneDecimal:
128 return TQString::number(value.toDouble(), 'f', 1);
129 case Percentage:
130 return TQString::number(value.toDouble(), 'f', 1) + "%";
131 case MinuteTime:
132 return timeFormat(value.toUInt());
133 case DateTime:
134 if ( value.toDateTime().isNull() ) return "--";
135 return TDEGlobal::locale()->formatDateTime(value.toDateTime());
136 case NoSpecial:
137 break;
138 }
139
140 return value.toString();
141}
142
143//-----------------------------------------------------------------------------
144Score::Score(ScoreType type)
145 : _type(type)
146{
147 const ItemArray &items = internal->scoreInfos();
148 for (uint i=0; i<items.size(); i++)
149 _data[items[i]->name()] = items[i]->item()->defaultValue();
150}
151
152Score::~Score()
153{}
154
155const TQVariant &Score::data(const TQString &name) const
156{
157 Q_ASSERT( _data.contains(name) );
158 return _data[name];
159}
160
161void Score::setData(const TQString &name, const TQVariant &value)
162{
163 Q_ASSERT( _data.contains(name) );
164 Q_ASSERT( _data[name].type()==value.type() );
165 _data[name] = value;
166}
167
168bool Score::isTheWorst() const
169{
170 Score s;
171 return score()==s.score();
172}
173
174bool Score::operator <(const Score &score)
175{
176 return internal->manager.isStrictlyLess(*this, score);
177}
178
179TQDataStream &operator <<(TQDataStream &s, const Score &score)
180{
181 s << (TQ_UINT8)score.type();
182 s << score._data;
183 return s;
184}
185
186TQDataStream &operator >>(TQDataStream &s, Score &score)
187{
188 TQ_UINT8 type;
189 s >> type;
190 score._type = (ScoreType)type;
191 s >> score._data;
192 return s;
193}
194
195//-----------------------------------------------------------------------------
196MultiplayerScores::MultiplayerScores()
197{}
198
199MultiplayerScores::~MultiplayerScores()
200{}
201
202void MultiplayerScores::clear()
203{
204 Score score;
205 for (uint i=0; i<_scores.size(); i++) {
206 _nbGames[i] = 0;
207 TQVariant name = _scores[i].data("name");
208 _scores[i] = score;
209 _scores[i].setData("name", name);
210 _scores[i]._data["mean score"] = double(0);
211 _scores[i]._data["nb won games"] = uint(0);
212 }
213}
214
215void MultiplayerScores::setPlayerCount(uint nb)
216{
217 _nbGames.resize(nb);
218 _scores.resize(nb);
219 clear();
220}
221
222void MultiplayerScores::setName(uint i, const TQString &name)
223{
224 _scores[i].setData("name", name);
225}
226
227void MultiplayerScores::addScore(uint i, const Score &score)
228{
229 TQVariant name = _scores[i].data("name");
230 double mean = _scores[i].data("mean score").toDouble();
231 uint won = _scores[i].data("nb won games").toUInt();
232 _scores[i] = score;
233 _scores[i].setData("name", name);
234 _nbGames[i]++;
235 mean += (double(score.score()) - mean) / _nbGames[i];
236 _scores[i]._data["mean score"] = mean;
237 if ( score.type()==Won ) won++;
238 _scores[i]._data["nb won games"] = won;
239}
240
241void MultiplayerScores::show(TQWidget *parent)
242{
243 // check consistency
244 if ( _nbGames.size()<2 ) kdWarning(11002) << "less than 2 players" << endl;
245 else {
246 bool ok = true;
247 uint nb = _nbGames[0];
248 for (uint i=1; i<_nbGames.size(); i++)
249 if ( _nbGames[i]!=nb ) ok = false;
250 if (!ok)
251 kdWarning(11002) << "players have not same number of games" << endl;
252 }
253
254 // order the players according to the number of won games
255 TQValueVector<Score> ordered;
256 for (uint i=0; i<_scores.size(); i++) {
257 uint won = _scores[i].data("nb won games").toUInt();
258 double mean = _scores[i].data("mean score").toDouble();
259 TQValueVector<Score>::iterator it;
260 for(it = ordered.begin(); it!=ordered.end(); ++it) {
261 uint cwon = (*it).data("nb won games").toUInt();
262 double cmean = (*it).data("mean score").toDouble();
263 if ( won<cwon || (won==cwon && mean<cmean) ) {
264 ordered.insert(it, _scores[i]);
265 break;
266 }
267 }
268 if ( it==ordered.end() ) ordered.push_back(_scores[i]);
269 }
270
271 // show the scores
272 KDialogBase dialog(KDialogBase::Plain, i18n("Multiplayers Scores"),
273 KDialogBase::Close, KDialogBase::Close,
274 parent, "show_multiplayers_score", true, true);
275 TQHBoxLayout *hbox = new TQHBoxLayout(dialog.plainPage(),
276 KDialog::marginHint(), KDialog::spacingHint());
277
278 TQVBox *vbox = new TQVBox(dialog.plainPage());
279 hbox->addWidget(vbox);
280 if ( _nbGames[0]==0 ) (void)new TQLabel(i18n("No game played."), vbox);
281 else {
282 (void)new TQLabel(i18n("Scores for last game:"), vbox);
283 (void)new LastMultipleScoresList(ordered, vbox);
284 }
285
286 if ( _nbGames[0]>1 ) {
287 vbox = new TQVBox(dialog.plainPage());
288 hbox->addWidget(vbox);
289 (void)new TQLabel(i18n("Scores for the last %1 games:")
290 .arg(_nbGames[0]), vbox);
291 (void)new TotalMultipleScoresList(ordered, vbox);
292 }
293
294 dialog.enableButtonSeparator(false);
295 dialog.exec();
296}
297
298TQDataStream &operator <<(TQDataStream &s, const MultiplayerScores &score)
299{
300 s << score._scores;
301 s << score._nbGames;
302 return s;
303}
304
305TQDataStream &operator >>(TQDataStream &s, MultiplayerScores &score)
306{
307 s >> score._scores;
308 s >> score._nbGames;
309 return s;
310}
311
312} // namespace
KExtHighscore::ItemArray
Manage a bunch of Item which are saved under the same group in KHighscores config file.
Definition: kexthighscore_internal.h:129
KExtHighscore::Item::pretty
virtual TQString pretty(uint i, const TQVariant &value) const
Definition: kexthighscore_item.cpp:106
KExtHighscore::Item::Format
Format
Possible display format.
Definition: kexthighscore_item.h:55
KExtHighscore::Item::read
virtual TQVariant read(uint i, const TQVariant &value) const
Definition: kexthighscore_item.cpp:44
KExtHighscore::Item::Special
Special
Possible special value for display format.
Definition: kexthighscore_item.h:72
KExtHighscore::Item::Item
Item(const TQVariant &def=TQVariant::Invalid, const TQString &label=TQString(), int alignment=TQt::AlignRight)
Constructor.
Definition: kexthighscore_item.cpp:36
KExtHighscore::Item::setPrettyFormat
void setPrettyFormat(Format format)
Set the display format.
Definition: kexthighscore_item.cpp:49
KExtHighscore::Item::setPrettySpecial
void setPrettySpecial(Special special)
Set the special value for display.
Definition: kexthighscore_item.cpp:73
KExtHighscore::MultiplayerScores
This class is used to store and show scores for multiplayer games.
Definition: kexthighscore_item.h:268
KExtHighscore::MultiplayerScores::clear
void clear()
Clear all scores.
Definition: kexthighscore_item.cpp:202
KExtHighscore::MultiplayerScores::setPlayerCount
void setPlayerCount(uint nb)
Set the number of players and clear the scores.
Definition: kexthighscore_item.cpp:215
KExtHighscore::MultiplayerScores::setName
void setName(uint player, const TQString &name)
Set the name of player.
Definition: kexthighscore_item.cpp:222
KExtHighscore::MultiplayerScores::addScore
void addScore(uint player, const Score &score)
Add the score of player.
Definition: kexthighscore_item.cpp:227
KExtHighscore::MultiplayerScores::show
void show(TQWidget *parent)
Show scores.
Definition: kexthighscore_item.cpp:241
KExtHighscore::Score
This class contains data for a score.
Definition: kexthighscore_item.h:176
KExtHighscore::Score::type
ScoreType type() const
Definition: kexthighscore_item.h:185
KExtHighscore::Score::operator<
bool operator<(const Score &score)
Comparison operator.
Definition: kexthighscore_item.cpp:174
KExtHighscore::Score::data
const TQVariant & data(const TQString &name) const
Definition: kexthighscore_item.cpp:155
KExtHighscore::Score::score
uint score() const
Definition: kexthighscore_item.h:209
KExtHighscore::Score::isTheWorst
bool isTheWorst() const
Definition: kexthighscore_item.cpp:168
KExtHighscore::Score::setData
void setData(const TQString &name, const TQVariant &value)
Set the data associated with the named Item.
Definition: kexthighscore_item.cpp:161

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.4
This website is maintained by Timothy Pearson.