Approved Infects (MySQL)

Gameplay Addons
Post Reply
User avatar
Jack GamePlay
Mod Developer
Mod Developer
Egypt
Posts: 541
Joined: 2 Years Ago
Location: Egypt
Has thanked: 31 times
Been thanked: 44 times
Age: 19
Contact:

Infects (MySQL)

#1

Post by Jack GamePlay » 1 Month Ago

| Description:
  • This plugin is going to be like infects counter for zombies only.

| Cvars:
  • Code: Select all

    ze_infects_host // Host to connect to
    ze_infects_user // User to login with
    ze_infects_pass // Password
    ze_infects_dbname // Database

| Code:
    1. #include <zombie_escape>
    2.  
    3. // Static (Change it if you need)
    4. new const g_szLogFile[] = "Zombie_Infects.log" // MySQL Errors log file
    5.  
    6. // MySQL Table
    7. new const g_szTable[] =
    8. " \
    9.     CREATE TABLE IF NOT EXISTS `Zombie_Infects` \
    10.     ( \
    11.         `id` int(12) NOT NULL AUTO_INCREMENT, \
    12.         `SteamID` varchar(34) NOT NULL, \
    13.         `Infects` int(16) NOT NULL, \
    14.         PRIMARY KEY (`id`) \
    15.     ); \
    16. "
    17.  
    18. // Variables
    19. new g_iInfects[33],
    20.     Handle:g_hTuple
    21.  
    22. // Cvars
    23. new g_pCvarDBInfo[4]
    24.  
    25. // Database
    26. enum
    27. {
    28.     Host = 0,
    29.     User,
    30.     Pass,
    31.     DB
    32. }
    33.  
    34. // Natives
    35. public plugin_natives()
    36. {
    37.     register_native("ze_get_user_infects", "native_ze_get_user_infects", 1)
    38.     register_native("ze_set_user_infects", "native_ze_set_user_infects", 1)
    39. }
    40.  
    41. public plugin_init()
    42. {
    43.     register_plugin("[ZE] Addons: Infects (MySQL)", "1.0", "Jack GamePlay")
    44.    
    45.     // Cvars
    46.     g_pCvarDBInfo[Host] = register_cvar("ze_infects_host", "localhost")
    47.     g_pCvarDBInfo[User] = register_cvar("ze_infects_user", "user")
    48.     g_pCvarDBInfo[Pass] = register_cvar("ze_infects_pass", "pass")
    49.     g_pCvarDBInfo[DB] = register_cvar("ze_infects_dbname", "dbname")
    50.  
    51.     // Initialize MySQL - Delay 0.1 second required so we make sure that our zombie_escape.cfg already executed and cvars values loaded from it
    52.     set_task(0.1, "Delay_MySQL_Init")
    53. }
    54.  
    55. public Delay_MySQL_Init()
    56. {
    57.     MySQL_Init()
    58. }
    59.  
    60. public MySQL_Init()
    61. {
    62.     new szHost[64], szUser[32], szPass[32], szDB[128]
    63.    
    64.     get_pcvar_string(g_pCvarDBInfo[Host], szHost, charsmax(szHost))
    65.     get_pcvar_string(g_pCvarDBInfo[User], szUser, charsmax(szUser))
    66.     get_pcvar_string(g_pCvarDBInfo[Pass], szPass, charsmax(szPass))
    67.     get_pcvar_string(g_pCvarDBInfo[DB], szDB, charsmax(szDB))
    68.    
    69.     g_hTuple = SQL_MakeDbTuple(szHost, szUser, szPass, szDB)
    70.    
    71.     // Let's ensure that the g_hTuple will be valid, we will access the database to make sure
    72.     new iErrorCode, szError[512], Handle:hSQLConnection
    73.    
    74.     hSQLConnection = SQL_Connect(g_hTuple, iErrorCode, szError, charsmax(szError))
    75.    
    76.     if(hSQLConnection != Empty_Handle)
    77.     {
    78.         log_amx("[MySQL][INFECTS] Successfully connected to host: %s (ALL IS OK).", szHost)
    79.         SQL_FreeHandle(hSQLConnection)
    80.     }
    81.     else
    82.     {
    83.         // Disable plugin, and display the error
    84.         set_fail_state("[INFECTS] Failed to connect to MySQL database: %s", szError)
    85.     }
    86.    
    87.     // Create our table
    88.     SQL_ThreadQuery(g_hTuple, "QueryCreateTable", g_szTable)
    89. }
    90.  
    91. public QueryCreateTable(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
    92. {
    93.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
    94. }
    95.  
    96. public client_putinserver(id)
    97. {
    98.     if (is_user_bot(id) || is_user_hltv(id))
    99.         return
    100.    
    101.     // Just 1 second delay
    102.     set_task(1.0, "DelayLoad", id)
    103. }
    104.  
    105. public DelayLoad(id)
    106. {
    107.     LoadInfects(id)
    108. }
    109.  
    110. public plugin_end()
    111. {
    112.     if (g_hTuple != Empty_Handle)
    113.     {
    114.         SQL_FreeHandle(g_hTuple)
    115.     }
    116. }
    117.  
    118. public ze_user_infected(iVictim, iInfector)
    119. {
    120.     if (iInfector == 0) // Server ID
    121.         return
    122.  
    123.     g_iInfects[iInfector] ++
    124.     SaveInfects(iInfector)
    125. }
    126.  
    127. LoadInfects(id)
    128. {
    129.     new szAuthID[35], szQuery[128], szData[5]
    130.     get_user_authid(id, szAuthID, charsmax(szAuthID))
    131.     formatex(szQuery, charsmax(szQuery), "SELECT `Infects` FROM `Zombie_Infects` WHERE ( `SteamID` = '%s' );", szAuthID)
    132.     num_to_str(id, szData, charsmax(szData))
    133.     SQL_ThreadQuery(g_hTuple, "QuerySelectData", szQuery, szData, charsmax(szData))
    134. }
    135.  
    136. public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
    137. {
    138.     if (SQL_IsFail(iFailState, iError, szError, g_szLogFile))
    139.         return
    140.    
    141.     new id = str_to_num(szData)
    142.    
    143.     // No results for this query means that player not saved before
    144.     if (!SQL_NumResults(hQuery))
    145.     {
    146.         // This is new player
    147.         g_iInfects[id] = 0
    148.        
    149.         // Get user steamid
    150.         new szAuthID[35], szQuery[128]
    151.         get_user_authid(id, szAuthID, charsmax(szAuthID))
    152.         formatex(szQuery, charsmax(szQuery), "INSERT INTO `Zombie_Infects` (`SteamID`, `Infects`) VALUES ('%s', '%d');", szAuthID, g_iInfects[id])
    153.         SQL_ThreadQuery(g_hTuple, "QueryInsertData", szQuery)
    154.         return
    155.     }
    156.    
    157.     g_iInfects[id] = SQL_ReadResult(hQuery, SQL_FieldNameToNum(hQuery, "Infects"))
    158. }
    159.  
    160. SaveInfects(id)
    161. {
    162.     new szAuthID[35]
    163.     get_user_authid(id, szAuthID, charsmax(szAuthID))
    164.    
    165.     new szQuery[128]
    166.     formatex(szQuery, charsmax(szQuery), "UPDATE `Zombie_Infects` SET `Infects` = '%d' WHERE `SteamID` = '%s';", g_iInfects[id], szAuthID)
    167.     SQL_ThreadQuery(g_hTuple, "QueryUpdateData", szQuery)
    168. }
    169.  
    170. public QueryInsertData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
    171. {
    172.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
    173. }
    174.  
    175. public QueryUpdateData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
    176. {
    177.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
    178. }
    179.  
    180. // Natives
    181. public native_ze_get_user_infects(id)
    182. {
    183.     if (!is_user_connected(id))
    184.     {
    185.         log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
    186.         return false
    187.     }
    188.    
    189.     return g_iInfects[id]
    190. }
    191.  
    192. public native_ze_set_user_infects(id, iAmount)
    193. {
    194.     if (!is_user_connected(id))
    195.     {
    196.         log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
    197.         return false
    198.     }
    199.    
    200.     g_iInfects[id] = iAmount
    201.     SaveInfects(id)
    202.     return true
    203. }
Image

User avatar
Muhammet20
Member
Member
Turkey
Posts: 95
Joined: 6 Months Ago
Has thanked: 8 times
Been thanked: 1 time
Contact:

#2

Post by Muhammet20 » 1 Month Ago

Great Work Jack
Don't Say This Is Impossible , Because Nothing Impossible.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests