1515//using UnityEngine;
1616using Rocket . Core . Plugins ;
1717using CDK . Enum ;
18+ using PermissionSync ;
1819
1920namespace CDK
2021{
@@ -93,7 +94,7 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
9394 } ) ;
9495 }
9596
96- if ( cdkdata . GrantPermissionGroup != string . Empty )
97+ if ( cdkdata . GrantPermissionGroup != string . Empty && ! cdkdata . UsePermissionSync )
9798 {
9899 switch ( R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) )
99100 {
@@ -108,17 +109,33 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
108109 break ;
109110 }
110111 }
112+ if ( cdkdata . GrantPermissionGroup != string . Empty && cdkdata . UsePermissionSync )
113+ {
114+ Main . ExecuteDependencyCode ( "PermissionSync" , ( IRocketPlugin plugin ) =>
115+ {
116+ PermissionSync . Main . Instance . databese . AddPermission ( "CDKPlugin" , player , cdkdata . GrantPermissionGroup , cdkdata . ValidUntil . ToString ( ) ) ;
117+ } ) ;
118+ }
111119
112- SaveLogToDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup ) ) ;
120+ SaveLogToDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
113121 IncreaseRedeemedTime ( CDK ) ;
114122 return RedeemCDKResult . Success ;
115123 }
116124 else if ( logdata != null && cdkdata . Renew )
117125 {
118- R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) ;
119- UpdateLogInDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup ) ) ;
120- UpdateRenew ( CDK ) ;
121- return RedeemCDKResult . Renewed ;
126+ if ( ! cdkdata . UsePermissionSync )
127+ {
128+ R . Permissions . AddPlayerToGroup ( cdkdata . GrantPermissionGroup , player ) ;
129+ UpdateLogInDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
130+ UpdateRenew ( CDK ) ;
131+ return RedeemCDKResult . Renewed ;
132+ }
133+ else
134+ {
135+ PermissionSync . Main . Instance . databese . UpdatePermission ( player , cdkdata . GrantPermissionGroup , cdkdata . ValidUntil , "CDKPlugin" ) ;
136+ UpdateLogInDB ( new LogData ( CDK , player . CSteamID , DateTime . Now , cdkdata . ValidUntil , cdkdata . GrantPermissionGroup , cdkdata . UsePermissionSync ) ) ;
137+ UpdateRenew ( CDK ) ;
138+ }
122139 }
123140 }
124141 else
@@ -156,12 +173,12 @@ private CDKData BuildCDKData(MySqlDataReader reader)
156173 {
157174 owner = new CSteamID ( Convert . ToUInt64 ( cid ) ) ;
158175 }
159- return new CDKData ( reader . GetString ( 0 ) , reader . GetString ( 1 ) , reader . GetString ( 2 ) , reader . GetUInt16 ( 3 ) , reader . GetUInt16 ( 4 ) , reader . GetDecimal ( 6 ) , reader . GetInt32 ( 5 ) , reader . GetString ( 7 ) , reader . GetInt32 ( 9 ) , reader . GetInt32 ( 8 ) , reader . GetDateTime ( 10 ) , owner , reader . GetBoolean ( 11 ) ) ;
176+ return new CDKData ( reader . GetString ( 0 ) , reader . GetString ( 1 ) , reader . GetString ( 2 ) , reader . GetUInt16 ( 3 ) , reader . GetUInt16 ( 4 ) , reader . GetDecimal ( 6 ) , reader . GetInt32 ( 5 ) , reader . GetString ( 7 ) , reader . GetInt32 ( 9 ) , reader . GetInt32 ( 8 ) , reader . GetDateTime ( 10 ) , owner , reader . GetBoolean ( 11 ) , reader . GetBoolean ( 12 ) ) ;
160177 }
161178 private LogData BuildLogData ( MySqlDataReader reader )
162179 {
163180 //Logger.LogWarning("Start Building LogData");
164- return new LogData ( reader . GetString ( 0 ) , ( CSteamID ) reader . GetUInt64 ( 1 ) , reader . GetDateTime ( 2 ) , reader . GetDateTime ( 3 ) , reader . GetString ( 4 ) ) ;
181+ return new LogData ( reader . GetString ( 0 ) , ( CSteamID ) reader . GetUInt64 ( 1 ) , reader . GetDateTime ( 2 ) , reader . GetDateTime ( 3 ) , reader . GetString ( 4 ) , reader . GetBoolean ( 5 ) ) ;
165182 }
166183
167184 public CDKData GetCDKData ( string cdk )
@@ -238,12 +255,12 @@ public LogData GetLogData(CSteamID steamID,ELogQueryType type,string parameter =
238255
239256 internal void SaveLogToDB ( LogData logData )
240257 {
241- ExecuteQuery ( true , $ "INSERT INTO `{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } ` (CDK,SteamID,`Redeemed Time`,ValidUntil,GrantPermissionGroup) VALUES('{ logData . CDK } ','{ logData . SteamID } ','{ logData . RedeemTime } ','{ logData . ValidUntil } ','{ logData . GrantPermissionGroup } ')") ;
258+ ExecuteQuery ( true , $ "INSERT INTO `{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } ` (CDK,SteamID,`Redeemed Time`,ValidUntil,GrantPermissionGroup,UsePermissionSync ) VALUES('{ logData . CDK } ','{ logData . SteamID } ','{ logData . RedeemTime } ','{ logData . ValidUntil } ','{ logData . GrantPermissionGroup } ',' { logData . UsePermissionSync } ')") ;
242259 }
243260
244261 internal void UpdateLogInDB ( LogData logData )
245262 {
246- ExecuteQuery ( true , $ "UPDATE `{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } ` SET `ValidUntil` = '{ logData . ValidUntil } ',`Redeemed Time` = { logData . RedeemTime } WHERE `SteamID` = '{ logData . SteamID } ' AND `CDK` = '{ logData . CDK } '") ;
263+ ExecuteQuery ( true , $ "UPDATE `{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } ` SET `ValidUntil` = '{ logData . ValidUntil } ',`Redeemed Time` = ' { logData . RedeemTime } ',`UsePermissionSync` = ' { logData . UsePermissionSync } ' WHERE `SteamID` = '{ logData . SteamID } ' AND `CDK` = '{ logData . CDK } '") ;
247264 }
248265 internal void UpdateRenew ( string cdk )
249266 {
@@ -258,16 +275,6 @@ internal void IncreaseRedeemedTime(string cdk)
258275 public bool IsPurchased ( UnturnedPlayer player , string CDK ) //check player if is first purchase
259276 {
260277 bool result = false ;
261- //var PermissionGroup = ExecuteQuery(true, $"select `GrantPermissionGroup` from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = '{CDK}';");
262- //var search = ExecuteQuery(true, $"select 1 from {Main.Instance.Configuration.Instance.DatabaseCDKTableName} where `GrantPermissionGroup` = '{PermissionGroup}' and `Owner` = '{player.Id}';");
263- //if (search == null)
264- //{
265- // result = false;
266- //}
267- //else
268- //{
269- // result = true;
270- //}
271278 var CdkData = GetCDKData ( CDK ) ;
272279 if ( CdkData != null )
273280 {
@@ -291,15 +298,23 @@ internal void CheckSchema() // intial mysql table
291298
292299 if ( cdk == null )
293300 ExecuteQuery ( false ,
294- $ "CREATE TABLE `{ Main . Instance . Configuration . Instance . DatabaseCDKTableName } ` (`CDK` varchar(32) NOT NULL,`Items` varchar(32) NOT NULL Default '0', `Amount` varchar(32) NOT NULL DEFAULT '0', `Vehicle` int(16) NOT NULL DEFAULT '0', `Experience` int(32) NOT NULL DEFAULT '0', `Reputation` int NOT NULL DEFAULT '0' , `Money` decimal(16,2) NOT NULL DEFAULT '0', `GrantPermissionGroup` varchar(32) NOT NULL DEFAULT '' , `MaxRedeem` int(32) NOT NULL DEFAULT '1', `RedeemedTimes` int(6) NOT NULL DEFAULT '0', `ValidUntil` datetime(6) NOT NULL DEFAULT '{ DateTime . MaxValue } ', `EnableRenew` BOOLEAN NOT NULL DEFAULT '0', `Owner` varchar(32) NOT NULL DEFAULT '' ,PRIMARY KEY (`CDK`))") ;
301+ $ "CREATE TABLE `{ Main . Instance . Configuration . Instance . DatabaseCDKTableName } ` (`CDK` varchar(32) NOT NULL,`Items` varchar(32) NOT NULL Default '0', `Amount` varchar(32) NOT NULL DEFAULT '0', `Vehicle` int(16) NOT NULL DEFAULT '0', `Experience` int(32) NOT NULL DEFAULT '0', `Reputation` int NOT NULL DEFAULT '0' , `Money` decimal(16,2) NOT NULL DEFAULT '0', `GrantPermissionGroup` varchar(32) NOT NULL DEFAULT '' , `MaxRedeem` int(32) NOT NULL DEFAULT '1', `RedeemedTimes` int(6) NOT NULL DEFAULT '0', `ValidUntil` datetime(6) NOT NULL DEFAULT '{ DateTime . MaxValue } ', `EnableRenew` BOOLEAN NOT NULL DEFAULT '0', `Owner` varchar(32) NOT NULL DEFAULT '' , `UsePermissionSync` BOOLEAN NOT NULL DEFAULT '0', PRIMARY KEY (`CDK`))") ;
295302
296303 var log = ExecuteQuery ( true ,
297304 $ "show tables like '{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } '") ;
298305
299306 if ( log == null )
300307 ExecuteQuery ( false ,
301308 $ "CREATE TABLE `{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } ` (`CDK` varchar(32) NOT NULL, `SteamID` varchar(32) NOT NULL, `Redeemed Time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `ValidUntil` datetime(6) NOT NULL, `GrantPermissionGroup` VARCHAR(32) NOT NULL DEFAULT '{ string . Empty } ')") ;
302-
309+ if ( Main . Instance . Configuration . Instance . MySQLTableVer == 1 )
310+ {
311+ ExecuteQuery ( true ,
312+ $ "ALTER TABLE `{ Main . Instance . Configuration . Instance . DatabaseCDKTableName } ` ADD `UsePermissionSync` BOOLEAN NOT NULL DEFAULT '0'") ;
313+ ExecuteQuery ( true ,
314+ $ "ALTER TABLE `{ Main . Instance . Configuration . Instance . DatabaseRedeemLogTableName } ` ADD `UsePermissionSync` BOOLEAN NOT NULL DEFAULT '0'") ;
315+ Main . Instance . Configuration . Instance . MySQLTableVer = 2 ;
316+ Main . Instance . Configuration . Save ( ) ;
317+ }
303318 }
304319
305320 private MySqlConnection CreateConnection ( )
0 commit comments