Skip to content

Commit a48d907

Browse files
author
zeng-github01
committed
Fix bugs about cdk redemption
1 parent 33cf6ad commit a48d907

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

CDK/CDK.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@
105105
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
106106
</Reference>
107107
<Reference Include="System.Transactions" />
108-
<Reference Include="System.Xml.Linq" />
108+
<Reference Include="System.Xml.Linq">
109+
<Private>True</Private>
110+
</Reference>
109111
<Reference Include="System.Data.DataSetExtensions" />
110112
<Reference Include="Microsoft.CSharp" />
111113
<Reference Include="System.Data" />

CDK/Data/CDKData.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ namespace CDK.Data
1010
public class CDKData
1111
{
1212
public string CDK { get; internal set; }
13-
public string[] Items { get; internal set; }
13+
public string Items { get; internal set; }
1414
//public string Items { get; internal set; }
1515
//public string Amount { get; internal set; }
16-
public string[] Amount { get; internal set; }
16+
public string Amount { get; internal set; }
1717
public ushort? Vehicle { get; internal set; }
1818
public ushort? Experience { get; internal set; }
1919
public decimal? Money { get; internal set; }
2020
public int? Reputation { get; internal set; }
2121
public string GrantPermissionGroup { get; internal set; }
2222
public int RedeemedTimes { get; internal set; }
23-
public int? MaxRedeem { get; internal set; }
23+
public int MaxRedeem { get; internal set; }
2424
public DateTime ValidUntil { get; internal set; }
2525
public bool Renew { get; internal set; }
2626
public ulong Owner { get; internal set; }
2727

2828
public bool UsePermissionSync { get; internal set; }
29-
public CDKData(string cdk,string items, string amount, ushort? vehicle, ushort? exp, decimal? money, int? reputation, string permissonGroup, int redeemedTimes, int? maxredeem, DateTime validuntill, ulong steamID, bool renew, bool usePermissionSync)
29+
public CDKData(string cdk, string items, string amount, ushort? vehicle, ushort? exp, decimal? money, int? reputation, string permissonGroup, int redeemedTimes, int maxredeem, DateTime validuntill, ulong steamID, bool renew, bool usePermissionSync)
3030
{
3131
CDK = cdk;
32-
Items = items.Split(',');
33-
Amount = amount.Split(',');
32+
Items = items;
33+
Amount = amount;
3434
Vehicle = vehicle;
3535
Experience = exp;
3636
Money = money;
@@ -44,5 +44,6 @@ public CDKData(string cdk,string items, string amount, ushort? vehicle, ushort?
4444
UsePermissionSync = usePermissionSync;
4545
}
4646

47+
public CDKData() { }
4748
}
4849
}

CDK/Data/LogData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public LogData(string cdk,ulong steamID, DateTime redeemtime, DateTime validtime
2626
GrantPermissionGroup = PermissionGroup;
2727
UsePermissionSync = usePermissionSync;
2828
}
29+
30+
public LogData() { }
2931
}
3032
}

CDK/DatabaseManager.cs

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,21 @@ internal DatabaseManager()
4040

