· 9 years ago · Oct 15, 2016, 02:44 PM
1/* Plugin generated by AMXX-Studio */
2
3#include < amxmodx >
4#include < fakemeta_util >
5
6#include < sqlx >
7#include < csx >
8
9#define PLUGIN "SQL Stats"
10#define VERSION "0.Ox"
11#define AUTHOR "Hades Ownage"
12
13#define Host ""
14#define User ""
15#define Pass ""
16#define Db ""
17#define Table "sqlstats"
18
19#define HEADSHOT_POINTS 2
20#define KILL_POINTS 1
21#define PLANT_POINTS 3
22#define DEFUSE_POINTS 3
23#define WALL_POINTS 2
24#define KNIFE_POINTS 10
25#define HE_POINTS 2
26
27enum _: UserStats {
28
29 Kills,
30 Deaths,
31 Points
32};
33
34new Player [ 33 ] [ UserStats ], Handle: g_SqlTuple, g_Error [ 512 ], g_iMaxPlayers, RankCount;
35
36public plugin_init ( ) {
37
38 register_plugin ( PLUGIN, VERSION, AUTHOR );
39
40 register_clcmd ( "say /rank", "Show_Rank" );
41 register_clcmd ( "say /top15", "Show_Top15" );
42
43 register_clcmd ( "say_team /rank", "Show_Rank" );
44 register_clcmd ( "say_team /top15", "Show_Top15" );
45
46 register_event ( "DeathMsg", "eDeath", "a" );
47
48 set_task ( 1.0, "MySql_Init" );
49
50 g_iMaxPlayers = get_maxplayers ( );
51}
52
53public plugin_end ( ) {
54
55 SQL_FreeHandle ( g_SqlTuple );
56
57}
58
59public client_putinserver ( id ) {
60
61 LoadData ( id );
62
63 new Data [ 2 ], szTemp [ 512 ];
64 Data [ 0 ] = id;
65
66 formatex ( szTemp, charsmax ( szTemp ),"SELECT COUNT(*) FROM `%s`", Table );
67 SQL_ThreadQuery ( g_SqlTuple, "Sql_Rank_Count", szTemp, Data, 1 );
68
69 return PLUGIN_CONTINUE;
70}
71
72public client_disconnect ( id ) {
73
74 SaveData ( id );
75
76}
77
78public MySql_Init ( ) {
79
80 g_SqlTuple = SQL_MakeDbTuple ( Host, User, Pass, Db );
81
82 new ErrorCode,Handle: SqlConnection = SQL_Connect ( g_SqlTuple, ErrorCode, g_Error, charsmax ( g_Error ) );
83
84 if ( SqlConnection == Empty_Handle )
85 set_fail_state ( g_Error );
86
87 new Handle: Queries;
88 Queries = SQL_PrepareQuery ( SqlConnection, "CREATE TABLE IF NOT EXISTS %s (nickname varchar(32), kills TEXT(11), deaths INT(11), points INT(11), name varchar(32))", Table );
89
90 if ( !SQL_Execute ( Queries ) ) {
91
92 SQL_QueryError ( Queries, g_Error, charsmax ( g_Error ) );
93 set_fail_state ( g_Error );
94 }
95
96 SQL_FreeHandle ( Queries );
97 SQL_FreeHandle ( SqlConnection );
98}
99
100public LoadData ( id ) {
101
102 new ErrorCode, Handle: SqlConnection = SQL_Connect ( g_SqlTuple, ErrorCode, g_Error, charsmax ( g_Error ) );
103
104 if ( g_SqlTuple == Empty_Handle )
105 set_fail_state ( g_Error );
106
107 new szName [ 32 ], szTemp [ 512 ];
108 get_user_name ( id, szName, charsmax ( szName ) );
109
110 new Data [ 1 ];
111 Data [ 0 ] = id;
112
113 formatex ( szTemp, charsmax ( szTemp ), "SELECT * FROM `%s` WHERE (`%s`.`nickname` = '%s')", Table, Table, szName );
114
115 SQL_ThreadQuery ( g_SqlTuple, "register_client", szTemp, Data, 1 );
116 SQL_FreeHandle ( SqlConnection );
117}
118
119public register_client ( FailState, Handle: Query, Error [ ], Errcode, Data [ ], DataSize ) {
120
121 if ( FailState == TQUERY_CONNECT_FAILED )
122 log_amx("[%s] Nu ma pot conecta la baza de date. [%d] %s", PLUGIN, Errcode, Error );
123
124 else if ( FailState == TQUERY_QUERY_FAILED )
125 log_amx("[%s] Load Query failed. [%d] %s", PLUGIN, Errcode, Error );
126
127 new id = Data [ 0 ];
128
129 if ( SQL_NumResults ( Query ) < 1 ) {
130
131 new szName [ 32 ], szTemp [ 512 ];
132 get_user_name ( id, szName, charsmax ( szName ) );
133
134 new ErrorCode, Handle: SqlConnection = SQL_Connect ( g_SqlTuple, ErrorCode, g_Error, charsmax ( g_Error ) );
135 if ( g_SqlTuple == Empty_Handle )
136 set_fail_state ( g_Error );
137
138 formatex ( szTemp, charsmax ( szTemp ),"INSERT INTO `%s`(`nickname`, `kills`, `deaths`, `points`) VALUES ('%s', '0', '0', '0')", Table, szName );
139
140 SQL_ThreadQuery ( g_SqlTuple, "IgnoreHandle", szTemp );
141 SQL_FreeHandle ( SqlConnection );
142 }
143
144 else {
145
146 Player [ id ] [ Kills ] = SQL_ReadResult ( Query, 1 );
147 Player [ id ] [ Deaths ] = SQL_ReadResult ( Query, 2 );
148 Player [ id ] [ Points ] = SQL_ReadResult ( Query, 3 );
149
150 }
151
152 return PLUGIN_CONTINUE;
153}
154
155public IgnoreHandle ( FailState, Handle: Query, Error [ ], Errcode, Data [ ], DataSize ) {
156
157 SQL_FreeHandle ( Query );
158 return PLUGIN_HANDLED;
159}
160
161public SaveData ( id ) {
162
163 new ErrorCode, Handle: SqlConnection = SQL_Connect ( g_SqlTuple, ErrorCode, g_Error, 511 );
164 if ( g_SqlTuple == Empty_Handle )
165 set_fail_state ( g_Error );
166
167 new szName [ 32 ], szTemp [ 512 ];
168 get_user_name ( id, szName, charsmax ( szName ) );
169
170 formatex ( szTemp, charsmax ( szTemp ), "UPDATE `%s` SET `kills` = '%d' , `deaths` = '%d' , `points` = '%d' WHERE `%s`.`nickname` = '%s';", Table, Player [ id ] [ Kills ], Player [ id ] [ Deaths ], Player [ id ] [ Points ], Table, szName );
171
172 SQL_ThreadQuery ( g_SqlTuple, "IgnoreHandle", szTemp );
173 SQL_FreeHandle ( SqlConnection );
174}
175
176public Show_Rank ( id ) {
177
178 for ( new i; i < g_iMaxPlayers; i++ ) {
179
180 if( is_user_connected ( i ) && !is_user_bot ( i ) )
181 SaveData ( i );
182 }
183
184 new Data [ 2 ], szTemp [ 512 ];
185 Data [ 0 ] = id;
186
187 formatex ( szTemp, charsmax ( szTemp ), "SELECT COUNT(*) FROM `%s` WHERE `kills` >= %d", Table, Player [ id ] [ Kills ] );
188
189 SQL_ThreadQuery ( g_SqlTuple, "Sql_Rank", szTemp, Data, 1 );
190
191 return PLUGIN_CONTINUE;
192}
193
194public Sql_Rank_Count ( FailState, Handle: Query, Error [ ], Errcode, Data [ ], DataSize ) {
195
196 if ( FailState == TQUERY_CONNECT_FAILED )
197 log_amx ( "[%s] Nu ma pot conecta la baza de date. [%d] %s", PLUGIN, Errcode, Error );
198
199 else if ( FailState == TQUERY_QUERY_FAILED )
200 log_amx ( "[%s] Load Query failed. [%d] %s", PLUGIN, Errcode, Error );
201
202 RankCount = SQL_ReadResult ( Query, 0 );
203
204 if ( RankCount == 0 )
205 RankCount = 1;
206
207 return PLUGIN_HANDLED;
208
209}
210
211public Sql_Rank ( FailState, Handle: Query, Error [ ], Errcode, Data [ ], DataSize ) {
212
213 if ( FailState == TQUERY_CONNECT_FAILED )
214 log_amx ( "[%s] Nu ma pot conecta la baza de date. [%d] %s", PLUGIN, Errcode, Error );
215
216 else if ( FailState == TQUERY_QUERY_FAILED )
217 log_amx ( "[%s] Load Query failed. [%d] %s", PLUGIN, Errcode, Error );
218
219 new count = 0;
220
221 count = SQL_ReadResult ( Query, 0 );
222
223 if ( count == 0 )
224 count = 1;
225
226 new id;
227 id = Data [ 0 ];
228
229 client_print ( id, print_chat, "Your rank is %i of %i with %i kills, %i deaths and %i points.", count, RankCount, Player [ id ] [ Kills ], Player [ id ] [ Deaths ], Player [ id ] [ Points ] );
230
231 return PLUGIN_HANDLED;
232}
233
234public ShowTop15 ( id ) {
235
236 new Data [ 1 ], szTemp [ 512 ];
237 Data [ 0 ] = id;
238
239 formatex ( szTemp, charsmax ( szTemp ), "SELECT 'nickname', 'kills', 'deaths', 'points' FROM '%s' ORDER BY 'kills' DESC LIMIT 15;", Table );
240
241 SQL_ThreadQuery ( g_SqlTuple, "Sql_Top15", szTemp, Data, 1 );
242 return PLUGIN_CONTINUE;
243}
244
245public Sql_Top15 ( FailState, Handle: hQuery, Error [ ], Errcode, Data [ ], DataSize ) {
246
247 new id = Data [ 0 ];
248
249 if( FailState == TQUERY_CONNECT_FAILED
250 || FailState == TQUERY_QUERY_FAILED ) {
251
252 log_amx( "[%s] Error on Top15 query (%i): %s", PLUGIN, Errcode, Error );
253 client_print ( id, print_chat, "Top15 is unavaible!" );
254 return;
255 }
256
257 new szName [ 32 ], iPos = 1, U_Name, U_Kills, U_Deaths, U_Points, a;
258
259 new motd [ 1024 ], len;
260
261 len = format ( motd, 1023, "<body bgcolor=#000000><center><font color=#FFB000><pre>" );
262 len += format ( motd [ len ], 1023-len, "%s %-22.22s %3s^n", "#", "Name", "Kills", "Deaths", "Points" );
263
264 while ( SQL_MoreResults ( hQuery ) ) {
265
266 a++;
267
268 SQL_ReadResult ( hQuery, 0, szName, charsmax ( szName ) );
269
270 U_Name = SQL_ReadResult ( hQuery, 0 );
271 U_Kills = SQL_ReadResult ( hQuery, 1 );
272 U_Deaths = SQL_ReadResult ( hQuery, 2 );
273 U_Points = SQL_ReadResult ( hQuery, 3 );
274
275 len += format ( motd [ len ], 1023-len,"%d %-22.22s %d %d %d^n", a+1, U_Name, U_Kills, U_Deaths, U_Points );
276
277 iPos++;
278 SQL_NextRow ( hQuery );
279 }
280
281 len += format ( motd [ len ], 1023-len,"</body></font></pre></center>" );
282 show_motd ( id, motd, "Top 15" );
283}
284
285public eDeath ( ) {
286
287 new iKiller = read_data ( 1 );
288 new iVictim = read_data ( 2 );
289 new Headshot = read_data ( 3 );
290
291 new weapon [ 32 ];
292 read_data ( 4, weapon, sizeof ( weapon ) -1 );
293
294 new bool: is_visible = fm_is_ent_visible ( iKiller, iVictim );
295
296 if ( iKiller == iVictim ) return 1;
297
298 if ( Headshot ) {
299
300 Player [ iKiller ] [ Points ] += HEADSHOT_POINTS;
301 client_print ( iKiller, print_chat, "** You get %d points for this kill.", HEADSHOT_POINTS );
302 }
303
304 else if ( equali ( weapon, "grenade" ) )
305 {
306 Player [ iKiller ] [ Points ] += HE_POINTS;
307 client_print ( iKiller, print_chat, "** You get %d points for this kill.", HE_POINTS );
308 }
309
310 else if ( equali ( weapon, "knife" ) )
311 {
312 Player [ iKiller ] [ Points ] += KNIFE_POINTS;
313 client_print ( iKiller, print_chat, "** You get %d points for this kill.", KNIFE_POINTS );
314 }
315
316 else if ( !is_visible ) {
317
318 Player [ iKiller ] [ Points ] += WALL_POINTS;
319 client_print ( iKiller, print_chat, "** You get %d points for this kill.", WALL_POINTS );
320 }
321
322 else {
323
324 Player [ iKiller ] [ Points ] += KILL_POINTS;
325 client_print ( iKiller, print_chat, "** You get %d points for this kill.", KILL_POINTS );
326 }
327
328 SaveData ( iKiller );
329 SaveData ( iVictim );
330
331 return 1;
332
333}
334
335public bomb_planted ( planter ) {
336
337 Player [ planter ] [ Points ] += PLANT_POINTS;
338 client_print ( planter, print_chat, "** You get %d points for this plant.", PLANT_POINTS );
339}
340
341public bomb_defused ( defuser ) {
342
343 Player [ defuser ] [ Points ] += DEFUSE_POINTS;
344 client_print ( defuser, print_chat, "** You get %d points for this defuse.", DEFUSE_POINTS );
345}