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