Advertisement
Biesmen

Automated Messages 4.0

Feb 17th, 2013
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.43 KB | None | 0 0
  1. //=========================================================================//
  2. //* Automatic Client Message Sender Filterscript
  3. //* Version: 4.0
  4. //* ==========================
  5. //* UPDATE NOTES 3.0 - 4.0:
  6. //* ==========================
  7. //* ADDITIONS/MODIFICATIONS
  8. //* ==========================
  9. //* Commands removed (except /nextline), replaced them with a dialog. Type /messages to open the dialog.
  10. //* Completely dynamic:
  11. //*                     Dialog Control Center (DCC)
  12. //*                     Lines can be edited in the DCC
  13. //*                     Lines can be added/removed in the DCC
  14. //*                     The delay between sending messages can be configured in the DCC
  15. //*                     You can toggle the "filterscript" in the DCC
  16. //* The filterscript creates messages.txt automatically
  17. //* ==========================
  18. //* FIXES
  19. //* ==========================
  20. //* Fixed the filterscript from repeating the same line
  21. //=========================================================================//
  22. //* Created by:
  23. //* Biesmen
  24. //          http://forum.sa-mp.com/member.php?u=52068
  25. //=========================================================================//
  26.  
  27. #include <a_samp>
  28. #include <sscanf2> // SSCANF by Y_Less
  29.  
  30. #define dcmd(%1,%2,%3) if ((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, "")))||(((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  31. #define SendDialogMessage(%1,%2,%3) ShowPlayerDialog(%1, 8763, DIALOG_STYLE_MSGBOX, %2, %3, "Ok", "")
  32.  
  33. #define MAX_LINES       20 // Available text lines: 19 (1 is used for the timer)
  34. #define MAX_CHARACTERS  128 // Max characters in a line
  35. #define MESSAGE_FILE    "messages.txt"
  36.  
  37. forward MessageTimer();
  38.  
  39. new Lines[MAX_LINES+1][MAX_CHARACTERS],
  40.     MessageTime = 1,
  41.     MessageTimerS,
  42.     MaxLines,
  43.     NextLine = 0,
  44.     PreviousLine;
  45.  
  46. new pChosenLine[MAX_PLAYERS];
  47.  
  48. stock WriteFileLine()
  49. {
  50.     new cline = 0;
  51.     new File:aFile = fopen(MESSAGE_FILE, io_write);
  52.     while(cline != MaxLines)
  53.     {
  54.         cline++;
  55.         fwrite(aFile, Lines[cline]);
  56.     }
  57.     fclose(aFile);
  58.     return 1;
  59. }
  60.  
  61. stock ReadLines()
  62. {
  63.     new str[MAX_CHARACTERS],
  64.         dlines,
  65.         File:aFile;
  66.  
  67.     if(!fexist(MESSAGE_FILE)) // If MESSAGE_FILE doesn't exist
  68.     {
  69.         aFile = fopen(MESSAGE_FILE, io_write); // Creates messages.txt
  70.         fwrite(aFile, "300"); // Sets the time per messages to 5 minutes by default.
  71.         fclose(aFile);
  72.         ReadLines();
  73.     }
  74.     else // If MESSAGE_FILE exists
  75.     {
  76.         aFile = fopen(MESSAGE_FILE, io_read);
  77.         while(fread(aFile, str))
  78.         {
  79.             dlines++;
  80.             format(Lines[dlines], MAX_CHARACTERS,"%s",str);
  81.         }
  82.         MessageTime = strval(Lines[1]);
  83.         MaxLines = dlines;
  84.         fclose(aFile);
  85.     }
  86.     return 1;
  87. }
  88.  
  89. isNumeric(const string[])
  90. {
  91.     for (new i = 0, j = strlen(string); i < j; i++)
  92.     {
  93.         if (string[i] > '9' || string[i] < '0') return 0;
  94.     }
  95.     return 1;
  96. }
  97.  
  98. stock LinesLeft()
  99. {
  100.     return MAX_LINES - MaxLines;
  101. }
  102.  
  103. stock MessageStatus()
  104. {
  105.     new str[180];
  106.     if(MessageTimerS != 0 )
  107.     {
  108.         str = "{FFFFFF}Add new line\nModify existing lines\nChange the message display time\nTurn the messages {FF0000}off";
  109.     }
  110.     else
  111.     {
  112.         str = "{FFFFFF}Add new line\nModify existing lines\nChange the message display time\nTurn the messages {00CD00}on";
  113.     }
  114.     return str;
  115. }
  116.  
  117. public OnFilterScriptInit()
  118. {
  119.     ReadLines();
  120.     MessageTimerS = SetTimer("MessageTimer", MessageTime*1000, true);
  121.     print("\n--------------------------------------");
  122.     print(" Automatic Client Message sender");
  123.     print(" Version: 4.0");
  124.     print(" Created by: Biesmen");
  125.     print("--------------------------------------\n");
  126.     return 1;
  127. }
  128.  
  129. public OnFilterScriptExit()
  130. {
  131.     KillTimer(MessageTimerS);
  132.     return 1;
  133. }
  134.  
  135. public MessageTimer()
  136. {
  137.     new rand = random(MaxLines+1);
  138.     if(MaxLines == 1)
  139.     {
  140.         MessageTime = 300;
  141.         SendClientMessageToAll(0xEE7621FF, "This server is using Automated Messages for the first time. Please configure the filterscript by logging in as rcon admin and type /messages");
  142.         print("This server is using Automated Messages for the first time. Please configure the filterscript by entering the game, logging in as rcon admin and type /messages");
  143.         return 1;
  144.     }
  145.     if(NextLine == 0)
  146.     {
  147.         if(rand == 1 || rand == 0) return MessageTimer();
  148.         if(rand == PreviousLine && MaxLines != 2) return MessageTimer();
  149.     }
  150.     else
  151.     {
  152.         rand = NextLine;
  153.         NextLine = 0;
  154.     }
  155.     SendClientMessageToAll(0xFFFFFFAA, Lines[rand]);
  156.     PreviousLine = rand;
  157.     return 1;
  158. }
  159.  
  160. public OnPlayerCommandText(playerid, cmdtext[])
  161. {
  162.     dcmd(nextline, 8, cmdtext);
  163.     if(strcmp("/messages", cmdtext, true) == 0)
  164.     {
  165.         if(IsPlayerAdmin(playerid))
  166.         {
  167.             ShowPlayerDialog(playerid, 1994, DIALOG_STYLE_LIST, "Automated Messages - Select your choice", MessageStatus(), "Select", "Exit");
  168.         }
  169.         return 1;
  170.     }
  171.     return 0;
  172. }
  173.  
  174. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  175. {
  176.     new LineList[(MAX_LINES+1)*MAX_CHARACTERS],
  177.         str[180];
  178.  
  179.     if(dialogid == 1994) // /messages will open this dialog
  180.     {
  181.         format(LineList, sizeof LineList, "{FFFFFF}%s\n", Lines[2]);
  182.         if(!response) return 1;
  183.         if(listitem == 0)
  184.         {
  185.             if(LinesLeft() <= 0) { format(str, sizeof str, "{FFFFFF}You have got {CD0000}%i lines{FFFFFF} left!\n{CD0000}Please remove some lines or increase MAX_LINES!", LinesLeft()); }
  186.             else { format(str, sizeof str, "{FFFFFF}You have got {00BFFF}%i lines{FFFFFF} left!\n", LinesLeft()); }
  187.             ShowPlayerDialog(playerid, 1995, DIALOG_STYLE_INPUT, "Automated Messages - Add a new line", str, "Add", "Back");
  188.         }
  189.         if(listitem == 1)
  190.         {
  191.             for(new i = 3; i < MaxLines+1; i++)
  192.             {
  193.     //          strins(Lines[i], "\n", 0); // Adding the \n command }
  194.                 strcat(LineList, Lines[i]);//                       } I do believe this isn't a very smart way. However, I think it is the only way. Whenever a new line has been made
  195.     //          strdel(Lines[i], 0, 1); // Removing the \n command } it will remove the "\n" command, or else you will see a space before the string when the Timer calls the string.
  196.             }
  197.             ShowPlayerDialog(playerid, 1996, DIALOG_STYLE_LIST, "Automated Messages - Modify existing lines", LineList, "Modify", "Back");
  198.         }
  199.         if(listitem == 2)
  200.         {
  201.             format(str, sizeof str, "{FFFFFF}Modify the message display time. Current interval in seconds: {00BFFF}%d{FFFFFF} seconds", MessageTime);
  202.             ShowPlayerDialog(playerid, 1999, DIALOG_STYLE_INPUT, "Automated Messages - Modify the message display time", str, "Done", "Cancel");
  203.         }
  204.         if(listitem == 3)
  205.         {
  206.             if(MessageTimerS != 0)
  207.             {
  208.                 KillTimer(MessageTimerS);
  209.                 MessageTimerS = 0;
  210.                 SendDialogMessage(playerid, "Automated Messages - Message Timer Status", "{FFFFFF}The Message Timer has been {FF0000}deactivated");
  211.             }
  212.             else
  213.             {
  214.                 MessageTimerS = SetTimer("MessageTimer", MessageTime*1000, true);
  215.                 SendDialogMessage(playerid, "Automated Messages - Message Timer Status", "{FFFFFF}The Message Timer has been {00CD00}activated");
  216.             }
  217.         }
  218.     }
  219.     if(dialogid == 1995) // Add new line dialog
  220.     {
  221.         if(!response)
  222.         {
  223.             ShowPlayerDialog(playerid, 1994, DIALOG_STYLE_LIST, "Automated Messages - Select your choice", MessageStatus(), "Select", "Exit");
  224.             return 1;
  225.         }
  226.         if(strlen(inputtext) > MAX_CHARACTERS)
  227.         {
  228.             format(str, sizeof str, "{CD0000}ERROR: You have exceeded the amount of max characters. Max characters:  {00BFFF}%i\n{FFFFFF}You have got {00BFFF}%i lines{FFFFFF} left!\n", MAX_CHARACTERS, LinesLeft());
  229.             ShowPlayerDialog(playerid, 1995, DIALOG_STYLE_INPUT, "Automated Messages - Add a new line", str, "Add", "Back");
  230.             return 1;
  231.         }
  232.         if(strlen(inputtext) < 1)
  233.         {
  234.             format(str, sizeof str, "{CD0000}ERROR: Please enter at least {00BFFF}1{CD0000} character!\n{FFFFFF}You have got {00BFFF}%i lines{FFFFFF} left!\n", LinesLeft());
  235.             ShowPlayerDialog(playerid, 1995, DIALOG_STYLE_INPUT, "Automated Messages - Add a new line", str, "Add", "Back");
  236.             return 1;
  237.         }
  238.         MaxLines = MaxLines+1;
  239.         format(Lines[MaxLines], MAX_CHARACTERS+4, "%s\r\n", inputtext); //+4 to ignore "\r\n"
  240.         WriteFileLine();
  241.     //  strdel(Lines[MaxLines], 0, 2);
  242.         format(str, 40+MAX_CHARACTERS, "{FFFFFF}You created a new line: {FF8040}%s", Lines[MaxLines]);
  243.         SendDialogMessage(playerid, "Automated Messages - New line added", str);
  244.     }
  245.     if(dialogid == 1996) // Modify existing lines dialog
  246.     {
  247.         if(!response)
  248.         {
  249.             ShowPlayerDialog(playerid, 1994, DIALOG_STYLE_LIST, "Automated Messages - Select your choice", MessageStatus(), "Select", "Exit");
  250.             return 1;
  251.         }
  252.         pChosenLine[playerid] = listitem+2;
  253.         ShowPlayerDialog(playerid, 1997, DIALOG_STYLE_LIST, "Automated Messages - Modify Existing Line", "Edit line\nRemove line", "Select", "Back");
  254.     }
  255.     if(dialogid == 1997) // Selected a line to modify
  256.     {
  257.         if(!response)
  258.         {
  259.             ShowPlayerDialog(playerid, 1994, DIALOG_STYLE_LIST, "Automated Messages - Select your choice", MessageStatus(), "Select", "Exit");
  260.             return 1;
  261.         }
  262.         if(listitem == 0) return ShowPlayerDialog(playerid, 1998, DIALOG_STYLE_INPUT, "Automated Messages - Modify a line", "{FFFFFF}Modify the line into:", "Done", "Cancel");
  263.         if(listitem == 1)
  264.         {
  265.             format(str, sizeof str, "%s", Lines[pChosenLine[playerid]]);
  266.             format(Lines[pChosenLine[playerid]], 5, "\r\n"); //I'm basically doing this so strdel won't remove any line after the line that has been removed. I've tried
  267.             strdel(Lines[pChosenLine[playerid]], 0, 5); //to remove "Lines[pChosenLine[playerid]], 0, strlen(Lines[pChosenLine[playerid]])", however it will remove any line after this one.
  268.             WriteFileLine();
  269.             format(str, sizeof str, "{FFFFFF}You have successfully removed the line: {FF8040}%s", str);
  270.             SendDialogMessage(playerid, "Automated Messages - Line removed", str);
  271.             SendClientMessage(playerid, 0xFFC68CFF, "The filterscript is now reloading.");
  272.             KillTimer(MessageTimerS);
  273.             ReadLines();
  274.             MessageTimerS = SetTimer("MessageTimer", MessageTime*1000, true);
  275.         }
  276.     }
  277.     if(dialogid == 1998)
  278.     {
  279.         if(!response)
  280.         {
  281.             ShowPlayerDialog(playerid, 1994, DIALOG_STYLE_LIST, "Automated Messages - Select your choice", MessageStatus(), "Select", "Exit");
  282.             return 1;
  283.         }
  284.         if(strlen(inputtext) > MAX_CHARACTERS)
  285.         {
  286.             format(str, sizeof str, "{CD0000}ERROR: You have exceeded the amount of max characters. Max characters:  {00BFFF}%i\n{FFFFFF}Modify the line into:\n", MAX_CHARACTERS);
  287.             ShowPlayerDialog(playerid, 1998, DIALOG_STYLE_INPUT, "Automated Messages - Modify a line", str, "Done", "Cancel");
  288.             return 1;
  289.         }
  290.         if(strlen(inputtext) < 1)
  291.         {
  292.             ShowPlayerDialog(playerid, 1998, DIALOG_STYLE_INPUT, "Automated Messages - Modify a line", "{CD0000}ERROR: Please enter at least {00BFFF}1{CD0000} character!\n{FFFFFF}Modify the line into:", "Done", "Cancel");
  293.             return 1;
  294.         }
  295.         format(Lines[pChosenLine[playerid]], MAX_CHARACTERS+4, "%s\r\n", inputtext);
  296.         WriteFileLine();
  297.         format(str, 40+MAX_CHARACTERS, "{FFFFFF}You have successfully modified the line into: {FF8040}%s", Lines[pChosenLine[playerid]]);
  298.         SendDialogMessage(playerid, "Automated Messages - Line modified", str);
  299.     }
  300.     if(dialogid == 1999)
  301.     {
  302.         if(!response)
  303.         {
  304.             ShowPlayerDialog(playerid, 1994, DIALOG_STYLE_LIST, "Automated Messages - Select your choice", MessageStatus(), "Select", "Exit");
  305.             return 1;
  306.         }
  307.         if(isNumeric(inputtext))
  308.         {
  309.             KillTimer(MessageTimerS);
  310.             MessageTime = strval(inputtext);
  311.             MessageTimerS = SetTimer("MessageTimer", MessageTime*1000, true);
  312.             format(str, sizeof str, "{FFFFFF}The messages will now display once every {00BFFF}%i{FFFFFF} seconds", MessageTime);
  313.             SendDialogMessage(playerid, "Automated Messages - Message Time modified", str);
  314.             format(Lines[1], 11, "%i\r\n", MessageTime);
  315.             WriteFileLine();
  316.         }
  317.         else
  318.         {
  319.             format(str, 180, "{CD0000}ERROR: Only use numbers! \n{FFFFFF}Modify the message display time. Current interval in seconds: {00BFFF}%d{FFFFFF} seconds", MessageTime);
  320.             ShowPlayerDialog(playerid, 1999, DIALOG_STYLE_INPUT, "Automated Messages - Modify the message display time", str, "Done", "Cancel");
  321.         }
  322.     }
  323.     return 0;
  324. }
  325.  
  326. dcmd_nextline(playerid, params[])
  327. {
  328.     new line, str[128];
  329.     if(IsPlayerAdmin(playerid))
  330.     {
  331.         if(sscanf(params, "i", line)) return SendClientMessage(playerid, 0xFF8040FF, "USAGE: /nextline [line]");
  332.         if(line > MaxLines-1 || line < 1) return SendClientMessage(playerid, 0xFF8040FF, "ERROR: This line doesn't exist!");
  333.         NextLine = line+1;
  334.         format(str, sizeof(str), "Next line to be announced is Line %i", line);
  335.         SendClientMessage(playerid, 0xFFC68CFF, str);
  336.     }
  337.     return 1;
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement