]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
7 // Copyright: (c) 1993-1998 Chris Breeze
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
22 #include "wx/textfile.h"
23 #include "wx/config.h"
24 #include "wx/fileconf.h"
28 ScoreFile::ScoreFile(const wxString
& appName
)
30 m_config
= new wxConfig(appName
, wxT("wxWidgets"), appName
, wxEmptyString
,
31 wxCONFIG_USE_LOCAL_FILE
); // only local
34 ScoreFile::~ScoreFile()
40 void ScoreFile::GetPlayerList( wxArrayString
&list
)
42 m_config
->SetPath(wxT("/Players"));
43 int length
= m_config
->GetNumberOfGroups();
45 if (length
<= 0) return;
49 if (m_config
->GetFirstGroup(player
, index
))
52 while (m_config
->GetNextGroup(player
, index
))
60 // Calculate an encrypted check number to prevent tampering with
62 long ScoreFile::CalcCheck(const wxString
& name
, int p1
, int p2
, int p3
)
65 size_t i
, max
= name
.length();
67 for(i
= 0; i
< max
; ++i
)
69 check
= (check
<< 1) ^ (long)name
[i
];
70 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
72 check
= (check
<< 1) ^ (long)p1
;
73 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
74 check
= (check
<< 1) ^ (long)p2
;
75 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
76 check
= (check
<< 1) ^ (long)p3
;
77 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
81 wxString
ScoreFile::GetPreviousPlayer() const
84 m_config
->SetPath(wxT("/General"));
85 m_config
->Read(wxT("LastPlayer"), &result
);
89 void ScoreFile::ReadPlayersScore(
90 const wxString
& player
,
96 long myWins
= 0, myGames
= 0, myScore
= 0;
98 games
= wins
= score
= 0;
100 m_config
->SetPath(wxT("/Players"));
101 m_config
->SetPath(player
);
102 if (m_config
->Read(wxT("Score"), &myScore
, 0L) &&
103 m_config
->Read(wxT("Games"), &myGames
, 0L) &&
104 m_config
->Read(wxT("Wins"), &myWins
, 0L) &&
105 m_config
->Read(wxT("Check"), &check
, 0L))
107 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
109 wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
110 wxOK
| wxICON_EXCLAMATION
);
119 WritePlayersScore(player
, wins
, games
, score
);
123 void ScoreFile::WritePlayersScore(const wxString
& player
, int wins
, int games
, int score
)
127 m_config
->SetPath(wxT("/General"));
128 m_config
->Write(wxT("LastPlayer"), wxString(player
)); // Without wxString tmp, thinks it's bool in VC++
130 m_config
->SetPath(wxT("/Players"));
131 m_config
->SetPath(player
);
132 m_config
->Write(wxT("Score"), (long)score
);
133 m_config
->Write(wxT("Games"), (long)games
);
134 m_config
->Write(wxT("Wins"), (long)wins
);
135 m_config
->Write(wxT("Check"), CalcCheck(player
, games
, wins
, score
));