Approved Escapes (MySQL)

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

Escapes (MySQL)

#1

Post by Jack GamePlay » 6 Months Ago

| Description:
  • This plugin is going to be like escapes counter for humans only.

| Cvars:
  • Code: Select all

    ze_escapes_host // Host to connect to
    ze_escapes_user // User to login with
    ze_escapes_pass // Password
    ze_escapes_dbname // Database

| Code:
    1. #include <zombie_escape>
    2.  
    3. // Static (Change it if you need)
    4. new const g_szLogFile[] = "Human_Escapes.log" // MySQL Errors log file
    5.  
    6. // MySQL Table
    7. new const g_szTable[] =
    8. " \
    9.     CREATE TABLE IF NOT EXISTS `Human_Escapes` \
    10.     ( \
    11.         `id` int(12) NOT NULL AUTO_INCREMENT, \
    12.         `SteamID` varchar(34) NOT NULL, \
    13.         `Escapes` int(16) NOT NULL, \
    14.         PRIMARY KEY (`id`) \
    15.     ); \
    16. "
    17.  
    18. // Variables
    19. new g_iEscapes[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_escapes", "native_ze_get_user_escapes", 1)
    38.     register_native("ze_set_user_escapes", "native_ze_set_user_escapes", 1)
    39. }
    40.  
    41. public plugin_init()
    42. {
    43.     register_plugin("[ZE] Addons: Escapes (MySQL)", "1.0", "Jack GamePlay")
    44.    
    45.     // Cvars
    46.     g_pCvarDBInfo[Host] = register_cvar("ze_escapes_host", "localhost")
    47.     g_pCvarDBInfo[User] = register_cvar("ze_escapes_user", "user")
    48.     g_pCvarDBInfo[Pass] = register_cvar("ze_escapes_pass", "pass")
    49.     g_pCvarDBInfo[DB] = register_cvar("ze_escapes_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][ESCAPES] 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("[ESCAPES] 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.     LoadEscapes(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_roundend(WinTeam)
    119. {
    120.     if (WinTeam == ZE_TEAM_HUMAN)
    121.     {
    122.         for (new id = 0; id <= get_member_game(m_nMaxPlayers); id++)
    123.         {
    124.             if (!is_user_alive(id) || ze_is_user_zombie(id))
    125.                 continue
    126.  
    127.             g_iEscapes[id] ++
    128.             SaveEscapes(id)
    129.         }
    130.     }
    131. }
    132.  
    133. LoadEscapes(id)
    134. {
    135.     new szAuthID[35], szQuery[128], szData[5]
    136.     get_user_authid(id, szAuthID, charsmax(szAuthID))
    137.     formatex(szQuery, charsmax(szQuery), "SELECT `Escapes` FROM `Human_Escapes` WHERE ( `SteamID` = '%s' );", szAuthID)
    138.     num_to_str(id, szData, charsmax(szData))
    139.     SQL_ThreadQuery(g_hTuple, "QuerySelectData", szQuery, szData, charsmax(szData))
    140. }
    141.  
    142. public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
    143. {
    144.     if (SQL_IsFail(iFailState, iError, szError, g_szLogFile))
    145.         return
    146.    
    147.     new id = str_to_num(szData)
    148.    
    149.     // No results for this query means that player not saved before
    150.     if (!SQL_NumResults(hQuery))
    151.     {
    152.         // This is new player
    153.         g_iEscapes[id] = 0
    154.        
    155.         // Get user steamid
    156.         new szAuthID[35], szQuery[128]
    157.         get_user_authid(id, szAuthID, charsmax(szAuthID))
    158.         formatex(szQuery, charsmax(szQuery), "INSERT INTO `Human_Escapes` (`SteamID`, `Escapes`) VALUES ('%s', '%d');", szAuthID, g_iEscapes[id])
    159.         SQL_ThreadQuery(g_hTuple, "QueryInsertData", szQuery)
    160.         return
    161.     }
    162.    
    163.     g_iEscapes[id] = SQL_ReadResult(hQuery, SQL_FieldNameToNum(hQuery, "Escapes"))
    164. }
    165.  
    166. SaveEscapes(id)
    167. {
    168.     new szAuthID[35], szQuery[128]
    169.     get_user_authid(id, szAuthID, charsmax(szAuthID))
    170.     formatex(szQuery, charsmax(szQuery), "UPDATE `Human_Escapes` SET `Escapes` = '%d' WHERE `SteamID` = '%s';", g_iEscapes[id], szAuthID)
    171.     SQL_ThreadQuery(g_hTuple, "QueryUpdateData", szQuery)
    172. }
    173.  
    174. public QueryInsertData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
    175. {
    176.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
    177. }
    178.  
    179. public QueryUpdateData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
    180. {
    181.     SQL_IsFail(iFailState, iError, szError, g_szLogFile)
    182. }
    183.  
    184. // Natives
    185. public native_ze_get_user_escapes(id)
    186. {
    187.     if (!is_user_connected(id))
    188.     {
    189.         log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
    190.         return false
    191.     }
    192.    
    193.     return g_iEscapes[id]
    194. }
    195.  
    196. public native_ze_set_user_escapes(id, iAmount)
    197. {
    198.     if (!is_user_connected(id))
    199.     {
    200.         log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
    201.         return false
    202.     }
    203.    
    204.     g_iEscapes[id] = iAmount
    205.     SaveEscapes(id)
    206.     return true
    207. }
Not interested in CS/Pawn anymore.
Accepting web projects.

Post Reply

Who is online

Users browsing this forum: Yandex [Bot] and 1 guest