Guest User

Untitled

a guest
Aug 24th, 2016
313
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define FILTERSCRIPT
  2.  
  3. #include <a_samp>
  4. #include <zcmd>
  5.  
  6. #define MAX_TEXT_SIZE 128
  7. #define MAX_MSG 25
  8. #define MSG_FILE "messages.ini"
  9.  
  10. forward RandMsg();
  11. new MsgIndex[MAX_MSG][MAX_TEXT_SIZE];
  12. new pOnMsg[MAX_PLAYERS];
  13. new Cooldown = 60;
  14. new Prefix[30];
  15. new send[MAX_TEXT_SIZE];
  16. new Timer;
  17.  
  18. public OnFilterScriptInit()
  19. {
  20.     print("\n--------------------------------------");
  21.     print(" Advanced Dynamic Random Messages v1.2 Loaded");
  22.     print(" Editor: Shinja");
  23.     print("--------------------------------------\n");
  24.     if(!fexist(MSG_FILE))
  25.     {
  26.         fcreate(MSG_FILE);
  27.         new string[6];
  28.         format(string,6,"%d",Cooldown);
  29.         fstrset(MSG_FILE,"Cooldown",string);
  30.         format(Prefix, 30, "{FFE100}[S:Random MSGs]");
  31.         fstrset(MSG_FILE,"Prefix",Prefix);
  32.     }
  33.     else ReloadConfig();
  34.     ReloadMessages();
  35.     Timer = SetTimer("RandMsg", Cooldown*1000, true);
  36.     return 1;
  37. }
  38.  
  39. public OnFilterScriptExit() { KillTimer(Timer); return 1; }
  40.  
  41. public RandMsg()
  42. {
  43.     new rand = random(GetMessagesCount());
  44.     if(rand == GetMessagesCount()) rand--;
  45.     format(send, MAX_TEXT_SIZE, "%s {FFFFFF}%s", Prefix, MsgIndex[rand]);
  46.     SendClientMessageToAll(-1, send);
  47.     return 1;
  48. }
  49.  
  50. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  51. {
  52.     switch(dialogid)
  53.     {
  54.         case 111:
  55.         {
  56.             if(response)
  57.             {
  58.                 switch(listitem)
  59.                 {
  60.                     case 0: ShowPlayerDialog(playerid, 112, DIALOG_STYLE_INPUT, "{2BFF00}Add New Message", "{23CF00}You have selected to add a new message", "Add", "Back");
  61.                     case 1: ShowPlayerDialog(playerid, 114, DIALOG_STYLE_INPUT, "{2BFF00}Cooldown - Seconds", "{23CF00}You have selected to change cooldown", "Change", "Back");
  62.                     case 2: ShowPlayerDialog(playerid, 115, DIALOG_STYLE_INPUT, "{2BFF00}Prefix", "{23CF00}You have selected to change the prefix", "Change", "Back");
  63.                     default:
  64.                     {
  65.                         new msg[20]; format(msg, sizeof(msg), "Message%d", listitem-2);
  66.                         new str[200]; format(str, sizeof(str), "{23CF00}You have selected to edit a message\n{9E9E9E}Message %d: {99C98F}%s", listitem-2, fstr(MSG_FILE, msg));
  67.                         ShowPlayerDialog(playerid, 113, DIALOG_STYLE_INPUT, "{2BFF00}Edit Message", str, "Edit", "Back");
  68.                         pOnMsg[playerid] = listitem-2;
  69.                     }
  70.                 }
  71.             }
  72.             return 1;
  73.         }
  74.         case 112:
  75.         {
  76.             if(!response) return ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  77.             if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, 112, DIALOG_STYLE_INPUT, "{2BFF00}Add New Message", "{23CF00}You have selected to add a new message\n{FF0000}A message must contain atleast 6 characters", "Add", "Back");
  78.             new msg[20]; format(msg, sizeof(msg), "Message%d", GetMessagesCount()+1);
  79.             fstrset(MSG_FILE, msg, inputtext);
  80.             ReloadMessages();
  81.             ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  82.             return 1;
  83.         }
  84.         case 114:
  85.         {
  86.             if(!response) return ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  87.             if(!(IsNumeric(inputtext))) return ShowPlayerDialog(playerid, 114, DIALOG_STYLE_INPUT, "{2BFF00}Cooldown - Seconds", "{23CF00}You have selected to change cooldown\n{FF0000}A cooldown must be numeric", "Change", "Back");
  88.             Cooldown = strval(inputtext);
  89.             new string[6];
  90.             format(string,6,"%d",Cooldown);
  91.             fstrset(MSG_FILE,"Cooldown",string);
  92.             KillTimer(Timer);
  93.             Timer = SetTimer("RandMsg", Cooldown*1000, true);
  94.             ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  95.             return 1;
  96.         }
  97.         case 115:
  98.         {
  99.             if(!response) return ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  100.             if(strlen(inputtext) < 3) return ShowPlayerDialog(playerid, 115, DIALOG_STYLE_INPUT, "{2BFF00}Prefix", "{23CF00}You have selected to change the prefix\n{FF0000}A prefix must contain atleast 3 characters", "Change", "Back");
  101.             fstrset(MSG_FILE,"Prefix",inputtext);
  102.             format(Prefix, 30, "%s", inputtext);
  103.             ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  104.             return 1;
  105.         }
  106.         case 113:
  107.         {
  108.             if(!response) return ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  109.             new msg[20]; format(msg, sizeof(msg), "Message%d", pOnMsg[playerid]);
  110.             new str[200]; format(str, sizeof(str), "{23CF00}You have selected to edit a message\n{9E9E9E}Message %d: {99C98F}%s\n{FF0000}A message must contain atleast 6 characters", pOnMsg[playerid], fstr(MSG_FILE, msg));
  111.             if(strlen(inputtext) < 6) return ShowPlayerDialog(playerid, 113, DIALOG_STYLE_INPUT, "{2BFF00}Edit Message", str, "Edit", "Back");
  112.             fstrset(MSG_FILE, msg, inputtext);
  113.             ReloadMessages();
  114.             ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  115.             return 1;
  116.         }
  117.     }
  118.     return 0;
  119. }
  120.  
  121. CMD:msettings(playerid)
  122. {
  123.     if(!IsPlayerAdmin(playerid)) return 0;
  124.     ShowPlayerDialog(playerid, 111, DIALOG_STYLE_TABLIST, "{1BA300}Random Messages Settings", GetMessages(), "Select", "Cancel");
  125.     return 1;
  126. }
  127.  
  128. stock GetMessages()
  129. {
  130.     new string[1280], get[MAX_TEXT_SIZE], msg[20];
  131.     format(string, sizeof(string), "{FA9E00}Add\t{2BFF00}Empty\n{FA9E00}Cooldown\t{2BFF00}%d Seconds\n{FA9E00}Prefix\t{2BFF00}%s\n", Cooldown, Prefix);
  132.     for(new i=0, j=MAX_MSG; i<j; i++)
  133.     {
  134.         format(msg, sizeof(msg), "Message%d", i+1);
  135.         if(!flineexist(MSG_FILE, msg)) break;
  136.         format(get, sizeof(get), "{9E9E9E}%s\t%s\n", msg, fstr(MSG_FILE, msg));
  137.         strcat(string, get);
  138.     }
  139.     return string;
  140. }
  141.  
  142. stock ReloadMessages()
  143. {
  144.     new msg[20];
  145.     for(new i, j=MAX_MSG; i<j; i++)
  146.     {
  147.         format(msg, sizeof(msg), "Message%d", i+1);
  148.         if(!flineexist(MSG_FILE, msg)) break;
  149.         format(MsgIndex[i], MAX_TEXT_SIZE, "%s", fstr(MSG_FILE, msg));
  150.     }
  151. }
  152.  
  153. stock ReloadConfig()
  154. {
  155.     format(Prefix, 30, "%s",fstr(MSG_FILE, "Prefix"));
  156.     Cooldown = strval(fstr(MSG_FILE, "Cooldown"));
  157. }
  158.  
  159. stock GetMessagesCount()
  160. {
  161.     new count, msg[20];
  162.     for(new i, j=MAX_MSG; i<j; i++)
  163.     {
  164.         format(msg, sizeof(msg), "Message%d", i+1);
  165.         if(!flineexist(MSG_FILE, msg)) break;
  166.         count++;
  167.     }
  168.     return count;
  169. }
  170.  
  171. stock IsNumeric(const string[])
  172. {
  173.     for (new i = 0, j = strlen(string); i < j; i++)
  174.     {
  175.         if (string[i] > '9' || string[i] < '0') return 0;
  176.     }
  177.     return 1;
  178. }
  179.  
  180. //Some file functions
  181. stock fcreate(file[]) {
  182.     if (fexist(file)) return false;
  183.     new File:f=fopen(file,io_write);
  184.     if (f) {
  185.         fclose(f);
  186.         return true;
  187.     }
  188.     return false;
  189. }
  190.  
  191. stock flineexist(file[],line[]) {
  192.     if(strlen(line)==0 || strlen(line)+2>255) return false;
  193.     new File:f=fopen(file,io_read);
  194.     if(!f) return false;
  195.     new tmp[255];
  196.     while(fread(f,tmp))
  197.     {
  198.         if(tmp[strlen(line)]=='=' && !strcmp(tmp, line, true, strlen(line)))
  199.         {
  200.             fclose(f);
  201.             return true;
  202.         }
  203.     }
  204.     fclose(f);
  205.     return false;
  206. }
  207.  
  208. stock fstr(file[],line[]) {
  209.     new tmp[255];
  210.     if(strlen(line)==0 || strlen(line)+2>255) return tmp;
  211.     new File:f=fopen(file,io_read);
  212.     if(!f) return tmp;
  213.     while(fread(f,tmp))
  214.     {
  215.         if(tmp[strlen(line)]=='=' && !strcmp(tmp, line, true, strlen(line)))
  216.         {
  217.             DINI_StripNewLine(tmp);
  218.             strmid(tmp, tmp, strlen(line) + 1, strlen(tmp), 255);
  219.             fclose(f);
  220.             return tmp;
  221.         }
  222.     }
  223.     fclose(f);
  224.     return tmp;
  225. }
  226.  
  227. stock fstrset(file[],line[],value[]) {
  228.     if(strlen(line)==0 || strlen(line)+strlen(value)+2>255) return false;
  229.     new File:f, File:f2;
  230.     new tmp[255];
  231.     new bool:wasset=false;
  232.     format(tmp,sizeof(tmp),"%s.part",file);
  233.     fremove(tmp);
  234.     f=fopen(file,io_read);
  235.     if(!f) return false;
  236.     f2=fopen(tmp,io_write);
  237.     if(!f2)
  238.     {
  239.         fclose(f);
  240.         return false;
  241.     }
  242.     while(fread(f,tmp))
  243.     {
  244.         if(!wasset && tmp[strlen(line)]=='=' && !strcmp(tmp, line, true, strlen(line)))
  245.         {
  246.             format(tmp,sizeof(tmp),"%s=%s",line,value);
  247.             wasset=true;
  248.         }
  249.         else
  250.         {
  251.             DINI_StripNewLine(tmp);
  252.         }
  253.         fwrite(f2,tmp);
  254.         fwrite(f2,"\r\n");
  255.     }
  256.     if(!wasset)
  257.     {
  258.         format(tmp,sizeof(tmp),"%s=%s",line,value);
  259.         fwrite(f2,tmp);
  260.         fwrite(f2,"\r\n");
  261.     }
  262.     fclose(f);
  263.     fclose(f2);
  264.     format(tmp,sizeof(tmp),"%s.part",file);
  265.     if(DINI_fcopytextfile(tmp,file)) return fremove(tmp);
  266.     return false;
  267. }
  268.  
  269. //from dini (Dracoblue)
  270. stock DINI_StripNewLine(string[]) {
  271.     new len = strlen(string);
  272.     if (string[0]==0) return ;
  273.     if ((string[len - 1] == '\n') || (string[len - 1] == '\r')) {
  274.         string[len - 1] = 0;
  275.         if (string[0]==0) return ;
  276.         if ((string[len - 2] == '\n') || (string[len - 2] == '\r')) string[len - 2] = 0;
  277.     }
  278. }
  279.  
  280. stock DINI_fcopytextfile(oldname[],newname[]) {
  281.     new File:ohnd,File:nhnd;
  282.     if (!fexist(oldname)) return false;
  283.     ohnd=fopen(oldname,io_read);
  284.     if (!ohnd) return false;
  285.     nhnd=fopen(newname,io_write);
  286.     if (!nhnd) {
  287.         fclose(ohnd);
  288.         return false;
  289.     }
  290.     new tmpres[255];
  291.     while (fread(ohnd,tmpres)) {
  292.         DINI_StripNewLine(tmpres);
  293.         format(tmpres,sizeof(tmpres),"%s\r\n",tmpres);
  294.         fwrite(nhnd,tmpres);
  295.     }
  296.     fclose(ohnd);
  297.     fclose(nhnd);
  298.     return true;
  299. }
RAW Paste Data