From: Chris D. <de...@st...> - 2010-12-29 02:40:50
|
Modified: branches/4.0/stepmania/src/JsonUtil.h branches/4.0/stepmania/src/Profile.cpp branches/4.0/stepmania/src/Profile.h Log: finish JSON serialization of Profile Modified: branches/4.0/stepmania/src/JsonUtil.h ============================================================================== --- branches/4.0/stepmania/src/JsonUtil.h (original) +++ branches/4.0/stepmania/src/JsonUtil.h Tue Dec 28 18:40:43 2010 @@ -64,6 +64,20 @@ namespace JsonUtil iter->Serialize( root[i++] ); } + template <typename M, typename E> + static void SerializeEnumToObjectMap(const M &m, const RString &fnEnumToString(E e), Json::Value &root) + { + for( M::const_iterator iter=m.begin(); iter!=m.end(); iter++ ) + iter->second.Serialize( root[ fnEnumToString(iter->first) ] ); + } + + template <typename M> + static void SerializeValueToValueMap(const M &m, Json::Value &root) + { + for( M::const_iterator iter=m.begin(); iter!=m.end(); iter++ ) + root[ (iter->first).GetString() ] = iter->second; + } + // Serialize a map that has a non-string key type template <typename V> static void SerializeMapAsArray(const V &v, const RString &sKeyName, const RString &sValueName, Json::Value &root) Modified: branches/4.0/stepmania/src/Profile.cpp ============================================================================== --- branches/4.0/stepmania/src/Profile.cpp (original) +++ branches/4.0/stepmania/src/Profile.cpp Tue Dec 28 18:40:43 2010 @@ -156,9 +156,7 @@ void Profile::InitCourseScores() void Profile::InitCategoryScores() { - for( int st=0; st<NUM_STEPS_TYPES; st++ ) - for( int rc=0; rc<NUM_RANKING_CATEGORIES; rc++ ) - m_CategoryHighScores[st][rc].Init(); + m_CategoryHighScores.clear(); } void Profile::InitScreenshotData() @@ -745,31 +743,34 @@ void Profile::IncrementCoursePlayCount( // void Profile::AddCategoryHighScore( StepsType st, RankingCategory rc, HighScore hs, int &iIndexOut ) { - m_CategoryHighScores[st][rc].AddHighScore( hs, iIndexOut, IsMachine() ); + m_CategoryHighScores[st].m_v[rc].AddHighScore( hs, iIndexOut, IsMachine() ); } const HighScoreList& Profile::GetCategoryHighScoreList( StepsType st, RankingCategory rc ) const { - return ((Profile *)this)->m_CategoryHighScores[st][rc]; + return ((Profile *)this)->m_CategoryHighScores[st].m_v[rc]; } HighScoreList& Profile::GetCategoryHighScoreList( StepsType st, RankingCategory rc ) { - return m_CategoryHighScores[st][rc]; + return m_CategoryHighScores[st].m_v[rc]; } int Profile::GetCategoryNumTimesPlayed( StepsType st ) const { int iNumTimesPlayed = 0; - FOREACH_RankingCategory( rc ) - iNumTimesPlayed += m_CategoryHighScores[st][rc].GetNumTimesPlayed(); + map<StepsType,RankingCategoryToHighScoreList>::const_iterator iter1 = m_CategoryHighScores.find(st); + if( iter1 == m_CategoryHighScores.end() ) + return 0; + FOREACHM_CONST( RankingCategory, HighScoreList, iter1->second.m_v, iter2 ) + iNumTimesPlayed += iter2->second.GetNumTimesPlayed(); return iNumTimesPlayed; } void Profile::IncrementCategoryPlayCount( StepsType st, RankingCategory rc ) { DateTime now = DateTime::GetNowDate(); - m_CategoryHighScores[st][rc].IncrementPlayCount( now ); + m_CategoryHighScores[st].m_v[rc].IncrementPlayCount( now ); } @@ -916,17 +917,35 @@ bool Profile::SaveAllToDir( RString sDir return bSaved; } + +void Profile::RankingCategoryToHighScoreList::Serialize( Json::Value &root ) const +{ + JsonUtil::SerializeEnumToObjectMap( m_v, RankingCategoryToString, root ); +} + +void Profile::RankingCategoryToHighScoreList::Deserialize( const Json::Value &root ) +{ + FAIL_M("unfinished"); +} + bool Profile::SaveStatsJsonToDir( RString sDir, bool bSignData ) const { Json::Value root; SaveGeneral( root["General"] ); - SaveSongScores( root["SongScores"] ); - SaveCourseScores( root["CourseScores"] ); - SaveCategoryScores( root["CategoryScores"] ); - SaveScreenshotData( root["ScreenshotData"] ); - SaveCalorieData( root["CalorieData"] ); - SaveRecentSongScores( root["RecentSongScores"] ); - SaveRecentCourseScores( root["RecentCourseScores"] ); + + JsonUtil::SerializeMapAsArray( m_SongHighScores, "Song", "HighScoresForASong", root["SongScores"] ); + + JsonUtil::SerializeMapAsArray( m_CourseHighScores, "Course", "HighScoresForACourse", root["CourseScores"] ); + + JsonUtil::SerializeEnumToObjectMap( m_CategoryHighScores, StepsTypeToString, root["CategoryScores"] ); + + JsonUtil::SerializeArrayObjects( m_vScreenshots, root["ScreenshotData"] ); + + JsonUtil::SerializeValueToValueMap( m_mapDayToCaloriesBurned, root["CalorieData"] ); + + JsonUtil::SerializeArrayObjects( m_vRecentStepsScores, root["RecentSongScores"] ); + + JsonUtil::SerializeArrayObjects( m_vRecentCourseScores, root["RecentCourseScores"] ); RString fn = sDir + STATS_JSON; bool bSaved = JsonUtil::WriteFile( root, fn, false ); @@ -1289,7 +1308,6 @@ void Profile::SaveSongScores( Json::Valu { CHECKPOINT; ASSERT( this ); - JsonUtil::SerializeMapAsArray( m_SongHighScores, "Song", "HighScoresForASong", root ); } void Profile::LoadSongScoresFromNode( const XNode* pSongScores ) @@ -1337,7 +1355,6 @@ void Profile::SaveCourseScores( Json::Va { CHECKPOINT; ASSERT( this ); - JsonUtil::SerializeMapAsArray( m_CourseHighScores, "Course", "HighScoresForACourse", root ); } void Profile::LoadCourseScoresFromNode( const XNode* pCourseScores ) @@ -1413,24 +1430,6 @@ void Profile::SaveCategoryScores( Json:: CHECKPOINT; ASSERT( this ); - FOREACH_StepsType( st ) - { - // skip steps types that have never been played - if( GetCategoryNumTimesPlayed( st ) == 0 ) - continue; - - Json::Value &v = root[ GameManager::StepsTypeToString(st) ]; - - FOREACH_RankingCategory( rc ) - { - // skip steps types/categories that have never been played - if( GetCategoryHighScoreList(st,rc).GetNumTimesPlayed() == 0 ) - continue; - - const HighScoreList &hsl = GetCategoryHighScoreList( (StepsType)st, (RankingCategory)rc ); - hsl.Serialize( v[ RankingCategoryToString(rc) ] ); - } - } } void Profile::LoadCategoryScoresFromNode( const XNode* pCategoryScores ) @@ -1514,7 +1513,6 @@ void Profile::LoadScreenshotDataFromNode void Profile::SaveScreenshotData( Json::Value &root ) const { CHECKPOINT; - JsonUtil::SerializeArrayObjects( m_vScreenshots, root ); } void Profile::LoadCalorieDataFromNode( const XNode* pCalorieData ) @@ -1547,8 +1545,6 @@ void Profile::SaveCalorieData( Json::Val CHECKPOINT; ASSERT( this ); - FOREACHM_CONST( DateTime, float, m_mapDayToCaloriesBurned, iter ) - root[ iter->first.GetString() ] = iter->second; } float Profile::GetCaloriesBurnedForDay( DateTime day ) const @@ -1626,7 +1622,6 @@ void Profile::SaveRecentSongScores( Json { CHECKPOINT; ASSERT( this ); - JsonUtil::SerializeArrayObjects( m_vRecentStepsScores, root ); } void Profile::AddStepsRecentScore( const Song* pSong, const Steps* pSteps, HighScore hs ) @@ -1709,7 +1704,6 @@ void Profile::SaveRecentCourseScores( Js { CHECKPOINT; ASSERT( this ); - JsonUtil::SerializeArrayObjects( m_vRecentCourseScores, root ); } void Profile::AddCourseRecentScore( const Course* pCourse, const Trail* pTrail, HighScore hs ) Modified: branches/4.0/stepmania/src/Profile.h ============================================================================== --- branches/4.0/stepmania/src/Profile.h (original) +++ branches/4.0/stepmania/src/Profile.h Tue Dec 28 18:40:43 2010 @@ -183,7 +183,7 @@ public: void Serialize( Json::Value &root ) const; void Deserialize( const Json::Value &root ); }; - std::map<CourseID,HighScoresForACourse> m_CourseHighScores; + map<CourseID,HighScoresForACourse> m_CourseHighScores; void AddCourseHighScore( const Course* pCourse, const Trail* pTrail, HighScore hs, int &iIndexOut ); HighScoreList& GetCourseHighScoreList( const Course* pCourse, const Trail* pTrail ); @@ -197,7 +197,14 @@ public: // // Category high scores // - HighScoreList m_CategoryHighScores[NUM_STEPS_TYPES][NUM_RANKING_CATEGORIES]; + struct RankingCategoryToHighScoreList + { + map<RankingCategory,HighScoreList> m_v; + + void Serialize( Json::Value &root ) const; + void Deserialize( const Json::Value &root ); + }; + map<StepsType,RankingCategoryToHighScoreList> m_CategoryHighScores; void AddCategoryHighScore( StepsType st, RankingCategory rc, HighScore hs, int &iIndexOut ); HighScoreList& GetCategoryHighScoreList( StepsType st, RankingCategory rc ); |