· 6 years ago · Jun 30, 2019, 05:12 PM
1
2namespace Oxide.Plugins
3{
4 [Info("VIPSync", "NoSharp", "1.0.0")]
5 class VIPSync : RustPlugin
6 {
7
8 #region Fields
9
10 // For when I'm debugging this plugin.
11 bool Debug = true;
12
13 // Instantiates the Umod/oxide SqlLibrary.
14 private Core.MySql.Libraries.MySql sqlLibrary = Interface.Oxide.GetLibrary<Core.MySql.Libraries.MySql>();
15
16 // A named variable for Sql Connection.
17 private Connection sqlConnection;
18
19 #endregion Fields
20
21 /**
22 * Table Plan
23 * TABLE: CONFIG[TABLE]
24 * VALUES
25 * UserID Player ID (string)
26 * Ranks CSV roles.
27 */
28
29 #region Oxide Hooks
30
31 /// <summary>
32 /// Establishes connection with the database.
33 /// </summary>
34 void Init()
35 {
36 // Establish connection to db with configured values.
37
38
39 // Run table check if the table exists.
40 TableCheck();
41
42
43 foreach (var player in BasePlayer.activePlayerList)
44 {
45 SelectPlayer(player);
46 }
47 }
48
49 void OnPlayerInit(BasePlayer player)
50 {
51 SelectPlayer(player);
52 }
53
54 /// <summary>
55 /// When a player is granted a group.
56 /// </summary>
57 /// <param name="id"></param>
58 /// <param name="permName"></param>
59 void OnUserGroupAdded(string id, string groupName)
60 {
61 if (Debug) Puts($"Groups Added {id} ({groupName}) ");
62
63 IPlayer player = covalence.Players.FindPlayerById(id);
64
65
66 UpdatePlayer(player);
67
68
69 }
70
71 /// <summary>
72 /// When a player is removed from a group.
73 /// </summary>
74 /// <param name="id"></param>
75 /// <param name="permName"></param>
76 void OnUserGroupRemoved(string id, string groupName)
77 {
78 if (Debug) Puts($"Groups Removed {id} ({groupName}) ");
79
80 IPlayer player = covalence.Players.FindPlayerById(id);
81 UpdatePlayer(player);
82 }
83
84 /// <summary>
85 /// Used when creating a default configuration file.
86 /// </summary>
87 protected override void LoadDefaultConfig()
88 {
89
90 Puts("CREATING NEW CONFIGURATION FILE");
91 Config["Table"] = "RanksTable";
92 Config["Ip"] = "127.0.0.1";
93 Config["Database"] = "MainDB";
94 Config["Username"] = "root";
95 Config["Password"] = "root";
96 Config["Port"] = "3306";
97
98 }
99
100 #endregion Oxide Hooks
101
102 #region MySql Utils
103
104 /// <summary>
105 /// Checks if a table exists, if not, let's create one.
106 /// </summary>
107 void TableCheck()
108 {
109
110
111 sqlConnection = sqlLibrary.OpenDb(
112 Config["Ip"] as string,
113 int.Parse(Config["Port"] as string),
114 Config["Database"] as string,
115 Config["Username"] as string,
116 Config["Password"] as string,
117 this);
118 // SQL syntax for table creation.
119 string tableQuery = $"CREATE TABLE IF NOT EXISTS {Config["Table"] as string}(UserID varchar(255), Ranks TEXT)";
120
121 // Create SQL command.
122 Sql command = Sql.Builder.Append(tableQuery);
123
124 // Don't need to do anything with the call back, and query the database.
125 sqlLibrary.Query(command, sqlConnection, callback => { });
126 }
127
128 /// <summary>
129 /// Get's the players' roles when they join.
130 /// </summary>
131 /// <param name="player"></param>
132 /// <returns>A list of player Roles</returns>
133 void SelectPlayer(BasePlayer player)
134 {
135 string userId = player.UserIDString;
136 string selectQuery = $"SELECT * FROM {Config["Table"] as string} WHERE UserID = {userId} ";
137 if (Debug) Puts(selectQuery);
138 Sql command = Sql.Builder.Append(selectQuery);
139 string rolesUnParsed = "";
140 List<string> ParsedRoles = new List<string>();
141 sqlLibrary.Query(command, sqlConnection, returned =>
142 {
143 if (returned.Count == 0)
144 {
145 if (Debug) Puts("Returned is Null");
146
147 InsertPlayer(player);
148
149 return;
150 }
151
152 if (Debug) Puts(Newtonsoft.Json.JsonConvert.SerializeObject(returned, Newtonsoft.Json.Formatting.Indented));
153
154 foreach (Dictionary<string, object> entry in returned)
155 {
156 if (entry["Ranks"] as string == null)
157 {
158 Puts("RANK IS NULL");
159 UpdatePlayer(player.IPlayer);
160 return;
161 }
162
163 if (Debug) Puts(entry["UserID"] as string + "|||" + entry["Ranks"] as string);
164
165
166
167 rolesUnParsed = entry["Ranks"] as string;
168 ParsedRoles = ParseRoles(rolesUnParsed);
169 GroupCheck(ParsedRoles, player);
170 return;
171 }
172 });
173
174
175 return;
176 }
177
178 /// <summary>
179 /// A function to insert a player into the Mysql Database.
180 /// </summary>
181 /// <param name="player"></param>
182 void InsertPlayer(BasePlayer player)
183 {
184
185 string ranks = string.Join(",", permission.GetUserGroups(player.IPlayer.Id));
186
187 string query = $"INSERT INTO {Config["Table"] as string} (UserID, Ranks) VALUES ({player.UserIDString}, \"{ranks}\")";
188
189 if (Debug) Puts(query);
190
191 Sql command = Sql.Builder.Append(query);
192
193 sqlLibrary.Insert(command, sqlConnection);
194
195 }
196
197 /// <summary>
198 /// Used to update the player on the Mysql Database.
199 /// </summary>
200 /// <param name="ply"></param>
201 void UpdatePlayer(IPlayer ply)
202 {
203 string ranks = string.Join(",", permission.GetUserGroups(ply.Id));
204 string userId = BasePlayer.activePlayerList.Find(x => x.UserIDString == ply.Id).UserIDString;
205 string query = $"UPDATE {Config["Table"] as string} SET Ranks = \"{ranks}\" WHERE UserID = {userId}";
206
207 if (Debug) Puts(query);
208
209 Sql command = Sql.Builder.Append(query);
210
211 sqlLibrary.Insert(command, sqlConnection);
212
213 }
214
215 #endregion MySql Utils
216
217 #region Utils
218
219 /// <summary>
220 /// Used to add groups to player.
221 /// </summary>
222 /// <param name="roles"></param>
223 /// <param name="player"></param>
224 void GroupCheck(List<string> roles, BasePlayer player)
225 {
226 UserData data = permission.GetUserData(player.IPlayer.Id);
227
228 foreach (var role in permission.GetUserGroups(player.IPlayer.Id))
229 {
230 if (!roles.Contains(role))
231 {
232 data.Groups.Remove(role);
233
234 }
235
236 }
237
238
239 foreach (string role in roles)
240 {
241
242
243 if (!permission.UserHasGroup(player.IPlayer.Id, role))
244 {
245
246 data.Groups.Add(role);
247 }
248
249 }
250
251 }
252
253 /// <summary>
254 /// Converts Comma Seperated Roles into a List<string>.
255 /// </summary>
256 /// <param name="roles"></param>
257 /// <returns> A list of roles from the Comma Seperated Roles</returns>
258 List<string> ParseRoles(string roles)
259 {
260
261 var ParsedRoles = new List<string>();
262 foreach (var role in roles.Split(','))
263 {
264 ParsedRoles.Add(role);
265 }
266
267 return ParsedRoles;
268 }
269
270 #endregion Utils
271
272 }
273}