Advertisement
Guest User

Untitled

a guest
Mar 4th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. #include <amxmodx>
  2. #include <sqlx>
  3.  
  4. new c_SqlHost, c_SqlUser, c_SqlPass, c_SqlDB, c_SqlType, g_Error[512], Handle:g_SqlTuple;
  5.  
  6. public plugin_init()
  7. {
  8. register_plugin("Player On", "1.0", "*Or!g!naL*")
  9. c_SqlHost = register_cvar( "zp_ammo_bank_host", "linuxclub.ro" )
  10. c_SqlUser = register_cvar( "zp_ammo_bank_user", "darkcs_zm" )
  11. c_SqlPass = register_cvar( "zp_ammo_bank_pass", "1234567890" )
  12. c_SqlDB = register_cvar( "zp_ammo_bank_db", "darkcs_zm" )
  13. c_SqlType = register_cvar( "zp_ammo_bank_type", "mysql" )
  14.  
  15. if( !_PrepareSQL() )
  16. {
  17. set_fail_state( "[ZP Ammo Bank] Fail to prepare SQL" )
  18. return;
  19. }
  20.  
  21. }
  22. public client_putinserver(id)
  23. {
  24. _LoadSQL(id)
  25. }
  26.  
  27. public client_disconnect(id)
  28. {
  29. _SaveSQL(id)
  30. }
  31. _PrepareSQL() {
  32.  
  33. new host[32], user[32], pass[32], db[32], type[12]
  34. get_pcvar_string( c_SqlHost, host, charsmax(host) )
  35. get_pcvar_string( c_SqlUser, user, charsmax(user) )
  36. get_pcvar_string( c_SqlPass, pass, charsmax(pass) )
  37. get_pcvar_string( c_SqlDB, db, charsmax(db) )
  38. get_pcvar_string( c_SqlType, type, charsmax(type) )
  39.  
  40. new get_type[12]
  41. SQL_GetAffinity( get_type, charsmax(get_type) )
  42.  
  43. log_amx( "[ZP Ammo Bank] SQL affinity type is %s", get_type )
  44.  
  45. if( !equali(get_type, type) )
  46. {
  47. if( !SQL_SetAffinity(type) )
  48. {
  49. log_amx( "[ZP Ammo Bank] Failed to set affinity from %s to %s", get_type, type )
  50. }
  51. }
  52.  
  53. g_SqlTuple = SQL_MakeDbTuple( host, user, pass, db )
  54.  
  55. if( g_SqlTuple == Empty_Handle )
  56. {
  57. log_amx( "[ZP Ammo Bank] Failed to create SQL tuple." )
  58. return 0;
  59. }
  60.  
  61. new const queryString[] = "CREATE TABLE IF NOT EXISTS `salutare` (\
  62. `auth` VARCHAR(64) NOT NULL,\
  63. `ZHunter` INT UNSIGNED NOT NULL,\
  64. PRIMARY KEY (`auth`) );"
  65.  
  66. new errcode, error[128];
  67. new Handle:link = SQL_Connect( g_SqlTuple, errcode, error, charsmax(error) )
  68.  
  69. if( link == Empty_Handle )
  70. {
  71. log_amx( "[ZP Ammo Bank] Failed to connect to database (%d): %s", errcode, error )
  72. return 0;
  73. }
  74.  
  75. new Handle:query = SQL_PrepareQuery( link, "%s", queryString )
  76.  
  77. if( !SQL_Execute(query) )
  78. {
  79. SQL_QueryError( query, error, charsmax(error) )
  80. log_amx( "[ZP Ammo Bank] Error creating table: %s", error )
  81.  
  82. return 0;
  83. }
  84. else
  85. {
  86. server_print( "[ZP Ammo Bank] SQL table successful create!" )
  87. }
  88.  
  89. SQL_FreeHandle(query)
  90. SQL_FreeHandle(link)
  91.  
  92. return 1;
  93. }
  94.  
  95. _MakeStringSQLSafe( output[], len ) {
  96.  
  97. replace_all( output, len, "'", "*" )
  98. replace_all( output, len, "^"", "*" ) //" - fix Sublime Text syntax highlight
  99. replace_all( output, len, "`", "*" )
  100. }
  101.  
  102. _LoadSQL(id) {
  103. new Name[64], onnli[64] = 1;
  104. get_user_name( id, Name, charsmax(Name) )
  105.  
  106. _MakeStringSQLSafe( Name, charsmax(Name) )
  107.  
  108. new queryString[512]
  109.  
  110. new data[2]
  111. data[0] = id
  112.  
  113. new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
  114. if(SqlConnection == Empty_Handle)
  115. set_fail_state(g_Error)
  116.  
  117. formatex( queryString, charsmax(queryString),
  118. "INSERT INTO salutare(auth, ZHunter) VALUES ('%s',1) ON DUPLICATE KEY UPDATE auth = VALUES(auth), ZHunter = VALUES(ZHunter)",
  119. Name, onnli);
  120.  
  121. new Handle:Query = SQL_PrepareQuery(SqlConnection,queryString)
  122.  
  123. if(!SQL_Execute(Query))
  124. {
  125. SQL_QueryError(Query,g_Error,511)
  126. }
  127.  
  128. while(SQL_MoreResults(Query))
  129. {
  130.  
  131. SQL_NextRow(Query)
  132. }
  133.  
  134. SQL_FreeHandle(Query)
  135.  
  136. SQL_FreeHandle(SqlConnection)
  137. }
  138.  
  139. _SaveSQL(id) {
  140. new data[2]
  141. data[0] = id
  142.  
  143. new Name[64], online[64] = 0;
  144. get_user_name( id, Name, charsmax(Name) )
  145.  
  146. _MakeStringSQLSafe( Name, charsmax(Name) )
  147.  
  148. new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
  149. if(SqlConnection == Empty_Handle)
  150. set_fail_state(g_Error)
  151.  
  152. new queryString[ 1024 ];
  153.  
  154. formatex( queryString, charsmax(queryString),
  155. "INSERT INTO salutare(auth, ZHunter) VALUES ('%s',0) ON DUPLICATE KEY UPDATE auth = VALUES(auth), ZHunter = VALUES(ZHunter)",
  156. Name, online);
  157.  
  158. new Handle:Query = SQL_PrepareQuery(SqlConnection,queryString)
  159. if(!SQL_Execute(Query))
  160. {
  161. SQL_QueryError(Query,g_Error,511)
  162. set_fail_state(g_Error)
  163. }
  164. SQL_FreeHandle(Query)
  165.  
  166. SQL_FreeHandle(SqlConnection)
  167. }
  168. public QuerySaveData( failstate, Handle:query, error[], errcode, data[], size, Float:queueTime )
  169. {
  170. if( failstate == TQUERY_CONNECT_FAILED )
  171. {
  172. log_amx( "[ZP Achievement] Failed connecting to save data (%d): %s", errcode, error )
  173. }
  174. else if( failstate == TQUERY_QUERY_FAILED )
  175. {
  176. log_amx( "[ZP Achievement] Failed query on save data (%d): %s", errcode, error )
  177. }
  178. else
  179. {
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement