· 6 years ago · Jul 26, 2019, 11:56 AM
1#include <amxmodx>
2#include <amxmisc>
3#include <sqlx>
4
5/*
6 -- This only for testing --
7
8 Schema:
9
10 CREATE TABLE IF NOT EXISTS `MySQLAdminsVIPs`
11 (
12 `id` int(16) NOT NULL,
13 `SteamID` varchar(36) NOT NULL,
14 `Flags` varchar(28) NOT NULL,
15 `ExpireDate` varchar(16) NOT NULL
16 PRIMARY KEY (`id`)
17 );
18
19 Insert a test row:
20
21 INSERT INTO `MySQLAdminsVIPs` (`id`, `SteamID`, `Flags`, `ExpireDate`) VALUES (`1`,'STEAM_0:1:6112132', 'abcd', '27-7-2019');
22*/
23
24#define MYSQL_TABLE "admins"
25
26// Static Vars
27new const g_szLogFile[] = "Escape-Coins.log" // MySQL Errors log file
28
29// Cvars
30new g_pCvarDBInfo[4],
31 Handle:g_hTuple,
32 g_szSteamID[MAX_PLAYERS+1][36],
33 g_szFlags[MAX_PLAYERS+1][56],
34 g_szExpirationDate[MAX_PLAYERS+1][16],
35 bool:g_bIsMySQLADMIN[33]
36
37// Structure
38enum
39{
40 Host = 0,
41 User,
42 Pass,
43 DB
44}
45
46public plugin_natives()
47{
48 register_native("is_mysql_admin", "native_is_mysql_admin", 1)
49}
50
51public plugin_init()
52{
53 register_plugin("SQL Load Admins", "1.0.0", "Raheem")
54
55 // Cvars
56 g_pCvarDBInfo[Host] = register_cvar("mysql_admins_host", "localhost")
57 g_pCvarDBInfo[User] = register_cvar("mysql_admins_user", "user")
58 g_pCvarDBInfo[Pass] = register_cvar("mysql_admins_password", "pass")
59 g_pCvarDBInfo[DB] = register_cvar("mysql_admins_dbname", "dbname")
60
61 // Initialize the connection
62 set_task(0.1, "Delay_MySQL_Init")
63
64 // Register /expire cmd, for who buys only
65 /*TODO*/
66}
67
68public Delay_MySQL_Init()
69{
70 new szHost[64], szUser[32], szPass[32], szDB[128]
71
72 get_pcvar_string(g_pCvarDBInfo[Host], szHost, charsmax(szHost))
73 get_pcvar_string(g_pCvarDBInfo[User], szUser, charsmax(szUser))
74 get_pcvar_string(g_pCvarDBInfo[Pass], szPass, charsmax(szPass))
75 get_pcvar_string(g_pCvarDBInfo[DB], szDB, charsmax(szDB))
76
77 g_hTuple = SQL_MakeDbTuple(szHost, szUser, szPass, szDB)
78
79 // Let's ensure that the g_hTuple will be valid, we will access the database to make sure
80 new iErrorCode, szError[512], Handle:hSQLConnection
81
82 hSQLConnection = SQL_Connect(g_hTuple, iErrorCode, szError, charsmax(szError))
83
84 if(hSQLConnection != Empty_Handle)
85 {
86 log_amx("[MySQL] Successfully connected to host: %s (ALL IS OK).", szHost)
87 SQL_FreeHandle(hSQLConnection)
88 }
89 else
90 {
91 // Disable plugin, and display the error
92 set_fail_state("Failed to connect to MySQL database: %s", szError)
93 }
94}
95
96public native_is_mysql_admin(id)
97{
98 return g_bIsMySQLADMIN[id]
99}
100
101public client_disconnected(id)
102{
103 // Reset Variables
104 g_bIsMySQLADMIN[id] = false
105
106 if (task_exists(id))
107 {
108 remove_task(id)
109 }
110}
111
112public client_putinserver(id)
113{
114 if (is_user_bot(id) || is_user_hltv(id) || !is_user_connected(id))
115 return
116 g_bIsMySQLADMIN[id] = false
117 LoadPlayerData(id)
118 set_task(0.5, "DelayChecks", id)
119}
120
121public DelayChecks(id)
122{
123 if (!g_bIsMySQLADMIN[id])
124 return
125
126 // Set user admin
127 set_user_flags(id, read_flags(g_szFlags[id]))
128}
129
130public LoadPlayerData(id)
131{
132 new szQuery[128], szData[5], szSteamId[36]
133
134 num_to_str(id, szData, charsmax(szData))
135 get_user_authid(id, szSteamId, charsmax(szSteamId))
136 formatex(szQuery, charsmax(szQuery), "SELECT * FROM `%s` WHERE ( `SteamID` = '%s' );", MYSQL_TABLE, szSteamId)
137 SQL_ThreadQuery(g_hTuple, "QuerySelectData", szQuery, szData, charsmax(szData))
138}
139
140public QuerySelectData(iFailState, Handle:hQuery, szError[], iError, szData[])
141{
142 if (SQL_IsFail(iFailState, iError, szError, g_szLogFile))
143 return
144
145 // SteamID is found
146 if (SQL_NumResults(hQuery))
147 {
148 new id = str_to_num(szData)
149
150 // Columns numbers
151 new iSteamIdCol = SQL_FieldNameToNum(hQuery, "SteamID")
152 new iFlagsCol = SQL_FieldNameToNum(hQuery, "Flags")
153 new iExpireCol = SQL_FieldNameToNum(hQuery, "ExpireDate")
154
155 // Read player data
156 SQL_ReadResult(hQuery, iSteamIdCol, g_szSteamID[id], charsmax(g_szSteamID[]))
157 SQL_ReadResult(hQuery, iFlagsCol, g_szFlags[id], charsmax(g_szFlags[]))
158 SQL_ReadResult(hQuery, iExpireCol, g_szExpirationDate[id], charsmax(g_szExpirationDate[]))
159 g_bIsMySQLADMIN[id] = true
160 CheckDate(g_szExpirationDate[id], id) // Check if this player expired or not
161 }
162}
163
164stock SQL_IsFail(iFailState, iError, szError[], const szLogFile[])
165{
166 if (iFailState == TQUERY_CONNECT_FAILED)
167 {
168 log_to_file(szLogFile, "[MySQL] Could not connect to SQL database: %s", szError)
169 return true
170 }
171 else if (iFailState == TQUERY_QUERY_FAILED)
172 {
173 log_to_file(szLogFile, "[MySQL] Query failed: %s", szError)
174 return true
175 }
176 else if (iError)
177 {
178 log_to_file(szLogFile, "[MySQL] Error on query: %s", szError)
179 return true
180 }
181
182 return false
183}
184
185/*
186* Dedicated stock for our plugin, to delete expired rows.
187* You should give it the expire date, and key.
188* Key should be unique like steam or name, so we can in another stock detect this row and delete it.
189*/
190stock CheckDate(szEndDate[], iKey)
191{
192 if (equali(szEndDate, "Infinite"))
193 return
194
195 new szCurrentDate[20],
196 szFormatedEndDate[20]
197
198 copy(szFormatedEndDate, charsmax(szFormatedEndDate), szEndDate)
199 get_time("%Y-%m-%d", szCurrentDate, charsmax(szCurrentDate))
200
201 if (equali(szCurrentDate, szFormatedEndDate))
202 {
203 Delete_Row(iKey)
204 }
205}
206
207/*
208* Stock used to delete row, based on key.
209* Key is steamid or name.
210*/
211stock Delete_Row(iKey)
212{
213 g_bIsMySQLADMIN[id] = false
214 new szQuery[128], szKey[36]
215 get_user_authid(iKey, szKey, charsmax(szKey))
216
217 formatex(szQuery, charsmax(szQuery), "DELETE FROM `%s` WHERE (`SteamID` = '%s');", MYSQL_TABLE, szKey)
218
219 SQL_ThreadQuery(g_hTuple, "QueryDeleteRow", szQuery)
220}
221
222public QueryDeleteRow(iFailState, Handle:hQuery, szError[], iError, szData[])
223{
224 SQL_IsFail(iFailState, iError, szError, g_szLogFile)
225}