- 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:
- #include <zombie_escape>
- // Static (Change it if you need)
- new const g_szLogFile[] = "Human_Escapes.log" // MySQL Errors log file
- // MySQL Table
- new const g_szTable[] =
- "ALTER TABLE `zombie_escape` ADD IF NOT EXISTS `Escapes` INT(10) NOT NULL DEFAULT '0';"
- // Variables
- new g_iEscapes[33],
- Handle:g_hTuple
- // Cvars
- new g_pCvarDBInfo[4]
- // Database
- enum
- {
- Host = 0,
- User,
- Pass,
- DB
- }
- // Natives
- public plugin_natives()
- {
- register_native("ze_get_user_escapes", "native_ze_get_user_escapes", 1)
- register_native("ze_set_user_escapes", "native_ze_set_user_escapes", 1)
- }
- public plugin_init()
- {
- register_plugin("[ZE] Addons: Escapes (MySQL)", "1.1", "Jack GamePlay")
- // Cvars
- g_pCvarDBInfo[Host] = register_cvar("ze_escapes_host", "localhost")
- g_pCvarDBInfo[User] = register_cvar("ze_escapes_user", "user")
- g_pCvarDBInfo[Pass] = register_cvar("ze_escapes_pass", "pass")
- g_pCvarDBInfo[DB] = register_cvar("ze_escapes_dbname", "dbname")
- // Initialize MySQL - Delay 0.1 second required so we make sure that our zombie_escape.cfg already executed and cvars values loaded from it
- set_task(0.1, "Delay_MySQL_Init")
- }
- public Delay_MySQL_Init()
- {
- MySQL_Init()
- }
- public MySQL_Init()
- {
- new szHost[64], szUser[32], szPass[32], szDB[128]
- get_pcvar_string(g_pCvarDBInfo[Host], szHost, charsmax(szHost))
- get_pcvar_string(g_pCvarDBInfo[User], szUser, charsmax(szUser))
- get_pcvar_string(g_pCvarDBInfo[Pass], szPass, charsmax(szPass))
- get_pcvar_string(g_pCvarDBInfo[DB], szDB, charsmax(szDB))
- g_hTuple = SQL_MakeDbTuple(szHost, szUser, szPass, szDB)
- // Let's ensure that the g_hTuple will be valid, we will access the database to make sure
- new iErrorCode, szError[512], Handle:hSQLConnection
- hSQLConnection = SQL_Connect(g_hTuple, iErrorCode, szError, charsmax(szError))
- if(hSQLConnection != Empty_Handle)
- {
- log_amx("[MySQL][ESCAPES] Successfully connected to host: %s (ALL IS OK).", szHost)
- SQL_FreeHandle(hSQLConnection)
- }
- else
- {
- // Disable plugin, and display the error
- set_fail_state("[ESCAPES] Failed to connect to MySQL database: %s", szError)
- }
- // Create our table
- SQL_ThreadQuery(g_hTuple, "QueryCreateTable", g_szTable)
- }
- public QueryCreateTable(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
- {
- SQL_IsFail(iFailState, iError, szError, g_szLogFile)
- }
- public client_putinserver(id)
- {
- if (is_user_bot(id) || is_user_hltv(id))
- return
- // Just 1 second delay
- set_task(1.0, "DelayLoad", id)
- }
- public DelayLoad(id)
- {
- LoadEscapes(id)
- }
- public plugin_end()
- {
- if (g_hTuple != Empty_Handle)
- {
- SQL_FreeHandle(g_hTuple)
- }
- }
- public ze_roundend(WinTeam)
- {
- if (WinTeam == ZE_TEAM_HUMAN)
- {
- for (new id = 0; id <= get_member_game(m_nMaxPlayers); id++)
- {
- if (!is_user_alive(id) || ze_is_user_zombie(id))
- continue
- g_iEscapes[id] ++
- SaveEscapes(id)
- }
- }
- }
- LoadEscapes(id)
- {
- new szAuthID[35], szQuery[128], szData[5]
- get_user_authid(id, szAuthID, charsmax(szAuthID))
- formatex(szQuery, charsmax(szQuery), "SELECT `Escapes` FROM `zombie_escape` WHERE ( `SteamID` = '%s' );", szAuthID)
- num_to_str(id, szData, charsmax(szData))
- SQL_ThreadQuery(g_hTuple, "QuerySelectData", szQuery, szData, charsmax(szData))
- }
- public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
- {
- if (SQL_IsFail(iFailState, iError, szError, g_szLogFile))
- return
- new id = str_to_num(szData)
- // No results for this query means that player not saved before
- if (!SQL_NumResults(hQuery))
- {
- // This is new player
- g_iEscapes[id] = 0
- return
- }
- g_iEscapes[id] = SQL_ReadResult(hQuery, SQL_FieldNameToNum(hQuery, "Escapes"))
- }
- SaveEscapes(id)
- {
- new szAuthID[36], szQuery[128]
- get_user_authid(id, szAuthID, charsmax(szAuthID))
- formatex(szQuery, charsmax(szQuery), "UPDATE `zombie_escape` SET `Escapes` = '%d' WHERE `SteamID` = '%s';", g_iEscapes[id], szAuthID)
- SQL_ThreadQuery(g_hTuple, "QueryUpdateData", szQuery)
- }
- public QueryInsertData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
- {
- SQL_IsFail(iFailState, iError, szError, g_szLogFile)
- }
- public QueryUpdateData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
- {
- SQL_IsFail(iFailState, iError, szError, g_szLogFile)
- }
- // Natives
- public native_ze_get_user_escapes(id)
- {
- if (!is_user_connected(id))
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
- return false
- }
- return g_iEscapes[id]
- }
- public native_ze_set_user_escapes(id, iAmount)
- {
- if (!is_user_connected(id))
- {
- log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
- return false
- }
- g_iEscapes[id] = iAmount
- SaveEscapes(id)
- return true
- }