using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Menu; namespace Porning; public class Vote { private List counters = new List(); private int best = -1; private Action? callback = null; private int time = 0; private string title = ""; private BasePlugin plugin = null!; private CenterHtmlMenu menu = null!; private List options = null!; public Vote(BasePlugin plugin, string message, List options) { this.plugin = plugin; this.options = options; title = $"Porning
{message}"; } public void BeginVote() { menu = new CenterHtmlMenu($"{title} (10 sec)", plugin){ ExitButton = false }; for (int i = 0; i < options.Count; i++) { counters.Add(0); int current = i; menu.AddMenuOption($"-> {options[i]} [0]", (player, action) => {counters[current]++;}); } foreach (CCSPlayerController player in Utilities.GetPlayers()) { MenuManager.OpenCenterHtmlMenu(plugin, player, menu); } plugin.AddTimer(1.0f, Tick); } public void Tick() { new Logger().Info($"TICK: {time}"); if (time >= 10) { EndVote(); return; } time++; menu.Title = $"{title} ({10-time} sec)"; for (int i = 0; i < options.Count; i++) { menu.MenuOptions[i].Text = $"-> {options[i]} [{counters[i]}]"; } foreach(CCSPlayerController player in Utilities.GetPlayers()) { MenuManager.OpenCenterHtmlMenu(plugin, player, menu); } plugin.AddTimer(1.0f, Tick); } public void EndVote() { foreach (CCSPlayerController player in Utilities.GetPlayers()) { MenuManager.CloseActiveMenu(player); } int maxVotes = 0; for (int i = 0; i < counters.Count; i++) { new Logger().Info($"Counter: {counters[i]}"); if (counters[i] > maxVotes) { maxVotes = counters[i]; best = i; } } callback?.Invoke(best); } public void OnEnd(Action function) { callback = function; } }