1
0

cMojangAPI updates cRankManager's playernames.

This commit is contained in:
Mattes D 2014-08-21 20:47:52 +02:00
parent 326dd7e4c6
commit 0c04bf962e
4 changed files with 87 additions and 2 deletions

View File

@ -10,6 +10,7 @@
#include "inifile/iniFile.h"
#include "json/json.h"
#include "PolarSSL++/BlockingSslClientSocket.h"
#include "../RankManager.h"
@ -300,6 +301,7 @@ void cMojangAPI::AddPlayerNameToUUIDMapping(const AString & a_PlayerName, const
cCSLock Lock(m_CSUUIDToName);
m_UUIDToName[UUID] = sProfile(a_PlayerName, UUID, "", "", Now);
}
NotifyNameUUID(a_PlayerName, a_UUID);
}
@ -322,6 +324,7 @@ void cMojangAPI::AddPlayerProfile(const AString & a_PlayerName, const AString &
cCSLock Lock(m_CSUUIDToProfile);
m_UUIDToProfile[UUID] = sProfile(a_PlayerName, UUID, a_Properties, Now);
}
NotifyNameUUID(a_PlayerName, a_UUID);
}
@ -669,6 +672,7 @@ void cMojangAPI::CacheNamesToUUIDs(const AStringVector & a_PlayerNames)
continue;
}
m_NameToUUID[StrToLower(JsonName)] = sProfile(JsonName, JsonUUID, "", "", Now);
NotifyNameUUID(JsonName, JsonUUID);
} // for idx - root[]
} // cCSLock (m_CSNameToUUID)
@ -792,6 +796,21 @@ void cMojangAPI::CacheUUIDToProfile(const AString & a_UUID)
cCSLock Lock(m_CSNameToUUID);
m_NameToUUID[StrToLower(PlayerName)] = sProfile(PlayerName, a_UUID, Properties, Now);
}
NotifyNameUUID(PlayerName, a_UUID);
}
void cMojangAPI::NotifyNameUUID(const AString & a_PlayerName, const AString & a_UUID)
{
// Notify the rank manager:
cCSLock Lock(m_CSRankMgr);
if (m_RankMgr != NULL)
{
m_RankMgr->NotifyNameUUID(a_PlayerName, a_UUID);
}
}

View File

@ -11,6 +11,13 @@
#include <time.h>
// fwd: ../RankManager.h"
class cRankManager;
namespace Json
{
class Value;
@ -82,6 +89,9 @@ public:
the profile to the respective mapping caches and updtes their datetime stamp to now. */
void AddPlayerProfile(const AString & a_PlayerName, const AString & a_UUID, const Json::Value & a_Properties);
/** Sets the m_RankMgr that is used for name-uuid notifications. Accepts NULL to remove the binding. */
void SetRankManager(cRankManager * a_RankManager) { m_RankMgr = a_RankManager; }
protected:
/** Holds data for a single player profile. */
struct sProfile
@ -162,6 +172,12 @@ protected:
/** Protects m_UUIDToProfile against simultaneous multi-threaded access. */
cCriticalSection m_CSUUIDToProfile;
/** The rank manager that is notified of the name-uuid pairings. May be NULL. Protected by m_CSRankMgr. */
cRankManager * m_RankMgr;
/** Protects m_RankMgr agains simultaneous multi-threaded access. */
cCriticalSection m_CSRankMgr;
/** Loads the caches from a disk storage. */
void LoadCachesFromDisk(void);
@ -178,6 +194,10 @@ protected:
UUIDs that are not valid will not be added into the cache.
ASSUMEs that a_UUID is a lowercased short UUID. */
void CacheUUIDToProfile(const AString & a_UUID);
/** Called for each name-uuid pairing that is discovered.
If assigned, notifies the m_RankManager of the event. */
void NotifyNameUUID(const AString & a_PlayerName, const AString & a_PlayerUUID);
} ; // tolua_export

View File

@ -365,7 +365,8 @@ protected:
cRankManager::cRankManager(void) :
m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE),
m_IsInitialized(false)
m_IsInitialized(false),
m_MojangAPI(NULL)
{
}
@ -373,6 +374,18 @@ cRankManager::cRankManager(void) :
cRankManager::~cRankManager()
{
if (m_MojangAPI != NULL)
{
m_MojangAPI->SetRankManager(NULL);
}
}
void cRankManager::Initialize(cMojangAPI & a_MojangAPI)
{
ASSERT(!m_IsInitialized); // Calling Initialize for the second time?
@ -386,6 +399,8 @@ void cRankManager::Initialize(cMojangAPI & a_MojangAPI)
m_IsInitialized = true;
a_MojangAPI.SetRankManager(this);
// Check if tables empty, migrate from ini files then
if (AreDBTablesEmpty())
{
@ -1655,6 +1670,28 @@ bool cRankManager::IsPermissionInGroup(const AString & a_Permission, const AStri
void cRankManager::NotifyNameUUID(const AString & a_PlayerName, const AString & a_UUID)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
SQLite::Statement stmt(m_DB, "UPDATE PlayerRank SET PlayerName = ? WHERE PlayerUUID = ?");
stmt.bind(1, a_PlayerName);
stmt.bind(2, a_UUID);
stmt.exec();
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to update DB: %s", __FUNCTION__, ex.what());
}
}
bool cRankManager::AreDBTablesEmpty(void)
{
return (

View File

@ -50,6 +50,8 @@ public:
/** Creates the rank manager. Needs to be initialized before other use. */
cRankManager(void);
~cRankManager();
/** Initializes the rank manager. Performs migration and default-setting if no data is found in the DB.
The a_MojangAPI param is used when migrating from old ini files, to look up player UUIDs. */
void Initialize(cMojangAPI & a_MojangAPI);
@ -194,6 +196,9 @@ public:
/** Returns true iff the specified group contains the specified permission. */
bool IsPermissionInGroup(const AString & a_Permission, const AString & a_GroupName);
/** Called by cMojangAPI whenever the playername-uuid pairing is discovered. Updates the DB. */
void NotifyNameUUID(const AString & a_PlayerName, const AString & a_UUID);
protected:
/** The database storage for all the data. Protected by m_CS. */
@ -205,6 +210,10 @@ protected:
/** Set to true once the manager is initialized. */
bool m_IsInitialized;
/** The MojangAPI instance that is used for translating playernames to UUIDs.
Set in Initialize(), may be NULL. */
cMojangAPI * m_MojangAPI;
/** Returns true if all the DB tables are empty, indicating a fresh new install. */
bool AreDBTablesEmpty(void);