[Kde-games-devel] KAtomic's highscores

Nicolas Hadacek kde-games-devel@mail.kde.org
Sun, 9 Feb 2003 23:04:20 +0100


--------------Boundary-00=_8NA2ME55EUEL3RCPNMLZ
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 8bit

Hi,

I've ported katomic to the new highscore framework.
Do you think it is ok to commit ?

Have a nice day,
Nicolas
--------------Boundary-00=_8NA2ME55EUEL3RCPNMLZ
Content-Type: text/x-diff;
  charset="us-ascii";
  name="katomic.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="katomic.diff"

? highscores.cpp
? highscores.h
Index: Makefile.am
===================================================================
RCS file: /home/kde/kdegames/katomic/Makefile.am,v
retrieving revision 1.34
diff -u -2 -d -p -b -r1.34 Makefile.am
--- Makefile.am	5 Feb 2003 21:41:27 -0000	1.34
+++ Makefile.am	9 Feb 2003 14:31:27 -0000
@@ -7,5 +7,5 @@ METASOURCES = AUTO
 bin_PROGRAMS = katomic
 katomic_SOURCES = feld.cpp molek.cpp main.cpp toplevel.cpp configbox.cpp\
-	gamewidget.cpp
+	gamewidget.cpp highscores.cpp
 
 pics_DATA = abilder.png molek.png
Index: feld.cpp
===================================================================
RCS file: /home/kde/kdegames/katomic/feld.cpp,v
retrieving revision 1.50
diff -u -2 -d -p -b -r1.50 feld.cpp
--- feld.cpp	4 Feb 2003 08:29:32 -0000	1.50
+++ feld.cpp	9 Feb 2003 14:31:28 -0000
@@ -253,5 +253,5 @@ void Feld::startAnimation (Direction d)
 	  return;
 	break;
-  default:
+  case None:
 	break;
   }
@@ -262,4 +262,6 @@ void Feld::startAnimation (Direction d)
   int x = 0, y = 0;
 
+  if ( moves==0 )
+      emit started();
   moves++;
   emit sendMoves(moves);
@@ -295,5 +297,5 @@ void Feld::startAnimation (Direction d)
       }
     break;
-  default:
+  case None:
     return;
   }
Index: feld.h
===================================================================
RCS file: /home/kde/kdegames/katomic/feld.h,v
retrieving revision 1.22
diff -u -2 -d -p -b -r1.22 feld.h
--- feld.h	3 Apr 2002 12:34:38 -0000	1.22
+++ feld.h	9 Feb 2003 14:31:29 -0000
@@ -42,4 +42,5 @@ public:
 
 signals:
+    void started();
     void gameOver(int moves);
     void sendMoves(int moves);
@@ -73,5 +74,5 @@ private:
 
     // number of movements
-    int moves;
+    uint moves;
 
     Direction dir;
Index: gamewidget.cpp
===================================================================
RCS file: /home/kde/kdegames/katomic/gamewidget.cpp,v
retrieving revision 1.16
diff -u -2 -d -p -b -r1.16 gamewidget.cpp
--- gamewidget.cpp	3 Apr 2002 12:34:38 -0000	1.16
+++ gamewidget.cpp	9 Feb 2003 14:31:29 -0000
@@ -23,4 +23,5 @@
 #include "feld.h"
 #include "molek.h"
+#include "highscores.h"
 
 #include <qscrollbar.h>
@@ -30,5 +31,4 @@
 #include <qlabel.h>
 
-#include <kscoredialog.h>
 #include <kmessagebox.h>
 #include <klocale.h>
@@ -36,4 +36,6 @@
 #include <kglobal.h>
 #include <ksimpleconfig.h>
+#include <kgamelcd.h>
+#include <kdebug.h>
 
 Options settings;
@@ -83,13 +85,15 @@ void GameWidget::getButton (int button)
 }
 
