Advertisement
pmiranda

(Include) sys_log

May 16th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.56 KB | None | 0 0
  1. #if defined _sys_log_included
  2.     #endinput
  3. #endif
  4.  
  5. #define _sys_log_included
  6.  
  7.  
  8. #include <a_samp>
  9. #include <a_mysql>
  10. #include <CTime>
  11.  
  12.  
  13. /*
  14. native InsertLog(const text[]);
  15. native DisplayLogs(playerid, limit);
  16. */
  17.  
  18.  
  19. #if !defined mysql_included
  20.     #error "a_mysql must be included"
  21. #endif
  22.  
  23. #if !defined _CTime_Included
  24.     #error "CTime must be included"
  25. #endif
  26.  
  27.  
  28. new
  29.     connectionHandle,
  30.     scriptString[256];
  31.    
  32.  
  33. #if defined FILTERSCRIPT
  34.    
  35. public OnFilterScriptInit() {
  36.  
  37.     connectionHandle = mysql_connect("localhost", "root", "sa-mp", "");
  38.     if(mysql_ping(connectionHandle) == -1)
  39.         return SendRconCommand("unloadfs sys_log");
  40.        
  41.     mysql_function_query(connectionHandle,
  42.         "CREATE TABLE IF NOT EXISTS `sys_log` (`log_date` INT(11) NOT NULL, `log_text` VARCHAR(128) NOT NULL)", false, #, #);
  43.        
  44.     if(funcidx("log_OnFilterScriptInit") != -1)
  45.         return CallLocalFunction("log_OnFilterScriptInit", #);
  46.  
  47.     return true;
  48. }
  49.  
  50.  
  51. public OnFilterScriptExit() {
  52.  
  53.     mysql_close(connectionHandle);
  54.  
  55.     if(funcidx("log_OnFilterScriptExit") != -1)
  56.         return CallLocalFunction("log_OnFilterScriptExit", #);
  57.  
  58.     return true;
  59. }
  60.  
  61.  
  62. #if defined _ALS_OnFilterScriptInit
  63.     #undef OnFilterScriptInit
  64. #else
  65.     #define _ALS_OnFilterScriptInit
  66. #endif
  67.  
  68. #define OnFilterScriptInit log_OnFilterScriptInit
  69.  
  70.  
  71. #if defined _ALS_OnFilterScriptExit
  72.     #undef OnFilterScriptExit
  73. #else
  74.     #define _ALS_OnFilterScriptExit
  75. #endif
  76.  
  77. #define OnFilterScriptExit log_OnFilterScriptExit
  78.  
  79.  
  80. forward log_OnFilterScriptInit();
  81. forward log_OnFilterScriptExit();
  82.  
  83.  
  84. #else
  85.  
  86.  
  87. public OnGameModeInit() {
  88.  
  89.     connectionHandle = mysql_connect("localhost", "root", "sa-mp", "");
  90.     if(mysql_ping(connectionHandle) == -1)
  91.         return SendRconCommand("unloadfs sys_log");
  92.        
  93.     mysql_function_query(connectionHandle,
  94.         "CREATE TABLE IF NOT EXISTS `sys_log` (`log_date` INT(11) NOT NULL, `log_text` VARCHAR(128) NOT NULL)", false, #, #);
  95.  
  96.     if(funcidx("log_OnGameModeInit") != -1)
  97.         return CallLocalFunction("log_OnGameModeInit", #);
  98.        
  99.     return true;
  100. }
  101.  
  102.  
  103. public OnGameModeExit() {
  104.  
  105.     mysql_close(connectionHandle);
  106.  
  107.     if(funcidx("log_OnGameModeExit") != -1)
  108.         return CallLocalFunction("log_OnGameModeExit", #);
  109.  
  110.     return true;
  111. }
  112.  
  113.  
  114. #if defined _ALS_OnGameModeInit
  115.     #undef OnGameModeInit
  116. #else
  117.     #define _ALS_OnGameModeInit
  118. #endif
  119.  
  120. #define OnGameModeInit log_OnGameModeInit
  121.  
  122.  
  123. #if defined _ALS_OnGameModeExit
  124.     #undef OnGameModeExit
  125. #else
  126.     #define _ALS_OnGameModeExit
  127. #endif
  128.  
  129. #define OnGameModeExit log_OnGameModeExit
  130.  
  131.  
  132. forward log_OnGameModeInit();
  133. forward log_OnGameModeExit();
  134.  
  135.  
  136. #endif
  137.  
  138.  
  139.  
  140. stock InsertLog(const text[])  {
  141.  
  142.     mysql_format(connectionHandle, scriptString, sizeof scriptString,
  143.         "INSERT INTO `sys_log` (`log_date`, `log_text`) VALUES (UNIX_TIMESTAMP(), '%s')", text);
  144.  
  145.     mysql_function_query(connectionHandle, scriptString, false, #, #);
  146.     return true;
  147. }
  148.  
  149.  
  150. stock DisplayLogs(playerid, limit) {
  151.  
  152.     mysql_format(connectionHandle, scriptString, sizeof scriptString,
  153.         "SELECT * FROM `sys_log` ORDER BY `log_date` ASC LIMIT %d", limit);
  154.     mysql_function_query(connectionHandle, scriptString, true, "r@DisplayLogs", "ii", playerid, limit);    
  155.     return true;
  156. }
  157.  
  158.  
  159.  
  160. r@DisplayLogs(playerid, limit); public r@DisplayLogs(playerid, limit) {
  161.  
  162.     new num_rows, num_fields;
  163.     cache_get_data(num_rows, num_fields, connectionHandle);
  164.    
  165.     if(!num_rows)
  166.         return SendClientMessage(playerid, 0xff0000ff, "(!) Não há nenhum log no banco de dados.");
  167.        
  168.     new
  169.         index = -1,
  170.         timestamp,
  171.         content[128],
  172.         tm<tmTime>;
  173.        
  174.     format(content, sizeof content, "Últimos %02d logs", limit);
  175.     SendClientMessage(playerid, 0xffffffff, content);
  176.  
  177.     while(++index < num_rows) {
  178.    
  179.         timestamp = cache_get_field_content_int(index, "log_date", connectionHandle);
  180.  
  181.         localtime(Time:timestamp, tmTime);
  182.         strftime(scriptString, sizeof scriptString, "%d/%m/%Y às %X", tmTime);
  183.        
  184.         cache_get_field_content(index, "log_text", content, connectionHandle);
  185.        
  186.         format(scriptString, sizeof scriptString,
  187.             "   %s, %s: %s", GetWeekDay(timestamp), scriptString, content);
  188.         SendClientMessage(playerid, 0xffffffff, scriptString);
  189.     }
  190.    
  191.     return true;
  192. }
  193.  
  194.  
  195.  
  196. stock GetWeekDay(timestamp) {
  197.  
  198.     new
  199.         tm<tmTime>,
  200.         buffer[32];
  201.        
  202.     localtime(Time:timestamp, tmTime);
  203.     switch(tmTime[tm_wday]) {
  204.         case 1: buffer = "Segunda-feira";
  205.         case 2: buffer = "Terça-feira";
  206.         case 3: buffer = "Quarta-feira";
  207.         case 4: buffer = "Quinta-feira";
  208.         case 5: buffer = "Sexta-feira";
  209.         case 6: buffer = "Sábado";
  210.         case 7: buffer = "Domingo";
  211.     }
  212.     return buffer;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement