]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
9d60a32cd686c77fa7fa649e9cc7830ad6668752
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"
23 #include "wx/textfile.h"
24 #include "wx/config.h"
25 #include "wx/fileconf.h"
29 ScoreFile::ScoreFile(const wxString
& appName
)
31 m_config
= new wxConfig(appName
, wxT("wxWidgets"), appName
, wxEmptyString
,
32 wxCONFIG_USE_LOCAL_FILE
); // only local
35 ScoreFile::~ScoreFile()
41 void ScoreFile::GetPlayerList( wxArrayString
&list
)
43 m_config
->SetPath(wxT("/Players"));
44 int length
= m_config
->GetNumberOfGroups();
46 if (length
<= 0) return;
50 if (m_config
->GetFirstGroup(player
, index
))
53 while (m_config
->GetNextGroup(player
, index
))
61 // Calculate an encrypted check number to prevent tampering with
63 long ScoreFile::CalcCheck(const wxString
& name
, int p1
, int p2
, int p3
)
66 size_t i
, max
= name
.length();
68 for(i
= 0; i
< max
; ++i
)
70 check
= (check
<< 1) ^ (long)name
[i
];
71 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
73 check
= (check
<< 1) ^ (long)p1
;
74 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
75 check
= (check
<< 1) ^ (long)p2
;
76 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
77 check
= (check
<< 1) ^ (long)p3
;
78 check
= ((check
>> 23) ^ check
) & 0xFFFFFF;
82 wxString
ScoreFile::GetPreviousPlayer() const
85 m_config
->SetPath(wxT("/General"));
86 m_config
->Read(wxT("LastPlayer"), &result
);
90 void ScoreFile::ReadPlayersScore(
91 const wxString
& player
,
97 long myWins
= 0, myGames
= 0, myScore
= 0;
99 games
= wins
= score
= 0;
101 m_config
->SetPath(wxT("/Players"));
102 m_config
->SetPath(player
);
103 if (m_config
->Read(wxT("Score"), &myScore
, 0L) &&
104 m_config
->Read(wxT("Games"), &myGames
, 0L) &&
105 m_config
->Read(wxT("Wins"), &myWins
, 0L) &&
106 m_config
->Read(wxT("Check"), &check
, 0L))
108 if (check
!= CalcCheck(player
, myGames
, myWins
, myScore
))
110 wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
111 wxOK
| wxICON_EXCLAMATION
);
120 WritePlayersScore(player
, wins
, games
, score
);
124 void ScoreFile::WritePlayersScore(const wxString
& player
, int wins
, int games
, int score
)
128 m_config
->SetPath(wxT("/General"));
129 m_config
->Write(wxT("LastPlayer"), wxString(player
)); // Without wxString tmp, thinks it's bool in VC++
131 m_config
->SetPath(wxT("/Players"));
132 m_config
->SetPath(player
);
133 m_config
->Write(wxT("Score"), (long)score
);
134 m_config
->Write(wxT("Games"), (long)games
);
135 m_config
->Write(wxT("Wins"), (long)wins
);
136 m_config
->Write(wxT("Check"), CalcCheck(player
, games
, wins
, score
));