77using Rocket . Unturned . Player ;
88using Steamworks ;
99using Rocket . Unturned . Chat ;
10+ using CDK . Data ;
11+ using CDK . Enum ;
12+ using fr34kyn01535 . Uconomy ;
13+ using Rocket . Core ;
14+ using Rocket . Core . Logging ;
1015
1116namespace CDK
1217{
@@ -30,30 +35,30 @@ public void Execute(IRocketPlayer caller,string[] args)
3035 {
3136 if ( ! Main . Instance . Database . IsPurchased ( UnturnedPlayer . FromName ( caller . DisplayName ) , args [ 0 ] ) )
3237 {
33- switch ( Main . Instance . Database . RedeemCDK ( UnturnedPlayer . FromName ( caller . DisplayName ) , args [ 0 ] ) )
38+ switch ( RedeemCDK ( UnturnedPlayer . FromName ( caller . DisplayName ) , args [ 0 ] ) )
3439 {
35- case DatabaseManager . RedeemCDKResult . Success :
40+ case EKeyReemeResult . Success :
3641 UnturnedChat . Say ( caller , Main . Instance . Translate ( "success" ) ) ;
3742 break ;
38- case DatabaseManager . RedeemCDKResult . Redeemed :
43+ case EKeyReemeResult . Redeemed :
3944 UnturnedChat . Say ( caller , Main . Instance . Translate ( "already_redeemed" ) , UnityEngine . Color . red ) ;
4045 break ;
41- case DatabaseManager . RedeemCDKResult . KeyNotFound :
46+ case EKeyReemeResult . KeyNotFound :
4247 UnturnedChat . Say ( caller , Main . Instance . Translate ( "key_dones't_exist" ) , UnityEngine . Color . red ) ;
4348 break ;
44- case DatabaseManager . RedeemCDKResult . MaxRedeemed :
49+ case EKeyReemeResult . MaxRedeemed :
4550 UnturnedChat . Say ( caller , Main . Instance . Translate ( "maxcount_reached" ) , UnityEngine . Color . red ) ;
4651 break ;
47- case DatabaseManager . RedeemCDKResult . Renewed :
52+ case EKeyReemeResult . Renewed :
4853 UnturnedChat . Say ( caller , Main . Instance . Translate ( "key_renewed" ) ) ;
4954 break ;
50- case DatabaseManager . RedeemCDKResult . Error :
55+ case EKeyReemeResult . Error :
5156 UnturnedChat . Say ( caller , Main . Instance . Translate ( "error" ) , UnityEngine . Color . red ) ;
5257 break ;
53- case DatabaseManager . RedeemCDKResult . PlayerNotMatch :
58+ case EKeyReemeResult . PlayerNotMatch :
5459 UnturnedChat . Say ( caller , Main . Instance . Translate ( "player_not_match" ) , UnityEngine . Color . red ) ;
5560 break ;
56- case DatabaseManager . RedeemCDKResult . KeyNotValid :
61+ case EKeyReemeResult . KeyNotValid :
5762 UnturnedChat . Say ( caller , Main . Instance . Translate ( "cdk_config_error" ) , UnityEngine . Color . red ) ;
5863 break ;
5964 }
@@ -68,5 +73,210 @@ public void Execute(IRocketPlayer caller,string[] args)
6873 UnturnedChat . Say ( caller , Main . Instance . Translate ( "invaild_param" , Syntax ) , UnityEngine . Color . red ) ;
6974 }
7075 }
76+
77+ private EKeyReemeResult RedeemCDK ( UnturnedPlayer player , string CDK )
78+ {
79+ var Database = Main . Instance . Database ;
80+ try
81+ {
82+ var cdkdata = Database . GetCDKData ( CDK ) ;
83+ var logdata = Database . GetLogData ( player . CSteamID . m_SteamID , ELogQueryType . ByCDK , CDK ) ;
84+ if ( cdkdata != null )
85+ {
86+ List < string > listItem = cdkdata . Items . Split ( ',' ) . ToList ( ) ;
87+ List < string > listAmount = cdkdata . Amount . Split ( ',' ) . ToList ( ) ;
88+ if ( cdkdata . Owner != 0 && cdkdata . Owner != player . CSteamID . m_SteamID )
89+ {
90+ return EKeyReemeResult . PlayerNotMatch ;
91+ }
92+
93+ if ( cdkdata . RedeemedTimes >= cdkdata . MaxRedeem )
94+ {
95+ return EKeyReemeResult . MaxRedeemed ;
96+ }
97+
98+ if ( logdata != null && ! cdkdata . Renew )
99+ {
100+ return EKeyReemeResult . Redeemed ;
101+ }
102+ else if ( logdata == null )
103+ {
104+ if ( ! KeyVailed ( cdkdata ) )
105+ {
106+ return EKeyReemeResult . KeyNotValid ;
107+ }
108+
109+ if ( listItem . Count != 0 && listAmount . Count == 0 )
110+ {
111+ for ( int i = 0 ; i < listItem . Count ; i ++ )
112+ {
113+ player . GiveItem ( ushort . Parse ( listItem [ i ] ) , 1 ) ;
114+ }
115+ }
116+ else if ( cdkdata . Items . Length != 0 && cdkdata . Amount . Length != 0 )
117+ {
118+
119+
120+ for ( int i = 0 ; i < listAmount . Count ; i ++ )
121+ {
122+ if ( ! player . GiveItem ( Convert . ToUInt16 ( listItem [ i ] ) , Convert . ToByte ( listAmount [ i ] ) ) )
123+ {
124+ UnturnedChat . Say ( player , Main . Instance . Translate ( "items_give_fail" ) ,
125+ UnityEngine . Color . red ) ;
126+ }
127+ }
128+ }
129+
130+ if ( cdkdata . Vehicle . HasValue )
131+ {
132+ player . GiveVehicle ( cdkdata . Vehicle . Value ) ;
133+ }
134+
135+ if ( cdkdata . Reputation . HasValue )
136+ {
137+ player . Player . skills . askRep ( cdkdata . Reputation . Value ) ;
138+ }
139+
140+ if ( cdkdata . Experience . HasValue )
141+ {
142+ player . Experience += cdkdata . Experience . Value ;
143+ }
144+
145+ if ( cdkdata . Money . HasValue )
146+ {
147+ Main . ExecuteDependencyCode ( "Uconomy" , ( IRocketPlugin uconomy ) =>
148+ {
149+ if ( uconomy . State == PluginState . Loaded )
150+ {
151+ Uconomy . Instance . Database . IncreaseBalance ( player . Id , cdkdata . Money . Value ) ;
152+ UnturnedChat . Say ( player ,
153+ Main . Instance . Translate ( "uconomy_gain" , cdkdata . Money . Value ,
154+ Uconomy . Instance . Configuration . Instance . MoneyName ) ) ;
155+ }
156+ } ) ;
157+ }
158+
159+ if ( cdkdata . GrantPermissionGroup != string . Empty && ! cdkdata . UsePermissionSync )
160+ {
161+ switch ( R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) )
162+ {
163+ case Rocket . API . RocketPermissionsProviderResult . Success :
164+ UnturnedChat . Say ( player ,
165+ Main . Instance . Translate ( "permission_granted" , cdkdata . GrantPermissionGroup ) ) ;
166+ break ;
167+ case Rocket . API . RocketPermissionsProviderResult . DuplicateEntry :
168+ UnturnedChat . Say ( player ,
169+ Main . Instance . Translate ( "permission_duplicate_entry" ,
170+ cdkdata . GrantPermissionGroup ) , UnityEngine . Color . yellow ) ;
171+ break ;
172+ default :
173+ UnturnedChat . Say ( player , Main . Instance . Translate ( "permission_grant_error" ) ,
174+ UnityEngine . Color . red ) ;
175+ break ;
176+ }
177+ }
178+ else if ( cdkdata . GrantPermissionGroup != string . Empty && cdkdata . UsePermissionSync )
179+ {
180+ Main . ExecuteDependencyCode ( "PermissionSync" , ( IRocketPlugin ps ) =>
181+ {
182+ if ( ps . State == PluginState . Loaded )
183+ {
184+ PermissionSync . Main . Instance . databese . AddPermission ( "CDKPlugin" , player ,
185+ cdkdata . GrantPermissionGroup , cdkdata . ValidUntil . ToString ( ) ) ;
186+ }
187+ } ) ;
188+ }
189+
190+ Database . SaveLogToDB ( new LogData ( CDK , player . CSteamID . m_SteamID , DateTime . Now , cdkdata . ValidUntil ,
191+ cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
192+ Database . IncreaseRedeemedTime ( CDK ) ;
193+ return EKeyReemeResult . Success ;
194+ }
195+ else if ( logdata != null && cdkdata . Renew )
196+ {
197+ if ( ! cdkdata . UsePermissionSync )
198+ {
199+ R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) ;
200+ Database . UpdateLogInDB ( new LogData ( CDK , player . CSteamID . m_SteamID , DateTime . Now , cdkdata . ValidUntil ,
201+ cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
202+ Database . UpdateRenew ( CDK ) ;
203+ return EKeyReemeResult . Renewed ;
204+ }
205+ else
206+ {
207+ Main . ExecuteDependencyCode ( "PermissionSync" , ( IRocketPlugin ps ) =>
208+ {
209+ if ( ps . State == PluginState . Loaded )
210+ {
211+ PermissionSync . Main . Instance . databese . UpdatePermission ( player ,
212+ cdkdata . GrantPermissionGroup , cdkdata . ValidUntil , "CDKPlugin" ) ;
213+ }
214+ } ) ;
215+ Database . UpdateLogInDB ( new LogData ( CDK , player . CSteamID . m_SteamID , DateTime . Now , cdkdata . ValidUntil ,
216+ cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
217+ Database . UpdateRenew ( CDK ) ;
218+ return EKeyReemeResult . Renewed ;
219+ }
220+ }
221+ }
222+ else
223+ {
224+ return EKeyReemeResult . KeyNotFound ;
225+ }
226+ }
227+ catch ( Exception ex )
228+ {
229+ Logger . LogException ( ex ) ;
230+ }
231+
232+ return EKeyReemeResult . Error ;
233+ }
234+
235+ private bool KeyVailed ( CDKData cdk )
236+ {
237+ if ( cdk . Amount == string . Empty && cdk . Items != string . Empty )
238+ {
239+ var list = cdk . Items . Split ( ',' ) . ToList ( ) ;
240+ for ( int i = 0 ; i < list . Count ; i ++ )
241+ {
242+ if ( ! ushort . TryParse ( list [ i ] , out ushort res ) )
243+ {
244+ Logger . LogError ( String . Format ( "CDK:{0} has id in Items not a ushort!" , cdk . CDK ) ) ;
245+ return false ;
246+ }
247+ }
248+ return true ;
249+ }
250+ List < string > listitem = cdk . Items . Split ( ',' ) . ToList ( ) ;
251+ List < string > listamount = cdk . Amount . Split ( ',' ) . ToList ( ) ;
252+ if ( listitem . Count != 0 && listamount . Count != 0 )
253+ {
254+ if ( listitem . Count != listamount . Count )
255+ {
256+ Logger . LogError ( String . Format ( "CDK:{0} Items and Amount Column length not equal! " , cdk . CDK ) ) ;
257+ return false ;
258+ }
259+
260+ for ( int i = 0 ; i < listitem . Count ; i ++ )
261+ {
262+ if ( ! ushort . TryParse ( listitem [ i ] , out ushort id ) )
263+ {
264+ Logger . LogError ( String . Format ( "CDK:{0} has id in Items not a ushort!" , cdk . CDK ) ) ;
265+ return false ;
266+ }
267+ }
268+
269+ for ( int i = 0 ; i < listamount . Count ; i ++ )
270+ {
271+ if ( ! byte . TryParse ( listamount [ i ] , out byte am ) )
272+ {
273+ Logger . LogError ( String . Format ( "CDK:{0} has amount in Amount not a byte. MAX 255!" , cdk . CDK ) ) ;
274+ return false ;
275+ }
276+ }
277+ }
278+
279+ return false ;
280+ }
71281 }
72282}
0 commit comments