From 40e15c5ad517a6f860b39d1b225596424c3d2506 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 5 Aug 2014 18:37:00 +0200 Subject: RankMgr: Initial interface declaration. --- src/RankManager.cpp | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 src/RankManager.cpp (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp new file mode 100644 index 000000000..954edd099 --- /dev/null +++ b/src/RankManager.cpp @@ -0,0 +1,153 @@ + +// RankManager.cpp + +// Implements the cRankManager class that represents the rank manager responsible for assigning permissions and message visuals to players + +#include "Globals.h" +#include "RankManager.h" +#include "Protocol/MojangAPI.h" +#include "inifile/iniFile.h" +#include + + + + + +/* +// This code is for internal testing while developing the cRankManager class +static class cRankMgrTest +{ +public: + cRankMgrTest(void) : + m_Mgr() + { + // Initialize the cMojangAPI so that it can convert playernames to UUIDs: + cIniFile Ini; + Ini.ReadFile("settings.ini"); + m_API.Start(Ini); + + // Test the cRankManager class: + ReportPlayer("xoft"); + } + + void ReportPlayer(const AString & a_PlayerName) + { + // Get the player's UUID and rank: + AString UUID = m_API.GetUUIDFromPlayerName(a_PlayerName); + std::cout << "Player " << a_PlayerName << " has UUID '" << UUID <<"'." << std::endl; + std::cout << " Rank: '" << m_Mgr.GetPlayerRankName(UUID) << "'." << std::endl; + + // List all the permission groups for the player: + AStringVector Groups = m_Mgr.GetPlayerPermissionGroups(UUID); + std::cout << " Groups(" << Groups.size() << "):" << std::endl; + for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) + { + std::cout << " '" << *itr << "'." << std::endl; + } // for itr - Groups[] + + // List all the permissions for the player: + AStringVector Permissions = m_Mgr.GetPlayerPermissions(UUID); + std::cout << " Permissions(" << Permissions.size() << "):" << std::endl; + for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) + { + std::cout << " '" << *itr << "'." << std::endl; + } // for itr - Groups[] + + std::cout << "Done." << std::endl; + } + +protected: + cRankManager m_Mgr; + cMojangAPI m_API; +} g_RankMgrTest; +//*/ + + + + + +cRankManager::cRankManager(void) : + m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) +{ + // Create the DB tables, if they don't exist: + m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID, Name, MsgPrefix, MsgPostfix, MsgNameColorCode)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionGroup (GroupID, Name)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermissionGroups (RankID, GroupID)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (GroupID, Permission)"); + + // TODO: Check if tables empty, add some defaults then +} + + + + + +AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) +{ + SQLite::Statement stmt(m_DB, "SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?"); + stmt.bind(1, a_PlayerUUID); + stmt.executeStep(); + if (stmt.isDone()) + { + // No data returned from the DB + return AString(); + } + return stmt.getColumn(0).getText(); +} + + + + + +AStringVector cRankManager::GetPlayerPermissionGroups(const AString & a_PlayerUUID) +{ + // Prepare the DB statement: + SQLite::Statement stmt(m_DB, + "SELECT PermissionGroup.Name FROM PermissionGroup " + "LEFT JOIN RankPermissionGroups " + "ON PermissionGroup.GroupID = RankPermissionGroups.GroupID " + "LEFT JOIN PlayerRank " + "ON PlayerRank.RankID = RankPermissionGroups.RankID " + "WHERE PlayerRank.PlayerUUID = ?" + ); + stmt.bind(1, a_PlayerUUID); + + // Execute and get results: + AStringVector res; + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + return res; +} + + + + + +AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) +{ + // Prepare the DB statement: + SQLite::Statement stmt(m_DB, + "SELECT PermissionItem.Permission FROM PermissionItem " + "LEFT JOIN RankPermissionGroups " + "ON PermissionItem.GroupID = RankPermissionGroups.GroupID " + "LEFT JOIN PlayerRank " + "ON PlayerRank.RankID = RankPermissionGroups.RankID " + "WHERE PlayerRank.PlayerUUID = ?" + ); + stmt.bind(1, a_PlayerUUID); + + // Execute and get results: + AStringVector res; + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + return res; +} + + + + -- cgit v1.2.3 From a717a7e712e6be1e6980cade95f09b5b85bdfe67 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 5 Aug 2014 22:32:26 +0200 Subject: RankMgr: Added SQL integer datatypes. --- src/RankManager.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 954edd099..18ed65b26 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -70,11 +70,11 @@ cRankManager::cRankManager(void) : m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) { // Create the DB tables, if they don't exist: - m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID, Name, MsgPrefix, MsgPostfix, MsgNameColorCode)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionGroup (GroupID, Name)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermissionGroups (RankID, GroupID)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (GroupID, Permission)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgPostfix, MsgNameColorCode)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionGroup (GroupID INTEGER PRIMARY KEY, Name)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermissionGroups (RankID INTEGER, GroupID INTEGER)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (GroupID INTEGER, Permission)"); // TODO: Check if tables empty, add some defaults then } -- cgit v1.2.3 From 670e94bfeb62a51ca64ffaa0e45086e1ca91057c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Aug 2014 09:56:28 +0200 Subject: RankMgr: Renamed PermissionGroup to Group in API and PermGroup in DB. "Group" is SQL keyword and shouldn't be used as table name. --- src/RankManager.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 18ed65b26..e77fb22b9 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -13,7 +13,7 @@ -/* +//* // This code is for internal testing while developing the cRankManager class static class cRankMgrTest { @@ -38,7 +38,7 @@ public: std::cout << " Rank: '" << m_Mgr.GetPlayerRankName(UUID) << "'." << std::endl; // List all the permission groups for the player: - AStringVector Groups = m_Mgr.GetPlayerPermissionGroups(UUID); + AStringVector Groups = m_Mgr.GetPlayerGroups(UUID); std::cout << " Groups(" << Groups.size() << "):" << std::endl; for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) { @@ -72,9 +72,9 @@ cRankManager::cRankManager(void) : // Create the DB tables, if they don't exist: m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgPostfix, MsgNameColorCode)"); m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionGroup (GroupID INTEGER PRIMARY KEY, Name)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermissionGroups (RankID INTEGER, GroupID INTEGER)"); - m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (GroupID INTEGER, Permission)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PermGroup (PermGroupID INTEGER PRIMARY KEY, Name)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (PermGroupID INTEGER, Permission)"); // TODO: Check if tables empty, add some defaults then } @@ -100,15 +100,15 @@ AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) -AStringVector cRankManager::GetPlayerPermissionGroups(const AString & a_PlayerUUID) +AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) { // Prepare the DB statement: SQLite::Statement stmt(m_DB, - "SELECT PermissionGroup.Name FROM PermissionGroup " - "LEFT JOIN RankPermissionGroups " - "ON PermissionGroup.GroupID = RankPermissionGroups.GroupID " + "SELECT Group.Name FROM Group " + "LEFT JOIN RankGroups " + "ON Group.GroupID = RankGroups.GroupID " "LEFT JOIN PlayerRank " - "ON PlayerRank.RankID = RankPermissionGroups.RankID " + "ON PlayerRank.RankID = RankGroups.RankID " "WHERE PlayerRank.PlayerUUID = ?" ); stmt.bind(1, a_PlayerUUID); @@ -131,10 +131,10 @@ AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) // Prepare the DB statement: SQLite::Statement stmt(m_DB, "SELECT PermissionItem.Permission FROM PermissionItem " - "LEFT JOIN RankPermissionGroups " - "ON PermissionItem.GroupID = RankPermissionGroups.GroupID " + "LEFT JOIN RankGroups " + "ON PermissionItem.GroupID = RankGroups.GroupID " "LEFT JOIN PlayerRank " - "ON PlayerRank.RankID = RankPermissionGroups.RankID " + "ON PlayerRank.RankID = RankGroups.RankID " "WHERE PlayerRank.PlayerUUID = ?" ); stmt.bind(1, a_PlayerUUID); -- cgit v1.2.3 From 65b81b4ab7f580c98ca80b8e57a13de961f19131 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Aug 2014 10:02:25 +0200 Subject: RankMgr: Implemented the basic API functions. --- src/RankManager.cpp | 738 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 679 insertions(+), 59 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index e77fb22b9..e02e63a24 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -5,15 +5,13 @@ #include "Globals.h" #include "RankManager.h" -#include "Protocol/MojangAPI.h" -#include "inifile/iniFile.h" -#include +#include "SQLiteCpp/Transaction.h" -//* +/* // This code is for internal testing while developing the cRankManager class static class cRankMgrTest { @@ -21,44 +19,72 @@ public: cRankMgrTest(void) : m_Mgr() { - // Initialize the cMojangAPI so that it can convert playernames to UUIDs: - cIniFile Ini; - Ini.ReadFile("settings.ini"); - m_API.Start(Ini); + // Initialize logging: + new cMCLogger(); + AString UUID = "b1caf24202a841a78055a079c460eee7"; // UUID for "xoft" + LOG("Testing UUID %s", UUID.c_str()); - // Test the cRankManager class: - ReportPlayer("xoft"); + // Test the initial state of the ranks: + LOG("Initial test:"); + ReportPlayer(UUID); + + // Add a rank, a few groups and permissions and set the player to use them: + LOG("Adding data..."); + m_Mgr.AddRank("TestRank", "[test]", "[/test]", "7"); + m_Mgr.AddGroup("TestGroup1"); + m_Mgr.AddGroup("TestGroup2"); + m_Mgr.AddGroupToRank("TestGroup1", "TestRank"); + m_Mgr.AddGroupToRank("TestGroup2", "TestRank"); + m_Mgr.AddPermissionToGroup("testpermission1.1", "TestGroup1"); + m_Mgr.AddPermissionToGroup("testpermission1.2", "TestGroup1"); + m_Mgr.AddPermissionToGroup("testpermission2.1", "TestGroup2"); + m_Mgr.SetPlayerRank(UUID, "TestRank"); + + // Test the added data: + LOG("Testing the added data:"); + LOG("IsGroupInRank(TestGroup1, TestRank) = %s", m_Mgr.IsGroupInRank("TestGroup1", "TestRank") ? "true" : "false"); + LOG("IsGroupInRank(TestGroup3, TestRank) = %s", m_Mgr.IsGroupInRank("TestGroup3", "TestRank") ? "true" : "false"); + LOG("IsPermissionInGroup(testpermission1.2, TestGroup1) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup1") ? "true" : "false"); + LOG("IsPermissionInGroup(testpermission1.2, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup2") ? "true" : "false"); + LOG("IsPermissionInGroup(testpermission1.3, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.3", "TestGroup2") ? "true" : "false"); + LOG("IsPlayerRankSet(%s) = %s", UUID.c_str(), m_Mgr.IsPlayerRankSet(UUID) ? "true" : "false"); + LOG("IsPlayerRankSet(%s1) = %s", UUID.c_str(), m_Mgr.IsPlayerRankSet(UUID + "1") ? "true" : "false"); + LOG("GroupExists(TestGroup1) = %s", m_Mgr.GroupExists("TestGroup1") ? "true" : "false"); + LOG("GroupExists(TestGroup3) = %s", m_Mgr.GroupExists("TestGroup3") ? "true" : "false"); + LOG("RankExists(TestRank) = %s", m_Mgr.RankExists("TestRank") ? "true" : "false"); + LOG("RankExists(NonexistentRank) = %s", m_Mgr.RankExists("NonexistentRank") ? "true" : "false"); + + // Test the assignments above: + LOG("After-assignment test:"); + ReportPlayer(UUID); + + LOG("Done."); } - void ReportPlayer(const AString & a_PlayerName) + void ReportPlayer(const AString & a_PlayerUUID) { // Get the player's UUID and rank: - AString UUID = m_API.GetUUIDFromPlayerName(a_PlayerName); - std::cout << "Player " << a_PlayerName << " has UUID '" << UUID <<"'." << std::endl; - std::cout << " Rank: '" << m_Mgr.GetPlayerRankName(UUID) << "'." << std::endl; + LOG(" Rank: '%s'", m_Mgr.GetPlayerRankName(a_PlayerUUID).c_str()); // List all the permission groups for the player: - AStringVector Groups = m_Mgr.GetPlayerGroups(UUID); - std::cout << " Groups(" << Groups.size() << "):" << std::endl; + AStringVector Groups = m_Mgr.GetPlayerGroups(a_PlayerUUID); + LOG(" Groups(%u):", (unsigned)Groups.size()); for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) { - std::cout << " '" << *itr << "'." << std::endl; + LOG(" '%s'" , itr->c_str()); } // for itr - Groups[] // List all the permissions for the player: - AStringVector Permissions = m_Mgr.GetPlayerPermissions(UUID); - std::cout << " Permissions(" << Permissions.size() << "):" << std::endl; + AStringVector Permissions = m_Mgr.GetPlayerPermissions(a_PlayerUUID); + LOG(" Permissions(%u):", (unsigned)Permissions.size()); for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) { - std::cout << " '" << *itr << "'." << std::endl; + LOG(" '%s'", itr->c_str()); } // for itr - Groups[] - - std::cout << "Done." << std::endl; } protected: cRankManager m_Mgr; - cMojangAPI m_API; } g_RankMgrTest; //*/ @@ -70,7 +96,7 @@ cRankManager::cRankManager(void) : m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) { // Create the DB tables, if they don't exist: - m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgPostfix, MsgNameColorCode)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgSuffix, MsgNameColorCode)"); m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)"); m_DB.exec("CREATE TABLE IF NOT EXISTS PermGroup (PermGroupID INTEGER PRIMARY KEY, Name)"); m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)"); @@ -85,15 +111,23 @@ cRankManager::cRankManager(void) : AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) { - SQLite::Statement stmt(m_DB, "SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?"); - stmt.bind(1, a_PlayerUUID); - stmt.executeStep(); - if (stmt.isDone()) + try { - // No data returned from the DB - return AString(); + SQLite::Statement stmt(m_DB, "SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?"); + stmt.bind(1, a_PlayerUUID); + stmt.executeStep(); + if (stmt.isDone()) + { + // No data returned from the DB + return AString(); + } + return stmt.getColumn(0).getText(); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Cannot get player rank name: %s", __FUNCTION__, ex.what()); } - return stmt.getColumn(0).getText(); + return AString(); } @@ -102,22 +136,29 @@ AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) { - // Prepare the DB statement: - SQLite::Statement stmt(m_DB, - "SELECT Group.Name FROM Group " - "LEFT JOIN RankGroups " - "ON Group.GroupID = RankGroups.GroupID " - "LEFT JOIN PlayerRank " - "ON PlayerRank.RankID = RankGroups.RankID " - "WHERE PlayerRank.PlayerUUID = ?" - ); - stmt.bind(1, a_PlayerUUID); - - // Execute and get results: AStringVector res; - while (stmt.executeStep()) + try { - res.push_back(stmt.getColumn(0).getText()); + // Prepare the DB statement: + SQLite::Statement stmt(m_DB, + "SELECT PermGroup.Name FROM PermGroup " + "LEFT JOIN RankPermGroup " + "ON PermGroup.PermGroupID = RankPermGroup.PermGroupID " + "LEFT JOIN PlayerRank " + "ON PlayerRank.RankID = RankPermGroup.RankID " + "WHERE PlayerRank.PlayerUUID = ?" + ); + stmt.bind(1, a_PlayerUUID); + + // Execute and get results: + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Cannot get player groups: %s", __FUNCTION__, ex.what()); } return res; } @@ -128,22 +169,29 @@ AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) { - // Prepare the DB statement: - SQLite::Statement stmt(m_DB, - "SELECT PermissionItem.Permission FROM PermissionItem " - "LEFT JOIN RankGroups " - "ON PermissionItem.GroupID = RankGroups.GroupID " - "LEFT JOIN PlayerRank " - "ON PlayerRank.RankID = RankGroups.RankID " - "WHERE PlayerRank.PlayerUUID = ?" - ); - stmt.bind(1, a_PlayerUUID); - - // Execute and get results: AStringVector res; - while (stmt.executeStep()) + try + { + // Prepare the DB statement: + SQLite::Statement stmt(m_DB, + "SELECT PermissionItem.Permission FROM PermissionItem " + "LEFT JOIN RankPermGroup " + "ON PermissionItem.PermGroupID = RankPermGroup.PermGroupID " + "LEFT JOIN PlayerRank " + "ON PlayerRank.RankID = RankPermGroup.RankID " + "WHERE PlayerRank.PlayerUUID = ?" + ); + stmt.bind(1, a_PlayerUUID); + + // Execute and get results: + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) { - res.push_back(stmt.getColumn(0).getText()); + LOGWARNING("%s: Cannot get player permissions: %s", __FUNCTION__, ex.what()); } return res; } @@ -151,3 +199,575 @@ AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) + +AStringVector cRankManager::GetRankGroups(const AString & a_RankName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + res.push_back(Printf("%s: DummyGroup", __FUNCTION__)); + return res; +} + + + + + +AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + res.push_back(Printf("%s: DummyPermission", __FUNCTION__)); + return res; +} + + + + + +AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + res.push_back(Printf("%s: DummyPermission", __FUNCTION__)); + return res; +} + + + + + +AStringVector cRankManager::GetAllRanks(void) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + res.push_back(Printf("%s: DummyRank", __FUNCTION__)); + return res; +} + + + + + +AStringVector cRankManager::GetAllGroups(void) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + res.push_back(Printf("%s: DummyGroup", __FUNCTION__)); + return res; +} + + + + + +AStringVector cRankManager::GetAllPermissions(void) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + res.push_back(Printf("%s: DummyPermission", __FUNCTION__)); + return res; +} + + + + + +void cRankManager::GetPlayerMsgVisuals( + const AString & a_PlayerUUID, + AString & a_MsgPrefix, + AString & a_MsgSuffix, + AString & a_MsgNameColorCode +) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + a_MsgPrefix = Printf("%s: DummyPrefix", __FUNCTION__); + a_MsgSuffix = Printf("%s: DummySuffix", __FUNCTION__); + a_MsgNameColorCode = Printf("%s: DummyMsgNameColorCode", __FUNCTION__); +} + + + + + +void cRankManager::AddRank( + const AString & a_RankName, + const AString & a_MsgPrefix, + const AString & a_MsgSuffix, + const AString & a_MsgNameColorCode +) +{ + try + { + // Check if such a rank name is already used: + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (stmt.executeStep()) + { + if (stmt.getColumn(0).getInt() > 0) + { + // Rank already exists, do nothing: + return; + } + } + } + + // Insert a new rank: + SQLite::Statement stmt(m_DB, "INSERT INTO Rank (Name, MsgPrefix, MsgSuffix, MsgNameColorCode) VALUES (?, ?, ?, ?)"); + stmt.bind(1, a_RankName); + stmt.bind(2, a_MsgPrefix); + stmt.bind(3, a_MsgSuffix); + stmt.bind(4, a_MsgNameColorCode); + if (stmt.exec() <= 0) + { + LOGWARNING("%s: Failed to add a new rank \"%s\".", __FUNCTION__, a_RankName.c_str()); + return; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to add a new rank \"%s\": %s", __FUNCTION__, a_RankName.c_str(), ex.what()); + } +} + + + + + +void cRankManager::AddGroup(const AString & a_GroupName) +{ + try + { + // Check if such a rank name is already used: + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (stmt.executeStep()) + { + if (stmt.getColumn(0).getInt() > 0) + { + // Group already exists, do nothing: + return; + } + } + } + + // Insert a new rank: + SQLite::Statement stmt(m_DB, "INSERT INTO PermGroup (Name) VALUES (?)"); + stmt.bind(1, a_GroupName); + if (stmt.exec() <= 0) + { + LOGWARNING("%s: Failed to add a new group \"%s\".", __FUNCTION__, a_GroupName.c_str()); + return; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to add a new group \"%s\": %s", __FUNCTION__, a_GroupName.c_str(), ex.what()); + } +} + + + + + +bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a_RankName) +{ + try + { + SQLite::Transaction trans(m_DB); + int GroupID, RankID; + + // Get the group's ID: + { + SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (!stmt.executeStep()) + { + LOGWARNING("%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str()); + return false; + } + GroupID = stmt.getColumn(0); + } + + // Get the rank's ID: + { + SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (!stmt.executeStep()) + { + LOGWARNING("%s: No such rank (%s), aborting.", __FUNCTION__, a_RankName.c_str()); + return false; + } + RankID = stmt.getColumn(0); + } + + // Check if the group is already there: + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM RankPermGroup WHERE RankID = ? AND PermGroupID = ?"); + stmt.bind(1, RankID); + stmt.bind(2, GroupID); + if (!stmt.executeStep()) + { + LOGWARNING("%s: Failed to check binding between rank %s and group %s, aborting.", __FUNCTION__, a_RankName.c_str(), a_GroupName.c_str()); + return false; + } + if (stmt.getColumn(0).getInt() > 0) + { + LOGD("%s: Group %s already present in rank %s, skipping and returning success.", + __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str() + ); + return true; + } + } + + // Add the group: + { + SQLite::Statement stmt(m_DB, "INSERT INTO RankPermGroup (RankID, PermGroupID) VALUES (?, ?)"); + stmt.bind(1, RankID); + stmt.bind(2, GroupID); + if (stmt.exec() <= 0) + { + LOGWARNING("%s: Failed to add group %s to rank %s, aborting.", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str()); + return false; + } + } + + // Adding succeeded: + trans.commit(); + return true; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to add group %s to rank %s: %s", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str(), ex.what()); + } + return false; +} + + + + + +bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AString & a_GroupName) +{ + try + { + // Wrapp the entire operation into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the group's ID: + int GroupID; + { + SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (!stmt.executeStep()) + { + LOGWARNING("%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str()); + return false; + } + GroupID = stmt.getColumn(0).getInt(); + } + + // Check if the permission is already present: + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?"); + stmt.bind(1, GroupID); + stmt.bind(2, a_Permission); + if (!stmt.executeStep()) + { + LOGWARNING("%s: Failed to check binding between permission %s and group %s, aborting.", __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str()); + return false; + } + if (stmt.getColumn(0).getInt() > 0) + { + LOGD("%s: Permission %s is already present in group %s, skipping and returning success.", + __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str() + ); + return true; + } + } + + // Add the permission: + { + SQLite::Statement stmt(m_DB, "INSERT INTO PermissionItem (Permission, PermGroupID) VALUES (?, ?)"); + stmt.bind(1, a_Permission); + stmt.bind(2, GroupID); + if (stmt.exec() <= 0) + { + LOGWARNING("%s: Failed to add permission %s to group %s, aborting.", __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str()); + return false; + } + } + + // Adding succeeded: + trans.commit(); + return true; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to add permission %s to group %s: %s", + __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str(), ex.what() + ); + } + return false; +} + + + + + +void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_ReplacementRankName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); +} + + + + + +void cRankManager::RemoveGroup(const AString & a_GroupName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); +} + + + + + +void cRankManager::RemoveGroupFromRank(const AString & a_RankName, const AString & a_GroupName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); +} + + + + + +void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const AString & a_GroupName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); +} + + + + + +bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + return false; +} + + + + + +bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewName) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); + return false; +} + + + + + +void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a_RankName) +{ + try + { + // Wrap the entire operation into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the rank ID: + int RankID; + { + SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (!stmt.executeStep()) + { + LOGWARNING("%s: There is no rank %s, aborting.", __FUNCTION__, a_RankName.c_str()); + return; + } + RankID = stmt.getColumn(0).getInt(); + } + + // Update the player's rank, if already in DB: + { + SQLite::Statement stmt(m_DB, "UPDATE PlayerRank SET RankID = ? WHERE PlayerUUID = ?"); + stmt.bind(1, RankID); + stmt.bind(2, a_PlayerUUID); + if (stmt.exec() > 0) + { + // Successfully updated the player's rank + trans.commit(); + return; + } + } + + // The player is not yet in the DB, add them: + SQLite::Statement stmt(m_DB, "INSERT INTO PlayerRank (RankID, PlayerUUID) VALUES (?, ?)"); + stmt.bind(1, RankID); + stmt.bind(2, a_PlayerUUID); + if (stmt.exec() > 0) + { + // Successfully added the player + trans.commit(); + return; + } + + LOGWARNING("%s: Failed to set player UUID %s to rank %s.", + __FUNCTION__, a_PlayerUUID.c_str(), a_RankName.c_str() + ); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to set player UUID %s to rank %s: %s", + __FUNCTION__, a_PlayerUUID.c_str(), a_RankName.c_str(), ex.what() + ); + } +} + + + + + +void cRankManager::SetRankVisuals( + const AString & a_RankName, + const AString & a_MsgPrefix, + const AString & a_MsgSuffix, + const AString & a_MsgNameColorCode +) +{ + LOGWARNING("%s: Not implemented yet", __FUNCTION__); +} + + + + + +bool cRankManager::RankExists(const AString & a_RankName) +{ + try + { + SQLite::Statement stmt(m_DB, "SELECT * FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (stmt.executeStep()) + { + // The rank was found + return true; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to query DB for rank %s: %s", __FUNCTION__, a_RankName.c_str(), ex.what()); + } + return false; +} + + + + + +bool cRankManager::GroupExists(const AString & a_GroupName) +{ + try + { + SQLite::Statement stmt(m_DB, "SELECT * FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (stmt.executeStep()) + { + // The group was found + return true; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to query DB for group %s: %s", __FUNCTION__, a_GroupName.c_str(), ex.what()); + } + return false; +} + + + + + +bool cRankManager::IsPlayerRankSet(const AString & a_PlayerUUID) +{ + try + { + SQLite::Statement stmt(m_DB, "SELECT * FROM PlayerRank WHERE PlayerUUID = ?"); + stmt.bind(1, a_PlayerUUID); + if (stmt.executeStep()) + { + // The player UUID was found, they have a rank + return true; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to query DB for player UUID %s: %s", __FUNCTION__, a_PlayerUUID.c_str(), ex.what()); + } + return false; +} + + + + + +bool cRankManager::IsGroupInRank(const AString & a_GroupName, const AString & a_RankName) +{ + try + { + SQLite::Statement stmt(m_DB, + "SELECT * FROM Rank " + "LEFT JOIN RankPermGroup ON Rank.RankID = RankPermGroup.RankID " + "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID " + "WHERE Rank.Name = ? AND PermGroup.Name = ?" + ); + stmt.bind(1, a_RankName); + stmt.bind(2, a_GroupName); + if (stmt.executeStep()) + { + // The group is in the rank + return true; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to query DB: %s", __FUNCTION__, ex.what()); + } + return false; +} + + + + + +bool cRankManager::IsPermissionInGroup(const AString & a_Permission, const AString & a_GroupName) +{ + try + { + SQLite::Statement stmt(m_DB, + "SELECT * FROM PermissionItem " + "LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID " + "WHERE PermissionItem.Permission = ? AND PermGroup.Name = ?" + ); + stmt.bind(1, a_Permission); + stmt.bind(2, a_GroupName); + if (stmt.executeStep()) + { + // The permission is in the group + return true; + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to query DB: %s", __FUNCTION__, ex.what()); + } + return false; +} + + + + -- cgit v1.2.3 From a5b35e09ce1e352d4c57e66750a72335fc60d4b2 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Aug 2014 16:38:38 +0200 Subject: RankMgr: Implemented GetXforY and GetAll APIs. --- src/RankManager.cpp | 171 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 156 insertions(+), 15 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index e02e63a24..ec9ac1b1d 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -11,7 +11,7 @@ -/* +//* // This code is for internal testing while developing the cRankManager class static class cRankMgrTest { @@ -53,6 +53,13 @@ public: LOG("GroupExists(TestGroup3) = %s", m_Mgr.GroupExists("TestGroup3") ? "true" : "false"); LOG("RankExists(TestRank) = %s", m_Mgr.RankExists("TestRank") ? "true" : "false"); LOG("RankExists(NonexistentRank) = %s", m_Mgr.RankExists("NonexistentRank") ? "true" : "false"); + ReportRankGroups("TestRank"); + ReportRankGroups("NonexistentRank"); + ReportGroupPermissions("TestGroup1"); + ReportGroupPermissions("NonexistentGroup"); + + // Report the contents of the DB: + ReportAll(); // Test the assignments above: LOG("After-assignment test:"); @@ -61,6 +68,35 @@ public: LOG("Done."); } + + void ReportAll(void) + { + // Report all ranks: + AStringVector Ranks = m_Mgr.GetAllRanks(); + LOG("All ranks (%u):", (unsigned)Ranks.size()); + for (AStringVector::const_iterator itr = Ranks.begin(), end = Ranks.end(); itr != end; ++itr) + { + LOG(" '%s'", itr->c_str()); + } + + // Report all groups: + AStringVector Groups = m_Mgr.GetAllGroups(); + LOG("All groups (%u):", (unsigned)Groups.size()); + for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) + { + LOG(" '%s'", itr->c_str()); + } + + // Report all permissions: + AStringVector Permissions = m_Mgr.GetAllPermissions(); + LOG("All permissions (%u):", (unsigned)Permissions.size()); + for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) + { + LOG(" '%s'", itr->c_str()); + } + } + + void ReportPlayer(const AString & a_PlayerUUID) { // Get the player's UUID and rank: @@ -68,7 +104,7 @@ public: // List all the permission groups for the player: AStringVector Groups = m_Mgr.GetPlayerGroups(a_PlayerUUID); - LOG(" Groups(%u):", (unsigned)Groups.size()); + LOG(" Groups (%u):", (unsigned)Groups.size()); for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) { LOG(" '%s'" , itr->c_str()); @@ -76,13 +112,41 @@ public: // List all the permissions for the player: AStringVector Permissions = m_Mgr.GetPlayerPermissions(a_PlayerUUID); - LOG(" Permissions(%u):", (unsigned)Permissions.size()); + LOG(" Permissions (%u):", (unsigned)Permissions.size()); for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) { LOG(" '%s'", itr->c_str()); } // for itr - Groups[] } + + void ReportRankGroups(const AString & a_RankName) + { + AStringVector Groups = m_Mgr.GetRankGroups(a_RankName); + LOG("Groups in rank %s: %u", a_RankName.c_str(), (unsigned)Groups.size()); + for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) + { + LOG(" '%s'", itr->c_str()); + } + AStringVector Permissions = m_Mgr.GetRankPermissions(a_RankName); + LOG("Permissions in rank %s: %u", a_RankName.c_str(), (unsigned)Permissions.size()); + for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) + { + LOG(" '%s'", itr->c_str()); + } + } + + + void ReportGroupPermissions(const AString & a_GroupName) + { + AStringVector Permissions = m_Mgr.GetGroupPermissions(a_GroupName); + LOG("Permissions in group %s: %u", a_GroupName.c_str(), (unsigned)Permissions.size()); + for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) + { + LOG(" '%s'", itr->c_str()); + } + } + protected: cRankManager m_Mgr; } g_RankMgrTest; @@ -202,9 +266,25 @@ AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) AStringVector cRankManager::GetRankGroups(const AString & a_RankName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); AStringVector res; - res.push_back(Printf("%s: DummyGroup", __FUNCTION__)); + try + { + SQLite::Statement stmt(m_DB, + "SELECT PermGroup.Name FROM PermGroup " + "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID " + "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID " + "WHERE Rank.Name = ?" + ); + stmt.bind(1, a_RankName); + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get rank groups from DB: %s", __FUNCTION__, ex.what()); + } return res; } @@ -214,9 +294,24 @@ AStringVector cRankManager::GetRankGroups(const AString & a_RankName) AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); AStringVector res; - res.push_back(Printf("%s: DummyPermission", __FUNCTION__)); + try + { + SQLite::Statement stmt(m_DB, + "SELECT PermissionItem.Permission FROM PermissionItem " + "LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID " + "WHERE PermGroup.Name = ?" + ); + stmt.bind(1, a_GroupName); + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get group permissions from DB: %s", __FUNCTION__, ex.what()); + } return res; } @@ -226,9 +321,25 @@ AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); AStringVector res; - res.push_back(Printf("%s: DummyPermission", __FUNCTION__)); + try + { + SQLite::Statement stmt(m_DB, + "SELECT PermissionItem.Permission FROM PermissionItem " + "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermissionItem.PermGroupID " + "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID " + "WHERE Rank.Name = ?" + ); + stmt.bind(1, a_RankName); + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get rank permissions from DB: %s", __FUNCTION__, ex.what()); + } return res; } @@ -238,9 +349,19 @@ AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) AStringVector cRankManager::GetAllRanks(void) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); AStringVector res; - res.push_back(Printf("%s: DummyRank", __FUNCTION__)); + try + { + SQLite::Statement stmt(m_DB, "SELECT Name FROM Rank"); + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what()); + } return res; } @@ -250,9 +371,19 @@ AStringVector cRankManager::GetAllRanks(void) AStringVector cRankManager::GetAllGroups(void) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); AStringVector res; - res.push_back(Printf("%s: DummyGroup", __FUNCTION__)); + try + { + SQLite::Statement stmt(m_DB, "SELECT Name FROM PermGroup"); + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get groups from DB: %s", __FUNCTION__, ex.what()); + } return res; } @@ -262,9 +393,19 @@ AStringVector cRankManager::GetAllGroups(void) AStringVector cRankManager::GetAllPermissions(void) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); AStringVector res; - res.push_back(Printf("%s: DummyPermission", __FUNCTION__)); + try + { + SQLite::Statement stmt(m_DB, "SELECT Permission FROM PermissionItem"); + while (stmt.executeStep()) + { + res.push_back(stmt.getColumn(0).getText()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get permissions from DB: %s", __FUNCTION__, ex.what()); + } return res; } -- cgit v1.2.3 From 89333c5870efb438a38786fd67b82daff0482d9a Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 8 Aug 2014 21:30:47 +0200 Subject: RankMgr: Finished API implementation. --- src/RankManager.cpp | 421 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 390 insertions(+), 31 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index ec9ac1b1d..8a3b19c15 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -11,7 +11,7 @@ -//* +/* // This code is for internal testing while developing the cRankManager class static class cRankMgrTest { @@ -30,30 +30,35 @@ public: // Add a rank, a few groups and permissions and set the player to use them: LOG("Adding data..."); - m_Mgr.AddRank("TestRank", "[test]", "[/test]", "7"); + m_Mgr.AddRank("TestRank1", "[test]", "[/test]", "7"); + m_Mgr.AddRank("TestRank2", "[t2]", "[/t2]", "8"); m_Mgr.AddGroup("TestGroup1"); m_Mgr.AddGroup("TestGroup2"); - m_Mgr.AddGroupToRank("TestGroup1", "TestRank"); - m_Mgr.AddGroupToRank("TestGroup2", "TestRank"); + m_Mgr.AddGroupToRank("TestGroup1", "TestRank1"); + m_Mgr.AddGroupToRank("TestGroup2", "TestRank1"); + m_Mgr.AddGroupToRank("TestGroup2", "TestRank2"); m_Mgr.AddPermissionToGroup("testpermission1.1", "TestGroup1"); m_Mgr.AddPermissionToGroup("testpermission1.2", "TestGroup1"); + m_Mgr.AddPermissionToGroup("testpermission1.3", "TestGroup1"); + m_Mgr.AddPermissionToGroup("common", "TestGroup1"); m_Mgr.AddPermissionToGroup("testpermission2.1", "TestGroup2"); - m_Mgr.SetPlayerRank(UUID, "TestRank"); + m_Mgr.AddPermissionToGroup("common", "TestGroup2"); + m_Mgr.SetPlayerRank(UUID, "xoft", "TestRank1"); // Test the added data: LOG("Testing the added data:"); - LOG("IsGroupInRank(TestGroup1, TestRank) = %s", m_Mgr.IsGroupInRank("TestGroup1", "TestRank") ? "true" : "false"); - LOG("IsGroupInRank(TestGroup3, TestRank) = %s", m_Mgr.IsGroupInRank("TestGroup3", "TestRank") ? "true" : "false"); - LOG("IsPermissionInGroup(testpermission1.2, TestGroup1) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup1") ? "true" : "false"); - LOG("IsPermissionInGroup(testpermission1.2, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup2") ? "true" : "false"); - LOG("IsPermissionInGroup(testpermission1.3, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.3", "TestGroup2") ? "true" : "false"); + LOG("IsGroupInRank(TestGroup1, TestRank1) = %s", m_Mgr.IsGroupInRank("TestGroup1", "TestRank1") ? "true" : "false"); + LOG("IsGroupInRank(TestGroup3, TestRank1) = %s", m_Mgr.IsGroupInRank("TestGroup3", "TestRank1") ? "true" : "false"); + LOG("IsPermissionInGroup(testpermission1.2, TestGroup1) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup1") ? "true" : "false"); // Existing permission, in group + LOG("IsPermissionInGroup(testpermission1.2, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup2") ? "true" : "false"); // Existing permission, not in group + LOG("IsPermissionInGroup(testpermission1.9, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.9", "TestGroup2") ? "true" : "false"); // Non-existing permission LOG("IsPlayerRankSet(%s) = %s", UUID.c_str(), m_Mgr.IsPlayerRankSet(UUID) ? "true" : "false"); LOG("IsPlayerRankSet(%s1) = %s", UUID.c_str(), m_Mgr.IsPlayerRankSet(UUID + "1") ? "true" : "false"); LOG("GroupExists(TestGroup1) = %s", m_Mgr.GroupExists("TestGroup1") ? "true" : "false"); LOG("GroupExists(TestGroup3) = %s", m_Mgr.GroupExists("TestGroup3") ? "true" : "false"); - LOG("RankExists(TestRank) = %s", m_Mgr.RankExists("TestRank") ? "true" : "false"); + LOG("RankExists(TestRank1) = %s", m_Mgr.RankExists("TestRank1") ? "true" : "false"); LOG("RankExists(NonexistentRank) = %s", m_Mgr.RankExists("NonexistentRank") ? "true" : "false"); - ReportRankGroups("TestRank"); + ReportRankGroups("TestRank1"); ReportRankGroups("NonexistentRank"); ReportGroupPermissions("TestGroup1"); ReportGroupPermissions("NonexistentGroup"); @@ -64,7 +69,65 @@ public: // Test the assignments above: LOG("After-assignment test:"); ReportPlayer(UUID); + + // Test removing a permission from a group: + LOG("Removing permission testpermission1.3 from group TestGroup1."); + m_Mgr.RemovePermissionFromGroup("testpermission1.3", "TestGroup1"); + ReportGroupPermissions("TestGroup1"); + LOG("Removing permission common from group TestGroup1."); + m_Mgr.RemovePermissionFromGroup("common", "TestGroup1"); + ReportGroupPermissions("TestGroup1"); // Check that it's not present + ReportGroupPermissions("TestGroup2"); // Check that it's still present here + + // Test removing a group from rank: + LOG("Removing group TestGroup2 from rank TestRank1."); + m_Mgr.RemoveGroupFromRank("TestGroup2", "TestRank1"); + ReportRankGroups("TestRank1"); + LOG("Removing group TestGroup3 from rank TestRank1."); + m_Mgr.RemoveGroupFromRank("TestGroup3", "TestRank1"); + ReportRankGroups("TestRank1"); + + // Test re-adding the groups: + LOG("Re-adding groups to TestRank1."); + m_Mgr.AddGroupToRank("TestGroup1", "TestRank1"); + m_Mgr.AddGroupToRank("TestGroup2", "TestRank1"); + ReportRankGroups("TestRank1"); + + // Test removing a group altogether: + LOG("Removing group TestGroup2"); + m_Mgr.RemoveGroup("TestGroup2"); + ReportAll(); + + // Test removing a rank: + LOG("Removing rank TestRank2, replacing with rank TestRank1."); + m_Mgr.RemoveRank("TestRank2", "TestRank1"); + ReportAll(); + LOG("Removing rank Test altogether."); + m_Mgr.RemoveRank("Test", ""); + ReportAll(); + + // Test renaming a rank: + LOG("Renaming rank TestRank1 to Test"); + m_Mgr.RenameRank("TestRank1", "Test"); + ReportRankGroups("TestRank1"); + ReportRankGroups("Test"); + LOG("Player after renaming:"); + ReportPlayer(UUID); + + // Test renaming a group: + LOG("Renaming group TestGroup1 to Test"); + m_Mgr.RenameGroup("TestGroup1", "Test"); + ReportGroupPermissions("TestGroup1"); + ReportGroupPermissions("Test"); + LOG("Player after renaming:"); + ReportPlayer(UUID); + m_Mgr.RenameGroup("Test", "TestGroup1"); + // Test removing the rank in favor of another one: + m_Mgr.RemoveRank("Test", "TestRank2"); + LOG("After-removal test:"); + ReportPlayer(UUID); + LOG("Done."); } @@ -238,7 +301,7 @@ AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) { // Prepare the DB statement: SQLite::Statement stmt(m_DB, - "SELECT PermissionItem.Permission FROM PermissionItem " + "SELECT DISTINCT(PermissionItem.Permission) FROM PermissionItem " "LEFT JOIN RankPermGroup " "ON PermissionItem.PermGroupID = RankPermGroup.PermGroupID " "LEFT JOIN PlayerRank " @@ -396,7 +459,7 @@ AStringVector cRankManager::GetAllPermissions(void) AStringVector res; try { - SQLite::Statement stmt(m_DB, "SELECT Permission FROM PermissionItem"); + SQLite::Statement stmt(m_DB, "SELECT DISTINCT(Permission) FROM PermissionItem"); while (stmt.executeStep()) { res.push_back(stmt.getColumn(0).getText()); @@ -413,17 +476,43 @@ AStringVector cRankManager::GetAllPermissions(void) -void cRankManager::GetPlayerMsgVisuals( +bool cRankManager::GetPlayerMsgVisuals( const AString & a_PlayerUUID, AString & a_MsgPrefix, AString & a_MsgSuffix, AString & a_MsgNameColorCode ) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); - a_MsgPrefix = Printf("%s: DummyPrefix", __FUNCTION__); - a_MsgSuffix = Printf("%s: DummySuffix", __FUNCTION__); - a_MsgNameColorCode = Printf("%s: DummyMsgNameColorCode", __FUNCTION__); + AStringVector res; + try + { + SQLite::Statement stmt(m_DB, + "SELECT Rank.MsgPrefix, Rank.MsgSuffix, Rank.MsgNameColorCode FROM Rank " + "LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID " + "WHERE PlayerRank.PlayerUUID = ?" + ); + stmt.bind(1, a_PlayerUUID); + if (!stmt.executeStep()) + { + LOGD("%s: Player UUID %s not found in the DB, returning empty values.", __FUNCTION__, a_PlayerUUID.c_str()); + a_MsgPrefix.clear(); + a_MsgSuffix.clear(); + a_MsgNameColorCode.clear(); + return false; + } + a_MsgPrefix = stmt.getColumn(0).getText(); + a_MsgSuffix = stmt.getColumn(1).getText(); + a_MsgNameColorCode = stmt.getColumn(2).getText(); + return true; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get ranks from DB: %s. Returning empty values.", __FUNCTION__, ex.what()); + } + a_MsgPrefix.clear(); + a_MsgSuffix.clear(); + a_MsgNameColorCode.clear(); + return false; } @@ -659,7 +748,73 @@ bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AStr void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_ReplacementRankName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + try + { + // Wrap everything into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the RankID for the rank being removed: + int RemoveRankID; + { + SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (!stmt.executeStep()) + { + LOGINFO("%s: Rank %s was not found. Skipping.", __FUNCTION__, a_RankName.c_str()); + return; + } + RemoveRankID = stmt.getColumn(0).getInt(); + } + + // Get the RankID for the replacement rank: + int ReplacementRankID = -1; + { + SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); + stmt.bind(1, a_ReplacementRankName); + if (stmt.executeStep()) + { + ReplacementRankID = stmt.getColumn(0).getInt(); + } + } + + // Remove the rank's bindings to groups: + { + SQLite::Statement stmt(m_DB, "DELETE FROM RankPermGroup WHERE RankID = ?"); + stmt.bind(1, RemoveRankID); + stmt.exec(); + } + + // Adjust players: + if (ReplacementRankID == -1) + { + // No replacement, just delete all the players that have the rank: + SQLite::Statement stmt(m_DB, "DELETE FROM PlayerRank WHERE RankID = ?"); + stmt.bind(1, RemoveRankID); + stmt.exec(); + } + else + { + // Replacement available, change all the player records: + SQLite::Statement stmt(m_DB, "UPDATE PlayerRank SET RankID = ? WHERE RankID = ?"); + stmt.bind(1, ReplacementRankID); + stmt.bind(2, RemoveRankID); + stmt.exec(); + } + + // Remove the rank from the DB: + { + SQLite::Statement stmt(m_DB, "DELETE FROM Rank WHERE RankID = ?"); + stmt.bind(1, RemoveRankID); + stmt.exec(); + } + + trans.commit(); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to remove rank from DB: %s", __FUNCTION__, ex.what()); + } } @@ -668,16 +823,105 @@ void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_Repl void cRankManager::RemoveGroup(const AString & a_GroupName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + try + { + // Wrap everything into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the ID of the group: + int GroupID; + { + SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (!stmt.executeStep()) + { + LOGINFO("%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str()); + return; + } + GroupID = stmt.getColumn(0).getInt(); + } + + // Remove all permissions from the group: + { + SQLite::Statement stmt(m_DB, "DELETE FROM PermissionItem WHERE PermGroupID = ?"); + stmt.bind(1, GroupID); + stmt.exec(); + } + + // Remove the group from all ranks that contain it: + { + SQLite::Statement stmt(m_DB, "DELETE FROM RankPermGroup WHERE PermGroupID = ?"); + stmt.bind(1, GroupID); + stmt.exec(); + } + + // Remove the group itself: + { + SQLite::Statement stmt(m_DB, "DELETE FROM PermGroup WHERE PermGroupID = ?"); + stmt.bind(1, GroupID); + stmt.exec(); + } + + trans.commit(); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to remove group %s from DB: %s", __FUNCTION__, a_GroupName.c_str(), ex.what()); + } } -void cRankManager::RemoveGroupFromRank(const AString & a_RankName, const AString & a_GroupName) +void cRankManager::RemoveGroupFromRank(const AString & a_GroupName, const AString & a_RankName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + try + { + // Wrap everything into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the IDs of the group and the rank: + int GroupID, RankID; + { + SQLite::Statement stmt(m_DB, + "SELECT PermGroup.PermGroupID, Rank.RankID FROM PermGroup " + "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID " + "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID " + "WHERE PermGroup.Name = ? AND Rank.Name = ?" + ); + stmt.bind(1, a_GroupName); + stmt.bind(2, a_RankName); + if (!stmt.executeStep()) + { + LOGINFO("%s: Group %s was not found in rank %s, skipping.", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str()); + return; + } + GroupID = stmt.getColumn(0).getInt(); + RankID = stmt.getColumn(1).getInt(); + } + + // Remove the group from all ranks that contain it: + { + SQLite::Statement stmt(m_DB, "DELETE FROM RankPermGroup WHERE PermGroupID = ?"); + stmt.bind(1, GroupID); + stmt.exec(); + } + + // Remove the group-to-rank binding: + { + SQLite::Statement stmt(m_DB, "DELETE FROM RankPermGroup WHERE PermGroupID = ? AND RankID = ?"); + stmt.bind(1, GroupID); + stmt.bind(1, RankID); + stmt.exec(); + } + + trans.commit(); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to remove group %s from rank %s in the DB: %s", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str(), ex.what()); + } } @@ -686,7 +930,40 @@ void cRankManager::RemoveGroupFromRank(const AString & a_RankName, const AString void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const AString & a_GroupName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + try + { + // Wrap everything into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the ID of the group: + int GroupID; + { + SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (!stmt.executeStep()) + { + LOGINFO("%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str()); + return; + } + GroupID = stmt.getColumn(0).getInt(); + } + + // Remove the permission from the group: + { + SQLite::Statement stmt(m_DB, "DELETE FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?"); + stmt.bind(1, GroupID); + stmt.bind(2, a_Permission); + stmt.exec(); + } + + trans.commit(); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to remove permission %s from group %s in DB: %s", + __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str(), ex.what() + ); + } } @@ -695,7 +972,39 @@ void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + try + { + // Wrap everything into a transaction: + SQLite::Transaction trans(m_DB); + + // Check that NewName doesn't exist: + { + SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); + stmt.bind(1, a_NewName); + if (stmt.executeStep()) + { + LOGD("%s: Rank %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str()); + return false; + } + } + + // Rename: + bool res; + { + SQLite::Statement stmt(m_DB, "UPDATE Rank SET Name = ? WHERE Name = ?"); + stmt.bind(1, a_NewName); + stmt.bind(2, a_OldName); + res = (stmt.exec() > 0); + } + + trans.commit(); + return res; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to rename rank %s to %s in DB: %s", + __FUNCTION__, a_OldName.c_str(), a_NewName.c_str(), ex.what()); + } return false; } @@ -705,7 +1014,39 @@ bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewNa bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewName) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + try + { + // Wrap everything into a transaction: + SQLite::Transaction trans(m_DB); + + // Check that NewName doesn't exist: + { + SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_NewName); + if (stmt.executeStep()) + { + LOGD("%s: Group %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str()); + return false; + } + } + + // Rename: + bool res; + { + SQLite::Statement stmt(m_DB, "UPDATE PermGroup SET Name = ? WHERE Name = ?"); + stmt.bind(1, a_NewName); + stmt.bind(2, a_OldName); + res = (stmt.exec() > 0); + } + + trans.commit(); + return res; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to rename group %s to %s in DB: %s", + __FUNCTION__, a_OldName.c_str(), a_NewName.c_str(), ex.what()); + } return false; } @@ -713,7 +1054,7 @@ bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewN -void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a_RankName) +void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a_PlayerName, const AString & a_RankName) { try { @@ -735,9 +1076,10 @@ void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a // Update the player's rank, if already in DB: { - SQLite::Statement stmt(m_DB, "UPDATE PlayerRank SET RankID = ? WHERE PlayerUUID = ?"); + SQLite::Statement stmt(m_DB, "UPDATE PlayerRank SET RankID = ?, PlayerName = ? WHERE PlayerUUID = ?"); stmt.bind(1, RankID); - stmt.bind(2, a_PlayerUUID); + stmt.bind(2, a_PlayerName); + stmt.bind(3, a_PlayerUUID); if (stmt.exec() > 0) { // Successfully updated the player's rank @@ -747,9 +1089,10 @@ void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a } // The player is not yet in the DB, add them: - SQLite::Statement stmt(m_DB, "INSERT INTO PlayerRank (RankID, PlayerUUID) VALUES (?, ?)"); + SQLite::Statement stmt(m_DB, "INSERT INTO PlayerRank (RankID, PlayerUUID, PlayerName) VALUES (?, ?, ?)"); stmt.bind(1, RankID); stmt.bind(2, a_PlayerUUID); + stmt.bind(3, a_PlayerName); if (stmt.exec() > 0) { // Successfully added the player @@ -780,7 +1123,23 @@ void cRankManager::SetRankVisuals( const AString & a_MsgNameColorCode ) { - LOGWARNING("%s: Not implemented yet", __FUNCTION__); + AStringVector res; + try + { + SQLite::Statement stmt(m_DB, "UPDATE Rank SET MsgPrefix = ?, MsgSuffix = ?, MsgNameColorCode = ? WHERE Name = ?"); + stmt.bind(1, a_MsgPrefix); + stmt.bind(2, a_MsgSuffix); + stmt.bind(1, a_MsgNameColorCode); + stmt.bind(2, a_RankName); + if (!stmt.executeStep()) + { + LOGINFO("%s: Rank %s not found, visuals not set.", __FUNCTION__, a_RankName.c_str()); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what()); + } } -- cgit v1.2.3 From 0001a7c9fc2359078968565a8ab464509362b776 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 9 Aug 2014 17:36:19 +0200 Subject: RankMgr: Added GetRankVisuals() function. --- src/RankManager.cpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 8a3b19c15..3627afadb 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -1123,14 +1123,13 @@ void cRankManager::SetRankVisuals( const AString & a_MsgNameColorCode ) { - AStringVector res; try { SQLite::Statement stmt(m_DB, "UPDATE Rank SET MsgPrefix = ?, MsgSuffix = ?, MsgNameColorCode = ? WHERE Name = ?"); stmt.bind(1, a_MsgPrefix); stmt.bind(2, a_MsgSuffix); - stmt.bind(1, a_MsgNameColorCode); - stmt.bind(2, a_RankName); + stmt.bind(3, a_MsgNameColorCode); + stmt.bind(4, a_RankName); if (!stmt.executeStep()) { LOGINFO("%s: Rank %s not found, visuals not set.", __FUNCTION__, a_RankName.c_str()); @@ -1146,6 +1145,38 @@ void cRankManager::SetRankVisuals( +bool cRankManager::GetRankVisuals( + const AString & a_RankName, + AString & a_MsgPrefix, + AString & a_MsgSuffix, + AString & a_MsgNameColorCode +) +{ + try + { + SQLite::Statement stmt(m_DB, "SELECT MsgPrefix, MsgSuffix, MsgNameColorCode FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (!stmt.executeStep()) + { + // Rank not found + return false; + } + a_MsgPrefix = stmt.getColumn(0).getText(); + a_MsgSuffix = stmt.getColumn(1).getText(); + a_MsgNameColorCode = stmt.getColumn(2).getText(); + return true; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what()); + } + return false; +} + + + + + bool cRankManager::RankExists(const AString & a_RankName) { try -- cgit v1.2.3 From e110f7226895b1629f72a52c7953d8f00a1f63c6 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 13 Aug 2014 09:53:33 +0200 Subject: RankMgr: Initial migration code. --- src/RankManager.cpp | 540 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 539 insertions(+), 1 deletion(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 3627afadb..9809f1d6c 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -6,6 +6,8 @@ #include "Globals.h" #include "RankManager.h" #include "SQLiteCpp/Transaction.h" +#include "inifile/iniFile.h" +#include "Protocol/MojangAPI.h" @@ -219,9 +221,367 @@ protected: +//////////////////////////////////////////////////////////////////////////////// +/** Migrates from groups.ini and users.ini into the rankmanager DB */ +class cRankManagerIniMigrator +{ +public: + cRankManagerIniMigrator(cRankManager & a_RankManager, cMojangAPI & a_MojangAPI) : + m_RankManager(a_RankManager), + m_MojangAPI(a_MojangAPI) + { + } + + + + /** Performs the complete migration from INI files to DB. */ + bool Migrate(void) + { + LOGD("Reading groups..."); + if (!ReadGroups()) + { + return false; + } + LOGD("Cleaning groups inheritance..."); + CleanGroupInheritance(); + LOGD("Creating groups..."); + CreateGroups(); + + LOGD("Reading users..."); + if (!ReadUsers()) + { + return false; + } + LOGD("Cleaning user groups..."); + CleanUserGroups(); + LOGD("Resolving user UUIDs..."); + ResolveUserUUIDs(); + + LOGD("Setting ranks..."); + SetRanks(); + + return true; + } + +protected: + + /** Container for a group read from an INI file. */ + struct sGroup + { + AString m_Name; + AString m_Color; + AStringVector m_Inherits; + AStringVector m_Permissions; + + sGroup(void) {} + + sGroup(const AString & a_Name, const AString & a_Color, const AStringVector & a_Inherits, const AStringVector & a_Permissions): + m_Name(a_Name), + m_Color(a_Color), + m_Inherits(a_Inherits), + m_Permissions(a_Permissions) + { + } + }; + typedef std::map sGroupMap; + + + /** Container for a single user read from an INI file. */ + struct sUser + { + AString m_Name; + AStringVector m_Groups; + + /** Assigned by ResolveUserUUIDs() */ + AString m_UUID; + + + sUser(void) {} + + sUser(const AString & a_Name, const AStringVector & a_Groups): + m_Name(a_Name), + m_Groups(a_Groups) + { + } + }; + typedef std::map sUserMap; + + typedef std::map cStringMap; + + + /** The parent Rank manager where we will create the groups, ranks and players */ + cRankManager & m_RankManager; + + /** The player name to UUID resolver */ + cMojangAPI & m_MojangAPI; + + /** List of all groups read from the ini file */ + sGroupMap m_Groups; + + /** List of all players read from the ini file. */ + sUserMap m_Users; + + /** Maps lists of groups to rank names. + Each group list is either a simple "" if there's only one group, + or ",,...", where the secondary groups are + lowercased and alpha-sorted. This makes the group lists comparable for equivalence, simply by comparing + their string names. + The ranks are named "" for single-group players, and "AutoMigratedRank_N" for the composite ranks, + where N is a unique number. */ + cStringMap m_GroupsToRanks; + + + + /** Reads the groups from the "groups.ini" file into m_Groups */ + bool ReadGroups(void) + { + // Read the file: + cIniFile Groups; + if (!Groups.ReadFile("groups.ini")) + { + return false; + } + + // Read all the groups into a map: + int NumGroups = Groups.GetNumKeys(); + for (int i = 0; i < NumGroups; i++) + { + AString GroupName = Groups.GetKeyName(i); + AString lcGroupName = StrToLower(GroupName); + if (m_Groups.find(lcGroupName) != m_Groups.end()) + { + LOGINFO("groups.ini contains a duplicate definition of group %s, ignoring the latter.", GroupName.c_str()); + continue; + } + m_Groups[lcGroupName] = sGroup( + GroupName, + Groups.GetValue(GroupName, "Color", ""), + StringSplitAndTrim(Groups.GetValue(GroupName, "Inherits"), ","), + StringSplitAndTrim(Groups.GetValue(GroupName, "Permissions"), ",") + ); + } // for i - Groups' keys + return true; + } + + + + /** Removes non-existent groups from all the groups' inheritance */ + void CleanGroupInheritance(void) + { + for (sGroupMap::iterator itrG = m_Groups.begin(), endG = m_Groups.end(); itrG != endG; ++itrG) + { + AStringVector & Inherits = itrG->second.m_Inherits; + for (AStringVector::iterator itrI = Inherits.begin(); itrI != Inherits.end();) + { + AString lcInherits = StrToLower(*itrI); + if (m_Groups.find(lcInherits) != m_Groups.end()) + { + // Inherited group exists, continue checking the next one + ++itrI; + continue; + } + // Inherited group doesn't exist, remove it from the list: + LOGWARNING("RankMigrator: Group \"%s\" inherits from a non-existent group \"%s\", this inheritance will be ignored.", + itrG->second.m_Name.c_str(), itrI->c_str() + ); + AStringVector::iterator itrI2 = itrI; + ++itrI2; + Inherits.erase(itrI); + itrI = itrI2; + } // for itrI - Inherits[] + } // for itrG - m_Groups[] + } + + + + /** Reads the users from the "users.ini" file into m_Users */ + bool ReadUsers(void) + { + // Read the file: + cIniFile Users; + if (!Users.ReadFile("users.ini")) + { + return false; + } + + // Read all the users into a map: + int NumUsers = Users.GetNumKeys(); + for (int i = 0; i < NumUsers; i++) + { + AString UserName = Users.GetKeyName(i); + AString lcUserName = StrToLower(UserName); + if (m_Users.find(lcUserName) != m_Users.end()) + { + LOGINFO("users.ini contains a duplicate definition of user %s, ignoring the latter.", UserName.c_str()); + continue; + } + m_Users[lcUserName] = sUser( + UserName, + StringSplitAndTrim(Users.GetValue(UserName, "Groups", ""), ",") + ); + } // for i - Users' keys + return true; + } + + + + /** Removes non-existent groups from each user's definition. */ + void CleanUserGroups(void) + { + for (sUserMap::iterator itrU = m_Users.begin(), endU = m_Users.end(); itrU != endU; ++itrU) + { + AStringVector & Groups = itrU->second.m_Groups; + for (AStringVector::iterator itrG = Groups.begin(); itrG != Groups.end();) + { + AString lcGroup = StrToLower(*itrG); + if (m_Groups.find(lcGroup) != m_Groups.end()) + { + // Assigned group exists, continue checking the next one + ++itrG; + continue; + } + // Assigned group doesn't exist, remove it from the list: + LOGWARNING("RankMigrator: User \"%s\" is assigned a non-existent group \"%s\", this assignment will be ignored.", + itrU->second.m_Name.c_str(), itrG->c_str() + ); + AStringVector::iterator itrG2 = itrG; + ++itrG2; + Groups.erase(itrG); + itrG = itrG2; + } // for itrG - Groups[] + } // for itrU - m_Users[] + } + + + + /** Creates groups based on m_Groups. + Ignores group inheritance. */ + void CreateGroups(void) + { + // Create each group, with its permissions: + for (sGroupMap::const_iterator itr = m_Groups.begin(), end = m_Groups.end(); itr != end; ++itr) + { + m_RankManager.AddGroup(itr->second.m_Name); + m_RankManager.AddPermissionsToGroup(itr->second.m_Permissions, itr->second.m_Name); + } // for itr - m_Groups[] + } + + + /** Resolves the UUID of each user in m_Users. + If a user doesn't resolve, they are removed and logged in the console. */ + void ResolveUserUUIDs(void) + { + // Resolve all PlayerNames at once (the API doesn't like single-name queries): + AStringVector PlayerNames; + for (sUserMap::const_iterator itr = m_Users.begin(), end = m_Users.end(); itr != end; ++itr) + { + PlayerNames.push_back(itr->second.m_Name); + } + m_MojangAPI.GetUUIDsFromPlayerNames(PlayerNames); + + // Assign the UUIDs back to players, remove those not resolved: + for (sUserMap::iterator itr = m_Users.begin(); itr != m_Users.end(); ) + { + AString UUID = m_MojangAPI.GetUUIDFromPlayerName(itr->second.m_Name); + if (UUID.empty()) + { + LOGWARNING("RankMigrator: Cannot resolve player %s to UUID, player will be left unranked", itr->second.m_Name.c_str()); + sUserMap::iterator itr2 = itr; + ++itr2; + m_Users.erase(itr); + itr = itr2; + } + else + { + itr->second.m_UUID = UUID; + ++itr; + } + } + } + + + + /** Adds the specified groups to the specified ranks. Recurses on the groups' inheritance. */ + void AddGroupsToRank(const AStringVector & a_Groups, const AString & a_RankName) + { + for (AStringVector::const_iterator itr = a_Groups.begin(), end = a_Groups.end(); itr != end; ++itr) + { + // Normalize the group name: + sGroup & Group = m_Groups[StrToLower(*itr)]; + + // Avoid loops, check if the group is already added: + if (m_RankManager.IsGroupInRank(Group.m_Name, a_RankName)) + { + continue; + } + + // Add the group, and all the groups it inherits from recursively: + m_RankManager.AddGroupToRank(Group.m_Name, a_RankName); + AddGroupsToRank(Group.m_Inherits, a_RankName); + } // for itr - a_Groups[] + } + + + + /** Creates a rank for each player, based on the master groups they are assigned. */ + void SetRanks(void) + { + for (sUserMap::const_iterator itr = m_Users.begin(), end = m_Users.end(); itr != end; ++itr) + { + // Ignore users with no groups: + const AStringVector & Groups = itr->second.m_Groups; + if (Groups.empty()) + { + LOGWARNING("RankMigrator: Player %s has no groups assigned to them, skipping the player.", itr->second.m_Name.c_str()); + continue; + } + + // Compose the rank name out of group names: + AString RankName; + for (AStringVector::const_iterator itrG = Groups.begin(), endG = Groups.end(); itrG != endG; ++itrG) + { + AString GroupName = m_Groups[StrToLower(*itrG)].m_Name; // Normalize group name + if (!RankName.empty()) + { + RankName.push_back(','); + } + RankName.append(GroupName); + } // for itrG - Groups[] + + // Create the rank, with al its groups: + if (!m_RankManager.RankExists(RankName)) + { + m_RankManager.AddRank(RankName, "", "", m_Groups[StrToLower(Groups[0])].m_Color); + AddGroupsToRank(Groups, RankName); + } + + // Set the rank to the user: + m_RankManager.SetPlayerRank(itr->second.m_UUID, itr->second.m_Name, RankName); + } // for itr - m_Users[] + } +}; + + + + + +//////////////////////////////////////////////////////////////////////////////// +// cRankManager: + cRankManager::cRankManager(void) : - m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE) + m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE), + m_IsInitialized(false) +{ +} + + + + + +void cRankManager::Initialize(cMojangAPI & a_MojangAPI) { + ASSERT(!m_IsInitialized); // Calling Initialize for the second time? + // Create the DB tables, if they don't exist: m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgSuffix, MsgNameColorCode)"); m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)"); @@ -229,6 +589,22 @@ cRankManager::cRankManager(void) : m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)"); m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (PermGroupID INTEGER, Permission)"); + m_IsInitialized = true; + + // Check if tables empty, migrate from ini files then + if (AreDBTablesEmpty()) + { + LOGINFO("There are no ranks, migrating old-style INI files to new DB ranks..."); + LOGINFO("(This might take a while)"); + cRankManagerIniMigrator Migrator(*this, a_MojangAPI); + if (Migrator.Migrate()) + { + LOGINFO("Ranks migrated."); + return; + } + } + + // Migration failed. // TODO: Check if tables empty, add some defaults then } @@ -238,6 +614,8 @@ cRankManager::cRankManager(void) : AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, "SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?"); @@ -263,6 +641,8 @@ AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -296,6 +676,8 @@ AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -329,6 +711,8 @@ AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) AStringVector cRankManager::GetRankGroups(const AString & a_RankName) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -357,6 +741,8 @@ AStringVector cRankManager::GetRankGroups(const AString & a_RankName) AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -384,6 +770,8 @@ AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -412,6 +800,8 @@ AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) AStringVector cRankManager::GetAllRanks(void) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -434,6 +824,8 @@ AStringVector cRankManager::GetAllRanks(void) AStringVector cRankManager::GetAllGroups(void) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -456,6 +848,8 @@ AStringVector cRankManager::GetAllGroups(void) AStringVector cRankManager::GetAllPermissions(void) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -483,6 +877,8 @@ bool cRankManager::GetPlayerMsgVisuals( AString & a_MsgNameColorCode ) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -526,6 +922,8 @@ void cRankManager::AddRank( const AString & a_MsgNameColorCode ) { + ASSERT(m_IsInitialized); + try { // Check if such a rank name is already used: @@ -566,6 +964,8 @@ void cRankManager::AddRank( void cRankManager::AddGroup(const AString & a_GroupName) { + ASSERT(m_IsInitialized); + try { // Check if such a rank name is already used: @@ -603,6 +1003,8 @@ void cRankManager::AddGroup(const AString & a_GroupName) bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a_RankName) { + ASSERT(m_IsInitialized); + try { SQLite::Transaction trans(m_DB); @@ -680,6 +1082,8 @@ bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AString & a_GroupName) { + ASSERT(m_IsInitialized); + try { // Wrapp the entire operation into a transaction: @@ -746,8 +1150,83 @@ bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AStr +bool cRankManager::AddPermissionsToGroup(const AStringVector & a_Permissions, const AString & a_GroupName) +{ + ASSERT(m_IsInitialized); + + try + { + // Wrapp the entire operation into a transaction: + SQLite::Transaction trans(m_DB); + + // Get the group's ID: + int GroupID; + { + SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); + stmt.bind(1, a_GroupName); + if (!stmt.executeStep()) + { + LOGWARNING("%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str()); + return false; + } + GroupID = stmt.getColumn(0).getInt(); + } + + for (AStringVector::const_iterator itr = a_Permissions.begin(), end = a_Permissions.end(); itr != end; ++itr) + { + // Check if the permission is already present: + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?"); + stmt.bind(1, GroupID); + stmt.bind(2, *itr); + if (!stmt.executeStep()) + { + LOGWARNING("%s: Failed to check binding between permission %s and group %s, aborting.", __FUNCTION__, itr->c_str(), a_GroupName.c_str()); + return false; + } + if (stmt.getColumn(0).getInt() > 0) + { + LOGD("%s: Permission %s is already present in group %s, skipping and returning success.", + __FUNCTION__, itr->c_str(), a_GroupName.c_str() + ); + continue; + } + } + + // Add the permission: + { + SQLite::Statement stmt(m_DB, "INSERT INTO PermissionItem (Permission, PermGroupID) VALUES (?, ?)"); + stmt.bind(1, *itr); + stmt.bind(2, GroupID); + if (stmt.exec() <= 0) + { + LOGWARNING("%s: Failed to add permission %s to group %s, skipping.", __FUNCTION__, itr->c_str(), a_GroupName.c_str()); + continue; + } + } + } // for itr - a_Permissions[] + + // Adding succeeded: + trans.commit(); + return true; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to add permissions to group %s: %s", + __FUNCTION__, a_GroupName.c_str(), ex.what() + ); + } + return false; +} + + + + + void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_ReplacementRankName) { + ASSERT(m_IsInitialized); + AStringVector res; try { @@ -823,6 +1302,8 @@ void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_Repl void cRankManager::RemoveGroup(const AString & a_GroupName) { + ASSERT(m_IsInitialized); + try { // Wrap everything into a transaction: @@ -876,6 +1357,8 @@ void cRankManager::RemoveGroup(const AString & a_GroupName) void cRankManager::RemoveGroupFromRank(const AString & a_GroupName, const AString & a_RankName) { + ASSERT(m_IsInitialized); + try { // Wrap everything into a transaction: @@ -930,6 +1413,8 @@ void cRankManager::RemoveGroupFromRank(const AString & a_GroupName, const AStrin void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const AString & a_GroupName) { + ASSERT(m_IsInitialized); + try { // Wrap everything into a transaction: @@ -972,6 +1457,8 @@ void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewName) { + ASSERT(m_IsInitialized); + try { // Wrap everything into a transaction: @@ -1014,6 +1501,8 @@ bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewNa bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewName) { + ASSERT(m_IsInitialized); + try { // Wrap everything into a transaction: @@ -1056,6 +1545,8 @@ bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewN void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a_PlayerName, const AString & a_RankName) { + ASSERT(m_IsInitialized); + try { // Wrap the entire operation into a transaction: @@ -1123,6 +1614,8 @@ void cRankManager::SetRankVisuals( const AString & a_MsgNameColorCode ) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, "UPDATE Rank SET MsgPrefix = ?, MsgSuffix = ?, MsgNameColorCode = ? WHERE Name = ?"); @@ -1152,6 +1645,8 @@ bool cRankManager::GetRankVisuals( AString & a_MsgNameColorCode ) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, "SELECT MsgPrefix, MsgSuffix, MsgNameColorCode FROM Rank WHERE Name = ?"); @@ -1179,6 +1674,8 @@ bool cRankManager::GetRankVisuals( bool cRankManager::RankExists(const AString & a_RankName) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, "SELECT * FROM Rank WHERE Name = ?"); @@ -1202,6 +1699,8 @@ bool cRankManager::RankExists(const AString & a_RankName) bool cRankManager::GroupExists(const AString & a_GroupName) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, "SELECT * FROM PermGroup WHERE Name = ?"); @@ -1225,6 +1724,8 @@ bool cRankManager::GroupExists(const AString & a_GroupName) bool cRankManager::IsPlayerRankSet(const AString & a_PlayerUUID) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, "SELECT * FROM PlayerRank WHERE PlayerUUID = ?"); @@ -1248,6 +1749,8 @@ bool cRankManager::IsPlayerRankSet(const AString & a_PlayerUUID) bool cRankManager::IsGroupInRank(const AString & a_GroupName, const AString & a_RankName) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, @@ -1277,6 +1780,8 @@ bool cRankManager::IsGroupInRank(const AString & a_GroupName, const AString & a_ bool cRankManager::IsPermissionInGroup(const AString & a_Permission, const AString & a_GroupName) { + ASSERT(m_IsInitialized); + try { SQLite::Statement stmt(m_DB, @@ -1302,3 +1807,36 @@ bool cRankManager::IsPermissionInGroup(const AString & a_Permission, const AStri + +bool cRankManager::AreDBTablesEmpty(void) +{ + return ( + IsDBTableEmpty("Rank") && + IsDBTableEmpty("PlayerRank") && + IsDBTableEmpty("PermGroup") && + IsDBTableEmpty("RankPermGroup") && + IsDBTableEmpty("PermissionItem") + ); +} + + + + + +bool cRankManager::IsDBTableEmpty(const AString & a_TableName) +{ + try + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM " + a_TableName); + return (stmt.executeStep() && (stmt.getColumn(0).getInt() == 0)); + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to query DB: %s", __FUNCTION__, ex.what()); + } + return false; +} + + + + -- cgit v1.2.3 From 5e415c5b9565564c41c4f2f4144c6863c106bc46 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 13 Aug 2014 12:33:31 +0200 Subject: RankMgr: Fixed multithreading issues. Only one thread is allowed to interact with a SQLite::Database object at a time. Additionally, improved performance of the migration by wrapping the entire thing in a transaction. --- src/RankManager.cpp | 126 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 48 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 9809f1d6c..e64a47eb6 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -5,7 +5,6 @@ #include "Globals.h" #include "RankManager.h" -#include "SQLiteCpp/Transaction.h" #include "inifile/iniFile.h" #include "Protocol/MojangAPI.h" @@ -237,6 +236,8 @@ public: /** Performs the complete migration from INI files to DB. */ bool Migrate(void) { + cRankManager::cMassChangeLock Lock(m_RankManager); + LOGD("Reading groups..."); if (!ReadGroups()) { @@ -615,6 +616,7 @@ void cRankManager::Initialize(cMojangAPI & a_MojangAPI) AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -642,6 +644,7 @@ AString cRankManager::GetPlayerRankName(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -677,6 +680,7 @@ AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -712,6 +716,7 @@ AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) AStringVector cRankManager::GetRankGroups(const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -742,6 +747,7 @@ AStringVector cRankManager::GetRankGroups(const AString & a_RankName) AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -771,6 +777,7 @@ AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName) AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -801,6 +808,7 @@ AStringVector cRankManager::GetRankPermissions(const AString & a_RankName) AStringVector cRankManager::GetAllRanks(void) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -825,6 +833,7 @@ AStringVector cRankManager::GetAllRanks(void) AStringVector cRankManager::GetAllGroups(void) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -849,6 +858,7 @@ AStringVector cRankManager::GetAllGroups(void) AStringVector cRankManager::GetAllPermissions(void) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -878,6 +888,7 @@ bool cRankManager::GetPlayerMsgVisuals( ) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try @@ -923,6 +934,7 @@ void cRankManager::AddRank( ) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -965,10 +977,11 @@ void cRankManager::AddRank( void cRankManager::AddGroup(const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Check if such a rank name is already used: + // Check if such a group name is already used: { SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM PermGroup WHERE Name = ?"); stmt.bind(1, a_GroupName); @@ -982,7 +995,7 @@ void cRankManager::AddGroup(const AString & a_GroupName) } } - // Insert a new rank: + // Insert a new group: SQLite::Statement stmt(m_DB, "INSERT INTO PermGroup (Name) VALUES (?)"); stmt.bind(1, a_GroupName); if (stmt.exec() <= 0) @@ -1001,16 +1014,58 @@ void cRankManager::AddGroup(const AString & a_GroupName) +void cRankManager::AddGroups(const AStringVector & a_GroupNames) +{ + ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); + + try + { + for (AStringVector::const_iterator itr = a_GroupNames.begin(), end = a_GroupNames.end(); itr != end; ++itr) + { + // Check if such the group name is already used: + { + SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM PermGroup WHERE Name = ?"); + stmt.bind(1, *itr); + if (stmt.executeStep()) + { + if (stmt.getColumn(0).getInt() > 0) + { + // Group already exists, do nothing: + return; + } + } + } + + // Insert a new group: + SQLite::Statement stmt(m_DB, "INSERT INTO PermGroup (Name) VALUES (?)"); + stmt.bind(1, *itr); + if (stmt.exec() <= 0) + { + LOGWARNING("%s: Failed to add a new group \"%s\".", __FUNCTION__, itr->c_str()); + return; + } + } // for itr - a_GroupNames[] + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to add new groups: %s", __FUNCTION__, ex.what()); + } +} + + + + + bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - SQLite::Transaction trans(m_DB); - int GroupID, RankID; - // Get the group's ID: + int GroupID; { SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); stmt.bind(1, a_GroupName); @@ -1023,6 +1078,7 @@ bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a } // Get the rank's ID: + int RankID; { SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); stmt.bind(1, a_RankName); @@ -1066,7 +1122,6 @@ bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a } // Adding succeeded: - trans.commit(); return true; } catch (const SQLite::Exception & ex) @@ -1083,12 +1138,10 @@ bool cRankManager::AddGroupToRank(const AString & a_GroupName, const AString & a bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrapp the entire operation into a transaction: - SQLite::Transaction trans(m_DB); - // Get the group's ID: int GroupID; { @@ -1134,7 +1187,6 @@ bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AStr } // Adding succeeded: - trans.commit(); return true; } catch (const SQLite::Exception & ex) @@ -1153,12 +1205,10 @@ bool cRankManager::AddPermissionToGroup(const AString & a_Permission, const AStr bool cRankManager::AddPermissionsToGroup(const AStringVector & a_Permissions, const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrapp the entire operation into a transaction: - SQLite::Transaction trans(m_DB); - // Get the group's ID: int GroupID; { @@ -1207,7 +1257,6 @@ bool cRankManager::AddPermissionsToGroup(const AStringVector & a_Permissions, co } // for itr - a_Permissions[] // Adding succeeded: - trans.commit(); return true; } catch (const SQLite::Exception & ex) @@ -1226,13 +1275,11 @@ bool cRankManager::AddPermissionsToGroup(const AStringVector & a_Permissions, co void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_ReplacementRankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); AStringVector res; try { - // Wrap everything into a transaction: - SQLite::Transaction trans(m_DB); - // Get the RankID for the rank being removed: int RemoveRankID; { @@ -1287,8 +1334,6 @@ void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_Repl stmt.bind(1, RemoveRankID); stmt.exec(); } - - trans.commit(); } catch (const SQLite::Exception & ex) { @@ -1303,12 +1348,10 @@ void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_Repl void cRankManager::RemoveGroup(const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrap everything into a transaction: - SQLite::Transaction trans(m_DB); - // Get the ID of the group: int GroupID; { @@ -1342,8 +1385,6 @@ void cRankManager::RemoveGroup(const AString & a_GroupName) stmt.bind(1, GroupID); stmt.exec(); } - - trans.commit(); } catch (const SQLite::Exception & ex) { @@ -1358,12 +1399,10 @@ void cRankManager::RemoveGroup(const AString & a_GroupName) void cRankManager::RemoveGroupFromRank(const AString & a_GroupName, const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrap everything into a transaction: - SQLite::Transaction trans(m_DB); - // Get the IDs of the group and the rank: int GroupID, RankID; { @@ -1398,8 +1437,6 @@ void cRankManager::RemoveGroupFromRank(const AString & a_GroupName, const AStrin stmt.bind(1, RankID); stmt.exec(); } - - trans.commit(); } catch (const SQLite::Exception & ex) { @@ -1414,12 +1451,10 @@ void cRankManager::RemoveGroupFromRank(const AString & a_GroupName, const AStrin void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrap everything into a transaction: - SQLite::Transaction trans(m_DB); - // Get the ID of the group: int GroupID; { @@ -1440,8 +1475,6 @@ void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const stmt.bind(2, a_Permission); stmt.exec(); } - - trans.commit(); } catch (const SQLite::Exception & ex) { @@ -1458,12 +1491,10 @@ void cRankManager::RemovePermissionFromGroup(const AString & a_Permission, const bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrap everything into a transaction: - SQLite::Transaction trans(m_DB); - // Check that NewName doesn't exist: { SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); @@ -1484,7 +1515,6 @@ bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewNa res = (stmt.exec() > 0); } - trans.commit(); return res; } catch (const SQLite::Exception & ex) @@ -1502,12 +1532,10 @@ bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewNa bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrap everything into a transaction: - SQLite::Transaction trans(m_DB); - // Check that NewName doesn't exist: { SQLite::Statement stmt(m_DB, "SELECT PermGroupID FROM PermGroup WHERE Name = ?"); @@ -1528,7 +1556,6 @@ bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewN res = (stmt.exec() > 0); } - trans.commit(); return res; } catch (const SQLite::Exception & ex) @@ -1546,12 +1573,10 @@ bool cRankManager::RenameGroup(const AString & a_OldName, const AString & a_NewN void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a_PlayerName, const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { - // Wrap the entire operation into a transaction: - SQLite::Transaction trans(m_DB); - // Get the rank ID: int RankID; { @@ -1574,7 +1599,6 @@ void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a if (stmt.exec() > 0) { // Successfully updated the player's rank - trans.commit(); return; } } @@ -1587,7 +1611,6 @@ void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a if (stmt.exec() > 0) { // Successfully added the player - trans.commit(); return; } @@ -1615,6 +1638,7 @@ void cRankManager::SetRankVisuals( ) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -1646,6 +1670,7 @@ bool cRankManager::GetRankVisuals( ) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -1675,6 +1700,7 @@ bool cRankManager::GetRankVisuals( bool cRankManager::RankExists(const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -1700,6 +1726,7 @@ bool cRankManager::RankExists(const AString & a_RankName) bool cRankManager::GroupExists(const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -1725,6 +1752,7 @@ bool cRankManager::GroupExists(const AString & a_GroupName) bool cRankManager::IsPlayerRankSet(const AString & a_PlayerUUID) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -1750,6 +1778,7 @@ bool cRankManager::IsPlayerRankSet(const AString & a_PlayerUUID) bool cRankManager::IsGroupInRank(const AString & a_GroupName, const AString & a_RankName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { @@ -1781,6 +1810,7 @@ bool cRankManager::IsGroupInRank(const AString & a_GroupName, const AString & a_ bool cRankManager::IsPermissionInGroup(const AString & a_Permission, const AString & a_GroupName) { ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); try { -- cgit v1.2.3 From 3a7089539cc6be6e4ed00e17497e0dd327b9d67b Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 14 Aug 2014 23:06:46 +0200 Subject: RankMgr: Removed unneeded testing code. --- src/RankManager.cpp | 210 +--------------------------------------------------- 1 file changed, 2 insertions(+), 208 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index e64a47eb6..96c4baa56 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -12,215 +12,9 @@ -/* -// This code is for internal testing while developing the cRankManager class -static class cRankMgrTest -{ -public: - cRankMgrTest(void) : - m_Mgr() - { - // Initialize logging: - new cMCLogger(); - AString UUID = "b1caf24202a841a78055a079c460eee7"; // UUID for "xoft" - LOG("Testing UUID %s", UUID.c_str()); - - // Test the initial state of the ranks: - LOG("Initial test:"); - ReportPlayer(UUID); - - // Add a rank, a few groups and permissions and set the player to use them: - LOG("Adding data..."); - m_Mgr.AddRank("TestRank1", "[test]", "[/test]", "7"); - m_Mgr.AddRank("TestRank2", "[t2]", "[/t2]", "8"); - m_Mgr.AddGroup("TestGroup1"); - m_Mgr.AddGroup("TestGroup2"); - m_Mgr.AddGroupToRank("TestGroup1", "TestRank1"); - m_Mgr.AddGroupToRank("TestGroup2", "TestRank1"); - m_Mgr.AddGroupToRank("TestGroup2", "TestRank2"); - m_Mgr.AddPermissionToGroup("testpermission1.1", "TestGroup1"); - m_Mgr.AddPermissionToGroup("testpermission1.2", "TestGroup1"); - m_Mgr.AddPermissionToGroup("testpermission1.3", "TestGroup1"); - m_Mgr.AddPermissionToGroup("common", "TestGroup1"); - m_Mgr.AddPermissionToGroup("testpermission2.1", "TestGroup2"); - m_Mgr.AddPermissionToGroup("common", "TestGroup2"); - m_Mgr.SetPlayerRank(UUID, "xoft", "TestRank1"); - - // Test the added data: - LOG("Testing the added data:"); - LOG("IsGroupInRank(TestGroup1, TestRank1) = %s", m_Mgr.IsGroupInRank("TestGroup1", "TestRank1") ? "true" : "false"); - LOG("IsGroupInRank(TestGroup3, TestRank1) = %s", m_Mgr.IsGroupInRank("TestGroup3", "TestRank1") ? "true" : "false"); - LOG("IsPermissionInGroup(testpermission1.2, TestGroup1) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup1") ? "true" : "false"); // Existing permission, in group - LOG("IsPermissionInGroup(testpermission1.2, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.2", "TestGroup2") ? "true" : "false"); // Existing permission, not in group - LOG("IsPermissionInGroup(testpermission1.9, TestGroup2) = %s", m_Mgr.IsPermissionInGroup("testpermission1.9", "TestGroup2") ? "true" : "false"); // Non-existing permission - LOG("IsPlayerRankSet(%s) = %s", UUID.c_str(), m_Mgr.IsPlayerRankSet(UUID) ? "true" : "false"); - LOG("IsPlayerRankSet(%s1) = %s", UUID.c_str(), m_Mgr.IsPlayerRankSet(UUID + "1") ? "true" : "false"); - LOG("GroupExists(TestGroup1) = %s", m_Mgr.GroupExists("TestGroup1") ? "true" : "false"); - LOG("GroupExists(TestGroup3) = %s", m_Mgr.GroupExists("TestGroup3") ? "true" : "false"); - LOG("RankExists(TestRank1) = %s", m_Mgr.RankExists("TestRank1") ? "true" : "false"); - LOG("RankExists(NonexistentRank) = %s", m_Mgr.RankExists("NonexistentRank") ? "true" : "false"); - ReportRankGroups("TestRank1"); - ReportRankGroups("NonexistentRank"); - ReportGroupPermissions("TestGroup1"); - ReportGroupPermissions("NonexistentGroup"); - - // Report the contents of the DB: - ReportAll(); - - // Test the assignments above: - LOG("After-assignment test:"); - ReportPlayer(UUID); - - // Test removing a permission from a group: - LOG("Removing permission testpermission1.3 from group TestGroup1."); - m_Mgr.RemovePermissionFromGroup("testpermission1.3", "TestGroup1"); - ReportGroupPermissions("TestGroup1"); - LOG("Removing permission common from group TestGroup1."); - m_Mgr.RemovePermissionFromGroup("common", "TestGroup1"); - ReportGroupPermissions("TestGroup1"); // Check that it's not present - ReportGroupPermissions("TestGroup2"); // Check that it's still present here - - // Test removing a group from rank: - LOG("Removing group TestGroup2 from rank TestRank1."); - m_Mgr.RemoveGroupFromRank("TestGroup2", "TestRank1"); - ReportRankGroups("TestRank1"); - LOG("Removing group TestGroup3 from rank TestRank1."); - m_Mgr.RemoveGroupFromRank("TestGroup3", "TestRank1"); - ReportRankGroups("TestRank1"); - - // Test re-adding the groups: - LOG("Re-adding groups to TestRank1."); - m_Mgr.AddGroupToRank("TestGroup1", "TestRank1"); - m_Mgr.AddGroupToRank("TestGroup2", "TestRank1"); - ReportRankGroups("TestRank1"); - - // Test removing a group altogether: - LOG("Removing group TestGroup2"); - m_Mgr.RemoveGroup("TestGroup2"); - ReportAll(); - - // Test removing a rank: - LOG("Removing rank TestRank2, replacing with rank TestRank1."); - m_Mgr.RemoveRank("TestRank2", "TestRank1"); - ReportAll(); - LOG("Removing rank Test altogether."); - m_Mgr.RemoveRank("Test", ""); - ReportAll(); - - // Test renaming a rank: - LOG("Renaming rank TestRank1 to Test"); - m_Mgr.RenameRank("TestRank1", "Test"); - ReportRankGroups("TestRank1"); - ReportRankGroups("Test"); - LOG("Player after renaming:"); - ReportPlayer(UUID); - - // Test renaming a group: - LOG("Renaming group TestGroup1 to Test"); - m_Mgr.RenameGroup("TestGroup1", "Test"); - ReportGroupPermissions("TestGroup1"); - ReportGroupPermissions("Test"); - LOG("Player after renaming:"); - ReportPlayer(UUID); - m_Mgr.RenameGroup("Test", "TestGroup1"); - - // Test removing the rank in favor of another one: - m_Mgr.RemoveRank("Test", "TestRank2"); - LOG("After-removal test:"); - ReportPlayer(UUID); - - LOG("Done."); - } - - - void ReportAll(void) - { - // Report all ranks: - AStringVector Ranks = m_Mgr.GetAllRanks(); - LOG("All ranks (%u):", (unsigned)Ranks.size()); - for (AStringVector::const_iterator itr = Ranks.begin(), end = Ranks.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } - - // Report all groups: - AStringVector Groups = m_Mgr.GetAllGroups(); - LOG("All groups (%u):", (unsigned)Groups.size()); - for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } - - // Report all permissions: - AStringVector Permissions = m_Mgr.GetAllPermissions(); - LOG("All permissions (%u):", (unsigned)Permissions.size()); - for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } - } - - - void ReportPlayer(const AString & a_PlayerUUID) - { - // Get the player's UUID and rank: - LOG(" Rank: '%s'", m_Mgr.GetPlayerRankName(a_PlayerUUID).c_str()); - - // List all the permission groups for the player: - AStringVector Groups = m_Mgr.GetPlayerGroups(a_PlayerUUID); - LOG(" Groups (%u):", (unsigned)Groups.size()); - for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) - { - LOG(" '%s'" , itr->c_str()); - } // for itr - Groups[] - - // List all the permissions for the player: - AStringVector Permissions = m_Mgr.GetPlayerPermissions(a_PlayerUUID); - LOG(" Permissions (%u):", (unsigned)Permissions.size()); - for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } // for itr - Groups[] - } - - - void ReportRankGroups(const AString & a_RankName) - { - AStringVector Groups = m_Mgr.GetRankGroups(a_RankName); - LOG("Groups in rank %s: %u", a_RankName.c_str(), (unsigned)Groups.size()); - for (AStringVector::const_iterator itr = Groups.begin(), end = Groups.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } - AStringVector Permissions = m_Mgr.GetRankPermissions(a_RankName); - LOG("Permissions in rank %s: %u", a_RankName.c_str(), (unsigned)Permissions.size()); - for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } - } - - - void ReportGroupPermissions(const AString & a_GroupName) - { - AStringVector Permissions = m_Mgr.GetGroupPermissions(a_GroupName); - LOG("Permissions in group %s: %u", a_GroupName.c_str(), (unsigned)Permissions.size()); - for (AStringVector::const_iterator itr = Permissions.begin(), end = Permissions.end(); itr != end; ++itr) - { - LOG(" '%s'", itr->c_str()); - } - } - -protected: - cRankManager m_Mgr; -} g_RankMgrTest; -//*/ - - - - - //////////////////////////////////////////////////////////////////////////////// +// cRankManagerIniMigrator: + /** Migrates from groups.ini and users.ini into the rankmanager DB */ class cRankManagerIniMigrator { -- cgit v1.2.3 From 326dd7e4c6baa6070d35a2a46ca20404c623c8e1 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 21 Aug 2014 16:55:39 +0200 Subject: RankMgr: Added cRankManager::RemovePlayerRank(). --- src/RankManager.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 96c4baa56..65e5d264c 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -1424,6 +1424,29 @@ void cRankManager::SetPlayerRank(const AString & a_PlayerUUID, const AString & a +void cRankManager::RemovePlayerRank(const AString & a_PlayerUUID) +{ + ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); + + try + { + SQLite::Statement stmt(m_DB, "DELETE FROM PlayerRank WHERE PlayerUUID = ?"); + stmt.bind(1, a_PlayerUUID); + stmt.exec(); + } + catch(const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to remove rank from player UUID %s: %s", + __FUNCTION__, a_PlayerUUID.c_str(), ex.what() + ); + } +} + + + + + void cRankManager::SetRankVisuals( const AString & a_RankName, const AString & a_MsgPrefix, -- cgit v1.2.3 From 0c04bf962ed025789c1979c6d4fb122735b1a46b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 21 Aug 2014 20:47:52 +0200 Subject: cMojangAPI updates cRankManager's playernames. --- src/RankManager.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 65e5d264c..349582950 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -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 ( -- cgit v1.2.3 From 0daacd14d9ab678c08c45a1a1562d5ec160a3ff3 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 23 Aug 2014 03:44:04 +0200 Subject: RankMgr: Implemented default rank, added defaults. --- src/RankManager.cpp | 200 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 179 insertions(+), 21 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 349582950..0cee7724b 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -7,6 +7,7 @@ #include "RankManager.h" #include "inifile/iniFile.h" #include "Protocol/MojangAPI.h" +#include "ClientHandle.h" @@ -54,6 +55,9 @@ public: LOGD("Setting ranks..."); SetRanks(); + + LOGD("Creating defaults..."); + CreateDefaults(); return true; } @@ -87,8 +91,11 @@ protected: AString m_Name; AStringVector m_Groups; - /** Assigned by ResolveUserUUIDs() */ + /** Assigned by ResolveUserUUIDs(), contains the online (Mojang) UUID of the player. */ AString m_UUID; + + /** Assigned by ResolveUserUUIDs(), contains the offline (generated) UUID of the player. */ + AString m_OfflineUUID; sUser(void) {} @@ -275,22 +282,15 @@ protected: m_MojangAPI.GetUUIDsFromPlayerNames(PlayerNames); // Assign the UUIDs back to players, remove those not resolved: - for (sUserMap::iterator itr = m_Users.begin(); itr != m_Users.end(); ) + for (sUserMap::iterator itr = m_Users.begin(); itr != m_Users.end(); ++itr) { AString UUID = m_MojangAPI.GetUUIDFromPlayerName(itr->second.m_Name); if (UUID.empty()) { - LOGWARNING("RankMigrator: Cannot resolve player %s to UUID, player will be left unranked", itr->second.m_Name.c_str()); - sUserMap::iterator itr2 = itr; - ++itr2; - m_Users.erase(itr); - itr = itr2; - } - else - { - itr->second.m_UUID = UUID; - ++itr; + LOGWARNING("RankMigrator: Cannot resolve player %s to online UUID, player will be left unranked in online mode", itr->second.m_Name.c_str()); } + itr->second.m_UUID = UUID; + itr->second.m_OfflineUUID = cClientHandle::GenerateOfflineUUID(itr->second.m_Name); } } @@ -350,10 +350,28 @@ protected: AddGroupsToRank(Groups, RankName); } - // Set the rank to the user: - m_RankManager.SetPlayerRank(itr->second.m_UUID, itr->second.m_Name, RankName); + // Set the rank to the user, using both the online and offline UUIDs: + m_RankManager.SetPlayerRank(itr->second.m_UUID, itr->second.m_Name, RankName); + m_RankManager.SetPlayerRank(itr->second.m_OfflineUUID, itr->second.m_Name, RankName); } // for itr - m_Users[] } + + + + /** Creates the Default rank that contains the Default group, if it exists. + Sets the RankManager's default rank. */ + void CreateDefaults(void) + { + if (!m_RankManager.RankExists("Default")) + { + m_RankManager.AddRank("Default", "", "", ""); + if (!m_RankManager.IsGroupInRank("Default", "Default")) + { + m_RankManager.AddGroupToRank("Default", "Default"); + } + } + m_RankManager.SetDefaultRank("Default"); + } }; @@ -396,6 +414,7 @@ void cRankManager::Initialize(cMojangAPI & a_MojangAPI) m_DB.exec("CREATE TABLE IF NOT EXISTS PermGroup (PermGroupID INTEGER PRIMARY KEY, Name)"); m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)"); m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (PermGroupID INTEGER, Permission)"); + m_DB.exec("CREATE TABLE IF NOT EXISTS DefaultRank (RankID INTEGER)"); m_IsInitialized = true; @@ -410,12 +429,39 @@ void cRankManager::Initialize(cMojangAPI & a_MojangAPI) if (Migrator.Migrate()) { LOGINFO("Ranks migrated."); + // The default rank has been set by the migrator return; } + + // Migration failed. Add some defaults + LOGINFO("Rank migration failed, creating default ranks..."); + CreateDefaults(); + LOGINFO("Default ranks created."); } - // Migration failed. - // TODO: Check if tables empty, add some defaults then + // Load the default rank: + try + { + SQLite::Statement stmt(m_DB, + "SELECT Rank.Name FROM Rank " + "LEFT JOIN DefaultRank ON Rank.RankID = DefaultRank.RankID" + ); + if (stmt.executeStep()) + { + m_DefaultRank = stmt.getColumn(0).getText(); + } + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Cannot load default rank: %s", __FUNCTION__, ex.what()); + return; + } + + // If the default rank cannot be loaded, use the first rank: + if (m_DefaultRank.empty()) + { + SetDefaultRank(GetAllRanks()[0]); + } } @@ -1085,6 +1131,13 @@ void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_Repl { ASSERT(m_IsInitialized); cCSLock Lock(m_CS); + + // Check if the default rank is being removed with a proper replacement: + if ((a_RankName == m_DefaultRank) && !RankExists(a_ReplacementRankName)) + { + LOGWARNING("%s: Cannot remove rank %s, it is the default rank and the replacement rank doesn't exist.", __FUNCTION__, a_RankName.c_str()); + return; + } AStringVector res; try @@ -1143,6 +1196,12 @@ void cRankManager::RemoveRank(const AString & a_RankName, const AString & a_Repl stmt.bind(1, RemoveRankID); stmt.exec(); } + + // Update the default rank, if it was the one being removed: + if (a_RankName == m_DefaultRank) + { + m_DefaultRank = a_RankName; + } } catch (const SQLite::Exception & ex) { @@ -1310,21 +1369,30 @@ bool cRankManager::RenameRank(const AString & a_OldName, const AString & a_NewNa stmt.bind(1, a_NewName); if (stmt.executeStep()) { - LOGD("%s: Rank %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str()); + LOGINFO("%s: Rank %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str()); return false; } } // Rename: - bool res; { SQLite::Statement stmt(m_DB, "UPDATE Rank SET Name = ? WHERE Name = ?"); stmt.bind(1, a_NewName); stmt.bind(2, a_OldName); - res = (stmt.exec() > 0); + if (stmt.exec() <= 0) + { + LOGINFO("%s: There is no rank %s, cannot rename to %s.", __FUNCTION__, a_OldName.c_str(), a_NewName.c_str()); + return false; + } } - return res; + // Update the default rank, if it was the one being renamed: + if (a_OldName == m_DefaultRank) + { + m_DefaultRank = a_NewName; + } + + return true; } catch (const SQLite::Exception & ex) { @@ -1692,6 +1760,57 @@ void cRankManager::NotifyNameUUID(const AString & a_PlayerName, const AString & +bool cRankManager::SetDefaultRank(const AString & a_RankName) +{ + ASSERT(m_IsInitialized); + cCSLock Lock(m_CS); + + try + { + // Find the rank's ID: + int RankID = 0; + { + SQLite::Statement stmt(m_DB, "SELECT RankID FROM Rank WHERE Name = ?"); + stmt.bind(1, a_RankName); + if (!stmt.executeStep()) + { + LOGINFO("%s: Cannot set rank %s as the default, it does not exist.", __FUNCTION__, a_RankName.c_str()); + return false; + } + } + + // Set the rank as the default: + { + SQLite::Statement stmt(m_DB, "UPDATE DefaultRank SET RankID = ?"); + stmt.bind(1, RankID); + if (stmt.exec() < 1) + { + // Failed to update, there might be none in the DB, try inserting: + SQLite::Statement stmt2(m_DB, "INSERT INTO DefaultRank (RankID) VALUES (?)"); + stmt2.bind(1, RankID); + if (stmt2.exec() < 1) + { + LOGINFO("%s: Cannot update the default rank in the DB to %s.", __FUNCTION__, a_RankName.c_str()); + return false; + } + } + } + + // Set the internal cache: + m_DefaultRank = a_RankName; + return true; + } + catch (const SQLite::Exception & ex) + { + LOGWARNING("%s: Failed to update DB: %s", __FUNCTION__, ex.what()); + return false; + } +} + + + + + bool cRankManager::AreDBTablesEmpty(void) { return ( @@ -1699,7 +1818,8 @@ bool cRankManager::AreDBTablesEmpty(void) IsDBTableEmpty("PlayerRank") && IsDBTableEmpty("PermGroup") && IsDBTableEmpty("RankPermGroup") && - IsDBTableEmpty("PermissionItem") + IsDBTableEmpty("PermissionItem") && + IsDBTableEmpty("DefaultRank") ); } @@ -1724,3 +1844,41 @@ bool cRankManager::IsDBTableEmpty(const AString & a_TableName) + +void cRankManager::CreateDefaults(void) +{ + // Wrap everything in a big transaction to speed things up: + cMassChangeLock Lock(*this); + + // Create ranks: + AddRank("Default", "", "", ""); + AddRank("VIP", "", "", ""); + AddRank("Operator", "", "", ""); + AddRank("Admin", "", "", ""); + + // Create groups: + AddGroup("Default"); + AddGroup("Kick"); + AddGroup("Teleport"); + AddGroup("Everything"); + + // Add groups to ranks: + AddGroupToRank("Default", "Default"); + AddGroupToRank("Teleport", "VIP"); + AddGroupToRank("Teleport", "Operator"); + AddGroupToRank("Kick", "Operator"); + AddGroupToRank("Everything", "Admin"); + + // Add permissions to groups: + AddPermissionToGroup("core.build", "Default"); + AddPermissionToGroup("core.tp", "Teleport"); + AddPermissionToGroup("core.kick", "Kick"); + AddPermissionToGroup("*", "Everything"); + + // Set the default rank: + SetDefaultRank("Default"); +} + + + + -- cgit v1.2.3 From 8630b20c523033b359e4f9d7513cb6e4aafec1cb Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 24 Aug 2014 20:00:45 +0200 Subject: RankMgr: Default rank is applied to players without any rank. --- src/RankManager.cpp | 69 ++++++++++------------------------------------------- 1 file changed, 12 insertions(+), 57 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 0cee7724b..65ce33b92 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -534,34 +534,12 @@ AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) AStringVector cRankManager::GetPlayerPermissions(const AString & a_PlayerUUID) { - ASSERT(m_IsInitialized); - cCSLock Lock(m_CS); - - AStringVector res; - try + AString Rank = GetPlayerRankName(a_PlayerUUID); + if (Rank.empty()) { - // Prepare the DB statement: - SQLite::Statement stmt(m_DB, - "SELECT DISTINCT(PermissionItem.Permission) FROM PermissionItem " - "LEFT JOIN RankPermGroup " - "ON PermissionItem.PermGroupID = RankPermGroup.PermGroupID " - "LEFT JOIN PlayerRank " - "ON PlayerRank.RankID = RankPermGroup.RankID " - "WHERE PlayerRank.PlayerUUID = ?" - ); - stmt.bind(1, a_PlayerUUID); - - // Execute and get results: - while (stmt.executeStep()) - { - res.push_back(stmt.getColumn(0).getText()); - } + Rank = m_DefaultRank; } - catch (const SQLite::Exception & ex) - { - LOGWARNING("%s: Cannot get player permissions: %s", __FUNCTION__, ex.what()); - } - return res; + return GetRankPermissions(Rank); } @@ -742,39 +720,16 @@ bool cRankManager::GetPlayerMsgVisuals( AString & a_MsgNameColorCode ) { - ASSERT(m_IsInitialized); - cCSLock Lock(m_CS); - - AStringVector res; - try - { - SQLite::Statement stmt(m_DB, - "SELECT Rank.MsgPrefix, Rank.MsgSuffix, Rank.MsgNameColorCode FROM Rank " - "LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID " - "WHERE PlayerRank.PlayerUUID = ?" - ); - stmt.bind(1, a_PlayerUUID); - if (!stmt.executeStep()) - { - LOGD("%s: Player UUID %s not found in the DB, returning empty values.", __FUNCTION__, a_PlayerUUID.c_str()); - a_MsgPrefix.clear(); - a_MsgSuffix.clear(); - a_MsgNameColorCode.clear(); - return false; - } - a_MsgPrefix = stmt.getColumn(0).getText(); - a_MsgSuffix = stmt.getColumn(1).getText(); - a_MsgNameColorCode = stmt.getColumn(2).getText(); - return true; - } - catch (const SQLite::Exception & ex) + AString Rank = GetPlayerRankName(a_PlayerUUID); + if (Rank.empty()) { - LOGWARNING("%s: Failed to get ranks from DB: %s. Returning empty values.", __FUNCTION__, ex.what()); + // Rank not found, return failure: + a_MsgPrefix.clear(); + a_MsgSuffix.clear(); + a_MsgNameColorCode.clear(); + return false; } - a_MsgPrefix.clear(); - a_MsgSuffix.clear(); - a_MsgNameColorCode.clear(); - return false; + return GetRankVisuals(Rank, a_MsgPrefix, a_MsgSuffix, a_MsgNameColorCode); } -- cgit v1.2.3 From d8527c5429aa7ecaa31cc78c8c2fd3833d59266d Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 27 Aug 2014 15:13:13 +0300 Subject: Fixed basic style violations. --- src/RankManager.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/RankManager.cpp') diff --git a/src/RankManager.cpp b/src/RankManager.cpp index 65ce33b92..dc6eec9e4 100644 --- a/src/RankManager.cpp +++ b/src/RankManager.cpp @@ -125,7 +125,7 @@ protected: /** Maps lists of groups to rank names. Each group list is either a simple "" if there's only one group, - or ",,...", where the secondary groups are + or ", , ...", where the secondary groups are lowercased and alpha-sorted. This makes the group lists comparable for equivalence, simply by comparing their string names. The ranks are named "" for single-group players, and "AutoMigratedRank_N" for the composite ranks, @@ -507,10 +507,8 @@ AStringVector cRankManager::GetPlayerGroups(const AString & a_PlayerUUID) // Prepare the DB statement: SQLite::Statement stmt(m_DB, "SELECT PermGroup.Name FROM PermGroup " - "LEFT JOIN RankPermGroup " - "ON PermGroup.PermGroupID = RankPermGroup.PermGroupID " - "LEFT JOIN PlayerRank " - "ON PlayerRank.RankID = RankPermGroup.RankID " + "LEFT JOIN RankPermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID " + "LEFT JOIN PlayerRank ON PlayerRank.RankID = RankPermGroup.RankID " "WHERE PlayerRank.PlayerUUID = ?" ); stmt.bind(1, a_PlayerUUID); @@ -1473,7 +1471,7 @@ void cRankManager::RemovePlayerRank(const AString & a_PlayerUUID) stmt.bind(1, a_PlayerUUID); stmt.exec(); } - catch(const SQLite::Exception & ex) + catch (const SQLite::Exception & ex) { LOGWARNING("%s: Failed to remove rank from player UUID %s: %s", __FUNCTION__, a_PlayerUUID.c_str(), ex.what() -- cgit v1.2.3