· 6 years ago · Jan 15, 2020, 05:30 PM
1#include <zombie_escape>
2
3// Static (Change it if you need)
4new const g_szLogFile[] = "Human_Escapes.log" // MySQL Errors log file
5
6// MySQL Table
7new const g_szTable[] =
8"ALTER TABLE `zombie_escape` ADD IF NOT EXISTS `Escapes` INT(10) NOT NULL DEFAULT '0';"
9
10// Variables
11new g_iEscapes[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_escapes", "native_ze_get_user_escapes", 1)
30 register_native("ze_set_user_escapes", "native_ze_set_user_escapes", 1)
31}
32
33public plugin_init()
34{
35 register_plugin("[ZE] Addons: Escapes (MySQL)", "1.1", "Jack GamePlay")
36
37 // Cvars
38 g_pCvarDBInfo[Host] = register_cvar("ze_escapes_host", "145.239.236.240")
39 g_pCvarDBInfo[User] = register_cvar("ze_escapes_user", "srv55745")
40 g_pCvarDBInfo[Pass] = register_cvar("ze_escapes_pass", "ZD1cJQ361M")
41 g_pCvarDBInfo[DB] = register_cvar("ze_escapes_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][ESCAPES] 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("[ESCAPES] 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 LoadEscapes(id)
100}
101
102public plugin_end()
103{
104 if (g_hTuple != Empty_Handle)
105 {
106 SQL_FreeHandle(g_hTuple)
107 }
108}
109
110public ze_roundend(WinTeam)
111{
112 if (WinTeam == ZE_TEAM_HUMAN)
113 {
114 for (new id = 0; id <= get_member_game(m_nMaxPlayers); id++)
115 {
116 if (!is_user_alive(id) || ze_is_user_zombie(id))
117 continue
118
119 g_iEscapes[id] ++
120 SaveEscapes(id)
121 }
122 }
123}
124
125LoadEscapes(id)
126{
127 new szAuthID[35], szQuery[128], szData[5]
128 get_user_authid(id, szAuthID, charsmax(szAuthID))
129 formatex(szQuery, charsmax(szQuery), "SELECT `Escapes` FROM `zombie_escape` WHERE ( `SteamID` = '%s' );", szAuthID)
130 num_to_str(id, szData, charsmax(szData))
131 SQL_ThreadQuery(g_hTuple, "QuerySelectData", szQuery, szData, charsmax(szData))
132}
133
134public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
135{
136 if (SQL_IsFail(iFailState, iError, szError, g_szLogFile))
137 return
138
139 new id = str_to_num(szData)
140
141 // No results for this query means that player not saved before
142 if (!SQL_NumResults(hQuery))
143 {
144 // This is new player
145 g_iEscapes[id] = 0
146 return
147 }
148
149 g_iEscapes[id] = SQL_ReadResult(hQuery, SQL_FieldNameToNum(hQuery, "Escapes"))
150}
151
152SaveEscapes(id)
153{
154 new szAuthID[36], szQuery[128]
155 get_user_authid(id, szAuthID, charsmax(szAuthID))
156 formatex(szQuery, charsmax(szQuery), "UPDATE `zombie_escape` SET `Escapes` = '%d' WHERE `SteamID` = '%s';", g_iEscapes[id], szAuthID)
157 SQL_ThreadQuery(g_hTuple, "QueryUpdateData", szQuery)
158}
159
160public QueryInsertData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
161{
162 SQL_IsFail(iFailState, iError, szError, g_szLogFile)
163}
164
165public QueryUpdateData(iFailState, Handle:hQuery, szError[], iError, szData[], iSize, Float:flQueueTime)
166{
167 SQL_IsFail(iFailState, iError, szError, g_szLogFile)
168}
169
170// Natives
171public native_ze_get_user_escapes(id)
172{
173 if (!is_user_connected(id))
174 {
175 log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
176 return false
177 }
178
179 return g_iEscapes[id]
180}
181
182public native_ze_set_user_escapes(id, iAmount)
183{
184 if (!is_user_connected(id))
185 {
186 log_error(AMX_ERR_NATIVE, "[ZE] Invalid Player id (%d)", id)
187 return false
188 }
189
190 g_iEscapes[id] = iAmount
191 SaveEscapes(id)
192 return true
193}