Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

CHighScores Class Reference

High score table. More...

#include <CHighScores.h>

List of all members.

Public Methods

TInt Number ()
 Number of entries in the table. More...

void AddScoreL (const TDesC &aName, TInt aScore)
 Add a new high score. More...

TBool GoodEnough (TInt aScore)
 Checks to see if a score qualifies for an entry. More...

TInt Score (TInt aEntry)
 Return the score for an entry. More...

const TDesC & Name (TInt aEntry)
 Return the name for an entry. More...

TInt LastAdded ()
 Last entry added. More...

 ~CHighScores ()
 Destructor. More...

void InitL ()
 Create a blank high score table. More...


Static Public Methods

CHighScores * NewL (const TDesC &aFileName)
 Leave safe construction. More...


Private Methods

void ConstructL (const TDesC &aFileName)
 2nd Phase construction. More...

void SaveL ()
 Save a high score table. More...

void LoadL ()
 Load a high score table. More...

void Reset ()
 Reset the high score arrays. More...


Private Attributes

HBufC * iFileName
 file to store high score table in on exit. More...

RArray< HBufC *> iNames
 Array of names. More...

RArray< TInt > iScores
 Array of scores. More...

TInt iLastAdded
 Last added, so that ui can highlite it. More...


Detailed Description

High score table.

Handles saving and restoring the high score table.

Definition at line 26 of file CHighScores.h.


Constructor & Destructor Documentation

CHighScores::~CHighScores  
 

Destructor.

Tries to save the high scores back to the file

Definition at line 133 of file CHighScores.cpp.

References iFileName, iNames, and SaveL().

00133                           {
00134   // if we can't save the high scores there isn't much we can
00135   // do about it, so just ignore the error.
00136   TRAPD(ignore,SaveL());
00137   delete iFileName;
00138   TInt count=iNames.Count();
00139   while(count--) {
00140     delete (iNames[count]);
00141   }
00142 }


Member Function Documentation

CHighScores * CHighScores::NewL const TDesC &    aFileName [static]
 

Leave safe construction.

Parameters:
aFileName  full path and filename of high score file

Definition at line 94 of file CHighScores.cpp.

References ConstructL().

Referenced by CGameState::ConstructL().

00094                                                      {
00095   CHighScores* self= new (ELeave) CHighScores;
00096   CleanupStack::PushL(self);
00097   self->ConstructL(aFileName);
00098   CleanupStack::Pop(self);
00099   return(self);
00100 }

TInt CHighScores::Number  
 

Number of entries in the table.

Definition at line 23 of file CHighScores.cpp.

References KHighScoreEntries.

Referenced by CGameAppView::DrawHighScores().

00023                          {
00024   return KHighScoreEntries;
00025 }

void CHighScores::AddScoreL const TDesC &    aName,
TInt    aScore
 

Add a new high score.

Finds the correct position in the table. Removes the last entry in the table. Then adds the new one.

Sets iLastAdded to the new entry so that the ui can highlite that line in the table.

Parameters:
aName  Players name
aScore  Players score

Definition at line 40 of file CHighScores.cpp.

References iLastAdded, iNames, iScores, and KHighScoreEntries.

Referenced by CScoreDialog::OkToExitL().

00040                                                            {
00041   for(TInt i=0;i<KHighScoreEntries;++i) {
00042     if(aScore > iScores[i]) {
00043       iNames.Remove(KHighScoreEntries-1);
00044       iScores.Remove(KHighScoreEntries-1);
00045 
00046       HBufC* name=aName.AllocL();
00047       TInt error=iNames.Insert(name,i);
00048       if(error!=KErrNone) {
00049         delete name;
00050         User::Leave(error);
00051       }
00052       User::LeaveIfError(iScores.Insert(aScore,i));
00053       iLastAdded=i;
00054       return;
00055     }
00056   }
00057 }

TBool CHighScores::GoodEnough TInt    aScore
 

Checks to see if a score qualifies for an entry.

Resets the iLastAdded as a new game has been played

Definition at line 64 of file CHighScores.cpp.

References iLastAdded, iScores, and KHighScoreEntries.

Referenced by CGameAppUi::HandleCommandL().

00064                                          {
00065   iLastAdded=-1;
00066   if(aScore > iScores[KHighScoreEntries-1]) 
00067     return ETrue;
00068   else
00069     return EFalse;
00070 }

TInt CHighScores::Score TInt    aEntry
 

Return the score for an entry.

Parameters:
aEntry  entry to return aEntry should be < number returned by Number() and >= 0

Definition at line 77 of file CHighScores.cpp.

References iScores.

Referenced by CGameAppView::DrawHighScores().

00077                                    {
00078   return(iScores[aEntry]);
00079 }

const TDesC & CHighScores::Name TInt    aEntry
 

Return the name for an entry.

Parameters:
aEntry  entry to return aEntry should be < number returned by Number() and >= 0

Definition at line 86 of file CHighScores.cpp.

References iNames.

