1998-07-24 15:43:32 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: scorefil.cpp
|
|
|
|
// Purpose: Forty Thieves patience game
|
|
|
|
// Author: Chris Breeze
|
|
|
|
// Modified by:
|
|
|
|
// Created: 21/07/97
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1993-1998 Chris Breeze
|
2004-05-25 08:44:56 -04:00
|
|
|
// Licence: wxWindows licence
|
1998-07-24 15:43:32 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#ifdef __WXGTK__
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#include "wx/textfile.h"
|
|
|
|
#include "wx/config.h"
|
|
|
|
#include "wx/fileconf.h"
|
|
|
|
|
|
|
|
#include "scorefil.h"
|
|
|
|
|
2002-12-22 14:57:17 -05:00
|
|
|
ScoreFile::ScoreFile(const wxString& appName)
|
1998-07-24 15:43:32 -04:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
m_config = new wxConfig(appName, wxT("wxWidgets"), appName, wxEmptyString,
|
2002-12-22 14:57:17 -05:00
|
|
|
wxCONFIG_USE_LOCAL_FILE); // only local
|
1998-07-24 15:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ScoreFile::~ScoreFile()
|
|
|
|
{
|
2004-05-25 08:44:56 -04:00
|
|
|
delete m_config;
|
1998-07-24 15:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-05 12:33:18 -04:00
|
|
|
void ScoreFile::GetPlayerList( wxArrayString &list )
|
1998-07-24 15:43:32 -04:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
m_config->SetPath(wxT("/Players"));
|
2004-05-25 08:44:56 -04:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
1998-07-24 15:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate an encrypted check number to prevent tampering with
|
|
|
|
// score file
|
2002-12-22 14:57:17 -05:00
|
|
|
long ScoreFile::CalcCheck(const wxString& name, int p1, int p2, int p3)
|
1998-07-24 15:43:32 -04:00
|
|
|
{
|
|
|
|
long check = 0;
|
2002-12-22 14:57:17 -05:00
|
|
|
size_t i, max = name.length();
|
|
|
|
|
|
|
|
for(i = 0; i < max; ++i )
|
2004-05-25 08:44:56 -04:00
|
|
|
{
|
|
|
|
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;
|
1998-07-24 15:43:32 -04:00
|
|
|
return check;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString ScoreFile::GetPreviousPlayer() const
|
|
|
|
{
|
2004-05-25 08:44:56 -04:00
|
|
|
wxString result;
|
2009-07-23 16:30:22 -04:00
|
|
|
m_config->SetPath(wxT("/General"));
|
|
|
|
m_config->Read(wxT("LastPlayer"), &result);
|
2004-05-25 08:44:56 -04:00
|
|
|
return result;
|
1998-07-24 15:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScoreFile::ReadPlayersScore(
|
2004-05-25 08:44:56 -04:00
|
|
|
const wxString& player,
|
|
|
|
int& wins,
|
|
|
|
int& games,
|
|
|
|
int& score)
|
1998-07-24 15:43:32 -04:00
|
|
|
{
|
2004-05-25 08:44:56 -04:00
|
|
|
long check = 0;
|
|
|
|
long myWins = 0, myGames = 0, myScore = 0;
|
|
|
|
|
|
|
|
games = wins = score = 0;
|
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
m_config->SetPath(wxT("/Players"));
|
2004-05-25 08:44:56 -04:00
|
|
|
m_config->SetPath(player);
|
2009-07-23 16:30:22 -04:00
|
|
|
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))
|
2004-05-25 08:44:56 -04:00
|
|
|
{
|
|
|
|
if (check != CalcCheck(player, myGames, myWins, myScore))
|
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
wxMessageBox(wxT("Score file corrupted"), wxT("Warning"),
|
2002-12-22 14:57:17 -05:00
|
|
|
wxOK | wxICON_EXCLAMATION);
|
2004-05-25 08:44:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
games = myGames;
|
|
|
|
wins = myWins;
|
|
|
|
score = myScore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WritePlayersScore(player, wins, games, score);
|
1998-07-24 15:43:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-22 14:57:17 -05:00
|
|
|
void ScoreFile::WritePlayersScore(const wxString& player, int wins, int games, int score)
|
1998-07-24 15:43:32 -04:00
|
|
|
{
|
2007-06-27 16:49:19 -04:00
|
|
|
if (!player.empty())
|
2004-05-25 08:44:56 -04:00
|
|
|
{
|
2009-07-23 16:30:22 -04:00
|
|
|
m_config->SetPath(wxT("/General"));
|
|
|
|
m_config->Write(wxT("LastPlayer"), wxString(player)); // Without wxString tmp, thinks it's bool in VC++
|
2004-05-25 08:44:56 -04:00
|
|
|
|
2009-07-23 16:30:22 -04:00
|
|
|
m_config->SetPath(wxT("/Players"));
|
2004-05-25 08:44:56 -04:00
|
|
|
m_config->SetPath(player);
|
2009-07-23 16:30:22 -04:00
|
|
|
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));
|
2004-05-25 08:44:56 -04:00
|
|
|
}
|
1998-07-24 15:43:32 -04:00
|
|
|
}
|