· 5 years ago · Aug 31, 2020, 01:36 AM
1#include <amxmodx>
2#include <sqlx>
3
4#define PLUGIN "Tutorial"
5#define VERSION "1.0"
6#define AUTHOR "Grim"
7
8// Ur Mysql Information
9new Host[] = "127.0.0.1"
10new User[] = "tithancs"
11new Pass[] = "vUahV6nFWsAZsWZ6"
12new Db[] = "tithancs"
13
14new g_id [ 33 ];
15new SteamID[33][32];
16new g_playername[33][32];
17// Ip
18new g_ip_actual[33][17]
19new UltimaIP[33][17];
20
21// variables otras
22new g_pesos[33]
23new g_tiempo_jugado[33] // segundos
24new g_FechadeRegistro[ 33 ][ 34 ];
25new g_FechadeUltima[ 33 ][ 34 ];
26new g_logeado[33]
27
28new const szTable [ ] = "mm_users";
29
30
31new Handle:g_SqlTuple
32new g_Error[512]
33
34
35new iExp[33]
36
37// Pcvar's
38new cKill
39new cHeadshot
40new cDeath
41
42public plugin_init() {
43 register_plugin(PLUGIN, VERSION, AUTHOR)
44
45 register_event("DeathMsg", "Event_DeathMsg", "a") // Register death event
46
47 // register the Pcvar's
48 cKill = register_cvar("exp_kill", "2")
49 cHeadshot = register_cvar("exp_headshot", "4")
50 cDeath = register_cvar("exp_death", "1")
51
52 set_task(1.0, "MySql_Init") // set a task to activate the mysql_init
53}
54
55public MySql_Init()
56{
57 // we tell the API that this is the information we want to connect to,
58 // just not yet. basically it's like storing it in global variables
59 g_SqlTuple = SQL_MakeDbTuple(Host,User,Pass,Db)
60
61 // ok, we're ready to connect
62 new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
63 if(SqlConnection == Empty_Handle)
64 // stop the plugin with an error message
65 set_fail_state(g_Error)
66
67 new Handle:Queries
68
69 new Query[512]
70 formatex(Query, charsmax(Query), "CREATE TABLE IF NOT EXISTS %s", szTable)
71 format(Query, charsmax(Query), "%s (id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,", Query)
72 format(Query, charsmax(Query), "%s Nombre varchar(32) NOT NULL UNIQUE,", Query)
73 format(Query, charsmax(Query), "%s SteamID varchar(34) DEFAULT '0',", Query)
74 format(Query, charsmax(Query), "%s FechadeRegistro timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,", Query)
75 format(Query, charsmax(Query), "%s FechadeUltima timestamp NOT NULL DEFAULT '0000-00-00 00:00:00,", Query)
76 format(Query, charsmax(Query), "%s Pesos int(10) DEFAULT '100',", Query)
77 format(Query, charsmax(Query), "%s UltimaIP varchar(34) DEFAULT '0',", Query)
78 format(Query, charsmax(Query), "%s TiempoJugado int(14) NOT NULL DEFAULT '0')", Query)
79
80 if(!SQL_Execute(Queries))
81 {
82 // if there were any problems
83 SQL_QueryError(Queries,g_Error,charsmax(g_Error))
84 set_fail_state(g_Error)
85
86 }
87
88 // close the handle
89 SQL_FreeHandle(Queries)
90
91 // you free everything with SQL_FreeHandle
92 SQL_FreeHandle(SqlConnection)
93
94
95}
96public plugin_end()
97{
98 // free the tuple - note that this does not close the connection,
99 // since it wasn't connected in the first place
100 SQL_FreeHandle(g_SqlTuple)
101}
102
103public Load_MySql(id)
104{
105 new szTemp[512]
106
107 new Data[1]
108 Data[0] = id
109
110 //we will now select from the table `tutorial` where the steamid match
111 format(szTemp,charsmax(szTemp),"SELECT * FROM %s WHERE SteamID = %s",szTable, SteamID[id])
112 SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1)
113}
114
115public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
116{
117 if(FailState == TQUERY_CONNECT_FAILED)
118 {
119 log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
120 }
121 else if(FailState == TQUERY_QUERY_FAILED)
122 {
123 log_amx("Load Query failed. [%d] %s", Errcode, Error)
124 }
125
126 new id
127 id = Data[0]
128
129 if(!SQL_NumResults(Query))
130 {
131 //.if there are no results found
132
133 new szSteamId[32]
134 get_user_authid(id, szSteamId, charsmax(szSteamId)) // get user's steamid
135
136 // if its still pending we can't do anything with it
137 if (equal(szSteamId,"ID_PENDING") || !equal(szSteamId, "STEAM_0:", 8))
138 {
139 server_print("%s jugador no steam, no se guardaran datos de este jugador [%s]", g_playername[id], SteamID[id])
140 return PLUGIN_HANDLED
141 }
142
143 new szTemp[512]
144
145 // now we will insturt the values into our table.
146 format(szTemp,charsmax(szTemp),"INSERT INTO %s ( `Nombre`, `SteamID`, `FechadeUltima`, `UltimaIP`)VALUES (^"%s^",^"%s^",now(),^"%s^")",szTable,g_playername[id],SteamID[id],UltimaIP[id])
147 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
148 }
149 else
150 {
151 // if there are results found
152 new col_id = SQL_FieldNameToNum(Query, "id")
153 new col_FechadeRegistro = SQL_FieldNameToNum(Query, "FechadeRegistro")
154 new col_FechadeUltima = SQL_FieldNameToNum(Query, "FechadeUltima")
155 new col_Pesos = SQL_FieldNameToNum(Query, "Pesos")
156 new col_UltimaIP = SQL_FieldNameToNum(Query, "UltimaIP")
157 new col_TiempoJugado = SQL_FieldNameToNum(Query, "TiempoJugado")
158
159 g_id[ id ] = SQL_ReadResult( Query, col_id );
160 SQL_ReadResult( Query, col_FechadeRegistro, g_FechadeRegistro[ id ], charsmax( g_FechadeRegistro[ ] ) );
161 SQL_ReadResult( Query, col_FechadeUltima, g_FechadeUltima[ id ], charsmax( g_FechadeUltima[ ] ) );
162 SQL_ReadResult( Query, col_UltimaIP, UltimaIP[ id ], charsmax( UltimaIP[ ] ) );
163 g_pesos[ id ] = SQL_ReadResult( Query, col_Pesos );
164 g_tiempo_jugado[id] = SQL_ReadResult( Query, col_TiempoJugado );
165 }
166
167 g_logeado[id] = true
168
169 return PLUGIN_HANDLED
170}
171
172public Save_MySql(id)
173{
174 new szTemp[512]
175 // Here we will update the user hes information in the database where the steamid matches.
176 formatex(szTemp, charsmax(szTemp),"UPDATE %s SET `Pesos` = %d, FechadeUltima = now(), `UltimaIP` = %s, `TiempoJugado` = %d WHERE `id` = %d;", szTable, g_pesos[id],g_ip_actual[id], g_tiempo_jugado[id] + get_user_time(id), g_id[id])
177 SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
178}
179
180public IgnoreHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
181{
182 SQL_FreeHandle(Query)
183
184 return PLUGIN_HANDLED
185}
186
187public client_putinserver(id)
188{
189 g_logeado[id] = false
190
191 get_user_authid(id, SteamID[ id ], charsmax(SteamID[]))
192 get_user_name(id,g_playername[id],charsmax(g_playername[]))
193 get_user_ip(id, g_ip_actual[id], charsmax(g_ip_actual[]), 1)
194
195 Load_MySql(id)
196}
197
198public client_disconnect(id)
199{
200 if(g_logeado[id])
201 Save_MySql(id)
202}
203
204public Event_DeathMsg()
205{
206 new iKiller = read_data(1) // read the data to get the killer and victim
207 new iVictim = read_data(2)
208
209 if(is_user_alive(iKiller)) // Check if the killer is alive in case he killed himself
210 {
211 if(read_data(3))
212 {
213 iExp[iKiller] += get_pcvar_num(cHeadshot) // Add the amount of the Pcvar to iExp
214 }
215 else
216 {
217 iExp[iKiller] += get_pcvar_num(cKill)
218 }
219 }
220 iExp[iVictim] -= get_pcvar_num(cDeath) // Decrease the amount of the Pcvar from iExp
221}
222
223stock print_EasyChat(const id, const input[], any:...) {
224 new iCount = 1, iPlayers[32]
225
226 static szMsg[191]
227 vformat(szMsg, charsmax(szMsg), input, 3)
228
229 replace_all(szMsg, 190, "\g", "^4")
230 replace_all(szMsg, 190, "\n", "^1")
231 replace_all(szMsg, 190, "\t", "^3")
232
233 if(id) iPlayers[0] = id
234
235 else get_players(iPlayers, iCount, "ch")
236
237 for (new i = 0; i < iCount; i++) {
238 if (is_user_connected(iPlayers[i])) {
239 message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), _, iPlayers[i])
240 write_byte(iPlayers[i])
241 write_string(szMsg)
242 message_end()
243 }
244 }
245}
246