2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
// RankManager.h
|
|
|
|
|
|
|
|
// Declares the cRankManager class that represents the rank manager responsible for assigning permissions and message visuals to players
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "SQLiteCpp/Database.h"
|
2014-08-13 06:33:31 -04:00
|
|
|
#include "SQLiteCpp/Transaction.h"
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-13 03:53:33 -04:00
|
|
|
class cMojangAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-05 12:37:00 -04:00
|
|
|
class cRankManager
|
|
|
|
{
|
|
|
|
public:
|
2014-08-13 06:33:31 -04:00
|
|
|
/** Acquire this lock to perform mass changes.
|
|
|
|
Improves performance by wrapping everything into a transaction.
|
|
|
|
Makes sure that no other thread is accessing the DB. */
|
|
|
|
class cMassChangeLock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cMassChangeLock(cRankManager & a_RankManager) :
|
|
|
|
m_Lock(a_RankManager.m_CS),
|
|
|
|
m_Transaction(a_RankManager.m_DB)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~cMassChangeLock()
|
|
|
|
{
|
|
|
|
m_Transaction.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
cCSLock m_Lock;
|
|
|
|
SQLite::Transaction m_Transaction;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-13 03:53:33 -04:00
|
|
|
/** Creates the rank manager. Needs to be initialized before other use. */
|
2014-08-05 12:37:00 -04:00
|
|
|
cRankManager(void);
|
2014-08-21 14:47:52 -04:00
|
|
|
|
|
|
|
~cRankManager();
|
2014-08-05 12:37:00 -04:00
|
|
|
|
2014-08-13 03:53:33 -04:00
|
|
|
/** Initializes the rank manager. Performs migration and default-setting if no data is found in the DB.
|
|
|
|
The a_MojangAPI param is used when migrating from old ini files, to look up player UUIDs. */
|
|
|
|
void Initialize(cMojangAPI & a_MojangAPI);
|
|
|
|
|
2014-08-05 12:37:00 -04:00
|
|
|
/** Returns the name of the rank that the specified player has assigned to them. */
|
|
|
|
AString GetPlayerRankName(const AString & a_PlayerUUID);
|
|
|
|
|
2014-08-08 03:56:28 -04:00
|
|
|
/** Returns the names of Groups that the specified player has assigned to them. */
|
|
|
|
AStringVector GetPlayerGroups(const AString & a_PlayerUUID);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Returns the permissions that the specified player has assigned to them. */
|
|
|
|
AStringVector GetPlayerPermissions(const AString & a_PlayerUUID);
|
|
|
|
|
2014-08-08 10:38:38 -04:00
|
|
|
/** Returns the names of groups that the specified rank has assigned to it.
|
|
|
|
Returns an empty vector if the rank doesn't exist. */
|
2014-08-08 03:56:28 -04:00
|
|
|
AStringVector GetRankGroups(const AString & a_RankName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
2014-08-08 10:38:38 -04:00
|
|
|
/** Returns the permissions that the specified group has assigned to it.
|
|
|
|
Returns an empty vector if the group doesn't exist. */
|
2014-08-08 03:56:28 -04:00
|
|
|
AStringVector GetGroupPermissions(const AString & a_GroupName);
|
|
|
|
|
2014-08-08 10:38:38 -04:00
|
|
|
/** Returns all permissions that the specified rank has assigned to it, through all its groups.
|
|
|
|
Returns an empty vector if the rank doesn't exist. Any non-existent groups are ignored. */
|
2014-08-08 03:56:28 -04:00
|
|
|
AStringVector GetRankPermissions(const AString & a_RankName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Returns the names of all defined ranks. */
|
|
|
|
AStringVector GetAllRanks(void);
|
|
|
|
|
|
|
|
/** Returns the names of all permission groups. */
|
2014-08-08 03:56:28 -04:00
|
|
|
AStringVector GetAllGroups(void);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Returns all the distinct permissions that are stored in the DB. */
|
|
|
|
AStringVector GetAllPermissions(void);
|
|
|
|
|
2014-08-08 15:30:47 -04:00
|
|
|
/** Returns the message visuals (prefix, postfix, color) for the specified player.
|
|
|
|
Returns true if the visuals were read from the DB, false if not (player not found etc). */
|
|
|
|
bool GetPlayerMsgVisuals(
|
2014-08-05 12:37:00 -04:00
|
|
|
const AString & a_PlayerUUID,
|
|
|
|
AString & a_MsgPrefix,
|
2014-08-05 16:28:37 -04:00
|
|
|
AString & a_MsgSuffix,
|
2014-08-05 12:37:00 -04:00
|
|
|
AString & a_MsgNameColorCode
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Adds a new rank. No action if the rank already exists. */
|
2014-08-05 16:28:37 -04:00
|
|
|
void AddRank(
|
|
|
|
const AString & a_RankName,
|
|
|
|
const AString & a_MsgPrefix,
|
|
|
|
const AString & a_MsgSuffix,
|
|
|
|
const AString & a_MsgNameColorCode
|
|
|
|
);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Adds a new permission group. No action if such a group already exists. */
|
2014-08-08 03:56:28 -04:00
|
|
|
void AddGroup(const AString & a_GroupName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
2014-08-13 06:33:31 -04:00
|
|
|
/** Bulk-adds groups. Group names that already exist are silently skipped. */
|
|
|
|
void AddGroups(const AStringVector & a_GroupNames);
|
|
|
|
|
2014-08-05 12:37:00 -04:00
|
|
|
/** Adds the specified permission group to the specified rank.
|
|
|
|
Fails if the rank or group names are not found.
|
|
|
|
Returns true if successful, false on error. */
|
2014-08-08 04:02:25 -04:00
|
|
|
bool AddGroupToRank(const AString & a_GroupName, const AString & a_RankName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Adds the specified permission to the specified permission group.
|
|
|
|
Fails if the permission group name is not found.
|
|
|
|
Returns true if successful, false on error. */
|
2014-08-08 03:56:28 -04:00
|
|
|
bool AddPermissionToGroup(const AString & a_Permission, const AString & a_GroupName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
2014-08-13 03:53:33 -04:00
|
|
|
/** Adds the specified permissions to the specified permission group.
|
|
|
|
Fails if the permission group name is not found.
|
|
|
|
Returns true if successful, false on error. */
|
|
|
|
bool AddPermissionsToGroup(const AStringVector & a_Permissions, const AString & a_GroupName);
|
|
|
|
|
2014-08-05 12:37:00 -04:00
|
|
|
/** Removes the specified rank.
|
2014-08-08 15:30:47 -04:00
|
|
|
All players assigned to that rank will be re-assigned to a_ReplacementRankName.
|
|
|
|
If a_ReplacementRankName is empty or not a valid rank, the player will be removed from the DB,
|
|
|
|
which means they will receive the default rank the next time they are queried. */
|
2014-08-05 12:37:00 -04:00
|
|
|
void RemoveRank(const AString & a_RankName, const AString & a_ReplacementRankName);
|
|
|
|
|
2014-08-08 03:56:28 -04:00
|
|
|
/** Removes the specified group completely.
|
|
|
|
The group will first be removed from all ranks using it, and then removed itself. */
|
|
|
|
void RemoveGroup(const AString & a_GroupName);
|
|
|
|
|
|
|
|
/** Removes the specified group from the specified rank.
|
|
|
|
The group will stay defined, even if no rank is using it. */
|
2014-08-08 15:30:47 -04:00
|
|
|
void RemoveGroupFromRank(const AString & a_GroupName, const AString & a_RankName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
2014-08-08 03:56:28 -04:00
|
|
|
/** Removes the specified permission from the specified group. */
|
|
|
|
void RemovePermissionFromGroup(const AString & a_Permission, const AString & a_GroupName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Renames the specified rank. No action if the rank name is not found.
|
|
|
|
Fails if the new name is already used.
|
|
|
|
Returns true on success, false on failure. */
|
|
|
|
bool RenameRank(const AString & a_OldName, const AString & a_NewName);
|
|
|
|
|
2014-08-08 03:56:28 -04:00
|
|
|
/** Renames the specified group. No action if the rank name is not found.
|
2014-08-05 12:37:00 -04:00
|
|
|
Fails if the new name is already used.
|
|
|
|
Returns true on success, false on failure. */
|
2014-08-08 03:56:28 -04:00
|
|
|
bool RenameGroup(const AString & a_OldName, const AString & a_NewName);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
|
|
|
/** Sets the specified player's rank.
|
2014-08-08 15:30:47 -04:00
|
|
|
If the player already had rank assigned to them, it is overwritten with the new rank and name.
|
2014-08-05 12:37:00 -04:00
|
|
|
Note that this doesn't change the cPlayer if the player is already connected, you need to update all the
|
2014-08-08 15:30:47 -04:00
|
|
|
cPlayer instances manually.
|
|
|
|
The PlayerName is provided for reference, so that GetRankPlayerNames() can work. */
|
|
|
|
void SetPlayerRank(const AString & a_PlayerUUID, const AString & a_PlayerName, const AString & a_RankName);
|
2014-08-21 10:55:39 -04:00
|
|
|
|
|
|
|
/** Removes the player's rank assignment. The player is left without a rank.
|
|
|
|
Note that this doesn't change the cPlayer instances for the already connected players, you need to update
|
|
|
|
all the instances manually.
|
|
|
|
No action if the player has no rank assigned to them already. */
|
|
|
|
void RemovePlayerRank(const AString & a_PlayerUUID);
|
2014-08-05 12:37:00 -04:00
|
|
|
|
2014-08-05 16:28:37 -04:00
|
|
|
/** Sets the message visuals of an existing rank. No action if the rank name is not found. */
|
|
|
|
void SetRankVisuals(
|
|
|
|
const AString & a_RankName,
|
|
|
|
const AString & a_MsgPrefix,
|
|
|
|
const AString & a_MsgSuffix,
|
|
|
|
const AString & a_MsgNameColorCode
|
|
|
|
);
|
2014-08-08 04:02:25 -04:00
|
|
|
|
2014-08-09 11:36:19 -04:00
|
|
|
/** Returns the message visuals of an existing rank.
|
|
|
|
Returns true if successful, false on error (rank doesn't exist). */
|
|
|
|
bool GetRankVisuals(
|
|
|
|
const AString & a_RankName,
|
|
|
|
AString & a_MsgPrefix,
|
|
|
|
AString & a_MsgSuffix,
|
|
|
|
AString & a_MsgNameColorCode
|
|
|
|
);
|
|
|
|
|
2014-08-08 04:02:25 -04:00
|
|
|
/** Returns true iff the specified rank exists in the DB. */
|
|
|
|
bool RankExists(const AString & a_RankName);
|
|
|
|
|
|
|
|
/** Returns true iff the specified group exists in the DB. */
|
|
|
|
bool GroupExists(const AString & a_GroupName);
|
|
|
|
|
|
|
|
/** Returns true iff the specified player has a rank assigned to them in the DB. */
|
|
|
|
bool IsPlayerRankSet(const AString & a_PlayerUUID);
|
|
|
|
|
|
|
|
/** Returns true iff the specified rank contains the specified group. */
|
|
|
|
bool IsGroupInRank(const AString & a_GroupName, const AString & a_RankName);
|
|
|
|
|
|
|
|
/** Returns true iff the specified group contains the specified permission. */
|
|
|
|
bool IsPermissionInGroup(const AString & a_Permission, const AString & a_GroupName);
|
|
|
|
|
2014-08-21 14:47:52 -04:00
|
|
|
/** Called by cMojangAPI whenever the playername-uuid pairing is discovered. Updates the DB. */
|
|
|
|
void NotifyNameUUID(const AString & a_PlayerName, const AString & a_UUID);
|
|
|
|
|
2014-08-05 12:37:00 -04:00
|
|
|
protected:
|
|
|
|
|
2014-08-13 06:33:31 -04:00
|
|
|
/** The database storage for all the data. Protected by m_CS. */
|
2014-08-05 12:37:00 -04:00
|
|
|
SQLite::Database m_DB;
|
2014-08-13 03:53:33 -04:00
|
|
|
|
2014-08-13 06:33:31 -04:00
|
|
|
/** The mutex protecting m_DB against multi-threaded access. */
|
|
|
|
cCriticalSection m_CS;
|
|
|
|
|
2014-08-13 03:53:33 -04:00
|
|
|
/** Set to true once the manager is initialized. */
|
|
|
|
bool m_IsInitialized;
|
2014-08-21 14:47:52 -04:00
|
|
|
|
|
|
|
/** The MojangAPI instance that is used for translating playernames to UUIDs.
|
|
|
|
Set in Initialize(), may be NULL. */
|
|
|
|
cMojangAPI * m_MojangAPI;
|
2014-08-13 03:53:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
/** Returns true if all the DB tables are empty, indicating a fresh new install. */
|
|
|
|
bool AreDBTablesEmpty(void);
|
|
|
|
|
|
|
|
/** Returns true iff the specified DB table is empty.
|
|
|
|
If there's an error while querying, returns false. */
|
|
|
|
bool IsDBTableEmpty(const AString & a_TableName);
|
2014-08-05 12:37:00 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|