#include "global.h" /* ----------------------------------------------------------------------------- Class: Bookkeeper Desc: See header. Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved. Chris Danford ----------------------------------------------------------------------------- */ #include "Bookkeeper.h" #include "RageUtil.h" #include "arch/arch.h" #include "PrefsManager.h" #include "RageLog.h" #include "IniFile.h" #include "GameConstantsAndTypes.h" #include "SongManager.h" #include "RageFile.h" #include Bookkeeper* BOOKKEEPER = NULL; // global and accessable from anywhere in our program static const CString COINS_DAT = "Data/Coins.dat"; const int COINS_DAT_VERSION = 1; Bookkeeper::Bookkeeper() { ClearAll(); ReadFromDisk(); UpdateLastSeenTime(); } Bookkeeper::~Bookkeeper() { WriteToDisk(); } #define WARN_AND_RETURN { LOG->Warn("Error parsing at %s:%d",__FILE__,__LINE__); return; } void Bookkeeper::ClearAll() { m_iLastSeenTime = time(NULL); for( int i=0; iTrace( "Couldn't open file \"%s\": %s", COINS_DAT.c_str(), f.GetError().c_str() ); return; } int iVer; if( !FileRead(f, iVer) ) WARN_AND_RETURN; if( iVer != COINS_DAT_VERSION ) WARN_AND_RETURN; if( !FileRead(f, m_iLastSeenTime) ) WARN_AND_RETURN; for (int i=0; iWarn( "Couldn't open file \"%s\" for writing: %s", COINS_DAT.c_str(), f.GetError().c_str() ); return; } FileWrite(f, COINS_DAT_VERSION ); FileWrite(f, m_iLastSeenTime); for (int i=0; iWarn( "The new time is older than the last seen time. Is someone fiddling with the system clock?" ); m_iLastSeenTime = lNewTime; return; } tm tOld, tNew; localtime_r( &lOldTime, &tOld ); localtime_r( &lNewTime, &tNew ); CLAMP( tOld.tm_year, tNew.tm_year-1, tNew.tm_year ); while( tOld.tm_year != tNew.tm_year || tOld.tm_yday != tNew.tm_yday || tOld.tm_hour != tNew.tm_hour ) { tOld.tm_hour++; if( tOld.tm_hour == HOURS_IN_DAY ) { tOld.tm_hour = 0; tOld.tm_yday++; } if( tOld.tm_yday == DAYS_IN_YEAR ) { tOld.tm_yday = 0; tOld.tm_year++; } m_iCoinsByHourForYear[tOld.tm_yday][tOld.tm_hour] = 0; } m_iLastSeenTime = lNewTime; } void Bookkeeper::CoinInserted() { UpdateLastSeenTime(); time_t lTime = m_iLastSeenTime; tm pTime; localtime_r( &lTime, &pTime ); m_iCoinsByHourForYear[pTime.tm_yday][pTime.tm_hour]++; } int Bookkeeper::GetCoinsForDay( int iDayOfYear ) { int iCoins = 0; for( int i=0; i