]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/scorefil.cpp
using scan-line polygon conversion for constructing wxregion
[wxWidgets.git] / demos / forty / scorefil.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: scorefil.cpp
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
5 // Modified by:
6 // Created: 21/07/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/textfile.h"
24 #include "wx/config.h"
25 #include "wx/fileconf.h"
26
27 #include "scorefil.h"
28
29 ScoreFile::ScoreFile(const wxString& appName)
30 {
31 m_config = new wxConfig(appName, wxT("wxWidgets"), appName, wxEmptyString,
32 wxCONFIG_USE_LOCAL_FILE); // only local
33 }
34
35 ScoreFile::~ScoreFile()
36 {
37 delete m_config;
38 }
39
40
41 void ScoreFile::GetPlayerList( wxArrayString &list )
42 {
43 m_config->SetPath(wxT("/Players"));
44 int length = m_config->GetNumberOfGroups();
45
46 if (length <= 0) return;
47
48 wxString player;
49 long index;
50 if (m_config->GetFirstGroup(player, index))
51 {
52 list.Add( player );
53 while (m_config->GetNextGroup(player, index))
54 {
55 list.Add( player );
56 }
57 }
58 }
59
60
61 // Calculate an encrypted check number to prevent tampering with
62 // score file
63 long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3)
64 {
65 long check = 0;
66 size_t i, max = name.length();
67
68 for(i = 0; i < max; ++i )
69 {
70 check = (check << 1) ^ (long)name[i];
71 check = ((check >> 23) ^ check) & 0xFFFFFF;
72 }
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;
79 return check;
80 }
81
82 wxString ScoreFile::GetPreviousPlayer() const
83 {
84 wxString result;
85 m_config->SetPath(wxT("/General"));
86 m_config->Read(wxT("LastPlayer"), &result);
87 return result;
88 }
89
90 void ScoreFile::ReadPlayersScore(
91 const wxString& player,
92 int& wins,
93 int& games,
94 int& score)
95 {
96 long check = 0;
97 long myWins = 0, myGames = 0, myScore = 0;
98
99 games = wins = score = 0;
100
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))
107 {
108 if (check != CalcCheck(player, myGames, myWins, myScore))
109 {
110 wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
111 wxOK | wxICON_EXCLAMATION);
112 }
113 else
114 {
115 games = myGames;
116 wins = myWins;
117 score = myScore;
118 }
119 }
120 WritePlayersScore(player, wins, games, score);
121 }
122
123
124 void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score)
125 {
126 if (!player.empty())
127 {
128 m_config->SetPath(wxT("/General"));
129 m_config->Write(wxT("LastPlayer"), wxString(player)); // Without wxString tmp, thinks it's bool in VC++
130
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));
137 }
138 }