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