Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sdktools>
- public Plugin myinfo =
- {
- name = "HidonimAPI",
- author = "JuSTaR",
- description = "Hidonim API",
- version = "1.0",
- url = "https://www.fxp.co.il/"
- }
- // Constants
- const int PROPERTIES_PER_LINE = 2; // 1 = Hidon display name, 2 = Hidon plugin name
- const int MAX_HIDONIM = 7;
- const int WORD_LENGTH = 32;
- const int MAX_ATTEMPS = 3;
- const int HIDON_SHOW_TIMES = 3;
- // Hidonim reference variables
- char Hidonim_Display_Names[MAX_HIDONIM][32];
- char Hidonim_Plugins_Names[MAX_HIDONIM][32];
- char Backup_Display_Names[MAX_HIDONIM][32];
- int Hidonim_Original_ID[MAX_HIDONIM];
- int Hidonim_Loaded;
- // Vote Menu variables
- Handle VotingTimer;
- Handle ShowNewHidon;
- bool HideVote[MAXPLAYERS];
- bool isVoted[MAXPLAYERS];
- float VotingResult[MAX_HIDONIM];
- int VotingVoices[MAX_HIDONIM];
- int VotingTime = 100;
- int TotalVotes;
- int VoteAttemp = 1;
- int HidonNumber = 0;
- // Next-Hidon CountDown Timer
- bool HidonStarted = false;
- int NextHidonTime = 9000; // 900 seconds = 15 mins
- // Hidon Start CountDown Timer
- Handle HidonCountDownTimer;
- int StartCountDown = 9; // CountDown goes from 9 -> 0 (10 times)
- char HidonCommand[64];
- char numbers[10][8] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
- public OnPluginStart()
- {
- RegAdminCmd("start_hidon", ManualStartHidonVote, ADMFLAG_RCON);
- RegAdminCmd("stop_hidon", StopHidon, ADMFLAG_RCON);
- RegConsoleCmd("hidon", NextHidon);
- RegConsoleCmd("next_hidon", NextHidon);
- CreateTimer(0.1, AutoStartHidonVote, _, TIMER_REPEAT); // creates menu update timer
- }
- public Action StopHidon(int client, int args)
- {
- if (ShowNewHidon != null)
- {
- KillTimer(ShowNewHidon);
- ShowNewHidon = null;
- }
- ResetHidonVote();
- StopAllHidonim();
- HidonStarted = false;
- }
- public void StopAllHidonim()
- {
- for (int i = 0; i < Hidonim_Loaded; i++)
- {
- Format(HidonCommand, sizeof(HidonCommand), "stop_%s", Hidonim_Plugins_Names[i]);
- ServerCommand(HidonCommand);
- }
- }
- public OnMapStart()
- {
- char buffer[64];
- for (int i = 0; i < 10; i++) // loading count down sounds
- {
- Format(buffer, sizeof(buffer), "%s%s%s", "*/Hidonim/CountDown/", numbers[i], ".wav");
- PrecacheSound(buffer);
- Format(buffer, sizeof(buffer), "%s%s%s", "sound/Hidonim/CountDown/", numbers[i], ".wav");
- AddFileToDownloadsTable(buffer);
- }
- }
- public Action NextHidon(int client, int args)
- {
- int minutes = NextHidonTime/600;
- int seconds = (NextHidonTime%600)/10;
- if (HidonStarted)
- PrintToChat(client, "[ Hidonim ] Hidon [#%d] is in progress.", HidonNumber);
- else
- PrintToChat(client, "[ Hidonim ] Next Hidon in %d min and %d seconds.", minutes, seconds);
- return Plugin_Handled;
- }
- public Action AutoStartHidonVote(Handle:timer)
- {
- if (!HidonStarted)
- {
- if (NextHidonTime == 0)
- {
- HidonNumber = 0;
- StartHidonVote();
- }
- else
- NextHidonTime--;
- }
- }
- public Action ManualStartHidonVote(int client, int args)
- {
- if (HidonStarted)
- PrintToChat(client, "[ Hidonim ] Hidon [#%d] is in progress.", HidonNumber);
- else
- {
- HidonNumber = 0;
- StartHidonVote();
- }
- return Plugin_Handled; // hides "unknown command" in console
- }
- public void StartHidonVote()
- {
- ResetHidonVote(); // stop menu and timer counting
- LoadHidonim(); // load names and plugins names from file
- HidonStarted = true;
- HidonNumber++;
- VotingTimer = CreateTimer(0.1, ShowVoteMenu, _, TIMER_REPEAT); // creates menu update timer
- PrintToChatAll("[ Hidonim ] Hidon vote [#%d] has started.", HidonNumber);
- }
- public Action ShowVoteMenu(Handle:timer)
- {
- VotingTime--; // couting down
- if (isVoteEnded())
- {
- if (TotalVotes == 0) // if no votes were recieved
- {
- PrintToChatAll("[ Hidonim ] No votes recieved at all.. skipping this Hidon.");
- ResetHidonVote(); // stop menu and timer counting
- HidonStarted = false;
- return Plugin_Handled;
- }
- int HighestResult = GetHighestResult();
- ArrayList ResultsID_List = new ArrayList();
- int numOfResults = CheckNumberOfResults(ResultsID_List, HighestResult);
- if (numOfResults == 1) // Start Hidon
- {
- ResetHidonVote(); // stop menu and timer counting
- char ChoosenHidonName[WORD_LENGTH];
- ChoosenHidonName = Backup_Display_Names[ResultsID_List.Get(0)]; // get choosen Hidon name
- int HidonID = FindHidonID(ChoosenHidonName);
- StartHidon(HidonID);
- }
- else if (numOfResults > 1) // if there is more than one choosen hidon
- {
- KillTimer(VotingTimer);
- VotingTimer = null;
- if (VoteAttemp == MAX_ATTEMPS) // if there was the aleady the last vote attemp
- {
- ChooseRandomHidon(ResultsID_List);
- PrintToChatAll("[ Hidonim ] Vote wasn't clear. Picking automatically a Random Hidon...");
- }
- else
- {
- // updating Hidonim_Display_Names to the relevent choosen Hidonim
- Arrange_Hidonim_Display_Name(Hidonim_Loaded, Hidonim_Display_Names, ResultsID_List);
- PrintToChatAll("[ Hidonim ] Vote wasn't clear. Starting a new vote in 5 seconds.");
- VoteAttemp++; // count number of attemps
- CreateTimer(5.0, StartVoteAgain); // start vote again
- }
- }
- }
- else
- {
- Menu menu = new Menu(ShowVoteMenu_Handler);
- SetMenuPagination(menu, MENU_NO_PAGINATION); // removes the next & prev buttons
- if (VoteAttemp == MAX_ATTEMPS)
- menu.SetTitle("[Hidonim] Final Vote: Pick your beloved Hidon:\n>> Vote ends in: %d", VotingTime/10);
- else
- menu.SetTitle("[Hidonim] Pick your beloved Hidon:\n>> Vote ends in: %d", VotingTime/10);
- new String:sItem[WORD_LENGTH]; // buffer for menu items
- for (int i = 0; i < Hidonim_Loaded; i++) // for each hidon item
- {
- if (TotalVotes == 0)
- VotingResult[i] = 0.0; // fixes wrong values in menu when theres no votes yet
- else
- VotingResult[i] = float(VotingVoices[i]*100)/float(TotalVotes); // shows vote precentage
- Format(sItem, sizeof(sItem), "%s [%.1f%%]", Hidonim_Display_Names[i], VotingResult[i]); // generates hidon item string
- menu.AddItem("item", sItem); // add hidon item
- }
- menu.AddItem("", "", ITEMDRAW_SPACER); // space
- menu.AddItem("hidemenu", "[ Hide Vote ]"); // hide hidon button
- menu.ExitButton = false; // removes exit button
- for (int i = 1; i < MaxClients; i++) // show menu to all players
- if(IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i) && !HideVote[i])
- menu.Display(i, 1);
- }
- return Plugin_Handled;
- }
- public Action StartVoteAgain(Handle:timer)
- {
- VotingTimer = CreateTimer(0.1, ShowVoteMenu, _, TIMER_REPEAT); // start vote again
- }
- public int FindHidonID(char HidonName[WORD_LENGTH])
- {
- for (int i = 0; i < Hidonim_Loaded; i++)
- if (StrEqual(HidonName, Backup_Display_Names[i]))
- return i;
- return 0;
- }
- StartHidon(int HidonID, bool isRandomHidon = false) // public function is not allowing to use defualt value, so i got rid of the public
- {
- ResetHidonVote(); // reset all
- Format(HidonCommand, sizeof(HidonCommand), "start_%s", Hidonim_Plugins_Names[HidonID]); // creates full command according to the plugin name
- HidonCountDownTimer = CreateTimer(5.0, StartHidonCountDown, HidonID); // count down to a Hidon start
- if (isRandomHidon)
- PrintToChatAll("[ Hidonim ] Hidon %s has Randomly choosen! starting in 5 seconds.", Backup_Display_Names[HidonID]);
- else
- PrintToChatAll("[ Hidonim ] The choosen Hidon Is: %s. starting in 5 seconds.", Backup_Display_Names[HidonID]);
- if (HidonNumber < HIDON_SHOW_TIMES)
- ShowNewHidon = CreateTimer(60.0, NewHidon, HidonID); // count down to a Hidon start
- else
- {
- HidonStarted = false;
- HidonNumber = 0;
- }
- }
- public Action NewHidon(Handle:timer)
- {
- StartHidonVote();
- }
- public Action StartHidonCountDown(Handle:time, any HidonID)
- {
- HidonCountDownTimer = null;
- if (StartCountDown < 0)
- ServerCommand(HidonCommand); // executes the command in order to start the requested Hidon
- else
- {
- char buffer[64];
- Format(buffer, sizeof(buffer), "%s%s%s", "*/Hidonim/CountDown/", numbers[StartCountDown], ".wav");
- PrintHintTextToAll("Hidon [ %s ] Starts In: %d Seconds", Backup_Display_Names[HidonID], StartCountDown+1);
- EmitSoundToAll(buffer);
- StartCountDown--;
- HidonCountDownTimer = CreateTimer(1.0, StartHidonCountDown, HidonID);
- }
- }
- public void ChooseRandomHidon(ArrayList ResultsID_List)
- {
- int RandomHidonID = GetRandomInt(0, GetArraySize(ResultsID_List));
- char ChoosenHidonName[WORD_LENGTH];
- ChoosenHidonName = Backup_Display_Names[RandomHidonID]; // get choosen Hidon name
- int HidonID = FindHidonID(ChoosenHidonName);
- StartHidon(HidonID, true);
- }
- public void Arrange_Hidonim_Display_Name(int ItemsToCopy, String:source[][], ArrayList ResultsID_List)
- {
- char buffer[MAX_HIDONIM][WORD_LENGTH]
- for (int i = 0; i < ItemsToCopy; i++) // copy all Hidonim_Display_Names to temp
- strcopy(buffer[i], WORD_LENGTH, source[i]);
- for (int i = 0; i < GetArraySize(ResultsID_List); i++) // copy from temp, back to Hidonim_Display_Names - only the most choosen Hidonim
- {
- strcopy(Hidonim_Display_Names[i], WORD_LENGTH, buffer[ResultsID_List.Get(i)]);
- Hidonim_Original_ID[i] = ResultsID_List.Get(i); // According to the Hdonim_Display_Names: saves the original id of the hidon
- }
- Hidonim_Loaded = GetArraySize(ResultsID_List);
- }
- public int CheckNumberOfResults(ArrayList ResultsID_List, int result)
- {
- for (int i = 0; i < MAX_HIDONIM; i++)
- if (VotingVoices[i] == result)
- ResultsID_List.Push(i);
- return GetArraySize(ResultsID_List);
- }
- public int GetHighestResult()
- {
- int Highest = VotingVoices[0];
- for (int i = 1; i < MAX_HIDONIM; i++)
- if (VotingVoices[i] > Highest)
- Highest = VotingVoices[i];
- return Highest;
- }
- public int ShowVoteMenu_Handler(Menu menu, MenuAction action, int client, int ChoosenItem)
- {
- if (action == MenuAction_Select)
- {
- if (ChoosenItem == Hidonim_Loaded+1) // if HideVote button clicked
- HideVote[client] = true;
- else
- {
- if (!isVoted[client])
- {
- TotalVotes++;
- VotingVoices[ChoosenItem]++;
- isVoted[client] = true;
- }
- else
- PrintToChat(client, "[ Hidonim ] You can't vote twice.");
- }
- }
- }
- public void ResetHidonVote()
- {
- VoteAttemp = 1;
- VotingTime = 100;
- TotalVotes = 0;
- StartCountDown = 9;
- NextHidonTime = 9000;
- if (VotingTimer != null)
- {
- KillTimer(VotingTimer);
- VotingTimer = null;
- }
- if (HidonCountDownTimer != null)
- {
- KillTimer(HidonCountDownTimer);
- HidonCountDownTimer = null;
- }
- for (int i = 0; i < MAX_HIDONIM; i++)
- {
- VotingResult[i] = 0.0;
- VotingVoices[i] = 0;
- }
- for (int i = 0; i < MAXPLAYERS; i++)
- {
- HideVote[i] = false;
- isVoted[i] = false;
- }
- }
- public bool isVoteEnded()
- {
- if (VotingTime == 0)
- return true;
- return false;
- }
- public void LoadHidonim()
- {
- new Handle:lines = CreateArray(128);
- char word[32];
- char line[128];
- ReadFromConfigFile(lines);
- Hidonim_Loaded = GetArraySize(lines);
- if (Hidonim_Loaded > 0) // if there are any Hidonim
- {
- for (int i = 0; i < Hidonim_Loaded; i++) // for each Hidon configuration
- {
- GetArrayString(lines, i, line, sizeof(line)); // get Hidon configuration line
- int len; // uses to move BreakString crusor - to go to the next word
- len = BreakString(line[len], word, sizeof(line)); // get first word in line
- Hidonim_Display_Names[i] = word;
- Backup_Display_Names[i] = word; // used when there is more than 1 vote attemp
- len += BreakString(line[len], word, sizeof(line)); // get second word in line
- Hidonim_Plugins_Names[i] = word;
- }
- }
- else
- SetFailState("[Hidonim] No Hidonim have found.");
- ValidPluginsNames(); // check if plugins are really loaded
- lines = null;
- }
- public void ValidPluginsNames()
- {
- new Handle:PFind;
- char pluginName[32];
- for (int i = 0; i < Hidonim_Loaded; i++)
- {
- Format(pluginName, sizeof(pluginName), "%s.smx", Hidonim_Plugins_Names[i]);
- PFind = FindPluginByFile(pluginName);
- if (PFind == null || GetPluginStatus(PFind) != Plugin_Running)
- SetFailState("[Hidonim] Unable to find plugin named: %s", pluginName);
- }
- }
- public void ReadFromConfigFile(Handle lines)
- {
- char path[64] = "addons\\sourcemod\\configs\\Hidonim\\config.ini";
- if (!FileExists(path))
- SetFailState("[Hidonim] ERROR: Unable to find Hidonim config file.");
- new Handle:file = OpenFile(path, "r");
- char buffer[256];
- while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer)))
- if (StrContains(buffer, "//") == -1)
- PushArrayString(lines, buffer);
- CloseHandle(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment