Files
plugin/utils/vote.cs
2026-07-07 16:36:30 +02:00

95 lines
2.3 KiB
C#

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Menu;
namespace Porning;
public class Vote
{
private List<int> counters = new List<int>();
private int best = -1;
private Action<int>? callback = null;
private int time = 0;
private string title = "";
private BasePlugin plugin = null!;
private CenterHtmlMenu menu = null!;
private List<string> options = null!;
public Vote(BasePlugin plugin, string message, List<string> options)
{
this.plugin = plugin;
this.options = options;
title = $"<font color=\"red\">Porning</font><br>{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<int> function)
{
callback = function;
}
}