· 7 years ago · Oct 21, 2018, 12:10 AM
1/*
2* Credits to anything else striker07 aka Sky-High from .:}O$G{:. Hell Yeah!
3*
4* Parts that I left before the rebuild... Infernuz.
5*
6* 1. fw_CounterEntThink( iEntity )
7* 2. Event registering in plugin_init()
8*
9* v. 1.0
10* Infernuz logs.
11*
12* 1. Built an user friendly function for running SQL queries throw tuple.
13* 2. Remade MySQL_init() a bit.
14* 3. Removed crap code.
15*
16*/
17#include <amxmodx>
18#include <engine>
19//#include <dhudmessage>
20#include <sqlx>
21#include <cstrike>
22#include <hamsandwich>
23#include <fakemeta>
24
25// ==================== GLOBAL VARIABLES ==================
26
27new szMapId[32];
28
29// TIMERS
30new g_iTimer;
31new g_iMinutes;
32new g_iSeconds;
33new g_iRecTimer;
34new g_iRecMinutes;
35new g_iRecSeconds;
36
37new g_bStopTimer = true;
38new g_bHideTimer = false;
39//new g_iRoundNum = 0;
40
41// SQL
42new Handle:g_SqlTuple;
43new g_Error[128];
44
45new g_iCounterEntity; // counter entity id
46
47// ==================== END VARIABLES ======================
48public plugin_init() {
49 register_plugin( "SaveRecord_SQL" , "1.0" , "Sky-High & Infernuz" ); // register plugin
50
51 g_iCounterEntity = create_entity( "info_target" ); // creates new entity and stores its id in g_iCounterEntity variable
52 entity_set_string( g_iCounterEntity , EV_SZ_classname , "counter_entity" ); // sets the classname to entity (countdown_entity)
53 register_think( "counter_entity" , "fw_CounterEntThink" ); /* registers entity think (something like set task but for entity),*/
54
55 register_logevent("EventNewRound", 2, "1=Round_Start");
56 register_logevent( "EventRoundEndCT", 2, "3=CTs_Win" );
57 //register_logevent("EventRoundEnd", 2, "1=Round_End");
58 //register_event( "TextMsg", "EventRoundEndT", "a", "2=#Terrorists_Win" );
59 //register_event( "TextMsg", "EventRoundEndCT", "a", "2=#CT_Win" );
60
61 entity_set_float( g_iCounterEntity , EV_FL_nextthink , get_gametime() + 0.01 ); // sets the next think of entity (gametime + 0.01 ) so next think is in 0.01 second.
62
63 MySql_Init("sql-1.verygames.net", "dg_site204", "mqbv3w8na", "dg_site204"); // host, user, pass, database, timeout(not required);
64
65 get_mapname( szMapId, charsmax(szMapId) );
66
67 set_task(5.0, "delayed_timer_check");
68}
69
70public client_disconnect(id) {
71
72 if(cs_get_user_vip(id)) {
73
74 new players[32], count, randomplayer;
75 get_players(players,count,"ae","CT");
76
77 randomplayer = players[random(count)];
78
79 cs_set_user_vip(randomplayer);
80 }
81}
82
83public EventNewRound() {
84 g_bStopTimer = false;
85 Reset_time();
86}
87
88public EventRoundEndCT() {
89 //g_iRoundNum++;
90
91 //if ( g_iRoundNum > 1) {
92
93 g_bStopTimer = true;
94
95 //if ( ( fnGetCounterTerrorists() > 0 ) && ( fnGetTerrorists() == 0 ) ) {
96
97 set_task(0.3, "Check_Times");
98 //}
99
100 /*else if( equali(szMapId[0], "zp_boss_city") ) {
101
102 if ( fnGetCounterTerrorists() > 0 )
103 {
104 set_task(0.3, "Check_Times");
105 }
106 }*/
107}
108
109public Check_Times() {
110 if( g_iRecMinutes > g_iMinutes || ( g_iRecMinutes == g_iMinutes && g_iRecSeconds > g_iSeconds ) || ( g_iRecMinutes == g_iMinutes && g_iRecSeconds == g_iSeconds && g_iRecTimer > g_iTimer ) )
111 {
112 client_print(0, print_chat, "New record set! saving new times..");
113
114 g_iRecMinutes = g_iMinutes;
115 g_iRecSeconds = g_iSeconds;
116 g_iRecTimer = g_iTimer;
117 Run_query(3); // update it!
118 }
119 /*if( ( (g_iTimer + g_iCounter) < g_iRecTimer) || g_iRecTimer == 0) {
120 client_print(0, print_chat, "New record set! saving new times..");
121 g_iRecTimer = g_iTimer + g_iCounter; // set before the update.
122
123 Run_query(3); // update it!
124 }*/
125}
126
127public Reset_time()
128{
129 //g_iCounter = 0;
130 g_iTimer = 0;
131 g_iSeconds = 0;
132 g_iMinutes = 0;
133}
134
135public fw_CounterEntThink( iEntity ) {
136
137 // checks if entity is countdown entity
138 if ( iEntity == g_iCounterEntity ) {
139
140 // increase timer
141 if( !g_bStopTimer)
142 {
143 g_iTimer++;
144
145 if(g_iTimer == 100)
146 {
147 g_iTimer = 0;
148 g_iSeconds++;
149
150 if(g_iSeconds == 60)
151 {
152 g_iSeconds = 0;
153 g_iMinutes++;
154 }
155 }
156 }
157
158 if( !g_bHideTimer ) {
159 set_hudmessage(255, 204, 0, 0.62, 0.96, 0, _, 0.2, 0.1, 0.1); // creates the hud message
160 //show_hudmessage( 0 ,"Time: %02d:%02d:%02d ^nRecord: %02d:%02d:%02d", (g_iTimer % (60*60)) / 60,(g_iTimer % (60*60)) % 60, g_iCounter, g_iRecTimer / (60*60),(g_iRecTimer % (60*60)) / (60),(g_iRecTimer % (60*60)) % (60));
161 show_hudmessage( 0 , "Time: %02d:%02d:%02d ^nRecord: %.2i:%02d:%02i" , g_iMinutes, g_iSeconds, g_iTimer, g_iRecMinutes, g_iRecSeconds, g_iRecTimer ); // show the counter in hudmessage
162
163 //server_cmd("TIMER NOW - min: %d s: %d, i: %d -- SQL TIMER - min: %d sec: %d i: %d", g_iTimer/60, g_iTimer/60*2, g_iTimer, g_iRecTimer/60, g_iRecTimer/60*2, g_iRecTimer);
164 }
165
166 entity_set_float( g_iCounterEntity , EV_FL_nextthink , get_gametime() + 0.01 ); // sets the next think in 0.01 second
167 }
168}
169
170/*fnGetCounterTerrorists() {
171 //Get's the number of living counter-terrorists
172 static iPlayers[32], iNum;
173 get_players(iPlayers, iNum, "ae", "CT");
174 return iNum;
175}*/
176
177/*fnGetTerrorists() {
178 // Get's the number of living terrorists
179 static iPlayers[32], iNum;
180 get_players(iPlayers, iNum, "ae", "TERRORIST");
181 return iNum;
182}*/
183
184// ==================== START OF SQL CODE ====================
185public delayed_timer_check() {
186 Run_query(1); // select
187}
188
189public MySql_Init(const host[], const user[], const pass[], const db[]) {
190 g_SqlTuple = SQL_MakeDbTuple(host,user,pass,db);
191
192 new ErrorCode,Handle:SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
193
194 if(SqlConnection == Empty_Handle) {
195 //SQL_QueryAndIgnore(SqlConnection,"DROP TABLE IF EXISTS `AM_recordtimes`")
196 SQL_QueryAndIgnore(SqlConnection,"CREATE TABLE IF NOT EXISTS `AM_recordtimes` (`id` int(12) NOT NULL AUTO_INCREMENT, `mapid` varchar(32) NOT NULL, `min_record` INT(11) DEFAULT '99', `sec_record` INT(11) DEFAULT '99', `ms_record` INT(11) DEFAULT '99', PRIMARY KEY (`id`))")
197 }
198 SQL_FreeHandle(SqlConnection)
199}
200
201public Run_query(query_type) {
202
203 new szTemp[150], data[2];
204
205
206 // this checks what to do, select update or create a new record.
207 switch(query_type) {
208 case 1: {
209 // selects values.
210 format(szTemp,charsmax(szTemp),"SELECT * FROM `AM_recordtimes` WHERE `mapid` = '%s' ", szMapId)
211
212 server_print("SQL loading record times %s",szMapId); // DEBUG, you can delete this after.
213 }
214 case 2: {
215 // inserts values.
216 format(szTemp,charsmax(szTemp),"INSERT INTO `AM_recordtimes` (mapid, min_record, sec_record, ms_record) VALUES('%s','99','99','99') ",szMapId)
217
218 server_print("SQL Inserting %s",szMapId); // debug, you can delete it.
219 }
220 case 3: {
221 // updates values.
222 format(szTemp,charsmax(szTemp),"UPDATE `AM_recordtimes` SET `min_record` = '%d', `sec_record` = '%d', `ms_record` = '%d' WHERE `mapid` = '%s'", g_iRecMinutes, g_iRecSeconds, g_iRecTimer, szMapId)
223
224 server_print("SQL Updating SZMAPID: %s TIMER: %i : %i : %i",szMapId, g_iRecMinutes, g_iRecSeconds, g_iRecTimer); // debug, you can delete it.
225 }
226 }
227 data[0] = query_type;
228 SQL_ThreadQuery(g_SqlTuple,"query_handler",szTemp,data,2)
229}
230
231public query_handler(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {
232 if(FailState == TQUERY_CONNECT_FAILED)
233 return log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
234 else if(FailState == TQUERY_QUERY_FAILED)
235 return log_amx("Load Query failed. [%d] %s", Errcode, Error)
236
237 if(Errcode)
238 return log_amx("Error on query: %s",Error);
239
240 /* Check from where are the data coming.
241 *
242 * 1 - Select from Run_query(1)
243 * 2 - Insert into Run_query(2)
244 * 3 - Update where Run_query(3)
245 *
246 */
247 switch(Data[0]) {
248 case 1: {
249
250 // If something is found in database. Try to get its content
251 if(SQL_NumResults(Query) > 0) {
252
253 // read it
254 new column_num_min = SQL_FieldNameToNum(Query, "min_record");
255 new column_num_sec = SQL_FieldNameToNum(Query, "sec_record");
256 new column_num_ms = SQL_FieldNameToNum(Query, "ms_record");
257
258 g_iRecTimer = SQL_ReadResult(Query, column_num_ms);
259 g_iRecSeconds = SQL_ReadResult(Query, column_num_sec);
260 g_iRecMinutes = SQL_ReadResult(Query, column_num_min);
261
262 // debug, you can delete it later
263 server_print("echo Got data from db RECTIMER: %d:%d:%d", g_iRecMinutes, g_iRecSeconds, g_iRecTimer);
264 } else {
265 Run_query(2); // No data found, create it!
266 Run_query(1); // check again biach!
267 }
268 }
269 }
270
271 return PLUGIN_HANDLED
272}
273
274public plugin_end() {
275 SQL_FreeHandle(g_SqlTuple)
276}
277
278/*
2791 Sec : 100 * 0.01 ( timer: 100)
2801 min : 60*(100 * 0.01) ( timer: 6000) ( 100
2811 h : 60*(60 * (100 * 0.01) ) ( timer: 360 000)
282
283als timer 100 is dan hebben we 1 seconde
284als timer 6000 is dan hebben we 1 minuut
285*/