1/*
2 Copyright (C) 2007-2009 Dmitry Suzdalev (dimsuz@gmail.com)
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17*/
18#include "highscores.h"
19
20#include <kuser.h>
21#include <kstandarddirs.h>
22#include <kconfiggroup.h>
23
24KAtomicHighscores::KAtomicHighscores()
25{
26 KUser user;
27 m_playerName = user.property(KUser::FullName).toString().isEmpty() ? user.loginName() : user.property(KUser::FullName).toString();
28
29 m_hsFile = KSharedConfig::openConfig( KStandardDirs::locateLocal("appdata", "highscores"), KConfig::SimpleConfig );
30}
31
32bool KAtomicHighscores::addScore( int numMoves, const QString& levelSetName, int level )
33{
34 KConfigGroup userHsGroup( m_hsFile, "Highscores_"+m_playerName );
35 KConfigGroup levelSetGroup( &userHsGroup, levelSetName );
36
37 QString keyStr = "Level"+QString::number(level);
38 int curHighScore = levelSetGroup.readEntry( keyStr, -1 );
39 if( numMoves < curHighScore || curHighScore == -1 ) // new highscore!
40 {
41 levelSetGroup.writeEntry( keyStr, numMoves );
42 return true;
43 }
44 return false;
45}
46
47int KAtomicHighscores::levelHighscore( const QString& levelSetName, int level ) const
48{
49 KConfigGroup userHsGroup( m_hsFile, "Highscores_"+m_playerName );
50 KConfigGroup levelSetGroup( &userHsGroup, levelSetName );
51
52 QString keyStr = "Level"+QString::number(level);
53 return levelSetGroup.readEntry( keyStr, 0 );
54}
55