-void GameWidget::gameOver(int moves) {
-    KMessageBox::information(this, i18n("You solved level %1 with %2 moves!").arg(level).arg(moves), i18n("Congratulations"));
+void GameWidget::gameOver(int moves)
+{
+    clock->stop();
+    KMessageBox::information(this, i18n("You solved level %1 with %2 moves!")
+                             .arg(level).arg(moves), i18n("Congratulations"));
 
-    KScoreDialog::FieldInfo scoreInfo;
+    KExtHighscore::Score score(KExtHighscore::Won);
+    score.setScore(moves);
+    score.setData("time", 3600 - clock->seconds());
+    KExtHighscore::submitScore(score, this);
 
-    if (high->addScore(moves, scoreInfo, true, true))
-    {
-       high->exec();
-    }
     updateLevel(level+1);
 }
@@ -97,31 +101,30 @@ void GameWidget::gameOver(int moves) {
 void GameWidget::getMoves(int moves)
 {
-    current.setNum(moves);
-    ys->setText(current);
+    ys->setText(QString::number(moves));
 }
 
-void GameWidget::updateLevel (int l)
+void GameWidget::updateLevel(int l)
 {
-    level=l;
     QString levelFile = locate("appdata", QString("levels/level_%1").arg(l));
     if (levelFile.isNull()) {
+        if ( l==1 ) kdFatal() << "level 1 is missing" << endl;
 	return updateLevel(1);
     }
 
+    level = l;
+    KExtHighscore::setGameType(l-1);
+
     KSimpleConfig cfg(levelFile, true);
     cfg.setGroup("Level");
     feld->load(cfg);
+    feld->repaint();
 
-    QString group = QString("High Scores Level %1").arg(level);
-    high->setConfigGroup(group);
-    QString caption = i18n("Level %1 High Scores").arg(level);
-    high->setCaption(caption);
-    highest.setNum(high->highScore());
-
-    hs->setText(highest);
+    KExtHighscore::Score first = KExtHighscore::firstScore();
+    QString str = (first.isTheWorst() ? "--" : QString::number(first.score()));
+    hs->setText(str);
     ys->setText("0");
     scrl->setValue(level);
 
-    feld->repaint();
+    clock->reset();
 }
 
@@ -131,10 +134,14 @@ void GameWidget::restartLevel()
 }
 
+uint GameWidget::nbLevels()
+{
+    return KGlobal::dirs()->findAllResources("appdata", "levels/level_*",
+                                             false, true).count();
+}
+
 GameWidget::GameWidget ( QWidget *parent, const char* name )
     : QWidget( parent, name )
 {
     level = 1;
-    nlevels = KGlobal::dirs()->findAllResources("appdata", "levels/level_*",
-				false, true).count();
 
     QHBoxLayout *top = new QHBoxLayout(this, 10);
@@ -151,5 +158,5 @@ GameWidget::GameWidget ( QWidget *parent
 
     // scrollbar
-    scrl = new QScrollBar(1, nlevels, 1, 
+    scrl = new QScrollBar(1, nbLevels(), 1,
 			5, 1, QScrollBar::Horizontal, vb, "scrl" );
     // scrl->setGeometry( MPOSX, 50, 160, 16 );
@@ -161,5 +168,4 @@ GameWidget::GameWidget ( QWidget *parent
 
     connect (feld, SIGNAL (gameOver(int)), SLOT(gameOver(int)));
-    connect (feld, SIGNAL (sendMoves(int)), SLOT(getMoves(int)));
 
     // the score group
@@ -172,5 +178,5 @@ GameWidget::GameWidget ( QWidget *parent
     slay->addWidget(new QLabel(i18n("Highest score:"), bg));
 
-    hs = new QLabel (highest, bg);
+    hs = new QLabel(bg);
     hs->setAlignment(Qt::AlignRight);
     hs->setFont(QFont("Helvetica", 18, QFont::Bold));
@@ -181,10 +187,12 @@ GameWidget::GameWidget ( QWidget *parent
     slay->addWidget(new QLabel(i18n("Your score so far:"), bg));
 
-    ys = new QLabel (current, bg);
+    ys = new QLabel(bg);
+    connect(feld, SIGNAL(sendMoves(int)), ys, SLOT(setNum(int)));
     ys->setAlignment(Qt::AlignRight);
     ys->setFont(QFont("Helvetica", 18, QFont::Bold));
     slay->addWidget(ys);
 
-    high = new KScoreDialog(KScoreDialog::Name | KScoreDialog::Score, this);
+    clock = new KGameLCDClock; // not shown at the moment
+    connect(feld, SIGNAL(started()), clock, SLOT(start()));
 
     updateLevel(1);
@@ -196,7 +204,4 @@ GameWidget::GameWidget ( QWidget *parent
 	settings.anim_speed = 1;
 
-    config->setGroup("level1");
-    highest = config->readEntry("Moves0", "100");
-
     config->setGroup("Colors");
 
@@ -208,9 +213,10 @@ GameWidget::GameWidget ( QWidget *parent
 GameWidget::~GameWidget()
 {
+    delete clock;
 }
 
 void GameWidget::showHighscores ()
 {
-    high->exec();
+    KExtHighscore::show(this);
 }
 
Index: gamewidget.h
===================================================================
RCS file: /home/kde/kdegames/katomic/gamewidget.h,v
retrieving revision 1.7
diff -u -2 -d -p -b -r1.7 gamewidget.h
--- gamewidget.h	3 Apr 2002 12:34:38 -0000	1.7
+++ gamewidget.h	9 Feb 2003 14:31:29 -0000
@@ -7,5 +7,5 @@ class Molek;
 class QScrollBar;
 class QLabel;
-class KScoreDialog;
+class KGameLCDClock;
 
 #include <qwidget.h>
@@ -18,7 +18,8 @@ class GameWidget : public QWidget
 
     GameWidget ( QWidget *parent, const char *name=0 );
-
     ~GameWidget();
 
+    static uint nbLevels();
+
  public slots:
     // bringt level auf neuesten stand
@@ -59,8 +60,6 @@ class GameWidget : public QWidget
     // important labels : highest and current scores
     QLabel *hs, *ys;
-    QString highest, current;
-    KScoreDialog *high;
     
-    int nlevels;
+    KGameLCDClock *clock;
 };
 
Index: main.cpp
===================================================================
RCS file: /home/kde/kdegames/katomic/main.cpp,v
retrieving revision 1.11
diff -u -2 -d -p -b -r1.11 main.cpp
--- main.cpp	4 Feb 2003 22:30:51 -0000	1.11
+++ main.cpp	9 Feb 2003 14:31:30 -0000
@@ -27,4 +27,6 @@
 #include <kaboutdata.h>
 
+#include "highscores.h"
+
 
 static const char *description =
@@ -55,4 +57,5 @@ int main(int argc, char **argv)
   KApplication a;
   KGlobal::locale()->insertCatalogue("libkdegames");
+  KExtHighscore::ExtManager manager;
 
   if ( a.isRestored() )
Index: settings.h
===================================================================
RCS file: /home/kde/kdegames/katomic/settings.h,v
retrieving revision 1.2
diff -u -2 -d -p -b -r1.2 settings.h
--- settings.h	8 Jul 1999 01:08:46 -0000	1.2
+++ settings.h	9 Feb 2003 14:31:30 -0000
@@ -22,6 +22,4 @@
 #define SETTINGS_H
 
-#include <qstring.h>
-
 #define MAX_SPEED 10
 

--------------Boundary-00=_8NA2ME55EUEL3RCPNMLZ
Content-Type: text/x-c++src;
  charset="us-ascii";
  name="highscores.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="highscores.cpp"

#include "highscores.h"

#include <klocale.h>
#include <kconfig.h>
#include <kapplication.h>

#include "gamewidget.h"


namespace KExtHighscore
{

ExtManager::ExtManager()
    : Manager(GameWidget::nbLevels())
{
    setShowMode(NeverShow);

    Item *item = createItem(ScoreDefault);
    item->setLabel(i18n("No. of Moves"));
    setScoreItem(100000, item); // 100000 is an arbitrary limit for no of moves
    addScoreItem("time", createItem(ElapsedTime));
}

bool ExtManager::isStrictlyLess(const Score &s1, const Score &s2) const
{
    if ( s1.score()==s2.score() )
        return s1.data("time").toUInt()<s2.data("time").toUInt();
    return s1.score()>s2.score();
}

QString ExtManager::gameTypeLabel(uint i, LabelType type) const
{
    switch (type) {
    case Icon:
    case WW:
    case Standard: return QString("level %1").arg(i+1);
    case I18N:     return i18n("Level %1").arg(i+1);
    }
    return QString::null;
}

void ExtManager::convertLegacy(uint gameType)
{
    KConfigGroupSaver cg(kapp->config(),
                         QString("High Scores Level %1").arg(gameType+1));
    for (uint i=0; i<10; i++) {
        QString key = QString("Pos%1").arg(i+1);
        QString score = cg.config()->readEntry(key + "Score", QString::null);
        bool ok;
        uint moves = score.toUInt(&ok);
        if ( !ok || moves==0 ) continue;
        Score s(Won);
        s.setScore(moves);
        QString name = cg.config()->readEntry(key + "Name", QString::null);
        if ( name.isEmpty() || name=="-" ) name = i18n("anonymous");
        s.setData("name", name);
        submitLegacyScore(s);
    }
}

};

--------------Boundary-00=_8NA2ME55EUEL3RCPNMLZ
Content-Type: text/x-chdr;
  charset="us-ascii";
  name="highscores.h"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="highscores.h"

#ifndef __HIGHSCORES_H
#define __HIGHSCORES_H

#include <kexthighscore.h>


namespace KExtHighscore
{

class ExtManager : public Manager
{
 public:
    ExtManager();

 protected:
    bool isStrictlyLess(const KExtHighscore::Score &,
                        const KExtHighscore::Score &) const;
    QString gameTypeLabel(uint gameType, LabelType type) const;
    void convertLegacy(uint gameType);
};

};

#endif

--------------Boundary-00=_8NA2ME55EUEL3RCPNMLZ--