Merge pull request #1472 from mc-server/Ranks
Added more cRankManager functions
This commit is contained in:
commit
d3c2c02b6b
@ -2034,8 +2034,10 @@ cPluginManager.AddHook(cPluginManager.HOOK_CHAT, OnChatMessage);
|
||||
AddGroupToRank = { Params = "GroupName, RankName", Return = "bool", Notes = "Adds the specified group to the specified rank. Returns true on success, false on failure - if the group name or the rank name is not found." },
|
||||
AddPermissionToGroup = { Params = "Permission, GroupName", Return = "bool", Notes = "Adds the specified permission to the specified group. Returns true on success, false on failure - if the group name is not found." },
|
||||
AddRank = { Params = "RankName, MsgPrefix, MsgSuffix, MsgNameColorCode", Return = "", Notes = "Adds a new rank of the specified name and with the specified message visuals. Logs an info message and does nothing if the rank already exists." },
|
||||
ClearPlayerRanks = { Params = "", Return = "", Notes = "Removes all player ranks from the database. Note that this doesn't change the cPlayer instances for the already connected players, you need to update all the instances manually." },
|
||||
GetAllGroups = { Params = "", Return = "array-table of groups' names", Notes = "Returns an array-table containing the names of all the groups that are known to the manager." },
|
||||
GetAllPermissions = { Params = "", Return = "array-table of permissions", Notes = "Returns an array-table containing all the permissions that are known to the manager." },
|
||||
GetAllPlayers = { Params = "", Return = "array-table of playernames", Notes = "Returns the short uuids of all defined players." },
|
||||
GetAllRanks = { Params = "", Return = "array-table of ranks' names", Notes = "Returns an array-table containing the names of all the ranks that are known to the manager." },
|
||||
GetDefaultRank = { Params = "", Return = "string", Notes = "Returns the name of the default rank. " },
|
||||
GetGroupPermissions = { Params = "GroupName", Return = "array-table of permissions", Notes = "Returns an array-table containing the permissions that the specified group contains." },
|
||||
@ -2043,6 +2045,7 @@ cPluginManager.AddHook(cPluginManager.HOOK_CHAT, OnChatMessage);
|
||||
GetPlayerMsgVisuals = { Params = "PlayerUUID", Return = "MsgPrefix, MsgSuffix, MsgNameColorCode", Notes = "Returns the message visuals assigned to the player. If the player is not explicitly assigned a rank, the default rank's visuals are returned. If there is an error, no value is returned at all." },
|
||||
GetPlayerPermissions = { Params = "PlayerUUID", Return = "array-table of permissions", Notes = "Returns the permissions that the specified player is assigned through their rank. Returns the default rank's permissions if the player has no explicit rank assigned to them. Returns an empty array on error." },
|
||||
GetPlayerRankName = { Params = "PlayerUUID", Return = "RankName", Notes = "Returns the name of the rank that is assigned to the specified player. An empty string (NOT the default rank) is returned if the player has no rank assigned to them." },
|
||||
GetPlayerName = { Params = "PlayerUUID", Return = "PlayerName", Notes = "Returns the last name that the specified player has, for a player in the ranks database. An empty string is returned if the player isn't in the database." },
|
||||
GetRankGroups = { Params = "RankName", Return = "array-table of groups' names", Notes = "Returns an array-table of the names of all the groups that are assigned to the specified rank. Returns an empty table if there is no such rank." },
|
||||
GetRankPermissions = { Params = "RankName", Return = "array-table of permissions", Notes = "Returns an array-table of all the permissions that are assigned to the specified rank through its groups. Returns an empty table if there is no such rank." },
|
||||
GetRankVisuals = { Params = "RankName", Return = "MsgPrefix, MsgSuffix, MsgNameColorCode", Notes = "Returns the message visuals for the specified rank. Returns no value if the specified rank does not exist." },
|
||||
|
@ -129,6 +129,27 @@ static int tolua_cRankManager_AddRank(lua_State * L)
|
||||
|
||||
|
||||
|
||||
/** Binds cRankManager::ClearPlayerRanks */
|
||||
static int tolua_cRankManager_ClearPlayerRanks(lua_State * L)
|
||||
{
|
||||
cLuaState S(L);
|
||||
if (
|
||||
!S.CheckParamUserTable(1, "cRankManager") ||
|
||||
!S.CheckParamEnd(2)
|
||||
)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Remove all players:
|
||||
cRoot::Get()->GetRankManager().ClearPlayerRanks();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** Binds cRankManager::GetAllGroups */
|
||||
static int tolua_cRankManager_GetAllGroups(lua_State * L)
|
||||
{
|
||||
@ -183,6 +204,33 @@ static int tolua_cRankManager_GetAllPermissions(lua_State * L)
|
||||
|
||||
|
||||
|
||||
/** Binds cRankManager::GetAllPlayerUUIDs */
|
||||
static int tolua_cRankManager_GetAllPlayerUUIDs(lua_State * L)
|
||||
{
|
||||
// Function signature:
|
||||
// cRankManager:GetAllPlayerUUIDs() -> 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().GetAllPlayerUUIDs();
|
||||
|
||||
// Push the results:
|
||||
S.Push(Players);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** Binds cRankManager::GetAllRanks */
|
||||
static int tolua_cRankManager_GetAllRanks(lua_State * L)
|
||||
{
|
||||
@ -400,6 +448,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)
|
||||
{
|
||||
@ -895,7 +975,7 @@ static int tolua_cRankManager_SetDefaultRank(lua_State * L)
|
||||
|
||||
// Set the rank, return the result:
|
||||
S.Push(cRoot::Get()->GetRankManager().SetDefaultRank(RankName));
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -972,8 +1052,10 @@ void ManualBindings::BindRankManager(lua_State * tolua_S)
|
||||
tolua_function(tolua_S, "AddGroupToRank", tolua_cRankManager_AddGroupToRank);
|
||||
tolua_function(tolua_S, "AddPermissionToGroup", tolua_cRankManager_AddPermissionToGroup);
|
||||
tolua_function(tolua_S, "AddRank", tolua_cRankManager_AddRank);
|
||||
tolua_function(tolua_S, "ClearPlayerRanks", tolua_cRankManager_ClearPlayerRanks);
|
||||
tolua_function(tolua_S, "GetAllGroups", tolua_cRankManager_GetAllGroups);
|
||||
tolua_function(tolua_S, "GetAllPermissions", tolua_cRankManager_GetAllPermissions);
|
||||
tolua_function(tolua_S, "GetAllPlayerUUIDs", tolua_cRankManager_GetAllPlayerUUIDs);
|
||||
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 +1063,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);
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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::GetAllPlayerUUIDs(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,53 @@ bool cRankManager::SetDefaultRank(const AString & a_RankName)
|
||||
|
||||
|
||||
|
||||
void cRankManager::ClearPlayerRanks(void)
|
||||
{
|
||||
ASSERT(m_IsInitialized);
|
||||
cCSLock Lock(m_CS);
|
||||
|
||||
try {
|
||||
SQLite::Statement stmt(m_DB, "DELETE FROM PlayerRank");
|
||||
stmt.exec();
|
||||
}
|
||||
catch (SQLite::Exception & ex)
|
||||
{
|
||||
LOGWARNING("%s: Failed to remove/clear all players: %s", __FUNCTION__, ex.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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 (
|
||||
|
@ -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.
|
||||
An empty string is returned if the player isn't in the database. */
|
||||
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 GetAllPlayerUUIDs(void);
|
||||
|
||||
/** Returns the names of all defined ranks. */
|
||||
AStringVector GetAllRanks(void);
|
||||
|
||||
@ -211,6 +218,13 @@ public:
|
||||
/** Returns the name of the default rank. */
|
||||
const AString & GetDefaultRank(void) const { return m_DefaultRank; }
|
||||
|
||||
/** Removes all player ranks from the database. Note that this doesn't change the cPlayer instances
|
||||
for the already connected players, you need to update all the instances manually. */
|
||||
void ClearPlayerRanks(void);
|
||||
|
||||
/** 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:
|
||||
|
||||
/** The database storage for all the data. Protected by m_CS. */
|
||||
|
Loading…
Reference in New Issue
Block a user