JuSTaR

[CS:GO] Hidonim API

Jun 21st, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.38 KB | None | 0 0
  1. #include <sdktools>
  2.  
  3. public Plugin myinfo =
  4. {
  5.     name = "HidonimAPI",
  6.     author = "JuSTaR",
  7.     description = "Hidonim API",
  8.     version = "1.0",
  9.     url = "https://www.fxp.co.il/"
  10. }
  11.  
  12. // Constants
  13. const int PROPERTIES_PER_LINE = 2; // 1 = Hidon display name, 2 = Hidon plugin name
  14. const int MAX_HIDONIM = 7;
  15. const int WORD_LENGTH = 32;
  16. const int MAX_ATTEMPS = 3;
  17. const int HIDON_SHOW_TIMES = 3;
  18.  
  19. // Hidonim reference variables
  20. char Hidonim_Display_Names[MAX_HIDONIM][32];
  21. char Hidonim_Plugins_Names[MAX_HIDONIM][32];
  22. char Backup_Display_Names[MAX_HIDONIM][32];
  23. int Hidonim_Original_ID[MAX_HIDONIM];
  24. int Hidonim_Loaded;
  25.  
  26. // Vote Menu variables
  27. Handle VotingTimer;
  28. Handle ShowNewHidon;
  29. bool HideVote[MAXPLAYERS];
  30. bool isVoted[MAXPLAYERS];
  31. float VotingResult[MAX_HIDONIM];
  32. int VotingVoices[MAX_HIDONIM];
  33. int VotingTime = 100;
  34. int TotalVotes;
  35. int VoteAttemp = 1;
  36. int HidonNumber = 0;
  37.  
  38. // Next-Hidon CountDown Timer
  39. bool HidonStarted = false;
  40. int NextHidonTime = 9000; // 900 seconds = 15 mins
  41.  
  42. // Hidon Start CountDown Timer
  43. Handle HidonCountDownTimer;
  44. int StartCountDown = 9; // CountDown goes from 9 -> 0 (10 times)
  45. char HidonCommand[64];
  46. char numbers[10][8] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
  47.  
  48. public OnPluginStart()
  49. {
  50.     RegAdminCmd("start_hidon", ManualStartHidonVote, ADMFLAG_RCON);
  51.     RegAdminCmd("stop_hidon", StopHidon, ADMFLAG_RCON);
  52.     RegConsoleCmd("hidon", NextHidon);
  53.     RegConsoleCmd("next_hidon", NextHidon);
  54.  
  55.     CreateTimer(0.1, AutoStartHidonVote, _, TIMER_REPEAT); // creates menu update timer
  56. }
  57.  
  58. public Action StopHidon(int client, int args)
  59. {
  60.     if (ShowNewHidon != null)
  61.     {
  62.         KillTimer(ShowNewHidon);
  63.         ShowNewHidon = null;
  64.     }
  65.  
  66.     ResetHidonVote();
  67.     StopAllHidonim();
  68.     HidonStarted = false;
  69. }
  70.  
  71. public void StopAllHidonim()
  72. {
  73.     for (int i = 0; i < Hidonim_Loaded; i++)
  74.     {
  75.         Format(HidonCommand, sizeof(HidonCommand), "stop_%s", Hidonim_Plugins_Names[i]);
  76.         ServerCommand(HidonCommand);
  77.     }
  78. }
  79.  
  80. public OnMapStart()
  81. {
  82.     char buffer[64];
  83.  
  84.     for (int i = 0; i < 10; i++) // loading count down sounds
  85.     {
  86.         Format(buffer, sizeof(buffer), "%s%s%s", "*/Hidonim/CountDown/", numbers[i], ".wav");
  87.         PrecacheSound(buffer);
  88.         Format(buffer, sizeof(buffer), "%s%s%s", "sound/Hidonim/CountDown/", numbers[i], ".wav");
  89.         AddFileToDownloadsTable(buffer);
  90.     }
  91. }
  92.  
  93. public Action NextHidon(int client, int args)
  94. {
  95.     int minutes = NextHidonTime/600;
  96.     int seconds = (NextHidonTime%600)/10;
  97.  
  98.     if (HidonStarted)
  99.         PrintToChat(client, "[ Hidonim ] Hidon [#%d] is in progress.", HidonNumber);
  100.     else
  101.         PrintToChat(client, "[ Hidonim ] Next Hidon in %d min and %d seconds.", minutes, seconds);
  102.  
  103.     return Plugin_Handled;
  104. }
  105.  
  106. public Action AutoStartHidonVote(Handle:timer)
  107. {
  108.     if (!HidonStarted)
  109.     {
  110.         if (NextHidonTime == 0)
  111.         {
  112.             HidonNumber = 0;
  113.             StartHidonVote();
  114.         }
  115.         else
  116.             NextHidonTime--;
  117.     }
  118. }
  119.  
  120. public Action ManualStartHidonVote(int client, int args)
  121. {
  122.     if (HidonStarted)
  123.         PrintToChat(client, "[ Hidonim ] Hidon [#%d] is in progress.", HidonNumber);
  124.     else
  125.     {
  126.         HidonNumber = 0;
  127.         StartHidonVote();
  128.     }
  129.    
  130.     return Plugin_Handled; // hides "unknown command" in console
  131. }
  132.  
  133. public void StartHidonVote()
  134. {
  135.     ResetHidonVote(); // stop menu and timer counting
  136.     LoadHidonim(); // load names and plugins names from file
  137.     HidonStarted = true;
  138.     HidonNumber++;
  139.     VotingTimer = CreateTimer(0.1, ShowVoteMenu, _, TIMER_REPEAT); // creates menu update timer
  140.     PrintToChatAll("[ Hidonim ] Hidon vote [#%d] has started.", HidonNumber);
  141. }
  142.  
  143. public Action ShowVoteMenu(Handle:timer)
  144. {
  145.     VotingTime--; // couting down
  146.  
  147.     if (isVoteEnded())
  148.     {
  149.         if (TotalVotes == 0) // if no votes were recieved
  150.         {
  151.             PrintToChatAll("[ Hidonim ] No votes recieved at all.. skipping this Hidon.");
  152.             ResetHidonVote(); // stop menu and timer counting
  153.             HidonStarted = false;
  154.             return Plugin_Handled;
  155.         }
  156.  
  157.         int HighestResult = GetHighestResult();
  158.  
  159.         ArrayList ResultsID_List = new ArrayList();
  160.  
  161.         int numOfResults = CheckNumberOfResults(ResultsID_List, HighestResult);
  162.  
  163.         if (numOfResults == 1) // Start Hidon
  164.         {
  165.             ResetHidonVote(); // stop menu and timer counting
  166.  
  167.             char ChoosenHidonName[WORD_LENGTH];
  168.             ChoosenHidonName = Backup_Display_Names[ResultsID_List.Get(0)]; // get choosen Hidon name
  169.  
  170.             int HidonID = FindHidonID(ChoosenHidonName);
  171.  
  172.             StartHidon(HidonID);
  173.         }
  174.         else if (numOfResults > 1) // if there is more than one choosen hidon
  175.         {
  176.             KillTimer(VotingTimer);
  177.             VotingTimer = null;
  178.  
  179.             if (VoteAttemp == MAX_ATTEMPS) // if there was the aleady the last vote attemp
  180.             {
  181.                 ChooseRandomHidon(ResultsID_List);
  182.                 PrintToChatAll("[ Hidonim ] Vote wasn't clear. Picking automatically a Random Hidon...");
  183.             }
  184.             else
  185.             {
  186.                 // updating Hidonim_Display_Names to the relevent choosen Hidonim
  187.                 Arrange_Hidonim_Display_Name(Hidonim_Loaded, Hidonim_Display_Names, ResultsID_List);
  188.  
  189.                 PrintToChatAll("[ Hidonim ] Vote wasn't clear. Starting a new vote in 5 seconds.");
  190.  
  191.                 VoteAttemp++; // count number of attemps
  192.  
  193.                 CreateTimer(5.0, StartVoteAgain); // start vote again
  194.             }
  195.         }
  196.     }
  197.     else
  198.     {
  199.         Menu menu = new Menu(ShowVoteMenu_Handler);
  200.  
  201.         SetMenuPagination(menu, MENU_NO_PAGINATION); // removes the next & prev buttons
  202.  
  203.         if (VoteAttemp == MAX_ATTEMPS)
  204.             menu.SetTitle("[Hidonim] Final Vote: Pick your beloved Hidon:\n>> Vote ends in: %d", VotingTime/10);
  205.         else
  206.             menu.SetTitle("[Hidonim] Pick your beloved Hidon:\n>> Vote ends in: %d", VotingTime/10);
  207.  
  208.         new String:sItem[WORD_LENGTH]; // buffer for menu items
  209.  
  210.         for (int i = 0; i < Hidonim_Loaded; i++) // for each hidon item
  211.         {
  212.             if (TotalVotes == 0)
  213.                 VotingResult[i] = 0.0; // fixes wrong values in menu when theres no votes yet
  214.             else
  215.                 VotingResult[i] = float(VotingVoices[i]*100)/float(TotalVotes); // shows vote precentage
  216.  
  217.             Format(sItem, sizeof(sItem), "%s [%.1f%%]", Hidonim_Display_Names[i], VotingResult[i]); // generates hidon item string
  218.             menu.AddItem("item", sItem); // add hidon item
  219.         }
  220.  
  221.         menu.AddItem("", "", ITEMDRAW_SPACER); // space
  222.         menu.AddItem("hidemenu", "[ Hide Vote ]"); // hide hidon button
  223.         menu.ExitButton = false; // removes exit button
  224.  
  225.         for (int i = 1; i < MaxClients; i++) // show menu to all players
  226.             if(IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i) && !HideVote[i])
  227.                 menu.Display(i, 1);
  228.     }
  229.  
  230.     return Plugin_Handled;
  231. }
  232.  
  233. public Action StartVoteAgain(Handle:timer)
  234. {
  235.     VotingTimer = CreateTimer(0.1, ShowVoteMenu, _, TIMER_REPEAT); // start vote again
  236. }
  237.  
  238. public int FindHidonID(char HidonName[WORD_LENGTH])
  239. {
  240.     for (int i =  0; i < Hidonim_Loaded; i++)
  241.         if (StrEqual(HidonName, Backup_Display_Names[i]))
  242.             return i;
  243.  
  244.     return 0;
  245. }
  246.  
  247. StartHidon(int HidonID, bool isRandomHidon = false) // public function is not allowing to use defualt value, so i got rid of the public
  248. {
  249.     ResetHidonVote(); // reset all
  250.  
  251.     Format(HidonCommand, sizeof(HidonCommand), "start_%s", Hidonim_Plugins_Names[HidonID]); // creates full command according to the plugin name
  252.  
  253.     HidonCountDownTimer = CreateTimer(5.0, StartHidonCountDown, HidonID); // count down to a Hidon start
  254.  
  255.     if (isRandomHidon)
  256.         PrintToChatAll("[ Hidonim ] Hidon %s has Randomly choosen! starting in 5 seconds.", Backup_Display_Names[HidonID]);
  257.     else
  258.         PrintToChatAll("[ Hidonim ] The choosen Hidon Is: %s. starting in 5 seconds.", Backup_Display_Names[HidonID]);
  259.  
  260.     if (HidonNumber < HIDON_SHOW_TIMES)
  261.         ShowNewHidon = CreateTimer(60.0, NewHidon, HidonID); // count down to a Hidon start
  262.     else
  263.     {
  264.         HidonStarted = false;
  265.         HidonNumber = 0;
  266.     }
  267. }
  268.  
  269. public Action NewHidon(Handle:timer)
  270. {
  271.     StartHidonVote();
  272. }
  273.  
  274. public Action StartHidonCountDown(Handle:time, any HidonID)
  275. {
  276.     HidonCountDownTimer = null;
  277.  
  278.     if (StartCountDown < 0)
  279.         ServerCommand(HidonCommand); // executes the command in order to start the requested Hidon
  280.     else
  281.     {
  282.         char buffer[64];
  283.         Format(buffer, sizeof(buffer), "%s%s%s", "*/Hidonim/CountDown/", numbers[StartCountDown], ".wav");
  284.         PrintHintTextToAll("Hidon [ %s ] Starts In: %d Seconds", Backup_Display_Names[HidonID], StartCountDown+1);
  285.         EmitSoundToAll(buffer);
  286.         StartCountDown--;
  287.         HidonCountDownTimer = CreateTimer(1.0, StartHidonCountDown, HidonID);
  288.     }
  289. }
  290.  
  291. public void ChooseRandomHidon(ArrayList ResultsID_List)
  292. {
  293.     int RandomHidonID = GetRandomInt(0, GetArraySize(ResultsID_List));
  294.  
  295.     char ChoosenHidonName[WORD_LENGTH];
  296.     ChoosenHidonName = Backup_Display_Names[RandomHidonID]; // get choosen Hidon name
  297.  
  298.     int HidonID = FindHidonID(ChoosenHidonName);
  299.  
  300.     StartHidon(HidonID, true);
  301. }
  302.  
  303. public void Arrange_Hidonim_Display_Name(int ItemsToCopy, String:source[][], ArrayList ResultsID_List)
  304. {
  305.     char buffer[MAX_HIDONIM][WORD_LENGTH]
  306.  
  307.     for (int i = 0; i < ItemsToCopy; i++) // copy all Hidonim_Display_Names to temp
  308.         strcopy(buffer[i], WORD_LENGTH, source[i]);
  309.  
  310.     for (int i = 0; i < GetArraySize(ResultsID_List); i++) // copy from temp, back to Hidonim_Display_Names - only the most choosen Hidonim
  311.     {
  312.         strcopy(Hidonim_Display_Names[i], WORD_LENGTH, buffer[ResultsID_List.Get(i)]);
  313.         Hidonim_Original_ID[i] = ResultsID_List.Get(i); // According to the Hdonim_Display_Names: saves the original id of the hidon
  314.     }
  315.  
  316.     Hidonim_Loaded = GetArraySize(ResultsID_List);
  317. }
  318.  
  319. public int CheckNumberOfResults(ArrayList ResultsID_List, int result)
  320. {
  321.     for (int i = 0; i < MAX_HIDONIM; i++)
  322.         if (VotingVoices[i] == result)
  323.             ResultsID_List.Push(i);
  324.  
  325.     return GetArraySize(ResultsID_List);
  326. }
  327.  
  328. public int GetHighestResult()
  329. {
  330.     int Highest = VotingVoices[0];
  331.  
  332.     for (int i = 1; i < MAX_HIDONIM; i++)
  333.         if (VotingVoices[i] > Highest)
  334.             Highest = VotingVoices[i];
  335.  
  336.     return Highest;
  337. }
  338.  
  339. public int ShowVoteMenu_Handler(Menu menu, MenuAction action, int client, int ChoosenItem)
  340. {
  341.     if (action == MenuAction_Select)
  342.     {
  343.         if (ChoosenItem == Hidonim_Loaded+1) // if HideVote button clicked
  344.             HideVote[client] = true;
  345.         else
  346.         {
  347.             if (!isVoted[client])
  348.             {
  349.                 TotalVotes++;
  350.                 VotingVoices[ChoosenItem]++;
  351.                 isVoted[client] = true;
  352.             }
  353.             else
  354.                 PrintToChat(client, "[ Hidonim ] You can't vote twice.");
  355.         }
  356.     }
  357. }
  358.  
  359. public void ResetHidonVote()
  360. {
  361.     VoteAttemp = 1;
  362.     VotingTime = 100;
  363.     TotalVotes = 0;
  364.     StartCountDown = 9;
  365.     NextHidonTime = 9000;
  366.  
  367.     if (VotingTimer != null)
  368.     {
  369.         KillTimer(VotingTimer);
  370.         VotingTimer = null;
  371.     }
  372.  
  373.     if (HidonCountDownTimer != null)
  374.     {
  375.         KillTimer(HidonCountDownTimer);
  376.         HidonCountDownTimer = null;
  377.     }
  378.  
  379.     for (int i = 0; i < MAX_HIDONIM; i++)
  380.     {
  381.         VotingResult[i] = 0.0;
  382.         VotingVoices[i] = 0;
  383.     }
  384.  
  385.     for (int i = 0; i < MAXPLAYERS; i++)
  386.     {
  387.         HideVote[i] = false;
  388.         isVoted[i] = false;
  389.     }
  390. }
  391.  
  392. public bool isVoteEnded()
  393. {
  394.     if (VotingTime == 0)
  395.         return true;
  396.  
  397.     return false;
  398. }
  399.  
  400. public void LoadHidonim()
  401. {
  402.     new Handle:lines = CreateArray(128);
  403.     char word[32];
  404.     char line[128];
  405.    
  406.     ReadFromConfigFile(lines);
  407.  
  408.     Hidonim_Loaded = GetArraySize(lines);
  409.  
  410.     if (Hidonim_Loaded > 0) // if there are any Hidonim
  411.     {
  412.         for (int i = 0; i < Hidonim_Loaded; i++) // for each Hidon configuration
  413.         {
  414.             GetArrayString(lines, i, line, sizeof(line)); // get Hidon configuration line
  415.  
  416.             int len; // uses to move BreakString crusor - to go to the next word
  417.  
  418.             len = BreakString(line[len], word, sizeof(line)); // get first word in line
  419.             Hidonim_Display_Names[i] = word;
  420.             Backup_Display_Names[i] = word; // used when there is more than 1 vote attemp
  421.  
  422.             len += BreakString(line[len], word, sizeof(line)); // get second word in line
  423.             Hidonim_Plugins_Names[i] = word;
  424.         }
  425.     }
  426.     else
  427.         SetFailState("[Hidonim] No Hidonim have found.");
  428.  
  429.     ValidPluginsNames(); // check if plugins are really loaded
  430.     lines = null;
  431. }
  432.  
  433. public void ValidPluginsNames()
  434. {
  435.     new Handle:PFind;
  436.     char pluginName[32];
  437.  
  438.     for (int i = 0; i < Hidonim_Loaded; i++)
  439.     {
  440.         Format(pluginName, sizeof(pluginName), "%s.smx", Hidonim_Plugins_Names[i]);
  441.  
  442.         PFind = FindPluginByFile(pluginName);
  443.  
  444.         if (PFind == null || GetPluginStatus(PFind) != Plugin_Running)
  445.             SetFailState("[Hidonim] Unable to find plugin named: %s", pluginName);
  446.     }
  447. }
  448.  
  449. public void ReadFromConfigFile(Handle lines)
  450. {
  451.     char path[64] = "addons\\sourcemod\\configs\\Hidonim\\config.ini";
  452.  
  453.     if (!FileExists(path))
  454.         SetFailState("[Hidonim] ERROR: Unable to find Hidonim config file.");
  455.  
  456.     new Handle:file = OpenFile(path, "r");
  457.     char buffer[256];
  458.  
  459.     while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
  460.         if (StrContains(buffer, "//") == -1)
  461.             PushArrayString(lines, buffer);
  462.  
  463.     CloseHandle(file);
  464. }
Advertisement
Add Comment
Please, Sign In to add comment