1
0

cRankManager: Added GetAllPlayers() and GetPlayerName()

This commit is contained in:
Howaner 2014-09-28 02:17:32 +02:00
parent 04ee8c43dd
commit 2fb3eb3532
4 changed files with 157 additions and 0 deletions

View File

@ -183,6 +183,33 @@ static int tolua_cRankManager_GetAllPermissions(lua_State * L)
/** Binds cRankManager::GetAllPlayers */
static int tolua_cRankManager_GetAllPlayers(lua_State * L)
{
// Function signature:
// cRankManager:GetAllPlayers() -> arraytable of Player UUID's
cLuaState S(L);
if (
!S.CheckParamUserTable(1, "cRankManager") ||
!S.CheckParamEnd(2)
)
{
return 0;
}
// Get the player uuid's:
AStringVector Players = cRoot::Get()->GetRankManager().GetAllPlayers();
// Push the results:
S.Push(Players);
return 1;
}
/** Binds cRankManager::GetAllRanks */
static int tolua_cRankManager_GetAllRanks(lua_State * L)
{
@ -400,6 +427,38 @@ static int tolua_cRankManager_GetPlayerRankName(lua_State * L)
/** Binds cRankManager::GetPlayerName */
static int tolua_cRankManager_GetPlayerName(lua_State * L)
{
// Function signature:
// cRankManager:GetPlayerName(PlayerUUID) -> string
cLuaState S(L);
if (
!S.CheckParamUserTable(1, "cRankManager") ||
!S.CheckParamString(2) ||
!S.CheckParamEnd(3)
)
{
return 0;
}
// Get the params:
AString PlayerUUID;
S.GetStackValue(2, PlayerUUID);
// Get the player name:
AString PlayerName = cRoot::Get()->GetRankManager().GetPlayerName(PlayerUUID);
// Push the result:
S.Push(PlayerName);
return 1;
}
/** Binds cRankManager::GetRankGroups */
static int tolua_cRankManager_GetRankGroups(lua_State * L)
{
@ -974,6 +1033,7 @@ void ManualBindings::BindRankManager(lua_State * tolua_S)
tolua_function(tolua_S, "AddRank", tolua_cRankManager_AddRank);
tolua_function(tolua_S, "GetAllGroups", tolua_cRankManager_GetAllGroups);
tolua_function(tolua_S, "GetAllPermissions", tolua_cRankManager_GetAllPermissions);
tolua_function(tolua_S, "GetAllPlayers", tolua_cRankManager_GetAllPlayers);
tolua_function(tolua_S, "GetAllRanks", tolua_cRankManager_GetAllRanks);
tolua_function(tolua_S, "GetDefaultRank", tolua_cRankManager_GetDefaultRank);
tolua_function(tolua_S, "GetGroupPermissions", tolua_cRankManager_GetGroupPermissions);
@ -981,6 +1041,7 @@ void ManualBindings::BindRankManager(lua_State * tolua_S)
tolua_function(tolua_S, "GetPlayerMsgVisuals", tolua_cRankManager_GetPlayerMsgVisuals);
tolua_function(tolua_S, "GetPlayerPermissions", tolua_cRankManager_GetPlayerPermissions);
tolua_function(tolua_S, "GetPlayerRankName", tolua_cRankManager_GetPlayerRankName);
tolua_function(tolua_S, "GetPlayerName", tolua_cRankManager_GetPlayerName);
tolua_function(tolua_S, "GetRankGroups", tolua_cRankManager_GetRankGroups);
tolua_function(tolua_S, "GetRankPermissions", tolua_cRankManager_GetRankPermissions);
tolua_function(tolua_S, "GetRankVisuals", tolua_cRankManager_GetRankVisuals);

View File

@ -2140,6 +2140,11 @@ void cPlayer::LoadRank(void)
{
m_Rank = RankMgr.GetDefaultRank();
}
else
{
// Update the name:
RankMgr.UpdatePlayerName(m_UUID, m_PlayerName);
}
m_Permissions = RankMgr.GetPlayerPermissions(m_UUID);
RankMgr.GetRankVisuals(m_Rank, m_MsgPrefix, m_MsgSuffix, m_MsgNameColorCode);

View File

@ -496,6 +496,33 @@ AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID)
AString cRankManager::GetPlayerName(const AString & a_PlayerUUID)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
// Prepare the DB statement:
SQLite::Statement stmt(m_DB, "SELECT PlayerName FROM PlayerRank WHERE PlayerUUID = ?");
stmt.bind(1, a_PlayerUUID);
if (stmt.executeStep())
{
return stmt.getColumn(0).getText();
}
}
catch (SQLite::Exception & ex)
{
LOGWARNING("%s: Cannot get player name: %s", __FUNCTION__, ex.what());
}
return AString();
}
AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID)
{
ASSERT(m_IsInitialized);
@ -636,6 +663,32 @@ AStringVector cRankManager::GetRankPermissions(const AString & a_RankName)
AStringVector cRankManager::GetAllPlayers(void)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB, "SELECT PlayerUUID FROM PlayerRank");
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get players from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetAllRanks(void)
{
ASSERT(m_IsInitialized);
@ -1764,6 +1817,34 @@ bool cRankManager::SetDefaultRank(const AString & a_RankName)
bool cRankManager::UpdatePlayerName(const AString & a_PlayerUUID, const AString & a_NewPlayerName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
SQLite::Statement stmt(m_DB, "UPDATE PlayerRank SET PlayerName = ? WHERE PlayerUUID = ?");
stmt.bind(1, a_NewPlayerName);
stmt.bind(2, a_PlayerUUID);
if (stmt.exec() > 0)
{
// The player name was changed, returns true
return true;
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to update player name from UUID %s: %s", __FUNCTION__, a_PlayerUUID.c_str(), ex.what());
}
return false;
}
bool cRankManager::AreDBTablesEmpty(void)
{
return (

View File

@ -60,6 +60,10 @@ public:
If the player has no rank assigned, returns an empty string (NOT the default rank). */
AString GetPlayerRankName(const AString & a_PlayerUUID);
/** Returns the last name that the specified player has.
If the player isn't in the database, this returns an empty string. */
AString GetPlayerName(const AString & a_PlayerUUID);
/** Returns the names of Groups that the specified player has assigned to them. */
AStringVector GetPlayerGroups(const AString & a_PlayerUUID);
@ -79,6 +83,9 @@ public:
Returns an empty vector if the rank doesn't exist. Any non-existent groups are ignored. */
AStringVector GetRankPermissions(const AString & a_RankName);
/** Returns the short uuids of all defined players. */
AStringVector GetAllPlayers(void);
/** Returns the names of all defined ranks. */
AStringVector GetAllRanks(void);
@ -210,6 +217,9 @@ public:
/** Returns the name of the default rank. */
const AString & GetDefaultRank(void) const { return m_DefaultRank; }
/** Updates the playername that is saved with this uuid. Returns false if a error occurred */
bool UpdatePlayerName(const AString & a_PlayerUUID, const AString & a_NewPlayerName);
protected: