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