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