· 7 years ago · Sep 08, 2018, 03:28 AM
1#include <amxmodx>
2#include <sqlx>
3#include <time>
4
5#define MOTD_LENGTH 1536
6#define TOP_NUM 15
7
8// SQL tiedot:
9new Host[] = "SQL09.FREEMYSQL.NET";
10new User[] = "mansfalk";
11new Pass[] = "riveba24";
12new Db[] = "played";
13
14new g_data_loaded, Handle:g_sql_tuple, g_Error[ 512 ], gTime[ 33 ];
15
16public plugin_init()
17{
18 register_plugin( "Play time", "1.0", "tpt" );
19
20 register_clcmd( "say /ptime","cmd_DisplayTime", ADMIN_ALL , "- Display your play time on server." );
21 register_clcmd( "say /top", "show_top_players" );
22
23 register_dictionary( "time.txt" );
24}
25
26public client_disconnect( id )
27 Save( id )
28
29public client_putinserver( id )
30 Load( id )
31
32public cmd_DisplayTime( id )
33{
34 new iCurTime = get_user_time( id );
35
36 new szTime[ 128 ]
37 get_time_length( id, gTime[ id ] + iCurTime, timeunit_seconds, szTime, charsmax( szTime ) );
38
39 client_print( id, print_chat, "* You have been on this server for %i minutes %i seconds.", ( iCurTime / 60 ), iCurTime );
40 client_print( id, print_chat, "* Your total recorded play time on the server is %s", szTime );
41}
42public plugin_precache()
43{
44 MySql_Init()
45
46 //g_sql_tuple = SQL_MakeDbTuple( Host, User, Pass, Db )
47 //SQL_ThreadQuery( g_sql_tuple, "QueryCreateTable", "CREATE TABLE IF NOT EXISTS `playedtime` ( `nick` VARCHAR(35) NOT NULL, `authid` VARCHAR(35) NOT NULL, `time` INT(11) NOT NULL );" );
48}
49
50/*
51public QueryCreateTable( failstate, Handle:query, error[], errnum, data[], size, Float:queuetime )
52{
53 if( failstate == TQUERY_CONNECT_FAILED
54 || failstate == TQUERY_QUERY_FAILED )
55 {
56 set_fail_state( error );
57 }
58}
59*/
60
61public MySql_Init()
62{
63 g_sql_tuple = SQL_MakeDbTuple( Host, User, Pass, Db );
64
65 new ErrorCode, Handle:SqlConnection = SQL_Connect( g_sql_tuple, ErrorCode, g_Error, charsmax( g_Error ) );
66 if ( SqlConnection == Empty_Handle )
67 set_fail_state(g_Error)
68
69 new Handle:Queries
70
71 Queries = SQL_PrepareQuery( SqlConnection, "CREATE TABLE IF NOT EXISTS `playedtime` ( `nick` VARCHAR(35) NOT NULL, `authid` VARCHAR(35) NOT NULL, `time` INT(11) NOT NULL );" );
72
73 if ( ! SQL_Execute( Queries ) )
74 {
75 SQL_QueryError( Queries, g_Error, charsmax( g_Error ) );
76 set_fail_state( g_Error );
77 }
78 SQL_FreeHandle( Queries );
79
80 SQL_FreeHandle( SqlConnection );
81}
82
83public Save( id )
84{
85 static szAuthId[ 35 ], szName[ 33 ], query[ 512 ];
86 get_user_authid( id, szAuthId, charsmax( szAuthId ) );
87 get_user_name( id, szName, charsmax( szName ) );
88
89 new playtime = get_user_time( id )
90
91 if( g_data_loaded )
92 {
93 formatex( query, charsmax( query ), "UPDATE `playedtime` SET `time` = time + %i WHERE `authid` = '%s';", playtime, szAuthId );
94 }
95 else
96 {
97 formatex( query, charsmax( query ), "INSERT INTO `playedtime` ( `nick`, `authid`, `time` ) VALUES ( '%s', '%s', %i );", szName, szAuthId, playtime );
98 }
99 SQL_ThreadQuery( g_sql_tuple, "QuerySaveData", query );
100}
101
102public QuerySaveData( failstate, Handle:query, error[], errnum, data[], size, Float:queuetime )
103{
104 if ( failstate == TQUERY_CONNECT_FAILED
105 || failstate == TQUERY_QUERY_FAILED )
106 {
107 set_fail_state( error );
108 }
109}
110
111public Load( id )
112{
113 static szSteamId[ 35 ], query[ 512 ];
114 get_user_authid(id, szSteamId, charsmax(szSteamId))
115
116 new Data[ 1 ]
117 Data[ 0 ] = id;
118
119 format( query, charsmax( query ), "SELECT * FROM `playedtime` WHERE authid = '%s'", szSteamId );
120
121 //format(g_sql_string,255,"SELECT * FROM `%s` WHERE authid='%s' AND class=%d",g_sql_table,authid,g_player_class[id]);
122 SQL_ThreadQuery( g_sql_tuple, "register_client", query, Data, 1 );
123}
124
125public register_client( FailState, Handle:Query, Error[], Errcode, Data[], DataSize )
126{
127 if( FailState == TQUERY_CONNECT_FAILED )
128 {
129 log_amx( "Load - Could not connect to SQL database. [%d] %s", Errcode, Error );
130 }
131 else if(FailState == TQUERY_QUERY_FAILED)
132 {
133 log_amx( "Load Query failed. [%d] %s", Errcode, Error );
134 }
135
136 new id = Data[ 0 ];
137
138 if( SQL_NumResults( Query ) )
139 {
140 g_data_loaded = 1;
141
142 gTime[ id ] = SQL_ReadResult( Query, 2 );
143 }
144 return PLUGIN_HANDLED
145}
146
147public show_top_players( id )
148{
149 static pBuffer[ MOTD_LENGTH + 1 ]; pBuffer = "";
150 format_motd_top( pBuffer );
151}
152
153format_motd_top( pBuffer[MOTD_LENGTH + 1])
154{
155 add(pBuffer, MOTD_LENGTH, "<html><head><meta http-equiv=^"refresh^" content=^"15^"><style>");
156 add(pBuffer, MOTD_LENGTH, "body { background-color:#000000; font-family:Tahoma; font-size:10px; color:#FFFFFF; }");
157
158 add(pBuffer, MOTD_LENGTH, ".tabel { border-style:solid; border-width:1px; border-color:#FFFFFF; font-family:Tahoma; font-size:10px; color:#FFFFFF; }");
159 add(pBuffer, MOTD_LENGTH, ".header { background-color:#292929; font-family:Verdana,Tahoma; font-size:10px; color:#FFB000; font-weight:800; }");
160
161 add(pBuffer, MOTD_LENGTH, "</style></head><body>");
162 add(pBuffer, MOTD_LENGTH, "<br><br><table border=0 cellspacing=0 cellpadding=1 width=90% align=center class=tabel>");
163
164 add(pBuffer, MOTD_LENGTH, "<tr><td class=header>#</td><td class=header>Name</td><td class=header>Time</td></tr>");
165
166 static sql[ 512 ]
167
168 formatex( sql, charsmax( sql ), "SELECT `time`, `nick` FROM `playedtime` ORDER BY `time` DESC LIMIT %i;", TOP_NUM );
169 SQL_ThreadQuery( g_sql_tuple, "sql_top_handle", sql, pBuffer, MOTD_LENGTH );
170}
171
172public sql_top_handle(failstate, Handle:Query, error[], errcode, pBuffer[], len, Float:queuetime)
173{
174 switch(failstate)
175 {
176 case TQUERY_CONNECT_FAILED: return set_fail_state("Could not connect to the SQL Database");
177 case TQUERY_QUERY_FAILED: return set_fail_state("The table Query Failed");
178 }
179
180 if(errcode) return log_amx("error on Query: %s", error);
181
182 static sName[ 33 ], num[ 8 ];
183 new iTime, times, iTime_str[ 8 ], id;
184
185 // replace( pBuffer, len, pBuffer[len], "" );
186
187 while( ( SQL_MoreResults( Query ) ) && ( MOTD_LENGTH - strlen( pBuffer ) > 0 ) )
188 {
189 times++;
190
191 iTime = SQL_ReadResult( Query, 0 );
192 SQL_ReadResult( Query, 1, sName, charsmax( sName ) );
193
194 num_to_str( iTime, iTime_str, 7 );
195 num_to_str( times, num, 7 );
196
197 new szTime[ 128 ], iCurTime = get_user_time( id );
198 get_time_length( id, iTime + iCurTime, timeunit_seconds, szTime, charsmax( szTime ) );
199
200 add(pBuffer, len, "<tr><td>");
201 add(pBuffer, len, num); // Place
202 add(pBuffer, len, "</td><td>");
203 add(pBuffer, len, sName); // Name
204 add(pBuffer, len, "</td><td>");
205 add(pBuffer, len, szTime ); // Time
206
207 if( times >= TOP_NUM ) break;
208 else SQL_NextRow( Query );
209 }
210
211 static title[ 33 ];
212 formatex(title, charsmax( title ), "Player time" );
213
214 add(pBuffer, MOTD_LENGTH, "</table></body></html>");
215 show_motd(id, pBuffer, title);
216
217 return 0;
218}
219
220public plugin_end()
221 SQL_FreeHandle( g_sql_tuple );