summaryrefslogtreecommitdiffstats
path: root/src/RankManager.cpp
blob: 954edd099af4a3a9e2f853766c3fa003544f3330 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 <iostream>





/*
// 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;
}