@@ -21,14 +21,41 @@ namespace CDK
2121{
2222 public class DatabaseManager
2323 {
24- public enum RedeemCDKResult { Success , Redeemed , KeyNotFound , MaxRedeemed , Renewed , Error , PlayerNotMatch }
24+ public enum RedeemCDKResult { Success , Redeemed , KeyNotFound , MaxRedeemed , Renewed , Error , PlayerNotMatch , KeyNotValid }
2525
2626 //public enum CreateCDKResult { Success,Failure,KeyExist,Error}
2727 internal DatabaseManager ( )
2828 {
2929 CheckSchema ( ) ;
3030 }
3131
32+ private bool KeyVailed ( CDKData cdk )
33+ {
34+ if ( cdk . Items . Length != cdk . Amount . Length )
35+ {
36+ Logger . LogError ( String . Format ( "CDK:{0} Items and Amount Colunm length not equal! " , cdk . CDK ) ) ;
37+ return false ;
38+ }
39+ for ( int i = 0 ; i < cdk . Items . Length ; i ++ )
40+ {
41+ if ( ! ushort . TryParse ( cdk . Items [ i ] . ToString ( ) , out ushort id ) )
42+ {
43+ Logger . LogError ( String . Format ( "CDK:{0} has id in Items not a ushort!" , cdk . CDK ) ) ;
44+ return false ;
45+ }
46+ }
47+ for ( int i = 0 ; i < cdk . Amount . Length ; i ++ )
48+ {
49+ if ( ! byte . TryParse ( cdk . Amount [ i ] . ToString ( ) , out byte am ) )
50+ {
51+ Logger . LogError ( String . Format ( "CDK:{0} has amount in Amount not a byte. MAX 255!" , cdk . CDK ) ) ;
52+ return false ;
53+ }
54+ }
55+
56+ return true ;
57+ }
58+
3259 public RedeemCDKResult RedeemCDK ( UnturnedPlayer player , string CDK )
3360 {
3461 try
@@ -37,7 +64,7 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
3764 var logdata = GetLogData ( player . CSteamID , ELogQueryType . ByCDK , CDK ) ;
3865 if ( cdkdata != null )
3966 {
40- if ( cdkdata . Owner != CSteamID . Nil && cdkdata . Owner != player . CSteamID )
67+ if ( /* cdkdata.Owner != CSteamID.Nil &&*/ cdkdata . Owner != player . CSteamID )
4168 {
4269 return RedeemCDKResult . PlayerNotMatch ;
4370 }
@@ -49,107 +76,100 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
4976 {
5077 return RedeemCDKResult . Redeemed ;
5178 }
52- else if ( logdata == null && ! cdkdata . Renew )
79+ else if ( logdata == null )
5380 {
54-
55- if ( cdkdata . Items != string . Empty && cdkdata . Amount == string . Empty )
81+ if ( ! KeyVailed ( cdkdata ) )
5682 {
57- //foreach (string item in cdkdata.Items.Split(','))
58- //{
59- // player.GiveItem(Convert.ToUInt16(item), 1);
60- //}
61- var items = cdkdata . Items . Split ( ',' ) ;
62- for ( int i = 0 ; i < items . Length ; i ++ )
83+ UnturnedChat . Say ( player , Main . Instance . Translate ( "cdk_config_error" ) , UnityEngine . Color . red ) ;
84+ return RedeemCDKResult . KeyNotValid ;
85+ }
86+ else
87+ {
88+ if ( cdkdata . Items != string . Empty && cdkdata . Amount == string . Empty )
6389 {
64- if ( ushort . TryParse ( items [ i ] , out ushort id ) )
90+
91+ var items = cdkdata . Items . Split ( ',' ) ;
92+ for ( int i = 0 ; i < items . Length ; i ++ )
6593 {
66- player . GiveItem ( id , 1 ) ;
94+ player . GiveItem ( ushort . Parse ( items [ i ] ) , 1 ) ;
6795 }
6896 }
69- }
70- else if ( cdkdata . Items != string . Empty && cdkdata . Amount != string . Empty )
71- {
72- var items = cdkdata . Items . Split ( ',' ) ;
73- var amount = cdkdata . Amount . Split ( ',' ) ;
74- if ( items . Length == amount . Length )
97+ else if ( cdkdata . Items != string . Empty && cdkdata . Amount != string . Empty )
7598 {
76- for ( int i = 0 ; i < amount . Length ; i ++ )
99+ var items = cdkdata . Items . Split ( ',' ) ;
100+ var amount = cdkdata . Amount . Split ( ',' ) ;
101+
102+ for ( int i = 0 ; i < amount . Length ; i ++ )
77103 {
78- try
79- {
80- if ( ! player . GiveItem ( Convert . ToUInt16 ( items [ i ] ) , Convert . ToByte ( amount [ i ] ) ) )
81- {
82- UnturnedChat . Say ( player , Main . Instance . Translate ( "items_give_fail" ) , UnityEngine . Color . red ) ;
83- }
84- }
85- catch ( Exception ex )
104+ //try
105+ //{
106+ if ( ! player . GiveItem ( Convert . ToUInt16 ( items [ i ] ) , Convert . ToByte ( amount [ i ] ) ) )
86107 {
87- Logger . LogException ( ex ) ;
88- UnturnedChat . Say ( player , Main . Instance . Translate ( "cdk_config_error" ) , UnityEngine . Color . red ) ;
108+ UnturnedChat . Say ( player , Main . Instance . Translate ( "items_give_fail" ) , UnityEngine . Color . red ) ;
89109 }
110+ //}
111+ //catch(Exception ex)
112+ //{
113+ // Logger.LogException(ex);
114+ // UnturnedChat.Say(player, Main.Instance.Translate("cdk_config_error"), UnityEngine.Color.red);
115+ //}
90116 }
91117 }
92- else
118+
119+ if ( cdkdata . Vehicle != 0 )
93120 {
94- Logger . LogError ( String . Format ( "CDK: {0} item and amount length not equals" , cdkdata . CDK ) ) ;
95- UnturnedChat . Say ( player , Main . Instance . Translate ( "cdk_config_error" ) , UnityEngine . Color . red ) ;
121+ player . GiveVehicle ( cdkdata . Vehicle . Value ) ;
96122 }
97- }
98-
99- if ( cdkdata . Vehicle != 0 )
100- {
101- player . GiveVehicle ( cdkdata . Vehicle . Value ) ;
102- }
103- if ( cdkdata . Reputation != 0 )
104- {
105- //UnturnedChat.Say(player, "[DEBUG]EXP:" + cdkdata.Reputation);
106- player . Player . skills . askRep ( cdkdata . Reputation . Value ) ;
107- }
108- if ( cdkdata . Experience != 0 )
109- {
110- player . Experience += cdkdata . Experience . Value ;
111- }
112- if ( cdkdata . Money != 0 )
113- {
114- Main . ExecuteDependencyCode ( "Uconomy" , ( IRocketPlugin uconomy ) =>
123+ if ( cdkdata . Reputation != 0 )
124+ {
125+ player . Player . skills . askRep ( cdkdata . Reputation . Value ) ;
126+ }
127+ if ( cdkdata . Experience != 0 )
128+ {
129+ player . Experience += cdkdata . Experience . Value ;
130+ }
131+ if ( cdkdata . Money != 0 )
115132 {
116- if ( uconomy . State == PluginState . Loaded )
133+ Main . ExecuteDependencyCode ( "Uconomy" , ( IRocketPlugin uconomy ) =>
117134 {
118- Uconomy . Instance . Database . IncreaseBalance ( player . Id , cdkdata . Money . Value ) ;
119- UnturnedChat . Say ( player , Main . Instance . Translate ( "uconomy_gain" , Convert . ToDecimal ( cdkdata . Money . Value ) , Uconomy . Instance . Configuration . Instance . MoneyName ) ) ;
120- }
121- } ) ;
122- }
135+ if ( uconomy . State == PluginState . Loaded )
136+ {
137+ Uconomy . Instance . Database . IncreaseBalance ( player . Id , cdkdata . Money . Value ) ;
138+ UnturnedChat . Say ( player , Main . Instance . Translate ( "uconomy_gain" , Convert . ToDecimal ( cdkdata . Money . Value ) , Uconomy . Instance . Configuration . Instance . MoneyName ) ) ;
139+ }
140+ } ) ;
141+ }
123142
124- if ( cdkdata . GrantPermissionGroup != string . Empty && ! cdkdata . UsePermissionSync )
125- {
126- switch ( R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) )
143+ if ( cdkdata . GrantPermissionGroup != string . Empty && ! cdkdata . UsePermissionSync )
127144 {
128- case Rocket . API . RocketPermissionsProviderResult . Success :
129- UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_granted" , cdkdata . GrantPermissionGroup ) ) ;
130- break ;
131- case Rocket . API . RocketPermissionsProviderResult . DuplicateEntry :
132- UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_duplicate_entry" , cdkdata . GrantPermissionGroup ) , UnityEngine . Color . yellow ) ;
133- break ;
134- default :
135- UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_grant_error" ) , UnityEngine . Color . red ) ;
136- break ;
145+ switch ( R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) )
146+ {
147+ case Rocket . API . RocketPermissionsProviderResult . Success :
148+ UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_granted" , cdkdata . GrantPermissionGroup ) ) ;
149+ break ;
150+ case Rocket . API . RocketPermissionsProviderResult . DuplicateEntry :
151+ UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_duplicate_entry" , cdkdata . GrantPermissionGroup ) , UnityEngine . Color . yellow ) ;
152+ break ;
153+ default :
154+ UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_grant_error" ) , UnityEngine . Color . red ) ;
155+ break ;
156+ }
137157 }
138- }
139- if ( cdkdata . GrantPermissionGroup != string . Empty && cdkdata . UsePermissionSync )
140- {
141- Main . ExecuteDependencyCode ( "PermissionSync" , ( IRocketPlugin ps ) =>
158+ if ( cdkdata . GrantPermissionGroup != string . Empty && cdkdata . UsePermissionSync )
142159 {
143- if ( ps . State == PluginState . Loaded )
160+ Main . ExecuteDependencyCode ( "PermissionSync" , ( IRocketPlugin ps ) =>
144161 {
145- PermissionSync . Main . Instance . databese . AddPermission ( "CDKPlugin" , player , cdkdata . GrantPermissionGroup , cdkdata . ValidUntil . ToString ( ) ) ;
146- }
147- } ) ;
148- }
162+ if ( ps . State == PluginState . Loaded )
163+ {
164+ PermissionSync . Main . Instance . databese . AddPermission ( "CDKPlugin" , player , cdkdata . GrantPermissionGroup , cdkdata . ValidUntil . ToString ( ) ) ;
165+ }
166+ } ) ;
167+ }
149168
150- SaveLogToDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
151- IncreaseRedeemedTime ( CDK ) ;
152- return RedeemCDKResult . Success ;
169+ SaveLogToDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
170+ IncreaseRedeemedTime ( CDK ) ;
171+ return RedeemCDKResult . Success ;
172+ }
153173 }
154174 else if ( logdata != null && cdkdata . Renew )
155175 {
@@ -166,12 +186,12 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
166186 {
167187 if ( ps . State == PluginState . Loaded )
168188 {
169- //PermissionSync.Main.Instance.databese.AddPermission("CDKPlugin", player, cdkdata.GrantPermissionGroup, cdkdata.ValidUntil.ToString());
170189 PermissionSync . Main . Instance . databese . UpdatePermission ( player , cdkdata . GrantPermissionGroup , cdkdata . ValidUntil , "CDKPlugin" ) ;
171190 }
172191 } ) ;
173192 UpdateLogInDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
174193 UpdateRenew ( CDK ) ;
194+ return RedeemCDKResult . Renewed ;
175195 }
176196 }
177197 }
@@ -187,7 +207,7 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
187207 return RedeemCDKResult . Error ;
188208 }
189209
190- public void CheckValid ( UnturnedPlayer player )
210+ internal void CheckValid ( UnturnedPlayer player )
191211 {
192212 LogData logData = GetLogData ( player . CSteamID , ELogQueryType . ByTime ) ;
193213 if ( logData != null && logData . GrantPermissionGroup != string . Empty && ! logData . UsePermissionSync )
@@ -214,7 +234,6 @@ private CDKData BuildCDKData(MySqlDataReader reader)
214234 }
215235 private LogData BuildLogData ( MySqlDataReader reader )
216236 {
217- //Logger.LogWarning("Start Building LogData");
218237 return new LogData ( reader . GetString ( 0 ) , new CSteamID ( Convert . ToUInt64 ( reader . GetUInt64 ( 1 ) ) ) , reader . GetDateTime ( 2 ) , reader . GetDateTime ( 3 ) , reader . GetString ( 4 ) , reader . GetBoolean ( 5 ) ) ;
219238 }
220239
0 commit comments