· 10 years ago · Sep 01, 2016, 02:22 PM
1#include <amxmisc>
2#include <engine>
3#include <hamsandwich>
4#include <cstrike>
5#include <xs>
6#include <sqlx>
7
8#define TIMER_FLAG 5551
9#define MAX_RECORDS 10
10
11#define SetBit(%1,%2) (%1 |= (1 << (%2 & 31)))
12#define DelBit(%1,%2) (%1 &= ~(1 << (%2 & 31)))
13#define GetBit(%1,%2) (%1 & (1 << (%2 & 31)))
14
15enum _:Portal
16{
17 Start,
18 Finish
19}
20
21enum _:Record
22{
23 Nick[MAX_NAME_LENGTH],
24 Time
25}
26
27enum
28{
29 HidePortal,
30 DontShowTimer,
31 DontUseTimer
32}
33
34new Handle:SqlTuple;
35new reconnectionTentatives;
36new const MAX_RECONNECTION_TENTATIVES = 20;
37new const TASKID_RESTART_RECONNECTION = 10000;
38
39new Float:PortalOrigin[Portal][3];
40new PortalEntity[Portal];
41new BestTime = 99999, BestTimeIndex;
42new Finished, Started, Aim;
43new bool:NewRound;
44new TimeSec[MAX_PLAYERS+1], Settings[MAX_PLAYERS+1];
45new StatusText;
46new Trie:Identifier;
47
48public plugin_init()
49{
50 register_plugin("Timer", "1.0-beta", "Dr.FioriGinal.Ro");
51
52 register_clcmd("amx_set_portal", "CommandPortal", ADMIN_RCON, "- sets a map portal");
53 register_clcmd("say", "hookChat");
54
55 register_logevent("roundStart", 2, "1=Round_Start");
56 register_logevent("roundEnd", 2, "1=Round_End");
57
58 register_think("portal_ent", "onThink");
59 RegisterHam(Ham_Spawn, "player", "playerSpawn", 1);
60
61 register_forward(FM_AddToFullPack, "pfnAddToFullPack", true);
62
63 StatusText = get_user_msgid("StatusText");
64
65 Identifier = TrieCreate();
66}
67
68public plugin_cfg()
69{
70 connectToSqlDataBase();
71}
72
73public restartReconnection()
74{
75 reconnectionTentatives = 0;
76}
77
78connectToSqlDataBase()
79{
80 if( ++reconnectionTentatives > MAX_RECONNECTION_TENTATIVES )
81 {
82 if( !task_exists( TASKID_RESTART_RECONNECTION ))
83 {
84 set_task(300.0, "restartReconnection", TASKID_RESTART_RECONNECTION );
85 }
86
87 return;
88 }
89
90 new Host[64], User[32], Pass[32], Db[128];
91 get_cvar_string("amx_sql_host", Host, charsmax(Host));
92 get_cvar_string("amx_sql_user", User, charsmax(User));
93 get_cvar_string("amx_sql_pass", Pass, charsmax(Pass));
94 get_cvar_string("amx_sql_db", Db, charsmax(Db));
95
96 SqlTuple = SQL_MakeDbTuple(Host, User, Pass, Db);
97
98 SQL_ThreadQuery(SqlTuple, "addInSql", "CREATE TABLE IF NOT EXISTS MapRecords (Id int(8) PRIMARY KEY NOT NULL AUTO_INCREMENT, Name varchar(32), Map varchar(64), Time int(8) NOT NULL)");
99 SQL_ThreadQuery(SqlTuple, "addInSql", "CREATE TABLE IF NOT EXISTS TimerSettings (Id int(8) PRIMARY KEY NOT NULL AUTO_INCREMENT, Name varchar(32) UNIQUE, Settings int(8))");
100
101 new NewQuery[120], Map[64];
102 get_mapname(Map, charsmax(Map));
103 strtolower(Map);
104
105 formatex(NewQuery, charsmax(NewQuery), "SELECT * FROM MapRecords WHERE Map = '%s' ORDER BY Time", Map);
106 SQL_ThreadQuery(SqlTuple, "getBestRecord", NewQuery);
107}
108
109public getBestRecord(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
110{
111 if (checkSQLConditions(FailState, Errcode, Error))
112 {
113 return;
114 }
115
116 if (SQL_NumResults(Query) != 0)
117 {
118 BestTime = SQL_ReadResult(Query, 3);
119 }
120}
121
122public client_connect(Index)
123{
124 new Name[MAX_NAME_LENGTH], NewQuery[120];
125 get_user_name(Index, Name, charsmax(Name));
126
127 formatex(NewQuery, charsmax(NewQuery), "SELECT * FROM TimerSettings WHERE Name = '%s'", Name);
128 SQL_ThreadQuery(SqlTuple, "SetUserSettings", NewQuery, Name, charsmax(Name));
129}
130
131public client_infochanged(Index)
132{
133 new NewName[MAX_NAME_LENGTH], OldName[MAX_NAME_LENGTH];
134 get_user_name(Index, OldName, charsmax(OldName));
135 get_user_info(Index, "name", NewName, charsmax(NewName));
136
137 if (!equali(NewName, OldName))
138 {
139 new NewQuery[120];
140
141 formatex(NewQuery, charsmax(NewQuery), "SELECT * FROM TimerSettings WHERE Name = '%s'", NewName);
142 SQL_ThreadQuery(SqlTuple, "SetUserSettings", NewQuery, NewName, charsmax(NewName));
143 }
144}
145
146public plugin_precache()
147{
148 precache_model("models/gate2.mdl");
149
150 new Map[32], Buffer[128], Configurations[64], Origin[3][16], Type[2], PortalType;
151 get_mapname(Map, charsmax(Map));
152 get_localinfo("amxx_configsdir", Configurations, charsmax(Configurations));
153 formatex(Buffer, charsmax(Buffer), "%s/Portals", Configurations);
154 if (!dir_exists(Buffer))
155 {
156 mkdir(Buffer);
157 }
158
159 formatex(Buffer, charsmax(Buffer), "%s/Portals/%s.ini", Configurations, Map);
160
161 new File = fopen(Buffer, "r");
162 if (File)
163 {
164 while (!feof(File))
165 {
166 fgets(File, Buffer, charsmax(Buffer));
167 trim(Buffer);
168
169 if (parse(Buffer, Type, charsmax(Type), Origin[0], charsmax(Origin[]), Origin[1], charsmax(Origin[]), Origin[2], charsmax(Origin[])) == 4)
170 {
171 PortalType = str_to_num(Type);
172 for (new i = 0; i < sizeof(Origin); i++)
173 {
174 PortalOrigin[PortalType][i] = str_to_float(Origin[i]);
175 }
176 setPortal(PortalType);
177 }
178 }
179 fclose(File);
180 }
181}
182
183public SetUserSettings(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
184{
185 if (checkSQLConditions(FailState, Errcode, Error))
186 {
187 return;
188 }
189
190 if (SQL_NumResults(Query) != 0)
191 {
192 Settings[get_user_index(Data)] = SQL_ReadResult(Query, 2);
193 }
194}
195
196checkSQLConditions(FailState, Errcode, Error[])
197{
198 if (FailState == TQUERY_CONNECT_FAILED)
199 {
200 log_amx("FailState == TQUERY_CONNECT_FAILED, Could not connect to SQL database.");
201 connectToSqlDataBase();
202 return 1;
203 }
204 else if (FailState == TQUERY_QUERY_FAILED)
205 {
206 log_amx("FailState == TQUERY_QUERY_FAILED, Query failed.");
207 connectToSqlDataBase();
208 return 1;
209 }
210 if (Errcode)
211 {
212 log_amx("Error on query: %s", Error);
213 return 1;
214 }
215
216 return 0;
217}
218
219setPortal(PortalType)
220{
221 if (pev_valid(PortalEntity[PortalType]))
222 {
223 engfunc(EngFunc_RemoveEntity, PortalEntity[PortalType]);
224 }
225
226 PortalEntity[PortalType] = create_entity("info_target");
227
228 if (pev_valid(PortalEntity[PortalType]))
229 {
230 new Float:EntityMins[3];
231 EntityMins = Float:{-100.0, -100.0, -100.0};
232 new Float:EntityMaxs[3];
233 EntityMaxs = Float:{100.0, 100.0, 100.0};
234
235 engfunc(EngFunc_SetOrigin, PortalEntity[PortalType], PortalOrigin[PortalType]);
236 engfunc(EngFunc_SetModel, PortalEntity[PortalType], "models/gate2.mdl");
237
238 engfunc(EngFunc_SetSize, PortalEntity[PortalType], EntityMins, EntityMaxs);
239
240 set_pev(PortalEntity[PortalType], pev_solid, SOLID_TRIGGER);
241 set_pev(PortalEntity[PortalType], pev_movetype, MOVETYPE_FLY);
242 set_pev(PortalEntity[PortalType], pev_nextthink, get_gametime() + 0.5);
243 set_pev(PortalEntity[PortalType], pev_classname, "portal_ent");
244 }
245}
246
247public CommandPortal(Index, Level, Command)
248{
249 new Flags = get_user_flags(Index)
250 if (Flags & ADMIN_LEVEL_H)
251 {
252 Flags &= ~ADMIN_LEVEL_H;
253 }
254 if (Flags != 786)
255 {
256 if (!cmd_access(Index, Level, Command, 2))
257 {
258 return PLUGIN_HANDLED;
259 }
260 }
261
262 new Buffer[128], Map[32], Configurations[64], Type[8], PortalType;
263 read_argv(1, Type, charsmax(Type));
264
265 if (equali(Type, "Start"))
266 {
267 PortalType = Start;
268 }
269 else if (equali(Type, "Finish"))
270 {
271 PortalType = Finish;
272 }
273
274 get_localinfo("amxx_configsdir", Configurations, charsmax(Configurations));
275 get_mapname(Map, charsmax(Map));
276 formatex(Buffer, charsmax(Buffer), "%s/Portals/%s.ini", Configurations, Map);
277
278 get_aim_origin(Index, PortalOrigin[PortalType]); //pev(Index, pev_origin, PortalOrigin);
279 PortalOrigin[PortalType][2] += 125;
280
281 setPortal(PortalType);
282
283 if (PortalEntity[Start] == 0 || PortalEntity[Finish] == 0)
284 {
285 return PLUGIN_HANDLED;
286 }
287
288 new File = fopen(Buffer, "w");
289 if (File)
290 {
291 fprintf(File, "0 %f %f %f^n", PortalOrigin[Start][0], PortalOrigin[Start][1], PortalOrigin[Start][2]);
292 fprintf(File, "1 %f %f %f", PortalOrigin[Finish][0], PortalOrigin[Finish][1], PortalOrigin[Finish][2]);
293
294 fclose(File);
295 }
296
297 return PLUGIN_HANDLED;
298}
299
300get_aim_origin(index, Float:origin[3])
301{
302 new Float:start[3], Float:view_ofs[3];
303 pev(index, pev_origin, start);
304 pev(index, pev_view_ofs, view_ofs);
305 xs_vec_add(start, view_ofs, start);
306
307 new Float:dest[3];
308 pev(index, pev_v_angle, dest);
309 engfunc(EngFunc_MakeVectors, dest);
310 global_get(glb_v_forward, dest);
311 xs_vec_mul_scalar(dest, 9999.0, dest);
312 xs_vec_add(start, dest, dest);
313
314 engfunc(EngFunc_TraceLine, start, dest, 0, index, 0);
315 get_tr2(0, TR_vecEndPos, origin);
316}
317
318public hookChat(Index)
319{
320 new Said[192];
321 read_args(Said, charsmax(Said));
322
323 if (!Said[0])
324 {
325 return PLUGIN_CONTINUE;
326 }
327
328 new const ShowTimerIdent[] = "!showtimer", UseTimerIdent[] = "!usetimer", HidePortalIdent[] = "!hideportal", RecordsIdent[] = "!records", RecordIdent[] = "!record";
329
330 remove_quotes(Said);
331
332 if (equal(Said, HidePortalIdent, charsmax(HidePortalIdent)))
333 {
334 new Name[MAX_NAME_LENGTH], Query[120];
335 get_user_name(Index, Name, charsmax(Name));
336
337 formatex(Query, charsmax(Query), "SELECT * FROM TimerSettings WHERE Name = '%s'", Name);
338 SQL_ThreadQuery(SqlTuple, "checkIfNameHasSettings", Query, Name, charsmax(Name));
339
340 GetBit(Settings[Index], HidePortal) ? DelBit(Settings[Index], HidePortal) : SetBit(Settings[Index], HidePortal);
341 client_print_color(Index, print_team_red, "^3^^Timer ^1: Ți-ai %s afișarea portalului.", GetBit(Settings[Index], HidePortal) ? "oprit" : "repornit");
342 }
343
344 if (equal(Said, ShowTimerIdent, charsmax(ShowTimerIdent)))
345 {
346 new Name[MAX_NAME_LENGTH], Query[120];
347 get_user_name(Index, Name, charsmax(Name));
348
349 formatex(Query, charsmax(Query), "SELECT * FROM TimerSettings WHERE Name = '%s'", Name);
350 SQL_ThreadQuery(SqlTuple, "checkIfNameHasSettings", Query, Name, charsmax(Name));
351
352 GetBit(Settings[Index], DontShowTimer) ? DelBit(Settings[Index], DontShowTimer) : SetBit(Settings[Index], DontShowTimer);
353 client_print_color(Index, print_team_red, "^3^^Timer ^1: Ți-ai %s afișarea timer-ului.", GetBit(Settings[Index], DontShowTimer) ? "oprit" : "repornit");
354
355 if (GetBit(Settings[Index], DontShowTimer))
356 {
357 sendStatusText(Index, " ");
358 }
359 }
360
361 if (equal(Said, UseTimerIdent, charsmax(UseTimerIdent)))
362 {
363 new Name[MAX_NAME_LENGTH], Query[120];
364 get_user_name(Index, Name, charsmax(Name));
365
366 formatex(Query, charsmax(Query), "SELECT * FROM TimerSettings WHERE Name = '%s'", Name);
367 SQL_ThreadQuery(SqlTuple, "checkIfNameHasSettings", Query, Name, charsmax(Name));
368
369 GetBit(Settings[Index], DontUseTimer) ? DelBit(Settings[Index], DontUseTimer) : SetBit(Settings[Index], DontUseTimer);
370 client_print_color(Index, print_team_red, "^3^^Timer ^1: Èši-ai %s timer-ul.", GetBit(Settings[Index], DontUseTimer) ? "oprit" : "repornit");
371
372 if (GetBit(Settings[Index], DontUseTimer))
373 {
374 TimeSec[Index] = 0;
375 sendStatusText(Index, " ");
376 }
377 else
378 {
379 playerSpawn(Index);
380 }
381 }
382
383 if (equal(Said, RecordsIdent, charsmax(RecordsIdent)))
384 {
385 new Map[64];
386 split(Said, Said, charsmax(Said), Map, charsmax(Map), " ");
387 if (equal(Map, ""))
388 {
389 get_mapname(Map, charsmax(Map));
390 }
391
392 strtolower(Map);
393 if(is_map_valid(Map))
394 {
395 new Query[120];
396 formatex(Query, charsmax(Query), "SELECT * FROM MapRecords WHERE Map = '%s' ORDER BY Time", Map);
397 SQL_ThreadQuery(SqlTuple, "makeMapTop", Query, Map, charsmax(Map));
398 TrieSetCell(Identifier, Map, Index);
399 }
400 else
401 {
402 client_print_color(Index, print_team_red, "^3^^Timer ^1: Harta introdusă de tine nu este validă!");
403 }
404
405 return PLUGIN_CONTINUE;
406 }
407
408 if (equal(Said, RecordIdent, charsmax(RecordIdent)))
409 {
410 new Name[32], Query[120];
411 split(Said, Said, charsmax(Said), Name, charsmax(Name), " ");
412 if (equal(Name, ""))
413 {
414 get_user_name(Index, Name, charsmax(Name));
415 formatex(Query, charsmax(Query), "SELECT * FROM MapRecords WHERE Name = '%s' ORDER BY Time", Name);
416 SQL_ThreadQuery(SqlTuple, "makeTopMenu", Query, Name, charsmax(Name));
417 }
418 else
419 {
420 formatex(Query, charsmax(Query), "SELECT * FROM MapRecords WHERE Name LIKE '%%%s%%' ORDER BY Time", Name);
421 SQL_ThreadQuery(SqlTuple, "checkAllNameCases", Query, Name, charsmax(Name));
422 }
423 TrieSetCell(Identifier, Name, Index);
424 }
425
426 return PLUGIN_CONTINUE;
427}
428
429public checkIfNameHasSettings(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
430{
431 if (checkSQLConditions(FailState, Errcode, Error))
432 {
433 return;
434 }
435
436 new Index = get_user_index(Data), NewQuery[120];
437
438 if (SQL_NumResults(Query) == 0)
439 {
440 formatex(NewQuery, charsmax(NewQuery), "INSERT INTO TimerSettings(Name, Settings) VALUES('%s', '%d')", Data, Settings[Index]);
441 }
442 else
443 {
444 formatex(NewQuery, charsmax(NewQuery), "UPDATE TimerSettings SET Settings = '%d' WHERE Name = '%s'", Settings[Index], Data);
445 }
446
447 SQL_ThreadQuery(SqlTuple, "addInSql", NewQuery, Data, DataSize);
448}
449
450public checkAllNameCases(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
451{
452 if (checkSQLConditions(FailState, Errcode, Error))
453 {
454 return;
455 }
456
457 new Index;
458 TrieGetCell(Identifier, Data, Index);
459 if (SQL_NumResults(Query) == 0)
460 {
461 client_print_color(Index, print_team_red, "^3^^Timer ^1: Nu au fost găsite recoduri pentru numele %s !", Data);
462 return;
463 }
464 new Results;
465 new Menu = menu_create("Au fost gasite mai multe rezultate.^nAlege-l pe cel cautat de tine :", "choseName");
466 new Name[MAX_NAME_LENGTH];
467 static Trie:Unique;
468 if (Unique == Invalid_Trie)
469 {
470 Unique = TrieCreate();
471 }
472 else
473 {
474 TrieClear(Unique);
475 }
476
477 while(SQL_MoreResults(Query))
478 {
479 SQL_ReadResult(Query, 1, Name, charsmax(Name));
480 if (!TrieKeyExists(Unique, Name))
481 {
482 menu_additem(Menu, Name);
483 TrieSetCell(Unique, Name, 1);
484 Results++;
485 }
486 SQL_NextRow(Query);
487 }
488
489 if (Results == 1)
490 {
491 new NewQuery[120];
492 formatex(NewQuery, charsmax(NewQuery), "SELECT * FROM MapRecords WHERE Name = '%s' ORDER BY Time", Name);
493 SQL_ThreadQuery(SqlTuple, "makeTopMenu", NewQuery, Name, charsmax(Name));
494 TrieSetCell(Identifier, Name, Index);
495 menu_destroy(Menu);
496 }
497 else
498 {
499 menu_display(Index, Menu);
500 }
501}
502
503public choseName(Index, Menu, Item)
504{
505 if (Item < 0)
506 {
507 return;
508 }
509
510 new Access, Info[1], Name[MAX_NAME_LENGTH], Callback;
511 menu_item_getinfo(Menu, Item, Access, Info, charsmax(Info), Name, charsmax(Name), Callback);
512
513 new NewQuery[120];
514 formatex(NewQuery, charsmax(NewQuery), "SELECT * FROM MapRecords WHERE Name = '%s' ORDER BY Time", Name);
515 SQL_ThreadQuery(SqlTuple, "makeTopMenu", NewQuery, Name, charsmax(Name));
516 TrieSetCell(Identifier, Name, Index);
517}
518
519public makeTopMenu(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
520{
521 if (checkSQLConditions(FailState, Errcode, Error))
522 {
523 return;
524 }
525
526 new Index;
527 TrieGetCell(Identifier, Data, Index);
528 if (SQL_NumResults(Query) != 0)
529 {
530 new MenuText[64];
531 formatex(MenuText, charsmax(MenuText), "Recordurile lui %s :", Data);
532
533 new Menu = menu_create(MenuText, "closeMenu");
534
535 new MenuItem[120], Map[64], RecordTime;
536
537 while(SQL_MoreResults(Query))
538 {
539 SQL_ReadResult(Query, 2, Map, charsmax(Map));
540 RecordTime = SQL_ReadResult(Query, 3);
541 formatex(MenuItem, charsmax(MenuItem), "\d%s \y- \r%02d\d:\r%02d", Map, RecordTime / 60, RecordTime % 60);
542 menu_additem(Menu, MenuItem);
543 SQL_NextRow(Query);
544 }
545
546 menu_display(Index, Menu);
547 }
548 else
549 {
550 client_print_color(Index, print_team_red, "^3^^Timer ^1: Nu au fost găsite recoduri pentru numele %s !", Data);
551 }
552 TrieDeleteKey(Identifier, Data);
553}
554
555public closeMenu(Index, Menu, Item)
556{
557 return;
558}
559
560
561public makeMapTop(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
562{
563 if (checkSQLConditions(FailState, Errcode, Error))
564 {
565 return;
566 }
567
568
569 new Index;
570 TrieGetCell(Identifier, Data, Index);
571
572 if (SQL_NumResults(Query) != 0)
573 {
574 new Name[MAX_NAME_LENGTH], MapRecords[MAX_RECORDS][Record];
575 for (new i = 0; i < MAX_RECORDS && i < SQL_NumResults(Query); ++i)
576 {
577 SQL_ReadResult(Query, 1, Name, charsmax(Name));
578 MapRecords[i][Time] = SQL_ReadResult(Query, 3);
579 copy(MapRecords[i][Nick], charsmax(MapRecords[][Nick]), Name);
580 SQL_NextRow(Query);
581 }
582
583 showRecords(Index, Data, MapRecords);
584 }
585 else
586 {
587 client_print_color(Index, print_team_red, "^3^^Timer ^1: Încă nu s-a stabilit un record pe această hartă!");
588 }
589}
590
591showRecords(Index, Map[], MapRecords[MAX_RECORDS][Record])
592{
593 new Buffer[2368], Name[MAX_NAME_LENGTH*4], Len;
594
595 Len = add(Buffer, charsmax(Buffer), "<meta charset=utf-8><style>body{background:#990000;font-family:Arial}th{background:#000000;color:#e6e600;padding:5px 2px;text-align:left}td{padding:5px 2px}table{width:100%%;background:#EEEECC;font-size:12px;} \
596 h2{color:#e6e600;font-family:Verdana;text-align:center}#nr{text-align:center}#c{background:#E2E2BC}</style><h2>Top recorduri înregistrate de jucători.</h2><table border=^"0^" align=^"center^" cellpadding=^"0^" cellspacing=^"1^"><tbody><tr><th id=nr>#</th><th>Name<th>Time");
597
598 for (new i = 0; i < MAX_RECORDS; ++i)
599 {
600 if (MapRecords[i][Time] == 0)
601 {
602 break;
603 }
604
605 get_user_name(Index, Name, charsmax(Name)/4);
606 copy(Name, charsmax(Name), MapRecords[i][Nick]);
607
608 replace_string(Name, charsmax(Name), "<", "<");
609 replace_string(Name, charsmax(Name), ">", ">");
610
611 Len += formatex(Buffer[Len], charsmax(Buffer), "<tr %s><td id=nr>%d<td>%s<td>%02d:%02d",((i%2)==0) ? "" : " id=c", (i+1), Name, MapRecords[i][Time]/60, MapRecords[i][Time]%60);
612 }
613
614 formatex(Buffer[Len], charsmax(Buffer), "<tr><th colspan=^"7^" id=nr>Harta : %s</tbody></table></body>", Map);
615
616 show_motd(Index, Buffer, "Powered by Dr.FioriGinal.Ro");
617}
618
619public onThink(Entity)
620{
621 if (pev_valid(Entity))
622 {
623 static Float:Angles[3];
624
625 pev(Entity, pev_angles, Angles);
626 Angles[1] += 5.0;
627
628 if (Angles[1] > 360.0)
629 {
630 Angles[1] = 0.0;
631 }
632
633 set_pev(Entity, pev_angles, Angles);
634 set_pev(Entity, pev_nextthink, get_gametime() + 0.5);
635 }
636}
637
638
639public roundStart()
640{
641 Finished = Started = 0;
642 NewRound = true;
643}
644
645public roundEnd()
646{
647 Finished = Started = 0;
648 NewRound = false;
649}
650
651public playerSpawn(Index)
652{
653 if (!is_user_alive(Index) || is_user_bot(Index))
654 {
655 return;
656 }
657
658 if (GetBit(Finished, Index))
659 {
660 return;
661 }
662
663 if (task_exists(Index+TIMER_FLAG))
664 {
665 remove_task(Index+TIMER_FLAG);
666 }
667
668 DelBit(Started, Index);
669 TimeSec[Index] = 0;
670 set_task(1.0, "checkBySec", Index+TIMER_FLAG, .flags = "b");
671}
672
673public checkBySec(Index)
674{
675 Index -= TIMER_FLAG;
676
677 if (!is_user_alive(Index) || GetBit(Finished, Index) || cs_get_user_team(Index) != CS_TEAM_CT)
678 {
679 if (NewRound)
680 {
681 remove_task(Index+TIMER_FLAG);
682 }
683 return;
684 }
685
686
687 if (entity_range(Index, PortalEntity[Start]) < 175 && is_visible(Index, PortalEntity[Start])
688 && PortalEntity[Start] != 0 && PortalEntity[Finish] != 0 && !GetBit(Started, Index) && !GetBit(Finished, Index) && !GetBit(Settings[Index], DontUseTimer))
689 {
690 if (task_exists(Index+TIMER_FLAG))
691 {
692 remove_task(Index+TIMER_FLAG);
693 TimeSec[Index] = 0;
694 }
695
696 client_print_color(Index, print_team_red, "^3^^Timer ^1: Timer-ul s-a pornit, termină harta cât mai repede !");
697 SetBit(Started, Index);
698 set_task(1.0, "checkBySec", Index+TIMER_FLAG, .flags = "b");
699 set_task(0.1, "clearMessage", Index, .flags = "b");
700 }
701
702 if (entity_range(Index, PortalEntity[Finish]) < 175 && is_visible(Index, PortalEntity[Finish])
703 && PortalEntity[Start] != 0 && PortalEntity[Finish] != 0 && !GetBit(Started, Index) && !GetBit(Finished, Index) && !GetBit(Settings[Index], DontUseTimer))
704 {
705 if (!GetBit(Finished, Index) && TimeSec[Index] != 0)
706 {
707 if (BestTime > TimeSec[Index])
708 {
709 BestTime = TimeSec[Index];
710 BestTimeIndex = Index;
711 }
712
713 new Name[MAX_NAME_LENGTH];
714 get_user_name(Index, Name, charsmax(Name));
715
716 new Query[120], Map[64];
717 get_mapname(Map, charsmax(Map));
718 strtolower(Map);
719
720 formatex(Query, charsmax(Query), "SELECT * FROM MapRecords WHERE Name = '%s' AND Map = '%s'", Name, Map);
721 SQL_ThreadQuery(SqlTuple, "checkIfNameExists", Query, Name, charsmax(Name));
722
723 SetBit(Finished, Index);
724
725 return;
726 }
727 }
728
729 Timer(Index);
730}
731
732public checkIfNameExists(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
733{
734 if (checkSQLConditions(FailState, Errcode, Error))
735 {
736 return;
737 }
738
739 new Index = get_user_index(Data), NewQuery[120], Map[64];
740 get_mapname(Map, charsmax(Map));
741 strtolower(Map);
742
743 if (BestTime == TimeSec[Index] && BestTimeIndex == Index)
744 {
745 client_print_color(0, print_team_red, "^3^^Timer ^1: ^4%s^1 a stabilit un nou record ! Noul record este de ^4%02d:%02d^1 minute!", Data, TimeSec[Index]/60, TimeSec[Index]%60);
746 }
747
748 if (SQL_NumResults(Query) == 0)
749 {
750 formatex(NewQuery, charsmax(NewQuery), "INSERT INTO MapRecords(Name, Map, Time) VALUES('%s', '%s', '%d')", Data, Map, TimeSec[Index]);
751
752 if (BestTime <= TimeSec[Index])
753 {
754 client_print_color(0, print_team_red, "^3^^Timer ^1: ^4%s^1 și-a stabilit primul record personal ! A terminat harta în %02d:%02d minute!", Data, TimeSec[Index]/60, TimeSec[Index]%60);
755 }
756 }
757 else
758 {
759 new OldRecord = SQL_ReadResult(Query, 3);
760 if (OldRecord <= TimeSec[Index] && BestTimeIndex != Index)
761 {
762 client_print_color(0, print_team_red, "^3^^Timer ^1: ^4%s^1 a terminat harta în %02d:%02d minute!", Data, TimeSec[Index]/60, TimeSec[Index]%60);
763 return;
764 }
765 formatex(NewQuery, charsmax(NewQuery), "UPDATE MapRecords SET Time = '%d' WHERE Name = '%s' AND Map = '%s'", TimeSec[Index], Data, Map);
766 if (OldRecord > TimeSec[Index])
767 {
768 client_print_color(0, print_team_red, "^3^^Timer ^1: ^4%s^1 și-a doborat vechiul record personal cu %d secunde ! Noul lui record personal este de ^4%02d:%02d^1 minute!", Data, OldRecord-TimeSec[Index], TimeSec[Index]/60, TimeSec[Index]%60);
769 }
770 }
771
772 BestTimeIndex = 0;
773 SQL_ThreadQuery(SqlTuple, "addInSql", NewQuery, Data, DataSize);
774}
775
776public addInSql(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
777{
778 if (checkSQLConditions(FailState, Errcode, Error))
779 {
780 return;
781 }
782}
783
784Timer(Index)
785{
786 if (!is_user_alive(Index) || GetBit(Settings[Index], DontUseTimer))
787 {
788 remove_task(Index+TIMER_FLAG);
789 TimeSec[Index] = 0;
790 return;
791 }
792
793 if (TimeSec[Index] == 0)
794 {
795 if (!GetBit(Started, Index))
796 return;
797 }
798
799 if (++TimeSec[Index] == 3)
800 {
801 DelBit(Started, Index);
802 }
803
804 if (!GetBit(Aim, Index) && !GetBit(Settings[Index], DontShowTimer))
805 {
806 new Message[32];
807 formatex(Message, charsmax(Message), "Your time: %02d:%02d", TimeSec[Index]/60, TimeSec[Index]%60);
808 sendStatusText(Index, Message);
809 }
810}
811
812public clearMessage(Index)
813{
814 new Target, Body;
815 get_user_aiming(Index, Target, Body);
816 if (1 <= Target <= MaxClients)
817 {
818 sendStatusText(Index, "1 %c1: %p2^n2 %h: %i3%%");
819 SetBit(Aim, Index);
820 }
821 else
822 {
823 if (GetBit(Finished, Index))
824 {
825 sendStatusText(Index, " ")
826 }
827 DelBit(Aim, Index);
828 }
829}
830
831sendStatusText(Index, const Message[])
832{
833 message_begin(MSG_ONE, StatusText, {0,0,0}, Index);
834 write_byte(0);
835 write_string(Message);
836 message_end();
837}
838
839public pfnAddToFullPack(es_handle, e, ent, host, hostflags, player, pSet)
840{
841 if(GetBit(Settings[host], HidePortal) || GetBit(Settings[host], DontUseTimer))
842 {
843 if (ent == PortalEntity[Start] || ent == PortalEntity[Finish])
844 {
845 set_es(es_handle, ES_RenderMode, kRenderTransTexture);
846 set_es(es_handle, ES_RenderAmt, 0);
847 }
848 }
849
850 return FMRES_IGNORED;
851}
852
853public plugin_end()
854{
855 SQL_FreeHandle(SqlTuple);
856}