Advertisement
Guest User

ReportSystem v1.0.0 by paul988

a guest
Nov 15th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.92 KB | None | 0 0
  1. //--------------------------------------------------
  2. //          ReportSystem v1.0.0 by paul988          
  3. //--------------------------------------------------
  4. #include <a_samp>
  5.  
  6. #undef MAX_PLAYERS
  7. #define MAX_PLAYERS         (50) // == MAX_PLAYERS (change to the amount of player slots you have on your server)
  8.  
  9. #include <a_mysql>
  10. #include <sscanf2>
  11. #include <zcmd>
  12.  
  13. //-------------(DO NOT CHANGE)------------------------------------
  14. #define AUTHOR              "paul988" // == AUTHOR (Do not change)
  15. #define VERSION             "v1.0.0" // == VERSION (Do not change)
  16. //----------------------------------------------------------------
  17.  
  18. #define MAX_REPORTS         (100)
  19.  
  20. // Define when to split the text into another line!
  21. #define EX_SPLITLENGTH      (118)
  22.  
  23. #define SQL_HOST            "" // == MySQL Host
  24. #define SQL_USER            "" // == MySQL Username
  25. #define SQL_DB              "" // == MYSQL Database
  26. #define SQL_PASS            "" // == MYSQL Password
  27.  
  28. #define SQL_PORT            (3306) // == MySQL Port
  29. #define SQL_AUTOCONNECT     true // == MySQL AutoConnect
  30. #define SQL_POOL-SIZE       (4) // == MySQL Pool_Size
  31.  
  32. #define DEBUG               false // == DEBUG Mode
  33.  
  34. enum ReportInfo
  35. {
  36.     ID,
  37.     Reporter[MAX_PLAYER_NAME],
  38.     Reported[MAX_PLAYER_NAME],
  39.     Reason[128]
  40. }
  41. new rInfo[MAX_REPORTS][ReportInfo];
  42. new ReportCount;
  43. new ReportTimer[MAX_PLAYERS];
  44.  
  45. static PlayerName[MAX_PLAYERS][MAX_PLAYER_NAME];
  46. static dbHandle = -1;
  47.  
  48. forward OnCreateTableIfNotExists();
  49. public OnCreateTableIfNotExists()
  50. {
  51.     LoadReports();
  52.     return 1;
  53. }
  54.  
  55. forward OnLoadReports();
  56. public OnLoadReports()
  57. {
  58.     new rows, fields;
  59.     cache_get_data(rows, fields);
  60.     if(rows)
  61.     {
  62.         new idx;
  63.         while(idx < rows)
  64.         {
  65.             rInfo[idx][ID] = cache_get_field_content_int(idx, "id");
  66.             cache_get_field_content(idx, "reporter", rInfo[idx][Reporter], dbHandle, MAX_PLAYER_NAME);
  67.             cache_get_field_content(idx, "reported", rInfo[idx][Reported], dbHandle, MAX_PLAYER_NAME);
  68.             cache_get_field_content(idx, "reason", rInfo[idx][Reason], dbHandle, 128);
  69.             ReportCount++;
  70.             idx++;
  71.         }
  72.     }
  73.     #if DEBUG == true
  74.         printf("[ReportSystem]: Loaded %d reports.", ReportCount);
  75.     #endif
  76.     return 1;
  77. }
  78.  
  79. forward OnDeleteReport(playerid, reportid);
  80. public OnDeleteReport(playerid, reportid)
  81. {
  82.     for(new ReportInfo:r; r < ReportInfo; ++r)
  83.     {
  84.         if(IsNumeric(rInfo[reportid][r])) rInfo[reportid][r] = -1;
  85.                 else format(rInfo[reportid][r], 128, "");
  86.     }
  87.     ReportCount--;
  88.     SendClientMessage(playerid, 0xD3D3D3AA, "Report successfully deleted!");
  89.     return 1;
  90. }
  91.  
  92. forward OnCreateReport(playerid, target, reason[128], reportid);
  93. public OnCreateReport(playerid, target, reason[128], reportid)
  94. {
  95.     rInfo[reportid][ID] = reportid;
  96.     format(rInfo[reportid][Reporter], MAX_PLAYER_NAME, "%s", PlayerName[playerid]);
  97.     format(rInfo[reportid][Reported], MAX_PLAYER_NAME, "%s", PlayerName[target]);
  98.     format(rInfo[reportid][Reason], 128, "%s", reason);
  99.     ReportTimer[playerid] = (GetTickCount() + 180000);
  100.     ReportCount++;
  101.     SendClientMessage(playerid, 0xD3D3D3AA, "Report successfully created!");
  102.     return 1;
  103. }
  104.  
  105. public OnFilterScriptInit()
  106. {
  107.     for(new idx = 0; idx < MAX_REPORTS; idx++) rInfo[idx][ID] = -1;
  108.     for(new i = 0; i < MAX_PLAYERS; i++)
  109.         if( (IsPlayerConnected(i)) && (i != INVALID_PLAYER_ID) )
  110.             GetPlayerName(i, PlayerName[i], MAX_PLAYER_NAME);
  111.     mysql_log(LOG_ERROR | LOG_WARNING, LOG_TYPE_HTML);
  112.     dbHandle = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS, SQL_PORT, SQL_AUTOCONNECT, SQL_POOL-SIZE);
  113.     #if DEBUG == true
  114.         if(mysql_errno() != 0) printf("[ReportSystem]: Could not connect to %s!", SQL_DB);
  115.             else printf("[ReportSystem]: Successfully connected to %s!", SQL_DB);
  116.     #endif
  117.     CreateTableIfNotExists();
  118.     printf("[ReportSystem]: Version %s by %s successfully loaded.", VERSION, AUTHOR);
  119.     return 1;
  120. }
  121.  
  122. public OnFilterScriptExit()
  123. {
  124.     mysql_close(dbHandle);
  125.     printf("[ReportSystem]: Version %s by %s successfully unloaded.", VERSION, AUTHOR);
  126.     return 1;
  127. }
  128.  
  129. public OnPlayerConnect(playerid)
  130. {
  131.     GetPlayerName(playerid, PlayerName[playerid], MAX_PLAYER_NAME);
  132.     ReportTimer[playerid] = (GetTickCount() - 1);
  133.     return 1;
  134. }
  135.  
  136. public OnPlayerDisconnect(playerid, reason)
  137. {
  138.     format(PlayerName[playerid], MAX_PLAYER_NAME, "");
  139.     ReportTimer[playerid] = (GetTickCount() - 1);
  140. }
  141.  
  142. CMD:report(playerid, params[])
  143. {
  144.     if(ReportTimer[playerid] > GetTickCount()) return SendClientMessage(playerid, 0xD3D3D3AA, "Command on cooldown! Please try again later!");
  145.     new target;
  146.     static reason[128];
  147.     if(sscanf(params, "ds[128]", target, reason)) return SendClientMessage(playerid, 0xD3D3D3AA, "Usage: /report [PlayerID/PartOfName] [Reason]");
  148.     if( (!IsPlayerConnected(target)) && (target == INVALID_PLAYER_ID) ) return SendClientMessage(playerid, 0xD3D3D3AA, "Target not connected!");
  149.     if(playerid == target) return SendClientMessage(playerid, 0xD3D3D3AA, "You are unable to report yourself!");
  150.     if(IsPlayerAdmin(target)) return SendClientMessage(playerid, 0xD3D3D3AA, "You are unable to report adminstrators!");
  151.     CreateReport(playerid, target, reason);
  152.     return 1;
  153. }
  154.  
  155. CMD:reports(playerid, params[])
  156. {
  157.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xD3D3D3AA, "You are not allowed to use this command!");
  158.     ShowReports(playerid);
  159.     return 1;
  160. }
  161.  
  162. CMD:reloadreports(playerid, params[])
  163. {
  164.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xD3D3D3AA, "You are not allowed to use this command!");
  165.     ReloadReports();
  166.     SendClientMessage(playerid, 0xD3D3D3AA, "Reports successfully reloaded!");
  167.     return 1;
  168. }
  169.  
  170. CMD:deletereport(playerid, params[])
  171. {
  172.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xD3D3D3AA, "You are not allowed to use this command!");
  173.     new reportid;
  174.     if(sscanf(params, "d", reportid)) return SendClientMessage(playerid, 0xD3D3D3AA, "Usage: /deletereport [ReportID]");
  175.     DeleteReport(playerid, reportid);
  176.     return 1;
  177. }
  178.  
  179. CMD:checkreports(playerid, params[])
  180. {
  181.     if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, 0xD3D3D3AA, "You are not allowed to use this command!");
  182.     new type, select[MAX_PLAYER_NAME];
  183.     if(sscanf(params, "ds[24]", type, select)) return SendClientMessage(playerid, 0xD3D3D3AA, "Usage: /checkreports [1/2/3] [ReportID/Reporter/Reported]");
  184.     CheckReport(playerid, type, select);
  185.     return 1;
  186. }
  187.  
  188. CreateTableIfNotExists()
  189. {
  190.     static query[256];
  191.     mysql_format(dbHandle, query, sizeof(query), "CREATE TABLE IF NOT EXISTS `reports` ( \
  192.         `id` INT(11) NOT NULL, \
  193.         `reporter` VARCHAR(24) NOT NULL, \
  194.         `reported` VARCHAR(24) NOT NULL, \
  195.         `reason` VARCHAR(128) NOT NULL, \
  196.         PRIMARY KEY (`id`) \
  197.     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
  198.     mysql_pquery(dbHandle, query, "OnCreateTableIfNotExists");
  199.     return 1;
  200. }
  201.  
  202. LoadReports()
  203. {  
  204.     static query[128];
  205.     mysql_format(dbHandle, query, sizeof(query), "SELECT * FROM `reports` ORDER BY `id` LIMIT %d", MAX_REPORTS);
  206.     mysql_pquery(dbHandle, query, "OnLoadReports");
  207.     return 1;
  208. }
  209.  
  210. ReloadReports()
  211. {
  212.     for(new idx = 0; idx < MAX_REPORTS; idx++)
  213.     {
  214.         for(new ReportInfo:r; r < ReportInfo; ++r)
  215.         {
  216.             if(IsNumeric(rInfo[idx][r])) rInfo[idx][r] = -1;
  217.                 else format(rInfo[idx][r], 128, "");
  218.         }
  219.     }
  220.     LoadReports();
  221. }
  222.  
  223. CreateReport(playerid, target, reason[128])
  224. {
  225.     new reportid = GetUnusedID();
  226.     if(reportid == -1) return 0;
  227.     if(ReportCount > MAX_REPORTS) return SendClientMessage(playerid, 0xD3D3D3AA, "Too many reports! Please try again later.");
  228.     static query[187];
  229.     mysql_format(dbHandle, query, sizeof(query), "INSERT INTO `reports` (`id`, `reporter`, `reported`, `reason`) VALUES (%d, '%e', '%e', '%e')",
  230.         reportid, PlayerName[playerid], PlayerName[target], reason);
  231.     mysql_pquery(dbHandle, query, "OnCreateReport", "ddsd", playerid, target, reason, reportid);
  232.     return 1;
  233. }
  234.  
  235. DeleteReport(playerid, reportid)
  236. {
  237.     if( (rInfo[reportid][ID] == -1) || (reportid > MAX_REPORTS) ) return SendClientMessage(playerid, 0xD3D3D3AA, "Invalid report id!");
  238.     static query[128];
  239.     mysql_format(dbHandle, query, sizeof(query), "DELETE FROM `reports` WHERE `id` = %d LIMIT 1", reportid);
  240.     mysql_pquery(dbHandle, query, "OnDeleteReport", "dd", playerid, reportid);
  241.     return 1;
  242. }
  243.  
  244. ShowReports(playerid)
  245. {
  246.     SendClientMessage(playerid, 0xD3D3D3AA, "______________________________{BEBEBE} Reports{D3D3D3} ______________________________");
  247.     SendClientMessage(playerid, 0xD3D3D3AA, "");
  248.     static str[237];
  249.     for(new idx = 0; idx < 7; idx++)
  250.     {
  251.         if(rInfo[idx][ID] != -1)
  252.         {
  253.             format(str, sizeof(str), "> ReportID: %d - Reporter: %s - Reported: %s - Reason: %s", rInfo[idx][ID], rInfo[idx][Reporter], rInfo[idx][Reported], rInfo[idx][Reason]);
  254.             SendSplitMessage(playerid, 0xD3D3D3AA, str);
  255.         }
  256.     }
  257.     SendClientMessage(playerid, 0xD3D3D3AA, "");
  258.     SendClientMessage(playerid, 0xD3D3D3AA, "____________________________________________________________________");
  259.     return 1;
  260. }
  261.  
  262. CheckReport(playerid, type, select[MAX_PLAYER_NAME])
  263. {
  264.     switch(type)
  265.     {
  266.         case 1:
  267.         {
  268.             if(!IsNumeric(select)) return SendClientMessage(playerid, 0xD3D3D3AA, "Usage: /checkreports [1/2/3] [ReportID/Reporter/Reported]");
  269.             new val = strval(select);
  270.             for(new idx = 0; idx < MAX_REPORTS; idx++)
  271.             {
  272.                 if(rInfo[idx][ID] == val)
  273.                 {
  274.                     static str[237];
  275.                     format(str, sizeof(str), "> ReportID: %d - Reporter: %s - Reported: %s - Reason: %s", rInfo[idx][ID], rInfo[idx][Reporter], rInfo[idx][Reported], rInfo[idx][Reason]);
  276.                     return SendSplitMessage(playerid, 0xD3D3D3AA, str);
  277.                 }
  278.             }
  279.             return SendClientMessage(playerid, 0xD3D3D3AA, "Report not found in database!");
  280.         }
  281.         case 2:
  282.         {
  283.             new count = 0;
  284.             for(new idx = 0; idx < MAX_REPORTS; idx++)
  285.             {
  286.                 if( (!strcmp(rInfo[idx][Reported], select, true)) && (rInfo[idx][ID] != -1) )
  287.                 {
  288.                     static str[237];
  289.                     format(str, sizeof(str), "> ReportID: %d - Reporter: %s - Reported: %s - Reason: %s", rInfo[idx][ID], rInfo[idx][Reporter], rInfo[idx][Reported], rInfo[idx][Reason]);
  290.                     SendSplitMessage(playerid, 0xD3D3D3AA, str);
  291.                     count++;
  292.                 }
  293.             }
  294.             if(count > 0) return 1;
  295.                 else SendClientMessage(playerid, 0xD3D3D3AA, "Report not found in database!");
  296.         }
  297.         case 3:
  298.         {
  299.             new count = 0;
  300.             for(new idx = 0; idx < MAX_REPORTS; idx++)
  301.             {
  302.                 if( (!strcmp(rInfo[idx][Reported], select, true)) && (rInfo[idx][ID] != -1) )
  303.                 {
  304.                     static str[237];
  305.                     format(str, sizeof(str), "> ReportID: %d - Reporter: %s - Reported: %s - Reason: %s", rInfo[idx][ID], rInfo[idx][Reporter], rInfo[idx][Reported], rInfo[idx][Reason]);
  306.                     SendSplitMessage(playerid, 0xD3D3D3AA, str);
  307.                     count++;
  308.                 }
  309.             }
  310.             if(count > 0) return 1;
  311.                 else SendClientMessage(playerid, 0xD3D3D3AA, "Report not found in database!");
  312.         }
  313.         default: SendClientMessage(playerid, 0xD3D3D3AA, "Usage: /checkreports [1/2/3] [ReportID/Reporter/Reported]");
  314.     }
  315.     return 1;
  316. }
  317.  
  318. stock GetUnusedID()
  319. {
  320.     new found = -1;
  321.     for(new idx = 0; idx < MAX_REPORTS; idx++)
  322.     {
  323.         if(rInfo[idx][ID] == -1)
  324.         {
  325.             return idx;
  326.         }
  327.     }
  328.     return found;
  329. }
  330.  
  331. // Credits to Mike (http://wiki.sa-mp.com/wiki/Useful_Functions#IsNumeric)
  332. stock IsNumeric(const string[])
  333. {
  334.         for (new i = 0, j = strlen(string); i < j; i++)
  335.         {
  336.                 if (string[i] > '9' || string[i] < '0') return 0;
  337.         }
  338.         return 1;
  339. }
  340.  
  341. // Credits to Extremo (http://forum.sa-mp.com/showpost.php?p=1916941&postcount=4)
  342. stock SendSplitMessage(playerid, color, final[])
  343. {
  344.     new buffer[EX_SPLITLENGTH+5];
  345.     new len = strlen(final);
  346.     if(len>EX_SPLITLENGTH)
  347.     {
  348.         new times = (len/EX_SPLITLENGTH);
  349.         for(new i = 0; i < times+1; i++)
  350.         {
  351.             strdel(buffer, 0, EX_SPLITLENGTH+5);
  352.             if(len-(i*EX_SPLITLENGTH)>EX_SPLITLENGTH)
  353.             {
  354.                 strmid(buffer, final, EX_SPLITLENGTH*i, EX_SPLITLENGTH*(i+1));
  355.                 format(buffer, sizeof(buffer), "%s ...", buffer);
  356.             }
  357.             else
  358.             {
  359.                 strmid(buffer, final, EX_SPLITLENGTH*i, len);
  360.                 format(buffer, sizeof(buffer), "... %s", buffer);
  361.             }
  362.             SendClientMessage(playerid, color, buffer);
  363.         }
  364.     }
  365.     else
  366.     {
  367.         SendClientMessage(playerid, color, final);
  368.     }
  369.     return 1;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement