diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 553217c1..59b53fbe 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -29,6 +29,7 @@ #include "cache_manager.h" #include "conversion.h" #include "steam.h" +#include "memory.h" #include "leak_dumper.h" @@ -5174,10 +5175,76 @@ void Game::DumpCRCWorldLogIfRequired(string fileSuffix) { } } -void saveStatsToSteam(Game* game, Stats& endStats) { - +void savePlayerStats(Game* game, Stats& endStats, PlayerAchievementsInterface *playerStats) { const double MIN_PLAY_TIME_MINUTES = 10.0; + // Write out achievements here + for (int factionIndex = 0; + factionIndex < game->getWorld()->getFactionCount(); ++factionIndex) { + if (factionIndex == game->getWorld()->getThisFactionIndex()) { + //printf("\nWriting out game stats for Faction Index: %d won status: %d\n",factionIndex,endStats.getVictory(factionIndex)); + if (endStats.getVictory(factionIndex)) { + double elapsedGameMinutes = (game->getWorld()->getStats()->getFramesToCalculatePlaytime() / GameConstants::updateFps / 60.0); + if(elapsedGameMinutes >= MIN_PLAY_TIME_MINUTES) { + int gamesWonCount = playerStats->getStatAsInt("games-won") + 1; + playerStats->setStatAsInt("games-won",gamesWonCount); + + if(playerStats->isUnlocked(EnumParser::getString(ACH_WIN_ONE_GAME).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_ONE_GAME).c_str()); + } + if(gamesWonCount >= 50 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_FIFTY_GAMES).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_FIFTY_GAMES).c_str()); + } + if(gamesWonCount >= 100 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str()); + } + if(gamesWonCount >= 250 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str()); + } + if(gamesWonCount >= 500 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str()); + } + if(gamesWonCount > 1000 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str()); + } + + + if (NetworkManager::getInstance().isNetworkGame()) { + int networkGamesWonCount = playerStats->getStatAsInt("network-games-won") + 1; + playerStats->setStatAsInt("network-games-won",networkGamesWonCount); + + if(playerStats->isUnlocked(EnumParser::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()); + } + if(networkGamesWonCount >= 50 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str()); + } + if(networkGamesWonCount >= 100 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str()); + } + if(networkGamesWonCount >= 250 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str()); + } + if(networkGamesWonCount >= 500 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str()); + } + if(networkGamesWonCount > 1000 && playerStats->isUnlocked(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES_ONLINE).c_str()) == false) { + playerStats->unlock(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES_ONLINE).c_str()); + } + } + + //printf("\nPlayer won the game with at least 10 minutes of play: %f!\n",elapsedGameMinutes); + } + else { + //printf("\nPlayer won the game BUT NOT with at least 10 minutes of play: %f!\n",elapsedGameMinutes); + } + } + } + } + playerStats->storeStats(); +} + +void saveStatsToSteam(Game* game, Stats& endStats) { Steam* steamInstance = CacheManager::getCachedItem(GameConstants::steamCacheInstanceKey); if (steamInstance != NULL) { printf("\nSTEAM detected, writing out end game stats for player!\n"); @@ -5235,28 +5302,10 @@ void saveStatsToSteam(Game* game, Stats& endStats) { } // Write out achievements here - for (int factionIndex = 0; - factionIndex < game->getWorld()->getFactionCount(); ++factionIndex) { - if (factionIndex == game->getWorld()->getThisFactionIndex()) { - //printf("\nWriting out game stats for Faction Index: %d won status: %d\n",factionIndex,endStats.getVictory(factionIndex)); - if (endStats.getVictory(factionIndex)) { - double elapsedGameMinutes = (game->getWorld()->getStats()->getFramesToCalculatePlaytime() / GameConstants::updateFps / 60.0); - if(elapsedGameMinutes >= MIN_PLAY_TIME_MINUTES) { - if(steamInstance->isUnlocked(EnumParser::getString(ACH_WIN_ONE_GAME).c_str()) == false) { - steamInstance->unlock(EnumParser::getString(ACH_WIN_ONE_GAME).c_str()); - } - if (NetworkManager::getInstance().isNetworkGame()) { - if(steamInstance->isUnlocked(EnumParser::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()) == false) { - steamInstance->unlock(EnumParser::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()); - } - } - } - } - } - } + savePlayerStats(game, endStats, steamInstance); //printf("\nSTEAM Store Stats!\n"); - steamInstance->storeStats(); + //steamInstance->storeStats(); //printf("\nSTEAM Refresh Stats!\n"); steamInstance->requestRefreshStats(); } @@ -5276,74 +5325,12 @@ void saveStatsToSteam(Game* game, Stats& endStats) { } saveFilePlayerLocalStats = userData + saveFilePlayerLocalStats; } - - Properties playerLocalStats; - if(fileExists(saveFilePlayerLocalStats)) { - playerLocalStats.load(saveFilePlayerLocalStats); - } + std::unique_ptr playerLocalStats(new SteamLocal(saveFilePlayerLocalStats)); + PlayerAchievementsInterface *playerStats = playerLocalStats.get(); // Write out achievements here - for (int factionIndex = 0; - factionIndex < game->getWorld()->getFactionCount(); ++factionIndex) { - if (factionIndex == game->getWorld()->getThisFactionIndex()) { - //printf("\nWriting out game stats for Faction Index: %d won status: %d\n",factionIndex,endStats.getVictory(factionIndex)); - if (endStats.getVictory(factionIndex)) { - double elapsedGameMinutes = (game->getWorld()->getStats()->getFramesToCalculatePlaytime() / GameConstants::updateFps / 60.0); - if(elapsedGameMinutes >= MIN_PLAY_TIME_MINUTES) { - int gamesWonCount = playerLocalStats.getInt("games-won",0) + 1; - playerLocalStats.setInt("games-won",gamesWonCount); - - if(playerLocalStats.getBool(EnumParser::getString(ACH_WIN_ONE_GAME).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_ONE_GAME).c_str(),true); - } - if(gamesWonCount >= 50 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_FIFTY_GAMES).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_FIFTY_GAMES).c_str(),true); - } - if(gamesWonCount >= 100 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str(),true); - } - if(gamesWonCount >= 250 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str(),true); - } - if(gamesWonCount >= 500 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str(),true); - } - if(gamesWonCount > 1000 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str(),true); - } - - - if (NetworkManager::getInstance().isNetworkGame()) { - if(playerLocalStats.getBool(EnumParser::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_ONE_GAME_ONLINE).c_str(),true); - } - if(gamesWonCount >= 50 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str(),true); - } - if(gamesWonCount >= 100 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str(),true); - } - if(gamesWonCount >= 250 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str(),true); - } - if(gamesWonCount >= 500 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str(),true); - } - if(gamesWonCount > 1000 && playerLocalStats.getBool(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES_ONLINE).c_str()) == false) { - playerLocalStats.setBool(EnumParser::getString(ACH_WIN_OVER_THOUSAND_GAMES_ONLINE).c_str(),true); - } - } - - //printf("\nPlayer won the game with at least 10 minutes of play: %f!\n",elapsedGameMinutes); - } - else { - //printf("\nPlayer won the game BUT NOT with at least 10 minutes of play: %f!\n",elapsedGameMinutes); - } - } - } - } - - playerLocalStats.save(saveFilePlayerLocalStats); + savePlayerStats(game, endStats, playerStats); + //playerLocalStats->save(saveFilePlayerLocalStats); } } diff --git a/source/glest_game/steam/steam.cpp b/source/glest_game/steam/steam.cpp index 01371dc1..bff8c0a5 100644 --- a/source/glest_game/steam/steam.cpp +++ b/source/glest_game/steam/steam.cpp @@ -4,6 +4,9 @@ #include #include "steamshim_child.h" #include "platform_common.h" +#include "properties.h" + +using Shared::PlatformCommon::fileExists; namespace Glest{ namespace Game{ @@ -309,4 +312,46 @@ void Steam::setDebugEnabled(bool value) { SteamPrivate::setDebugEnabled(value); } +/* SteamLocal */ +SteamLocal::SteamLocal(string file) : p(new Properties()) { + saveFilePlayerLocalStats = file; + if(fileExists(saveFilePlayerLocalStats)) { + p->load(saveFilePlayerLocalStats); + } +} + +SteamLocal::~SteamLocal() { + delete p; +} + +void SteamLocal::unlock(const char *name) { + //p->setAchievement(name, true); + p->setBool(name,true); +} + +void SteamLocal::lock(const char *name) { + //p->setAchievement(name, false); +} + +bool SteamLocal::isUnlocked(const char *name) { + //return p->isAchievementSet(name); + return p->getBool(name,"false"); +} + +int SteamLocal::getStatAsInt(const char *name) const { + return p->getInt(name,"0"); +} + +void SteamLocal::setStatAsInt(const char *name, int value) { + p->setInt(name,value); +} + +//void SteamLocal::save(const string &path) { +// p->save(path); +//} + +void SteamLocal::storeStats() const { + p->save(saveFilePlayerLocalStats); +} + }}//end namespace diff --git a/source/glest_game/steam/steam.h b/source/glest_game/steam/steam.h index 4780b5cc..b5a486c9 100644 --- a/source/glest_game/steam/steam.h +++ b/source/glest_game/steam/steam.h @@ -5,6 +5,10 @@ #include #include "game_constants.h" +namespace Shared{ namespace Util{ + class Properties; +}} + namespace Glest{ namespace Game{ struct SteamPrivate; @@ -74,7 +78,22 @@ inline EnumParser::EnumParser() { enumMap["ACH_WIN_OVER_THOUSAND_GAMES_ONLINE"] = ACH_WIN_OVER_THOUSAND_GAMES_ONLINE; } -class Steam +// +// This interface describes the methods a callback object must implement +// +class PlayerAchievementsInterface { +public: + virtual void unlock(const char *name) = 0; + virtual void lock(const char *name) = 0; + virtual bool isUnlocked(const char *name) = 0; + virtual int getStatAsInt(const char *name) const = 0; + virtual void setStatAsInt(const char *name, int value) = 0; + virtual void storeStats() const = 0; + + virtual ~PlayerAchievementsInterface() {} +}; + +class Steam: public PlayerAchievementsInterface { public: void unlock(const char *name); @@ -120,6 +139,27 @@ private: } }; +class SteamLocal: public PlayerAchievementsInterface +{ +public: + SteamLocal(string file); + ~SteamLocal(); + + void unlock(const char *name); + void lock(const char *name); + bool isUnlocked(const char *name); + int getStatAsInt(const char *name) const; + + void setStatAsInt(const char *name, int value); + + //void save(const string &path); + void storeStats() const; + +private: + Properties *p; + string saveFilePlayerLocalStats; +}; + }}//end namespace #endif // STEAM_H