Advertisement
Eduardo_AC

Private Messages System

Mar 20th, 2018
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.62 KB | None | 0 0
  1. #if defined _pm_included
  2.     #endinput
  3. #endif
  4. #define _pm_included
  5.  
  6. /*
  7.         Private Messages System by F1N4L
  8.  
  9.         ________________________________________
  10.  
  11.         SendPrivateMessageToPlayerName(from_playerid, to_player_name[], message[], bool:case_sensitive)
  12.         SendPrivateMessageToPlayerID(from_playerid, to_playerid, message[])
  13.         ShowPrivateMessages(player_name[], messages[])
  14.         RemovePrivateMessageByID(player_name[], line_id)
  15.         ClearPrivateMessages(player_name[])
  16. */
  17.  
  18. #define FILE_SAVE_MODE                  2 // 1 = Txt File | 2 = MySQL
  19.  
  20. #if defined FILE_SAVE_MODE
  21.     #if FILE_SAVE_MODE < 1 || FILE_SAVE_MODE > 2
  22.         #error [PM Inc] Invalid save mode. Please select between 1-2.
  23.     #endif
  24. #endif
  25.  
  26. // -- Include Config
  27. #if FILE_SAVE_MODE == 1
  28.     #define PRIVATE_MESSAGE_PATH            "Private Messages/%s.pm"
  29. #elseif FILE_SAVE_MODE == 2
  30.     #define MYSQL_HOST                      "127.0.0.1"
  31.     #define MYSQL_USER                      "root"
  32.     #define MYSQL_PASS                      ""
  33.     #define MYSQL_DB                        "samp_db"
  34. #endif
  35.  
  36. #define MAX_PRIVATE_MESSAGE_LINES       (10) // Per file
  37. #define MAX_PRIVATE_MESSAGE_LENGHT      (100) // Per line
  38.  
  39. // -- PM Error Code
  40. #define PM_ERR_NONE                     (0)
  41. #define PM_ERR_INVALID_PLAYER_ID        (1)
  42. #define PM_ERR_EXCEEDED_FILE_LINES      (2)
  43.  
  44. // -- Personalized Format
  45. #define PRIVATE_MESSAGE_FORMAT          "[PM]: %s disse: %s" // [1]-From Player | [2]-Message
  46.  
  47. // -- Forward's
  48. forward OnPlayerSendPrivateMessageOn(from_playerid, to_playerid, message[]);
  49. forward OnPlayerSendPrivateMessageOff(from_playerid, to_player_name[], message[], error);
  50.  
  51. #if FILE_SAVE_MODE == 2
  52.     static MySQL:MyConnnection;
  53.  
  54.     public OnGameModeInit()
  55.     {
  56.         MyConnnection = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
  57.  
  58.         if(mysql_errno(MyConnnection) != 0)
  59.             printf("Erro ao conectar-se na base de dados MySQL (%d)", mysql_errno(MyConnnection));
  60.  
  61.         #if defined PM_OnGameModeInit
  62.             PM_OnGameModeInit();
  63.         #endif
  64.         return true;
  65.     }
  66.     #if defined _ALS_OnGameModeInit
  67.         #undef OnGameModeInit
  68.     #else
  69.         #define _ALS_OnGameModeInit
  70.     #endif
  71.     #define OnGameModeInit PM_OnGameModeInit
  72.     #if defined PM_OnGameModeInit
  73.         forward PM_OnGameModeInit();
  74.     #endif
  75. #endif
  76.  
  77. stock SendPrivateMessageToPlayerName(from_playerid, to_player_name[MAX_PLAYER_NAME], message[MAX_PRIVATE_MESSAGE_LENGHT - MAX_PLAYER_NAME - 19], bool:case_sensitive)
  78. {
  79.     static player_name[MAX_PLAYER_NAME];
  80.  
  81.     for(new i = 0, x = GetPlayerPoolSize()+1; i < x; ++ i)
  82.     {
  83.         GetPlayerName(i, player_name, MAX_PLAYER_NAME);
  84.  
  85.         if(strcmp(to_player_name, player_name, case_sensitive, MAX_PLAYER_NAME) == 0)
  86.         {
  87.             OnPlayerSendPrivateMessageOn(from_playerid, i, message);
  88.             return true;
  89.         }
  90.     }
  91.  
  92.     SaveNewPrivateMessage(from_playerid, to_player_name, message);
  93.  
  94.     return true;
  95. }
  96.  
  97. //                                                                          100                         24            PRIVATE_MESSAGE_FORMAT ([%d] [PM]: %s disse: )
  98. //                                                                          |                           |             |
  99. stock SendPrivateMessageToPlayerID(from_playerid, to_playerid, message[MAX_PRIVATE_MESSAGE_LENGHT - MAX_PLAYER_NAME - 19])
  100. {
  101.     new to_playerid_str[4];
  102.  
  103.     valstr(to_playerid_str, to_playerid, false);
  104.  
  105.     if(!IsPlayerConnected(to_playerid))
  106.         return OnPlayerSendPrivateMessageOff(from_playerid, to_playerid_str, message, PM_ERR_INVALID_PLAYER_ID);
  107.  
  108.     OnPlayerSendPrivateMessageOn(from_playerid, to_playerid, message);
  109.  
  110.     return true;
  111. }
  112.  
  113.  
  114. stock ShowPrivateMessages(player_name[], messages[])
  115. {
  116.     #if FILE_SAVE_MODE == 1
  117.         static lines_str[MAX_PRIVATE_MESSAGE_LENGHT], get_message[MAX_PRIVATE_MESSAGE_LENGHT], player_file[MAX_PLAYER_NAME+20];
  118.         new lines_count;
  119.  
  120.         format(player_file, sizeof player_file, PRIVATE_MESSAGE_PATH, player_name);
  121.  
  122.         if(!fexist(player_file))
  123.             return false;
  124.  
  125.         new File:_priv_file = fopen(player_file, io_read);
  126.  
  127.         while(fread(_priv_file, lines_str))
  128.         {
  129.             lines_count ++;
  130.  
  131.             format(get_message, sizeof get_message, "%d | %s", lines_count, lines_str);
  132.             strcat(messages, get_message, 1024);
  133.         }
  134.     #elseif FILE_SAVE_MODE == 2
  135.         static string[129], Cache:rows, id, from_playerid[MAX_PLAYER_NAME], get_message[MAX_PRIVATE_MESSAGE_LENGHT], message[MAX_PRIVATE_MESSAGE_LENGHT];
  136.        
  137.         mysql_format(MyConnnection, string, sizeof string, "SELECT * FROM `private messages` WHERE `to player` = '%e' LIMIT %d", player_name, MAX_PRIVATE_MESSAGE_LINES);
  138.         rows = mysql_query(MyConnnection, string);
  139.  
  140.         if(cache_num_rows())
  141.         {
  142.             for(new x = 0; x < cache_num_rows(); ++ x)
  143.             {
  144.                 cache_get_value_name_int(x, "id", id);
  145.                 cache_get_value_name(x, "from player", from_playerid);
  146.                 cache_get_value_name(x, "message", message);
  147.  
  148.                 format(get_message, sizeof get_message, "%d | "PRIVATE_MESSAGE_FORMAT"\n", id, from_playerid, message);
  149.  
  150.                 strcat(messages, get_message, 1024);
  151.             }
  152.         }
  153.  
  154.         cache_delete(rows);
  155.     #endif
  156.  
  157.     return true;
  158. }
  159.  
  160.  
  161. stock RemovePrivateMessageByID(player_name[], line_id)
  162. {
  163.     if(line_id < 1 || line_id > MAX_PRIVATE_MESSAGE_LINES)
  164.             return false;
  165.  
  166.     #if FILE_SAVE_MODE == 1
  167.         static player_file[MAX_PLAYER_NAME+20];
  168.         format(player_file, sizeof player_file, PRIVATE_MESSAGE_PATH, player_name);
  169.  
  170.         new File:_priv_file = fopen(player_file, io_read), lines_count, lines_str[MAX_PRIVATE_MESSAGE_LENGHT], save_str[MAX_PRIVATE_MESSAGE_LINES * MAX_PRIVATE_MESSAGE_LENGHT];
  171.        
  172.         while(fread(_priv_file, lines_str))
  173.         {
  174.             lines_count ++;
  175.            
  176.             if(lines_count == line_id)
  177.                 continue;
  178.  
  179.             strcat(save_str, lines_str, 1024);
  180.         }
  181.  
  182.         fclose(_priv_file);
  183.  
  184.         _priv_file = fopen(player_file, io_write);
  185.         fwrite(_priv_file, save_str);
  186.         fclose(_priv_file);
  187.     #elseif FILE_SAVE_MODE == 2
  188.         static string[100];
  189.         mysql_format(MyConnnection, string, sizeof string, "DELETE FROM `private messages` WHERE `id` = '%i' AND `to player` = '%e'", line_id, player_name);
  190.         mysql_tquery(MyConnnection, string);
  191.     #endif
  192.  
  193.     return true;
  194. }
  195.  
  196. stock ClearPrivateMessages(player_name[])
  197. {
  198.     #if FILE_SAVE_MODE == 1
  199.         static player_file[MAX_PLAYER_NAME+20];
  200.         format(player_file, sizeof player_file, PRIVATE_MESSAGE_PATH, player_name);
  201.  
  202.         if(fexist(player_file))
  203.             fremove(player_file);
  204.         else return false;
  205.     #elseif FILE_SAVE_MODE == 2
  206.         static string[90];
  207.         mysql_format(MyConnnection, string, sizeof string, "DELETE FROM `private messages` WHERE `to player` = '%e'", player_name);
  208.         mysql_tquery(MyConnnection, string);
  209.     #endif
  210.  
  211.     return true;
  212. }
  213.  
  214. static stock SaveNewPrivateMessage(from_playerid, to_player_name[MAX_PLAYER_NAME], message[])
  215. {
  216.     static player_name[MAX_PLAYER_NAME], get_message[MAX_PRIVATE_MESSAGE_LENGHT], save_str[MAX_PRIVATE_MESSAGE_LENGHT];
  217.     get_message[0] = EOS;
  218.    
  219.     GetPlayerName(from_playerid, player_name, MAX_PLAYER_NAME);
  220.     strcat(get_message, message, MAX_PRIVATE_MESSAGE_LENGHT);
  221.     format(save_str, sizeof save_str, PRIVATE_MESSAGE_FORMAT"\r\n", player_name, get_message);
  222.  
  223.     #if FILE_SAVE_MODE == 1
  224.         static to_player_file[MAX_PLAYER_NAME+20], lines_str[MAX_PRIVATE_MESSAGE_LENGHT];
  225.         new lines_count;
  226.  
  227.         format(to_player_file, sizeof to_player_file, PRIVATE_MESSAGE_PATH, to_player_name);
  228.  
  229.         new File:_priv_file = fopen(to_player_file, io_readwrite);
  230.        
  231.         while(fread(_priv_file, lines_str))
  232.         {
  233.             lines_count ++;
  234.             if(lines_count >= MAX_PRIVATE_MESSAGE_LINES)
  235.             {
  236.                 OnPlayerSendPrivateMessageOff(from_playerid, to_player_name, get_message, PM_ERR_EXCEEDED_FILE_LINES);
  237.                 return false;
  238.             }
  239.         }
  240.  
  241.         fwrite(_priv_file, save_str);
  242.         fclose(_priv_file);
  243.  
  244.         OnPlayerSendPrivateMessageOff(from_playerid, to_player_name, get_message, PM_ERR_NONE);
  245.     #elseif FILE_SAVE_MODE == 2
  246.         static string[300];
  247.         mysql_format(MyConnnection, string, sizeof string, "INSERT INTO `private messages` (`from player`, `to player`, `message`) VALUES ('%e', '%e', '%e')", player_name, to_player_name, get_message);
  248.         mysql_tquery(MyConnnection, string);
  249.     #endif
  250.  
  251.     return true;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement