fix bookkeeping bugs

This commit is contained in:
Chris Danford
2003-12-10 10:30:53 +00:00
parent aac40fec42
commit e8c7e79729
4 changed files with 34 additions and 16 deletions
+18 -13
View File
@@ -28,9 +28,9 @@ static const CString COINS_DAT = "Data/Coins.dat";
const int COINS_DAT_VERSION = 1; const int COINS_DAT_VERSION = 1;
tm GetDaysAgo( tm start, int iDaysAgo ) tm AddDays( tm start, int iDaysToMove )
{ {
start.tm_mday -= iDaysAgo; start.tm_mday += iDaysToMove;
time_t seconds = mktime( &start ); time_t seconds = mktime( &start );
ASSERT( seconds != (time_t)-1 ); ASSERT( seconds != (time_t)-1 );
tm time = *localtime( &seconds ); tm time = *localtime( &seconds );
@@ -39,17 +39,19 @@ tm GetDaysAgo( tm start, int iDaysAgo )
tm GetYesterday( tm start ) tm GetYesterday( tm start )
{ {
return GetDaysAgo( start, -1 ); return AddDays( start, -1 );
} }
int GetDayOfWeek( tm time ) int GetDayOfWeek( tm time )
{ {
return time.tm_wday; int iDayOfWeek = time.tm_wday;
ASSERT( iDayOfWeek < DAYS_IN_WEEK );
return iDayOfWeek;
} }
tm GetLastSunday( tm start ) tm GetNextSunday( tm start )
{ {
return GetDaysAgo( start, -GetDayOfWeek(start) ); return AddDays( start, DAYS_IN_WEEK-GetDayOfWeek(start) );
} }
@@ -163,16 +165,18 @@ void Bookkeeper::UpdateLastSeenTime()
m_iCoinsByHourForYear[tOld.tm_yday][tOld.tm_hour] = 0; m_iCoinsByHourForYear[tOld.tm_yday][tOld.tm_hour] = 0;
} }
m_iLastSeenTime = lNewTime;
} }
void Bookkeeper::CoinInserted() void Bookkeeper::CoinInserted()
{ {
UpdateLastSeenTime(); UpdateLastSeenTime();
long lOldTime = m_iLastSeenTime; long lTime = m_iLastSeenTime;
tm *pNewTime = localtime( &lOldTime ); tm *pTime = localtime( &lTime );
m_iCoinsByHourForYear[pNewTime->tm_yday][pNewTime->tm_hour]++; m_iCoinsByHourForYear[pTime->tm_yday][pTime->tm_hour]++;
} }
int Bookkeeper::GetCoinsForDay( int iDayOfYear ) int Bookkeeper::GetCoinsForDay( int iDayOfYear )
@@ -193,8 +197,8 @@ void Bookkeeper::GetCoinsLastDays( int coins[NUM_LAST_DAYS] )
for( int i=0; i<NUM_LAST_DAYS; i++ ) for( int i=0; i<NUM_LAST_DAYS; i++ )
{ {
time = GetYesterday( time );
coins[i] = GetCoinsForDay( time.tm_yday ); coins[i] = GetCoinsForDay( time.tm_yday );
time = GetYesterday( time );
} }
} }
@@ -206,7 +210,8 @@ void Bookkeeper::GetCoinsLastWeeks( int coins[NUM_LAST_WEEKS] )
long lOldTime = m_iLastSeenTime; long lOldTime = m_iLastSeenTime;
tm time = *localtime( &lOldTime ); tm time = *localtime( &lOldTime );
time = GetLastSunday( time ); time = GetNextSunday( time );
time = GetYesterday( time );
for( int w=0; w<NUM_LAST_WEEKS; w++ ) for( int w=0; w<NUM_LAST_WEEKS; w++ )
{ {
@@ -214,8 +219,8 @@ void Bookkeeper::GetCoinsLastWeeks( int coins[NUM_LAST_WEEKS] )
for( int d=0; d<DAYS_IN_WEEK; d++ ) for( int d=0; d<DAYS_IN_WEEK; d++ )
{ {
time = GetYesterday( time );
coins[w] += GetCoinsForDay( time.tm_yday ); coins[w] += GetCoinsForDay( time.tm_yday );
time = GetYesterday( time );
} }
} }
} }
@@ -232,8 +237,8 @@ void Bookkeeper::GetCoinsByDayOfWeek( int coins[DAYS_IN_WEEK] )
for( int d=0; d<DAYS_PER_YEAR; d++ ) for( int d=0; d<DAYS_PER_YEAR; d++ )
{ {
time = GetYesterday( time );
coins[GetDayOfWeek(time)] += GetCoinsForDay( time.tm_yday ); coins[GetDayOfWeek(time)] += GetCoinsForDay( time.tm_yday );
time = GetYesterday( time );
} }
} }
+1 -1
View File
@@ -31,7 +31,7 @@ const CString LAST_DAYS_NAME[NUM_LAST_DAYS] =
"7 Days Ago", "7 Days Ago",
}; };
const CString DAY_TO_NAME[DAYS_IN_WEEK] = const CString DAY_OF_WEEK_TO_NAME[DAYS_IN_WEEK] =
{ {
"Sunday", "Sunday",
"Monday", "Monday",
+14 -1
View File
@@ -1290,11 +1290,24 @@ void ProfileManager::SaveStatsWebPageToDir( CString sDir, MemoryCard mc )
PRINT_DIV_END; PRINT_DIV_END;
} }
// GetCoinsByDayOfWeek
{
int coins[DAYS_IN_WEEK];
BOOKKEEPER->GetCoinsByDayOfWeek( coins );
PRINT_DIV_START( "Coins by Day of Week" );
for( int i=0; i<DAYS_IN_WEEK; i++ )
{
CString sDay = DAY_OF_WEEK_TO_NAME[i];
PRINT_LINE_I( sDay.c_str(), coins[i] );
}
PRINT_DIV_END;
}
// GetCoinsByHour // GetCoinsByHour
{ {
int coins[HOURS_PER_DAY]; int coins[HOURS_PER_DAY];
BOOKKEEPER->GetCoinsByHour( coins ); BOOKKEEPER->GetCoinsByHour( coins );
PRINT_DIV_START( ssprintf("Coins for Last %d Weeks",HOURS_PER_DAY).c_str() ); PRINT_DIV_START( ssprintf("Coins for Last %d Hours",HOURS_PER_DAY).c_str() );
for( int i=0; i<HOURS_PER_DAY; i++ ) for( int i=0; i<HOURS_PER_DAY; i++ )
{ {
CString sHour = ssprintf("hour %d",i); CString sHour = ssprintf("hour %d",i);
+1 -1
View File
@@ -187,7 +187,7 @@ void ScreenBookkeeping::ChangeView( View newView )
CString sTitle, sData; CString sTitle, sData;
for( int i=0; i<DAYS_IN_WEEK; i++ ) for( int i=0; i<DAYS_IN_WEEK; i++ )
{ {
sTitle += DAY_TO_NAME[i] + "\n"; sTitle += DAY_OF_WEEK_TO_NAME[i] + "\n";
sData += ssprintf("%d",coins[i]) + "\n"; sData += ssprintf("%d",coins[i]) + "\n";
} }