Referenced by CGameAppView::DrawHighScores().

00086                                           {
00087   return(*(iNames[aEntry]));
00088 }

TInt CHighScores::LastAdded  
 

Last entry added.

Returns:
last entry added, or -1 if no entry has been added recently

Definition at line 107 of file CHighScores.cpp.

References iLastAdded.

Referenced by CGameAppView::DrawHighScores().

00107                             {
00108   return iLastAdded;
00109 }

void CHighScores::InitL  
 

Create a blank high score table.

Definition at line 159 of file CHighScores.cpp.

References iNames, iScores, KHighScoreEntries, and Reset().

Referenced by ConstructL().

00159                         {
00160   Reset();
00161   _LIT(KEmpty,"No One");
00162   TInt count=KHighScoreEntries;
00163   while(count--) {
00164     HBufC* name=KEmpty().AllocL();
00165     TInt error=iNames.Append(name);
00166     if(error!=KErrNone) {
00167       delete name;
00168       User::Leave(error);
00169     }
00170     User::LeaveIfError(iScores.Append(0));
00171   }
00172 }

void CHighScores::ConstructL const TDesC &    aFileName [private]
 

2nd Phase construction.

Load the high score table from the file, if that fails create a blank table.

Definition at line 118 of file CHighScores.cpp.

References iFileName, iLastAdded, InitL(), and LoadL().

Referenced by NewL().

00118                                                    {
00119   iLastAdded=-1;
00120   iFileName=aFileName.AllocL();
00121   // try and load high scores, if that fails,
00122   // create empty high score sheet.
00123   TRAPD(error,LoadL());
00124   if(error!=KErrNone) {
00125     InitL();
00126   }
00127 }

void CHighScores::SaveL   [private]
 

Save a high score table.

Definition at line 177 of file CHighScores.cpp.

References iFileName, iNames, iScores, and KHighScoreEntries.

Referenced by ~CHighScores().

00177                         {  
00178   RFs fs;
00179   User::LeaveIfError(fs.Connect());
00180   CleanupClosePushL(fs);
00181 
00182   RFileWriteStream file;
00183   User::LeaveIfError(file.Replace(fs,*iFileName,EFileWrite));
00184   CleanupClosePushL(file);
00185 
00186   for(TInt i=0;i<KHighScoreEntries;++i) {
00187     file << *(iNames[i]);
00188     file.WriteInt32L(iScores[i]);
00189   }
00190 
00191   CleanupStack::PopAndDestroy(2,&fs);      
00192 }

void CHighScores::LoadL   [private]
 

Load a high score table.

Definition at line 197 of file CHighScores.cpp.

References iFileName, iNames, iScores, and KHighScoreEntries.

Referenced by ConstructL().

00197                         {
00198   RFs fs;
00199   User::LeaveIfError(fs.Connect());
00200   CleanupClosePushL(fs);
00201 
00202   RFileReadStream file;
00203   User::LeaveIfError(file.Open(fs,*iFileName,EFileRead));
00204   CleanupClosePushL(file);
00205 
00206   for(TInt i=0;i<KHighScoreEntries;++i) {
00207     HBufC* name=HBufC::NewL(file,KMaxTInt);
00208     TInt error=iNames.Append(name);
00209     if(error!=KErrNone) {
00210       delete name;
00211       User::Leave(error);
00212     }
00213     TInt score=file.ReadInt32L();
00214     User::LeaveIfError(iScores.Append(score));
00215   }
00216 
00217   CleanupStack::PopAndDestroy(2,&fs);
00218 }

void CHighScores::Reset   [private]
 

Reset the high score arrays.

Definition at line 147 of file CHighScores.cpp.

References iNames, and iScores.

Referenced by InitL().

00147                         {
00148   TInt count=iNames.Count();
00149   while(count--) {
00150     delete (iNames[count]);
00151   }
00152   iNames.Reset();
00153   iScores.Reset();
00154 }


Member Data Documentation

HBufC* CHighScores::iFileName [private]
 

file to store high score table in on exit.

Definition at line 48 of file CHighScores.h.

Referenced by ConstructL(), LoadL(), SaveL(), and ~CHighScores().

RArray<HBufC*> CHighScores::iNames [private]
 

Array of names.

Definition at line 49 of file CHighScores.h.

Referenced by AddScoreL(), InitL(), LoadL(), Name(), Reset(), SaveL(), and ~CHighScores().

RArray<TInt> CHighScores::iScores [private]
 

Array of scores.

Definition at line 50 of file CHighScores.h.

Referenced by AddScoreL(), GoodEnough(), InitL(), LoadL(), Reset(), SaveL(), and Score().

TInt CHighScores::iLastAdded [private]
 

Last added, so that ui can highlite it.

Definition at line 51 of file CHighScores.h.

Referenced by AddScoreL(), ConstructL(), GoodEnough(), and LastAdded().


The documentation for this class was generated from the following files:
Documentation for Game (Beta) version 1.44.