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