- common interface / code for saving player stats
This commit is contained in:
parent
00db8cf249
commit
aa4569965e
|
@ -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<SteamAchievementName>::getString(ACH_WIN_ONE_GAME).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME).c_str());
|
||||
}
|
||||
if(gamesWonCount >= 50 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES).c_str());
|
||||
}
|
||||
if(gamesWonCount >= 100 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str());
|
||||
}
|
||||
if(gamesWonCount >= 250 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str());
|
||||
}
|
||||
if(gamesWonCount >= 500 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str());
|
||||
}
|
||||
if(gamesWonCount > 1000 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::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<SteamAchievementName>::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME_ONLINE).c_str());
|
||||
}
|
||||
if(networkGamesWonCount >= 50 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str());
|
||||
}
|
||||
if(networkGamesWonCount >= 100 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str());
|
||||
}
|
||||
if(networkGamesWonCount >= 250 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str());
|
||||
}
|
||||
if(networkGamesWonCount >= 500 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str());
|
||||
}
|
||||
if(networkGamesWonCount > 1000 && playerStats->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_OVER_THOUSAND_GAMES_ONLINE).c_str()) == false) {
|
||||
playerStats->unlock(EnumParser<SteamAchievementName>::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<Steam*>(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<SteamAchievementName>::getString(ACH_WIN_ONE_GAME).c_str()) == false) {
|
||||
steamInstance->unlock(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME).c_str());
|
||||
}
|
||||
if (NetworkManager::getInstance().isNetworkGame()) {
|
||||
if(steamInstance->isUnlocked(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()) == false) {
|
||||
steamInstance->unlock(EnumParser<SteamAchievementName>::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<SteamLocal> 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<SteamAchievementName>::getString(ACH_WIN_ONE_GAME).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 50 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 100 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 250 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 500 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount > 1000 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_OVER_THOUSAND_GAMES).c_str(),true);
|
||||
}
|
||||
|
||||
|
||||
if (NetworkManager::getInstance().isNetworkGame()) {
|
||||
if(playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME_ONLINE).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_GAME_ONLINE).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 50 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIFTY_GAMES_ONLINE).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 100 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_ONE_HUNDRED_GAMES_ONLINE).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 250 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_TWO_HUNDRED_FIFTY_GAMES_ONLINE).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount >= 500 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_FIVE_HUNDRED_GAMES_ONLINE).c_str(),true);
|
||||
}
|
||||
if(gamesWonCount > 1000 && playerLocalStats.getBool(EnumParser<SteamAchievementName>::getString(ACH_WIN_OVER_THOUSAND_GAMES_ONLINE).c_str()) == false) {
|
||||
playerLocalStats.setBool(EnumParser<SteamAchievementName>::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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
#include <SDL.h>
|
||||
#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
|
||||
|
|
|
@ -5,6 +5,10 @@
|
|||
#include <map>
|
||||
#include "game_constants.h"
|
||||
|
||||
namespace Shared{ namespace Util{
|
||||
class Properties;
|
||||
}}
|
||||
|
||||
namespace Glest{ namespace Game{
|
||||
|
||||
struct SteamPrivate;
|
||||
|
@ -74,7 +78,22 @@ inline EnumParser<SteamAchievementName>::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
|
||||
|
|
Loading…
Reference in New Issue