4141
private bool KeyVailed(CDKData cdk)
4242
{
43-
if (cdk.Items.Length != 0 && cdk.Amount.Length != 0)
43+
if(cdk.Amount == string.Empty && cdk.Items != string.Empty)
4444
{
45-
if (cdk.Items.Length != cdk.Amount.Length)
45+
return true;
46+
}
47+
List<string> listitem = cdk.Items.Split(',').ToList();
48+
List<string> listamount = cdk.Amount.Split(',').ToList();
49+
if (listitem.Count != 0 && listamount.Count != 0)
50+
{
51+
if (listitem.Count != listamount.Count)
4652
{
4753
Logger.LogError(String.Format("CDK:{0} Items and Amount Column length not equal! ", cdk.CDK));
4854
return false;
4955
}
5056

51-
for (int i = 0; i < cdk.Items.Length; i++)
57+
for (int i = 0; i < listitem.Count; i++)
5258
{
5359
if (!ushort.TryParse(cdk.Items[i].ToString(), out ushort id))
5460
{
@@ -57,7 +63,7 @@ private bool KeyVailed(CDKData cdk)
5763
}
5864
}
5965

60-
for (int i = 0; i < cdk.Amount.Length; i++)
66+
for (int i = 0; i < listamount.Count; i++)
6167
{
6268
if (!byte.TryParse(cdk.Amount[i].ToString(), out byte am))
6369
{
@@ -67,7 +73,7 @@ private bool KeyVailed(CDKData cdk)
6773
}
6874
}
6975

70-
return true;
76+
return false;
7177
}
7278

7379
public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
@@ -78,12 +84,14 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
7884
var logdata = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByCDK, CDK);
7985
if (cdkdata != null)
8086
{
87+
List<string> listItem = cdkdata.Items.Split(',').ToList();
88+
List<string> listAmount = cdkdata.Amount.Split(',').ToList();
8189
if (cdkdata.Owner != 0 && cdkdata.Owner != player.CSteamID.m_SteamID)
8290
{
8391
return RedeemCDKResult.PlayerNotMatch;
8492
}
8593

86-
if (cdkdata.MaxRedeem.HasValue && cdkdata.RedeemedTimes >= cdkdata.MaxRedeem.Value)
94+
if (cdkdata.RedeemedTimes >= cdkdata.MaxRedeem)
8795
{
8896
return RedeemCDKResult.MaxRedeemed;
8997
}
@@ -101,45 +109,43 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
101109

102110
//else
103111
//{
104-
if (cdkdata.Items.Length != 0 && cdkdata.Amount.Length == 0)
112+
if (listItem.Count != 0 && listAmount.Count == 0)
105113
{
106-
var items = cdkdata.Items;
107-
for (int i = 0; i < items.Length; i++)
114+
for (int i = 0; i < listItem.Count; i++)
108115
{
109-
player.GiveItem(ushort.Parse(items[i]), 1);
116+
player.GiveItem(ushort.Parse(listItem[i]), 1);
110117
}
111118
}
112119
else if (cdkdata.Items.Length != 0 && cdkdata.Amount.Length != 0)
113120
{
114-
var items = cdkdata.Items;
115-
var amount = cdkdata.Amount;
121+
116122

117-
for (int i = 0; i < amount.Length; i++)
123+
for (int i = 0; i < listAmount.Count; i++)
118124
{
119-
if (!player.GiveItem(Convert.ToUInt16(items[i]), Convert.ToByte(amount[i])))
125+
if (!player.GiveItem(Convert.ToUInt16(listItem[i]), Convert.ToByte(listAmount[i])))
120126
{
121127
UnturnedChat.Say(player, Main.Instance.Translate("items_give_fail"),
122128
UnityEngine.Color.red);
123129
}
124130
}
125131
}
126132

127-
if (cdkdata.Vehicle != 0)
133+
if (cdkdata.Vehicle.HasValue)
128134
{
129135
player.GiveVehicle(cdkdata.Vehicle.Value);
130136
}
131137

132-
if (cdkdata.Reputation != 0)
138+
if (cdkdata.Reputation.HasValue)
133139
{
134140
player.Player.skills.askRep(cdkdata.Reputation.Value);
135141
}
136142

137-
if (cdkdata.Experience != 0)
143+
if (cdkdata.Experience.HasValue)
138144
{
139145
player.Experience += cdkdata.Experience.Value;
140146
}
141147

142-
if (cdkdata.Money != 0)
148+
if (cdkdata.Money.HasValue)
143149
{
144150
Main.ExecuteDependencyCode("Uconomy", (IRocketPlugin uconomy) =>
145151
{
@@ -253,9 +259,10 @@ public CDKData GetCDKData(string key)
253259

254260
try
255261
{
256-
cdkData = con.QuerySingle<CDKData>(
257-
$"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;",
258-
new { CDK = key });
262+
var parameter = new DynamicParameters();
263+
parameter.Add("@CDK", key);
264+
cdkData = con.QueryFirstOrDefault<CDKData>(
265+
$"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;", parameter);
259266
}
260267
catch (Exception ex)
261268
{
@@ -269,27 +276,29 @@ public CDKData GetCDKData(string key)
269276
return cdkData;
270277
}
271278

272-
public LogData GetLogData(ulong steamid, ELogQueryType type, string parameter = "")
279+
public LogData GetLogData(ulong steamid, ELogQueryType type, string keyword = "")
273280
{
274281
LogData logData = null;
275282
var con = CreateConnection();
276283
try
277284
{
285+
var parameter = new DynamicParameters();
286+
parameter.Add("@SteamID", steamid);
278287
switch (type)
279288
{
280289
case ELogQueryType.ByCDK:
281-
logData = con.QuerySingle<LogData>(
282-
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '{steamid}' and`CDK` = '@CDK'",
283-
new { CDK = parameter });
290+
parameter.Add("@CDK", keyword);
291+
logData = con.QueryFirstOrDefault<LogData>(
292+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = @SteamID and `CDK` = @CDK", parameter);
284293
break;
285294
case ELogQueryType.ByTime:
286-
logData = con.QuerySingle<LogData>(
287-
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '{steamid}' and ValidUtil < now()");
295+
logData = con.QueryFirstOrDefault<LogData>(
296+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = @SteamID and ValidUntil < now()",parameter);
288297
break;
289298
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 });
299+
parameter.Add("@permission", keyword);
300+
logData = con.QueryFirstOrDefault<LogData>(
301+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = @SteamID and `GrantPermissionGroup` = @permission", parameter);
293302
break;
294303
}
295304
}

CDK/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("3.3.1.0")]
36-
[assembly: AssemblyFileVersion("3.3.1.0")]
35+
[assembly: AssemblyVersion("3.3.1.2")]
36+
[assembly: AssemblyFileVersion("3.3.1.2")]

0 commit comments

Comments
 (0)