· 8 years ago · Jun 09, 2018, 01:18 PM
1#include < amxmodx >
2#include < sqlx >
3#include < cstrike >
4#include < hamsandwich >
5
6/* Database */
7new Host[ ] = ""
8new User[ ] = ""
9new Pass[ ] = ""
10new Db[ ] = ""
11
12new MySQL_Query[ 512 ]
13new Handle:MySQL_Tuple
14new Handle:MySQL_Connection
15new bool:g_Loaded[ 33 ] = false
16new Kills[33], Deaths[33];
17new Team[33], OldName[33][32]
18
19new g_logfile[64];
20
21new const VERSION[ ] = "0.0.3"
22
23public plugin_init( ) {
24 register_plugin( "Scoreboard to MySQL", VERSION, "Airkish" )
25 register_clcmd("say /rs", "ResetScore")
26 register_event( "TeamInfo", "event_team_info", "a" );
27 register_event("HLTV", "RoundStart", "a", "1=0", "2=0");
28}
29
30public client_putinserver( id )
31{
32 OldName[id] = "";
33 set_task(5.0, "Presql_Load", id)
34 g_Loaded[ id ] = false
35}
36
37public client_infochanged(id)
38{
39 new newname[32],oldname[32]
40 get_user_info(id, "name", newname,31)
41 get_user_name(id, oldname, 31)
42
43 if(!equali(newname, oldname))
44 {
45 OldName[id] = oldname;
46 MySQL_UpdateName(id);
47 return PLUGIN_HANDLED;
48 }
49 return PLUGIN_CONTINUE;
50}
51
52public Presql_Load(id) {
53 if ( cs_get_user_team(id) == CS_TEAM_T) {
54 Team[id] = 1;
55 }
56 else if ( cs_get_user_team(id) == CS_TEAM_CT ) {
57 Team[id] = 2;
58 }
59 else {
60 Team[id] = 3;
61 }
62 MySQL_Load(id)
63}
64
65public client_death(Killer, Victim, wpnindex, hitplace, TK)
66{
67 if(Killer == Victim) {
68 Deaths[Killer]++;
69 MySQL_Update(Killer);
70 return PLUGIN_HANDLED;
71 }
72 Kills[Killer]++;
73 Deaths[Victim]++;
74 MySQL_Update(Killer)
75 MySQL_Update(Victim)
76
77 return PLUGIN_CONTINUE;
78}
79
80public ResetScore(id) {
81 Kills[id] = 0;
82 Deaths[id] = 0;
83 MySQL_Update(id);
84}
85
86public event_team_info()
87{
88 new id = read_data( 1 );
89
90 new team[12];
91 read_data( 2, team, sizeof team - 1 );
92
93 switch( team[0] )
94 {
95 case 'C': {
96 Team[id] = 2;
97 }
98 case 'T': {
99 Team[id] = 1;
100 }
101 case 'S': {
102 Team[id] = 3;
103 }
104 }
105 MySQL_Update(id);
106}
107
108public client_disconnected( id )
109{
110 OldName[id] = "";
111 if( g_Loaded[ id ] )
112 MySQL_Delete( id )
113}
114
115public plugin_precache( )
116{
117 get_time("online_players_%Y%m%d.log", g_logfile, charsmax(g_logfile));
118 log_errors("-------------------- == --------------------");
119
120 MySQL_Tuple = SQL_MakeDbTuple( Host, User, Pass, Db )
121 #if AMXX_VERSION_NUM >= 183
122 SQL_SetCharset(MySQL_Tuple,"utf8");
123 #endif
124
125 new ErrorCode
126 MySQL_Connection = SQL_Connect( MySQL_Tuple, ErrorCode, MySQL_Query, charsmax( MySQL_Query ) )
127
128 if( MySQL_Connection == Empty_Handle )
129 set_fail_state( MySQL_Query )
130
131 SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", "CREATE TABLE IF NOT EXISTS online_players (steamid VARCHAR(40), username VARCHAR(128) COLLATE utf8_lithuanian_ci NOT NULL, kills INT(11), deaths INT(11), team INT(1) )" )
132
133 SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", "DELETE FROM online_players" )
134}
135
136public MySQL_Load( id )
137{
138 new Temp[ 1 ]
139 Temp[ 0 ] = id
140
141 formatex( MySQL_Query, charsmax( MySQL_Query ), "SELECT * FROM `online_players` WHERE `username` = '%s'", get_user_name_ex(id) )
142 SQL_ThreadQuery( MySQL_Tuple, "Load_PlayerInfo", MySQL_Query, Temp, sizeof( Temp ) )
143}
144
145public Load_PlayerInfo( FailState, Handle:Query, Error[ ], Errcode, Data[ ], DataSize )
146{
147 if( FailState == TQUERY_CONNECT_FAILED )
148 {
149 new Players[ 32 ], PlayersNum
150 get_players( Players, PlayersNum, "ch" )
151
152 for( new i; i < PlayersNum; i++ )
153 log_errors( "Load - Could not connect to SQL database. [%d] %s", Errcode, Error )
154 }
155
156 else if( FailState == TQUERY_QUERY_FAILED )
157 {
158 new Players[ 32 ], PlayersNum
159 get_players( Players, PlayersNum, "ch" )
160
161 for( new i; i < PlayersNum; i++ )
162 {
163 log_errors( "Load Query failed. [%d] %s", Errcode, Error )
164 }
165 }
166 new id = Data[ 0 ]
167
168 if( !is_user_connected( id ) )
169 return
170
171 if( SQL_NumResults( Query ) < 1 )
172 {
173 formatex( MySQL_Query, charsmax( MySQL_Query ), "INSERT INTO `online_players` (`steamid`, `username`, `kills`, `deaths`, `team`) VALUES ('%s', '%s', '%d', '%d', '%d')", get_user_steamid_ex(id), get_user_name_ex(id), Kills[id], Deaths[id], Team[id])
174 SQL_ThreadQuery( MySQL_Tuple, "SQL_TrashHandler", MySQL_Query )
175 log_errors("%s", MySQL_Query)
176 g_Loaded[ id ] = true
177 }
178}
179
180public MySQL_Update( id ) {
181 formatex( MySQL_Query, charsmax( MySQL_Query ), "UPDATE `online_players` SET `kills` = '%d', `deaths` = '%d', `team` = '%d' WHERE `username` = '%s'", Kills[id], Deaths[id], Team[id], get_user_name_ex(id) )
182 SQL_ThreadQuery(MySQL_Tuple, "SQL_TrashHandler", MySQL_Query);
183}
184
185public MySQL_UpdateName(id) {
186 replace_all(OldName[id], 32, "'", "\'");
187 replace_all(OldName[id], 32, "^"", "\^"");
188
189 formatex( MySQL_Query, charsmax( MySQL_Query ), "UPDATE `online_players` SET `username` = '%s' WHERE `username` = '%s'", get_user_name_ex(id), OldName[id] )
190 SQL_ThreadQuery(MySQL_Tuple, "SQL_TrashHandler", MySQL_Query);
191}
192
193public MySQL_Delete( const id )
194{
195 formatex( MySQL_Query, charsmax( MySQL_Query ), "DELETE from `online_players` WHERE `username` = '%s'", get_user_name_ex(id) )
196 SQL_ThreadQuery(MySQL_Tuple, "SQL_TrashHandler", MySQL_Query);
197}
198
199public SQL_TrashHandler( FailState,Handle:Query, Error[ ], Errcode,Data[ ], DataSize )
200{
201 if( FailState == TQUERY_CONNECT_FAILED )
202 log_errors( "Load - Could not connect to SQL database. [%d] %s", Errcode, Error )
203
204 else if( FailState == TQUERY_QUERY_FAILED )
205 log_errors( "Load Query failed. [%d] %s", Errcode, Error )
206
207 SQL_FreeHandle( Query )
208}
209
210public plugin_end( )
211{
212 SQL_FreeHandle( MySQL_Connection )
213}
214
215stock log_errors(const szText[], any:...)
216{
217 static szMessage[256];
218 vformat(szMessage, 256, szText, 2);
219
220 log_to_file(g_logfile, szMessage);
221
222 return PLUGIN_HANDLED;
223}
224
225stock get_user_name_ex(id)
226{
227 new szName[33];
228 get_user_name(id, szName, charsmax(szName));
229
230 replace_all(szName, charsmax(szName), "'", "\'");
231 replace_all(szName, charsmax(szName), "^"", "\^"");
232
233 return szName;
234}
235
236stock get_user_steamid_ex(id) {
237 new steamid[33]
238 get_user_authid( id, steamid, charsmax(steamid))
239
240 return steamid;
241}