]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/scorefil.cpp
Change version to 3.0.0.
[wxWidgets.git] / demos / forty / scorefil.cpp
CommitLineData
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
63cafd27 7// Copyright: (c) 1993-1998 Chris Breeze
2a21ac15 8// Licence: wxWindows licence
63cafd27
JS
9/////////////////////////////////////////////////////////////////////////////
10
63cafd27
JS
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
63cafd27
JS
22#include "wx/textfile.h"
23#include "wx/config.h"
24#include "wx/fileconf.h"
25
26#include "scorefil.h"
27
f37c24e0 28ScoreFile::ScoreFile(const wxString& appName)
63cafd27 29{
9a83f860 30 m_config = new wxConfig(appName, wxT("wxWidgets"), appName, wxEmptyString,
f37c24e0 31 wxCONFIG_USE_LOCAL_FILE); // only local
63cafd27
JS
32}
33
34ScoreFile::~ScoreFile()
35{
2a21ac15 36 delete m_config;
63cafd27
JS
37}
38
39
54ff4a70 40void ScoreFile::GetPlayerList( wxArrayString &list )
63cafd27 41{
9a83f860 42 m_config->SetPath(wxT("/Players"));
2a21ac15
DS
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 }
63cafd27
JS
57}
58
59
60// Calculate an encrypted check number to prevent tampering with
61// score file
f37c24e0 62long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3)
63cafd27
JS
63{
64 long check = 0;
f37c24e0
MB
65 size_t i, max = name.length();
66
67 for(i = 0; i < max; ++i )
2a21ac15
DS
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;
63cafd27
JS
78 return check;
79}
80
81wxString ScoreFile::GetPreviousPlayer() const
82{
2a21ac15 83 wxString result;
9a83f860
VZ
84 m_config->SetPath(wxT("/General"));
85 m_config->Read(wxT("LastPlayer"), &result);
2a21ac15 86 return result;
63cafd27
JS
87}
88
89void ScoreFile::ReadPlayersScore(
2a21ac15
DS
90 const wxString& player,
91 int& wins,
92 int& games,
93 int& score)
63cafd27 94{
2a21ac15
DS
95 long check = 0;
96 long myWins = 0, myGames = 0, myScore = 0;
97
98 games = wins = score = 0;
99
9a83f860 100 m_config->SetPath(wxT("/Players"));
2a21ac15 101 m_config->SetPath(player);
9a83f860
VZ
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))
2a21ac15
DS
106 {
107 if (check != CalcCheck(player, myGames, myWins, myScore))
108 {
9a83f860 109 wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
f37c24e0 110 wxOK | wxICON_EXCLAMATION);
2a21ac15
DS
111 }
112 else
113 {
114 games = myGames;
115 wins = myWins;
116 score = myScore;
117 }
118 }
119 WritePlayersScore(player, wins, games, score);
63cafd27
JS
120}
121
122
f37c24e0 123void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score)
63cafd27 124{
bd0b594d 125 if (!player.empty())
2a21ac15 126 {
9a83f860
VZ
127 m_config->SetPath(wxT("/General"));
128 m_config->Write(wxT("LastPlayer"), wxString(player)); // Without wxString tmp, thinks it's bool in VC++
2a21ac15 129
9a83f860 130 m_config->SetPath(wxT("/Players"));
2a21ac15 131 m_config->SetPath(player);
9a83f860
VZ
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));
2a21ac15 136 }
63cafd27 137}