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