]>
Commit | Line | Data |
---|---|---|
63cafd27 JS |
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 | |
2a21ac15 | 9 | // Licence: wxWindows licence |
63cafd27 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
63cafd27 JS |
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 | #ifdef __WXGTK__ | |
24 | #include <unistd.h> | |
25 | #include <sys/stat.h> | |
26 | #include <fcntl.h> | |
27 | #endif | |
28 | #include "wx/textfile.h" | |
29 | #include "wx/config.h" | |
30 | #include "wx/fileconf.h" | |
31 | ||
32 | #include "scorefil.h" | |
33 | ||
f37c24e0 | 34 | ScoreFile::ScoreFile(const wxString& appName) |
63cafd27 | 35 | { |
2a21ac15 | 36 | m_config = new wxConfig(appName, _T("wxWidgets"), appName, wxEmptyString, |
f37c24e0 | 37 | wxCONFIG_USE_LOCAL_FILE); // only local |
63cafd27 JS |
38 | } |
39 | ||
40 | ScoreFile::~ScoreFile() | |
41 | { | |
2a21ac15 | 42 | delete m_config; |
63cafd27 JS |
43 | } |
44 | ||
45 | ||
54ff4a70 | 46 | void ScoreFile::GetPlayerList( wxArrayString &list ) |
63cafd27 | 47 | { |
2a21ac15 DS |
48 | m_config->SetPath(_T("/Players")); |
49 | int length = m_config->GetNumberOfGroups(); | |
50 | ||
51 | if (length <= 0) return; | |
52 | ||
53 | wxString player; | |
54 | long index; | |
55 | if (m_config->GetFirstGroup(player, index)) | |
56 | { | |
57 | list.Add( player ); | |
58 | while (m_config->GetNextGroup(player, index)) | |
59 | { | |
60 | list.Add( player ); | |
61 | } | |
62 | } | |
63cafd27 JS |
63 | } |
64 | ||
65 | ||
66 | // Calculate an encrypted check number to prevent tampering with | |
67 | // score file | |
f37c24e0 | 68 | long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3) |
63cafd27 JS |
69 | { |
70 | long check = 0; | |
f37c24e0 MB |
71 | size_t i, max = name.length(); |
72 | ||
73 | for(i = 0; i < max; ++i ) | |
2a21ac15 DS |
74 | { |
75 | check = (check << 1) ^ (long)name[i]; | |
76 | check = ((check >> 23) ^ check) & 0xFFFFFF; | |
77 | } | |
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; | |
63cafd27 JS |
84 | return check; |
85 | } | |
86 | ||
87 | wxString ScoreFile::GetPreviousPlayer() const | |
88 | { | |
2a21ac15 DS |
89 | wxString result; |
90 | m_config->SetPath(_T("/General")); | |
91 | m_config->Read(_T("LastPlayer"), &result); | |
92 | return result; | |
63cafd27 JS |
93 | } |
94 | ||
95 | void ScoreFile::ReadPlayersScore( | |
2a21ac15 DS |
96 | const wxString& player, |
97 | int& wins, | |
98 | int& games, | |
99 | int& score) | |
63cafd27 | 100 | { |
2a21ac15 DS |
101 | long check = 0; |
102 | long myWins = 0, myGames = 0, myScore = 0; | |
103 | ||
104 | games = wins = score = 0; | |
105 | ||
106 | m_config->SetPath(_T("/Players")); | |
107 | m_config->SetPath(player); | |
108 | if (m_config->Read(_T("Score"), &myScore, 0L) && | |
109 | m_config->Read(_T("Games"), &myGames, 0L) && | |
110 | m_config->Read(_T("Wins"), &myWins, 0L) && | |
111 | m_config->Read(_T("Check"), &check, 0L)) | |
112 | { | |
113 | if (check != CalcCheck(player, myGames, myWins, myScore)) | |
114 | { | |
115 | wxMessageBox(_T("Score file corrupted"), _T("Warning"), | |
f37c24e0 | 116 | wxOK | wxICON_EXCLAMATION); |
2a21ac15 DS |
117 | } |
118 | else | |
119 | { | |
120 | games = myGames; | |
121 | wins = myWins; | |
122 | score = myScore; | |
123 | } | |
124 | } | |
125 | WritePlayersScore(player, wins, games, score); | |
63cafd27 JS |
126 | } |
127 | ||
128 | ||
f37c24e0 | 129 | void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score) |
63cafd27 | 130 | { |
bd0b594d | 131 | if (!player.empty()) |
2a21ac15 DS |
132 | { |
133 | m_config->SetPath(_T("/General")); | |
134 | m_config->Write(_T("LastPlayer"), wxString(player)); // Without wxString tmp, thinks it's bool in VC++ | |
135 | ||
136 | m_config->SetPath(_T("/Players")); | |
137 | m_config->SetPath(player); | |
138 | m_config->Write(_T("Score"), (long)score); | |
139 | m_config->Write(_T("Games"), (long)games); | |
140 | m_config->Write(_T("Wins"), (long)wins); | |
141 | m_config->Write(_T("Check"), CalcCheck(player, games, wins, score)); | |
142 | } | |
63cafd27 | 143 | } |