|
12 | 12 | using Rocket.API; |
13 | 13 | using CDK.Data; |
14 | 14 | using Steamworks; |
15 | | -//using UnityEngine; |
16 | 15 | using Rocket.Core.Plugins; |
17 | 16 | using CDK.Enum; |
18 | 17 | using PermissionSync; |
| 18 | +using Dapper; |
19 | 19 |
|
20 | 20 | namespace CDK |
21 | 21 | { |
@@ -75,7 +75,7 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK) |
75 | 75 | try |
76 | 76 | { |
77 | 77 | var cdkdata = GetCDKData(CDK); |
78 | | - var logdata = GetLogData(player.CSteamID, ELogQueryType.ByCDK, CDK); |
| 78 | + var logdata = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByCDK, CDK); |
79 | 79 | if (cdkdata != null) |
80 | 80 | { |
81 | 81 | if (cdkdata.Owner != 0 && cdkdata.Owner != player.CSteamID.m_SteamID) |
@@ -232,116 +232,74 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK) |
232 | 232 |
|
233 | 233 | internal void CheckValid(UnturnedPlayer player) |
234 | 234 | { |
235 | | - LogData logData = GetLogData(player.CSteamID, ELogQueryType.ByTime); |
| 235 | + LogData logData = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByTime); |
236 | 236 | if (logData != null && logData.GrantPermissionGroup != string.Empty && !logData.UsePermissionSync) |
237 | 237 | { |
238 | 238 | do |
239 | 239 | { |
240 | 240 | CDKData cDKData = GetCDKData(logData.CDK); |
241 | 241 | R.Permissions.RemovePlayerFromGroup(cDKData.GrantPermissionGroup, player); |
242 | 242 | UnturnedChat.Say(player, Main.Instance.Translate("key_expired", logData.CDK)); |
243 | | - logData = GetLogData(player.CSteamID, ELogQueryType.ByTime); |
| 243 | + logData = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByTime); |
244 | 244 | } while (logData == null); |
245 | 245 | } |
246 | 246 | } |
247 | 247 |
|
248 | | - private CDKData BuildCDKData(MySqlDataReader reader) |
| 248 | + |
| 249 | + public CDKData GetCDKData(string key) |
249 | 250 | { |
250 | | - ulong owner = reader.IsDBNull(12) ? UInt64.MinValue : reader.GetUInt64(12); |
251 | | - string items = reader.IsDBNull(1) ? String.Empty : reader.GetString(1); |
252 | | - string amount = reader.IsDBNull(2) ? String.Empty : reader.GetString(2); |
253 | | - ushort vehicle = reader.IsDBNull(2) ? UInt16.MinValue : reader.GetUInt16(2); |
254 | | - ushort exp = reader.IsDBNull(4) ? UInt16.MinValue : reader.GetUInt16(4); |
255 | | - decimal money = reader.IsDBNull(6) ? Decimal.Zero : reader.GetDecimal(6); |
256 | | - int rep = reader.IsDBNull(7) ? Int32.MinValue : reader.GetInt32(7); |
257 | | - string permission = reader.IsDBNull(7) ? String.Empty : reader.GetString(7); |
258 | | - |
259 | | - return new CDKData(reader.GetString(0), items, |
260 | | - amount, vehicle, |
261 | | - exp, money, |
262 | | - rep, permission, reader.GetInt32(9), |
263 | | - reader.GetInt32(8), reader.GetDateTime(10), owner, reader.GetBoolean(11), reader.GetBoolean(13)); |
264 | | - } |
| 251 | + CDKData cdkData = null; |
| 252 | + var con = CreateConnection(); |
265 | 253 |
|
266 | | - private LogData BuildLogData(MySqlDataReader reader) |
267 | | - { |
268 | | - return new LogData(reader.GetString(0), reader.GetUInt64(1), reader.GetDateTime(2), |
269 | | - reader.GetDateTime(3), Convert.ToString(reader[4]), reader.GetBoolean(5)); |
270 | | - } |
271 | | - |
272 | | - public CDKData GetCDKData(string cdk) |
273 | | - { |
274 | | - CDKData data = null; |
275 | | - MySqlConnection connection = CreateConnection(); |
276 | 254 | try |
277 | 255 | { |
278 | | - MySqlCommand command = connection.CreateCommand(); |
279 | | - command.Parameters.AddWithValue("@CDK", cdk); |
280 | | - command.CommandText = |
281 | | - $"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;"; |
282 | | - connection.Open(); |
283 | | - MySqlDataReader reader = command.ExecuteReader(); |
284 | | - if (reader.Read()) |
285 | | - { |
286 | | - data = BuildCDKData(reader); |
287 | | - } |
| 256 | + cdkData = con.QuerySingle<CDKData>( |
| 257 | + $"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;", |
| 258 | + new { CDK = key }); |
288 | 259 | } |
289 | 260 | catch (Exception ex) |
290 | 261 | { |
291 | | - Logger.LogError("[Error] GetCDKData:"); |
292 | 262 | Logger.LogException(ex); |
293 | 263 | } |
294 | 264 | finally |
295 | 265 | { |
296 | | - connection.Close(); |
| 266 | + con.Close(); |
297 | 267 | } |
298 | 268 |
|
299 | | - return data; |
| 269 | + return cdkData; |
300 | 270 | } |
301 | 271 |
|
302 | | - public LogData GetLogData(CSteamID steamID, ELogQueryType type, string parameter = "") |
| 272 | + public LogData GetLogData(ulong steamid, ELogQueryType type, string parameter = "") |
303 | 273 | { |
304 | 274 | LogData logData = null; |
305 | | - MySqlConnection connection = CreateConnection(); |
| 275 | + var con = CreateConnection(); |
306 | 276 | try |
307 | 277 | { |
308 | | - MySqlCommand command = connection.CreateCommand(); |
309 | 278 | switch (type) |
310 | 279 | { |
311 | 280 | case ELogQueryType.ByCDK: |
312 | | - command.Parameters.AddWithValue("@steamid", steamID); |
313 | | - command.Parameters.AddWithValue("@cdk", parameter); |
314 | | - command.CommandText = |
315 | | - $"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = @steamid AND `CDK` = @cdk;"; |
316 | | - break; |
317 | | - case ELogQueryType.ByPermissionGroup: |
318 | | - command.Parameters.AddWithValue("@steamid", steamID); |
319 | | - command.Parameters.AddWithValue("@PermissionGroup", parameter); |
320 | | - command.CommandText = |
321 | | - $"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = @steamid AND `GrantPermissionGroup` = '@PermissionGroup';"; |
| 281 | + logData = con.QuerySingle<LogData>( |
| 282 | + $"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '{steamid}' and`CDK` = '@CDK'", |
| 283 | + new { CDK = parameter }); |
322 | 284 | break; |
323 | 285 | case ELogQueryType.ByTime: |
324 | | - command.Parameters.AddWithValue("@steamid", steamID); |
325 | | - command.CommandText = |
326 | | - $"select * from `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = @steamid and `ValidUntil` < now() LIMIT 1;"; |
| 286 | + logData = con.QuerySingle<LogData>( |
| 287 | + $"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '{steamid}' and ValidUtil < now()"); |
| 288 | + break; |
| 289 | + case ELogQueryType.ByPermissionGroup: |
| 290 | + logData = con.QuerySingle<LogData>( |
| 291 | + $"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = '{steamid}' and`GrantPermissionGroup` = '@permission'", |
| 292 | + new { permission = parameter }); |
327 | 293 | break; |
328 | | - } |
329 | | - |
330 | | - connection.Open(); |
331 | | - MySqlDataReader reader = command.ExecuteReader(); |
332 | | - if (reader.Read()) |
333 | | - { |
334 | | - logData = BuildLogData(reader); |
335 | 294 | } |
336 | 295 | } |
337 | | - catch (Exception ex) |
| 296 | + catch (Exception e) |
338 | 297 | { |
339 | | - Logger.LogError("[Error] GetLogData"); |
340 | | - Logger.LogException(ex); |
| 298 | + Logger.LogException(e); |
341 | 299 | } |
342 | 300 | finally |
343 | 301 | { |
344 | | - connection.Close(); |
| 302 | + con.Close(); |
345 | 303 | } |
346 | 304 |
|
347 | 305 | return logData; |
@@ -397,7 +355,7 @@ internal void IncreaseRedeemedTime(string cdk) |
397 | 355 | var CdkData = GetCDKData(CDK); |
398 | 356 | if (CdkData != null) |
399 | 357 | { |
400 | | - var log = GetLogData(player.CSteamID, ELogQueryType.ByPermissionGroup, CdkData.GrantPermissionGroup); |
| 358 | + var log = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByPermissionGroup, CdkData.GrantPermissionGroup); |
401 | 359 | if (log != null) |
402 | 360 | { |
403 | 361 | result = true; |
|
0 commit comments