]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
28 #include "wx/textfile.h"
29 #include "wx/config.h"
30 #include "wx/fileconf.h"
34 ScoreFile::ScoreFile(const wxString
& appName
)
36 m_config
= new wxConfig(appName
, wxT("wxWidgets"), appName
, wxEmptyString
,
37 wxCONFIG_USE_LOCAL_FILE
); // only local
40 ScoreFile::~ScoreFile()
46 void ScoreFile::GetPlayerList( wxArrayString
&list
)
48 m_config
->SetPath(wxT("/Players"));
49 int length
= m_config
->GetNumberOfGroups();
51 if (length
<= 0) return;
55 if (m_config
->GetFirstGroup(player
, index
))
58 while (m_config
->GetNextGroup(player
, index
))
66 // Calculate an encrypted check number to prevent tampering with
68 long ScoreFile::CalcCheck(const wxString
& name
, int p1
, int p2
, int p3
)
71 size_t i
, max
= name
.length();
73 for(i
= 0; i
< max
; ++i
)
75 check
= (check
<< 1) ^ (long)name
[i
];
76 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
78 check
= (check
<< 1) ^ (long)p1
;
79 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
80 check
= (check
<< 1) ^ (long)p2
;
81 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
82 check
= (check
<< 1) ^ (long)p3
;
83 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
87 wxString
ScoreFile::GetPreviousPlayer() const
90 m_config
->SetPath(wxT("/General"));
91 m_config
->Read(wxT("LastPlayer"), &result
);
95 void ScoreFile::ReadPlayersScore(
96 const wxString
& player
,
102 long myWins
= 0, myGames
= 0, myScore
= 0;
104 games
= wins
= score
= 0;
106 m_config
->SetPath(wxT("/Players"));
107 m_config
->SetPath(player
);
108 if (m_config
->Read(wxT("Score"), &myScore
, 0L) &&
109 m_config
->Read(wxT("Games"), &myGames
, 0L) &&
110 m_config
->Read(wxT("Wins"), &myWins
, 0L) &&
111 m_config
->Read(wxT("Check"), &check
, 0L))
113 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
115 wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
116 wxOK
| wxICON_EXCLAMATION
);
125 WritePlayersScore(player
, wins
, games
, score
);
129 void ScoreFile::WritePlayersScore(const wxString
& player
, int wins
, int games
, int score
)
133 m_config
->SetPath(wxT("/General"));
134 m_config
->Write(wxT("LastPlayer"), wxString(player
)); // Without wxString tmp, thinks it's bool in VC++
136 m_config
->SetPath(wxT("/Players"));
137 m_config
->SetPath(player
);
138 m_config
->Write(wxT("Score"), (long)score
);
139 m_config
->Write(wxT("Games"), (long)games
);
140 m_config
->Write(wxT("Wins"), (long)wins
);
141 m_config
->Write(wxT("Check"), CalcCheck(player
, games
, wins
, score
));