From eac836cc406ee3ee63f82d5d3ff73da4dace5b5a Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Mon, 8 Dec 2003 04:02:43 +0000 Subject: [PATCH] move many bookkeeping numbers to ProfileManager --- stepmania/src/Bookkeeper.cpp | 95 ++++++++++++----------------- stepmania/src/Bookkeeper.h | 4 -- stepmania/src/GameState.cpp | 39 +++++++++++- stepmania/src/GameState.h | 5 +- stepmania/src/ProfileManager.cpp | 79 +++++------------------- stepmania/src/ProfileManager.h | 6 ++ stepmania/src/RageUtil.cpp | 59 ++++++++++++++++++ stepmania/src/RageUtil.h | 13 ++++ stepmania/src/Screen.cpp | 4 ++ stepmania/src/ScreenMusicScroll.cpp | 5 -- stepmania/src/ScreenMusicScroll.h | 2 - 11 files changed, 177 insertions(+), 134 deletions(-) diff --git a/stepmania/src/Bookkeeper.cpp b/stepmania/src/Bookkeeper.cpp index fae203511f..fd9e8836aa 100644 --- a/stepmania/src/Bookkeeper.cpp +++ b/stepmania/src/Bookkeeper.cpp @@ -24,10 +24,9 @@ Bookkeeper* BOOKKEEPER = NULL; // global and accessable from anywhere in our program -static const CString BOOKKEEPING_INI = BASE_PATH "Data" SLASH "Bookkeeping.ini"; static const CString COINS_DAT = BASE_PATH "Data" SLASH "Coins.dat"; - +const int COINS_DAT_VERSION = 1; tm GetDaysAgo( tm start, int iDaysAgo ) { @@ -60,10 +59,6 @@ Bookkeeper::Bookkeeper() int i, j; m_iLastSeenTime = time(NULL); - m_iTotalCoins = 0; - m_iTotalUptimeSeconds = 0; - m_iTotalPlaySeconds = 0; - m_iTotalPlays = 0; for( i=0; iWarn("Error parsing at %s:%d",__FILE__,__LINE__); return; } + void Bookkeeper::ReadFromDisk() { - // read ini - IniFile ini; - ini.SetPath( BOOKKEEPING_INI ); - ini.ReadFile(); + RageFile f; + if( !f.Open(COINS_DAT, RageFile::READ) ) + { + LOG->Warn( "Couldn't open file '%s'", COINS_DAT.c_str() ); + return; + } - ini.GetValue( "MachineStatistics", "LastSeenTime", m_iLastSeenTime ); - ini.GetValue( "MachineStatistics", "TotalCoins", m_iTotalCoins ); - ini.GetValue( "MachineStatistics", "TotalUptimeSeconds", m_iTotalUptimeSeconds ); - ini.GetValue( "MachineStatistics", "TotalPlaySeconds", m_iTotalPlaySeconds ); - ini.GetValue( "MachineStatistics", "TotalPlays", m_iTotalPlays ); + int iVer; + if( !FileRead(f, iVer) ) + WARN_AND_RETURN; + if( iVer != COINS_DAT_VERSION ) + WARN_AND_RETURN; - // read dat - RageFile file( COINS_DAT ); - if (file.IsOpen()) - { - const CString line = file.GetLine(); + if( !FileRead(f, m_iLastSeenTime) ) + WARN_AND_RETURN; - vector parts; - split( line, " ", parts, true ); + for (int i=0; i= parts.size() ) - { - LOG->Warn( "Parse error in %s", COINS_DAT.c_str() ); - return; - } - - m_iCoinsByHourForYear[i][j] = atoi( parts[p++] ); - } - } + m_iCoinsByHourForYear[i][j] = iCoins; + } + } } void Bookkeeper::WriteToDisk() { - // write ini - IniFile ini; - ini.SetPath( BOOKKEEPING_INI ); - - ini.SetValue( "MachineStatistics", "LastSeenTime", m_iLastSeenTime ); - ini.SetValue( "MachineStatistics", "TotalCoins", m_iTotalCoins ); - ini.SetValue( "MachineStatistics", "TotalUptimeSeconds", m_iTotalUptimeSeconds ); - ini.SetValue( "MachineStatistics", "TotalPlaySeconds", m_iTotalPlaySeconds ); - ini.SetValue( "MachineStatistics", "TotalPlays", m_iTotalPlays ); - - ini.WriteFile(); - // write dat - RageFile file( COINS_DAT, RageFile::WRITE ); + RageFile f; + if( !f.Open(COINS_DAT, RageFile::WRITE) ) + { + LOG->Warn( "Couldn't open file '%s'", COINS_DAT.c_str() ); + return; + } - if (file.IsOpen()) - { - CString line; - for (int i=0; i #define DEFAULT_MODIFIERS THEME->GetMetric( "Common","DefaultModifiers" ) @@ -63,10 +64,15 @@ GameState::~GameState() void GameState::Reset() { + if( m_timeGameStated != 0 && m_vPlayedStageStats.size() ) // we were in the middle of a game and played at least one song + EndGame(); + + ASSERT( THEME ); int p; + m_timeGameStated = 0; m_CurStyle = STYLE_INVALID; for( p=0; pSetLightMode( LIGHTMODE_ATTRACT ); - // HACK: save stats intermitently in case of crash +} + +void GameState::BeginGame() +{ + m_timeGameStated = time(NULL); +} + +void GameState::EndGame() +{ + // Update profile stats + + time_t now = time(NULL); + int iPlaySeconds = now - m_timeGameStated; + if( iPlaySeconds < 0 ) + iPlaySeconds = 0; + + for( int p=0; pGetProfile( (PlayerNumber)p ); + if( pProfile==NULL ) + continue; + + pProfile->m_iTotalPlaySeconds += iPlaySeconds; + pProfile->m_iTotalGameplaySeconds += iGameplaySeconds; + pProfile->m_iTotalPlays++; + } + BOOKKEEPER->WriteToDisk(); PROFILEMAN->SaveMachineScoresToDisk(); } diff --git a/stepmania/src/GameState.h b/stepmania/src/GameState.h index f0a9daf4e8..82cba2f52e 100644 --- a/stepmania/src/GameState.h +++ b/stepmania/src/GameState.h @@ -37,6 +37,8 @@ public: GameState(); ~GameState(); void Reset(); + void BeginGame(); // called when first player joins + void EndGame(); // called on ScreenGameOver, ScreenMusicScroll, ScreenCredits void Update( float fDelta ); @@ -52,7 +54,8 @@ public: int m_iCoins; // not "credits" PlayerNumber m_MasterPlayerNumber; // used in Styles where one player controls both sides bool m_bIsOnSystemMenu; // system screens will not be effected by the operator key -- Miryokuteki - bool m_bDifficultCourses; //used in nonstop + bool m_bDifficultCourses; // used in nonstop + time_t m_timeGameStated; // from the moment the first player pressed Start /* This is set to a random number per-game/round; it can be used for a random seed. */ int m_iGameSeed, m_iRoundSeed; diff --git a/stepmania/src/ProfileManager.cpp b/stepmania/src/ProfileManager.cpp index 1a6e371f24..2d6cdd9ee9 100644 --- a/stepmania/src/ProfileManager.cpp +++ b/stepmania/src/ProfileManager.cpp @@ -245,20 +245,26 @@ bool Profile::LoadFromIni( CString sIniPath ) if( !ini.ReadFile() ) return false; - ini.GetValue( "Profile", "DisplayName", m_sName ); - ini.GetValue( "Profile", "LastUsedHighScoreName", m_sLastUsedHighScoreName ); - ini.GetValue( "Profile", "UsingProfileDefaultModifiers", m_bUsingProfileDefaultModifiers ); - ini.GetValue( "Profile", "DefaultModifiers", m_sDefaultModifiers ); + ini.GetValue( "Profile", "DisplayName", m_sName ); + ini.GetValue( "Profile", "LastUsedHighScoreName", m_sLastUsedHighScoreName ); + ini.GetValue( "Profile", "UsingProfileDefaultModifiers", m_bUsingProfileDefaultModifiers ); + ini.GetValue( "Profile", "DefaultModifiers", m_sDefaultModifiers ); + ini.GetValue( "Profile", "TotalPlays", m_iTotalPlays ); + ini.GetValue( "Profile", "TotalPlaySeconds", m_iTotalPlaySeconds ); + ini.GetValue( "Profile", "TotalGameplaySeconds", m_iTotalGameplaySeconds ); return true; } bool Profile::SaveToIni( CString sIniPath ) { IniFile ini( sIniPath ); - ini.SetValue( "Profile", "DisplayName", m_sName ); - ini.SetValue( "Profile", "LastUsedHighScoreName", m_sLastUsedHighScoreName ); - ini.SetValue( "Profile", "UsingProfileDefaultModifiers", m_bUsingProfileDefaultModifiers ); - ini.SetValue( "Profile", "DefaultModifiers", m_sDefaultModifiers ); + ini.SetValue( "Profile", "DisplayName", m_sName ); + ini.SetValue( "Profile", "LastUsedHighScoreName", m_sLastUsedHighScoreName ); + ini.SetValue( "Profile", "UsingProfileDefaultModifiers", m_bUsingProfileDefaultModifiers ); + ini.SetValue( "Profile", "DefaultModifiers", m_sDefaultModifiers ); + ini.SetValue( "Profile", "TotalPlays", m_iTotalPlays ); + ini.SetValue( "Profile", "TotalPlaySeconds", m_iTotalPlaySeconds ); + ini.SetValue( "Profile", "TotalGameplaySeconds", m_iTotalGameplaySeconds ); ini.WriteFile(); return true; } @@ -360,63 +366,6 @@ void ProfileManager::CategoryData::AddHighScore( HighScore hs, int &iIndexOut ) } } -// -// Helper function for reading/writing scores -// -bool FileRead(RageFile& f, CString& sOut) -{ - if (f.AtEOF()) - return false; - sOut = f.GetLine(); - return true; -} - -bool FileRead(RageFile& f, int& iOut) -{ - CString s; - if (!FileRead(f, s)) - return false; - iOut = atoi(s); - return true; -} - -bool FileRead(RageFile& f, unsigned& uOut) -{ - CString s; - if (!FileRead(f, s)) - return false; - uOut = atoi(s); - return true; -} - -bool FileRead(RageFile& f, float& fOut) -{ - CString s; - if (!FileRead(f, s)) - return false; - fOut = (float)atof(s); - return true; -} - -void FileWrite(RageFile& f, const CString& sWrite) -{ - f.PutLine( sWrite ); -} - -void FileWrite(RageFile& f, int iWrite) -{ - f.PutLine( ssprintf("%d", iWrite) ); -} - -void FileWrite(RageFile& f, size_t uWrite) -{ - f.PutLine( ssprintf("%lu", uWrite) ); -} - -void FileWrite(RageFile& f, float fWrite) -{ - f.PutLine( ssprintf("%f", fWrite) ); -} #define WARN_AND_RETURN { LOG->Warn("Error parsing file '%s' at %s:%d",fn.c_str(),__FILE__,__LINE__); return; } diff --git a/stepmania/src/ProfileManager.h b/stepmania/src/ProfileManager.h index 643d0ba8d6..cda3defce6 100644 --- a/stepmania/src/ProfileManager.h +++ b/stepmania/src/ProfileManager.h @@ -25,6 +25,9 @@ struct Profile m_sLastUsedHighScoreName = ""; m_bUsingProfileDefaultModifiers = false; m_sDefaultModifiers = ""; + m_iTotalPlays = 0; + m_iTotalPlaySeconds = 0; + m_iTotalGameplaySeconds = 0; } bool LoadFromIni( CString sIniPath ); @@ -33,6 +36,9 @@ struct Profile CString m_sLastUsedHighScoreName; bool m_bUsingProfileDefaultModifiers; CString m_sDefaultModifiers; + int m_iTotalPlays; + int m_iTotalPlaySeconds; + int m_iTotalGameplaySeconds; }; class ProfileManager diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index d21d78d9f0..942ca5cdaa 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -964,3 +964,62 @@ char char_traits_char_nocase::uptab[256] = '\xE0','\xE1','\xE2','\xE3','\xE4','\xE5','\xE6','\xE7','\xE8','\xE9','\xEA','\xEB','\xEC','\xED','\xEE','\xEF', '\xF0','\xF1','\xF2','\xF3','\xF4','\xF5','\xF6','\xF7','\xF8','\xF9','\xFA','\xFB','\xFC','\xFD','\xFE','\xFF', }; + + +// +// Helper function for reading/writing scores +// +bool FileRead(RageFile& f, CString& sOut) +{ + if (f.AtEOF()) + return false; + sOut = f.GetLine(); + return true; +} + +bool FileRead(RageFile& f, int& iOut) +{ + CString s; + if (!FileRead(f, s)) + return false; + iOut = atoi(s); + return true; +} + +bool FileRead(RageFile& f, unsigned& uOut) +{ + CString s; + if (!FileRead(f, s)) + return false; + uOut = atoi(s); + return true; +} + +bool FileRead(RageFile& f, float& fOut) +{ + CString s; + if (!FileRead(f, s)) + return false; + fOut = (float)atof(s); + return true; +} + +void FileWrite(RageFile& f, const CString& sWrite) +{ + f.PutLine( sWrite ); +} + +void FileWrite(RageFile& f, int iWrite) +{ + f.PutLine( ssprintf("%d", iWrite) ); +} + +void FileWrite(RageFile& f, size_t uWrite) +{ + f.PutLine( ssprintf("%lu", uWrite) ); +} + +void FileWrite(RageFile& f, float fWrite) +{ + f.PutLine( ssprintf("%f", fWrite) ); +} diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index af133cc9ae..e3955972f3 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -13,6 +13,7 @@ */ #include +#include "RageFile.h" //----------------------------------------------------------------------------- // SAFE_ Macros @@ -284,5 +285,17 @@ unsigned GetFileSizeInBytes( const CString &sFilePath ); int GetFileModTime( const CString &sPath ); void FlushDirCache(); +// helper file functions used by Bookkeeper and ProfileManager +// +// Helper function for reading/writing scores +// +bool FileRead(RageFile& f, CString& sOut); +bool FileRead(RageFile& f, int& iOut); +bool FileRead(RageFile& f, unsigned& uOut); +bool FileRead(RageFile& f, float& fOut); +void FileWrite(RageFile& f, const CString& sWrite); +void FileWrite(RageFile& f, int iWrite); +void FileWrite(RageFile& f, size_t uWrite); +void FileWrite(RageFile& f, float fWrite); #endif diff --git a/stepmania/src/Screen.cpp b/stepmania/src/Screen.cpp index 65c5cb860c..b1a00def74 100644 --- a/stepmania/src/Screen.cpp +++ b/stepmania/src/Screen.cpp @@ -198,6 +198,10 @@ bool Screen::JoinInput( const DeviceInput& DeviceI, const InputEventType type, c SOUND->PlayOnce( THEME->GetPathToS("Common start") ); + // if first player to join, set start time + if( GAMESTATE->GetNumSidesJoined() == 1 ) + GAMESTATE->BeginGame(); + return true; } diff --git a/stepmania/src/ScreenMusicScroll.cpp b/stepmania/src/ScreenMusicScroll.cpp index 6cfb1782d3..584905d733 100644 --- a/stepmania/src/ScreenMusicScroll.cpp +++ b/stepmania/src/ScreenMusicScroll.cpp @@ -97,8 +97,3 @@ void ScreenMusicScroll::Update( float fDeltaTime ) } } - -void ScreenMusicScroll::HandleScreenMessage( const ScreenMessage SM ) -{ - ScreenAttract::HandleScreenMessage( SM ); -} diff --git a/stepmania/src/ScreenMusicScroll.h b/stepmania/src/ScreenMusicScroll.h index 862a619d45..a268e3fccb 100644 --- a/stepmania/src/ScreenMusicScroll.h +++ b/stepmania/src/ScreenMusicScroll.h @@ -21,8 +21,6 @@ public: virtual void Update( float fDeltaTime ); - virtual void HandleScreenMessage( const ScreenMessage SM ); - private: vector m_textLines; };