Files
itgmania212121/stepmania/src/Bookkeeper.cpp
T

270 lines
6.4 KiB
C++
Raw Normal View History

2003-10-13 08:33:39 +00:00
#include "global.h"
#include "Bookkeeper.h"
#include "RageUtil.h"
#include "PrefsManager.h"
#include "RageLog.h"
#include "IniFile.h"
#include "GameConstantsAndTypes.h"
#include "SongManager.h"
#include "RageFile.h"
2005-05-05 08:18:52 +00:00
#include "XmlFile.h"
2004-04-05 05:22:32 +00:00
#include <ctime>
2003-10-13 08:33:39 +00:00
Bookkeeper* BOOKKEEPER = NULL; // global and accessable from anywhere in our program
2005-05-05 08:18:52 +00:00
static const CString COINS_DAT = "Data/Coins.xml";
2003-10-13 08:33:39 +00:00
Bookkeeper::Bookkeeper()
{
ClearAll();
2003-10-13 08:33:39 +00:00
ReadFromDisk();
}
Bookkeeper::~Bookkeeper()
{
WriteToDisk();
}
#define WARN_AND_RETURN { LOG->Warn("Error parsing at %s:%d",__FILE__,__LINE__); return; }
void Bookkeeper::ClearAll()
{
2005-05-05 08:18:52 +00:00
m_mapCoinsForHour.clear();
}
2005-05-05 08:18:52 +00:00
bool Bookkeeper::Date::operator<( const Date &rhs ) const
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
if( m_iYear != rhs.m_iYear )
return m_iYear < rhs.m_iYear;
if( m_iDayOfYear != rhs.m_iDayOfYear )
return m_iDayOfYear < rhs.m_iDayOfYear;
return m_iHour < rhs.m_iHour;
}
void Bookkeeper::Date::Set( time_t t )
{
tm ltime;
localtime_r( &t, &ltime );
Set( ltime );
}
void Bookkeeper::Date::Set( tm pTime )
{
m_iHour = pTime.tm_hour;
m_iDayOfYear = pTime.tm_yday;
m_iYear = pTime.tm_year + 1900;
}
void Bookkeeper::LoadFromNode( const XNode *pNode )
{
if( pNode->m_sName != "Bookkeeping" )
{
2005-05-05 08:18:52 +00:00
LOG->Warn( "Error loading bookkeeping: unexpected \"%s\"", pNode->m_sName.c_str() );
return;
}
2005-05-05 08:18:52 +00:00
const XNode *pData = pNode->GetChild( "Data" );
if( pData == NULL )
{
2005-05-06 14:41:46 +00:00
LOG->Warn( "Error loading bookkeeping: Data node missing" );
2005-05-05 08:18:52 +00:00
return;
}
2005-05-05 08:18:52 +00:00
FOREACH_CONST_Child( pData, day )
{
2005-05-05 08:18:52 +00:00
Date d;
if( !day->GetAttrValue( "Hour", d.m_iHour ) ||
!day->GetAttrValue( "Day", d.m_iDayOfYear ) ||
!day->GetAttrValue( "Year", d.m_iYear ) )
{
2005-05-05 08:18:52 +00:00
LOG->Warn( "Incomplete date field" );
continue;
}
2005-05-05 08:18:52 +00:00
int iCoins;
day->GetValue( iCoins );
m_mapCoinsForHour[d] = iCoins;
}
2003-10-13 08:33:39 +00:00
}
2005-05-05 08:18:52 +00:00
XNode* Bookkeeper::CreateNode() const
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
XNode *xml = new XNode;
xml->m_sName = "Bookkeeping";
{
2005-05-05 08:18:52 +00:00
XNode* pData = xml->AppendChild("Data");
for( map<Date,int>::const_iterator it = m_mapCoinsForHour.begin(); it != m_mapCoinsForHour.end(); ++it )
{
XNode *pDay = pData->AppendChild( "Coins" );
const Date &d = it->first;
pDay->AppendAttr( "Hour", d.m_iHour );
pDay->AppendAttr( "Day", d.m_iDayOfYear );
pDay->AppendAttr( "Year", d.m_iYear );
int iCoins = it->second;
pDay->SetValue( iCoins );
}
}
2005-05-05 08:18:52 +00:00
return xml;
2003-10-13 08:33:39 +00:00
}
2005-05-05 08:18:52 +00:00
void Bookkeeper::ReadFromDisk()
2003-10-13 08:33:39 +00:00
{
2005-05-05 23:30:48 +00:00
if( !IsAFile(COINS_DAT) )
return;
2005-05-05 08:18:52 +00:00
XNode xml;
if( !xml.LoadFromFile(COINS_DAT) )
2003-10-13 08:33:39 +00:00
return;
2005-05-05 08:18:52 +00:00
LoadFromNode( &xml );
}
2003-10-13 08:33:39 +00:00
2005-05-05 08:18:52 +00:00
void Bookkeeper::WriteToDisk()
{
// Write data. Use SLOW_FLUSH, to help ensure that we don't lose coin data.
RageFile f;
if( !f.Open(COINS_DAT, RageFile::WRITE|RageFile::SLOW_FLUSH) )
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
LOG->Warn( "Couldn't open file \"%s\" for writing: %s", COINS_DAT.c_str(), f.GetError().c_str() );
return;
2003-10-13 08:33:39 +00:00
}
2003-12-10 10:30:53 +00:00
2005-05-05 08:18:52 +00:00
DISP_OPT opt;
XNode *xml = CreateNode();
xml->SaveToFile( f, &opt );
delete xml;
2003-10-13 08:33:39 +00:00
}
void Bookkeeper::CoinInserted()
{
2005-05-05 08:18:52 +00:00
Date d;
d.Set( time(NULL) );
2003-10-13 08:33:39 +00:00
2005-05-05 08:18:52 +00:00
++m_mapCoinsForHour[d];
2003-10-13 08:33:39 +00:00
}
2005-05-05 08:18:52 +00:00
/* Return the number of coins between [beginning,ending). */
int Bookkeeper::GetNumCoinsInRange( map<Date,int>::const_iterator begin, map<Date,int>::const_iterator end ) const
2003-10-13 08:33:39 +00:00
{
int iCoins = 0;
2005-05-05 08:18:52 +00:00
while( begin != end )
{
iCoins += begin->second;
++begin;
}
2003-10-13 08:33:39 +00:00
return iCoins;
}
2005-05-05 08:18:52 +00:00
int Bookkeeper::GetNumCoins( Date beginning, Date ending ) const
{
return GetNumCoinsInRange( m_mapCoinsForHour.lower_bound( beginning ),
m_mapCoinsForHour.lower_bound( ending ) );
}
2003-10-13 08:33:39 +00:00
2005-05-05 08:18:52 +00:00
int Bookkeeper::GetCoinsTotal() const
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
return GetNumCoinsInRange( m_mapCoinsForHour.begin(), m_mapCoinsForHour.end() );
}
2003-10-13 08:33:39 +00:00
2005-05-05 08:18:52 +00:00
void Bookkeeper::GetCoinsLastDays( int coins[NUM_LAST_DAYS] ) const
{
time_t lOldTime = time(NULL);
2004-02-01 01:22:43 +00:00
tm time;
localtime_r( &lOldTime, &time );
2003-10-13 08:33:39 +00:00
2005-05-05 08:18:52 +00:00
time.tm_hour = 0;
2003-12-10 05:03:17 +00:00
for( int i=0; i<NUM_LAST_DAYS; i++ )
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
tm EndTime = AddDays( time, +1 );
coins[i] = GetNumCoins( time, EndTime );
2003-12-10 10:30:53 +00:00
time = GetYesterday( time );
2003-10-13 08:33:39 +00:00
}
}
2005-05-05 08:18:52 +00:00
void Bookkeeper::GetCoinsLastWeeks( int coins[NUM_LAST_WEEKS] ) const
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
time_t lOldTime = time(NULL);
2004-02-01 01:22:43 +00:00
tm time;
localtime_r( &lOldTime, &time );
2003-10-13 08:33:39 +00:00
2003-12-10 10:30:53 +00:00
time = GetNextSunday( time );
time = GetYesterday( time );
2003-10-13 08:33:39 +00:00
2003-12-10 05:03:17 +00:00
for( int w=0; w<NUM_LAST_WEEKS; w++ )
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
tm StartTime = AddDays( time, -DAYS_IN_WEEK );
coins[w] = GetNumCoins( StartTime, time );
time = StartTime;
2003-10-13 08:33:39 +00:00
}
}
2005-05-05 08:18:52 +00:00
/* iDay is days since Jan 1. iYear is eg. 2005. Return the day of the week, where
* 0 is Sunday. */
void Bookkeeper::GetCoinsByDayOfWeek( int coins[DAYS_IN_WEEK] ) const
2003-10-13 08:33:39 +00:00
{
for( int i=0; i<DAYS_IN_WEEK; i++ )
coins[i] = 0;
2005-05-05 08:18:52 +00:00
for( map<Date,int>::const_iterator it = m_mapCoinsForHour.begin(); it != m_mapCoinsForHour.end(); ++it )
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
const Date &d = it->first;
int iDayOfWeek = GetDayInYearAndYear( d.m_iDayOfYear, d.m_iYear ).tm_wday;
coins[iDayOfWeek] += it->second;
2003-10-13 08:33:39 +00:00
}
}
2005-05-05 08:18:52 +00:00
void Bookkeeper::GetCoinsByHour( int coins[HOURS_IN_DAY] ) const
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
memset( coins, 0, sizeof(int) * HOURS_IN_DAY );
for( map<Date,int>::const_iterator it = m_mapCoinsForHour.begin(); it != m_mapCoinsForHour.end(); ++it )
2003-10-13 08:33:39 +00:00
{
2005-05-05 08:18:52 +00:00
const Date &d = it->first;
if( d.m_iHour >= HOURS_IN_DAY )
{
LOG->Warn( "Hour %i >= %i", d.m_iHour, HOURS_IN_DAY );
continue;
}
2003-10-13 08:33:39 +00:00
2005-05-05 08:18:52 +00:00
coins[d.m_iHour] += it->second;
2003-10-13 08:33:39 +00:00
}
}
2004-06-08 01:24:17 +00:00
/*
2005-05-05 08:18:52 +00:00
* (c) 2003-2005 Chris Danford, Glenn Maynard
2004-06-08 01:24:17 +00:00
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/