IW4M-Admin/Plugins/FastRestart/Plugin.cs
RaidMax 9699f7c3f1 changed player graph update interval to every 5 minutes
hopefully fixed skipping the logging of Kayak library issue.
I'm an idiot, 59 in playerhistory, not 60
added IW4 callback gsc for custom scriptkills.
Fixed duplicate death events
Trusted group can be enabled/disabled with !enable/disabletrusted
2017-11-02 11:49:45 -05:00

129 lines
4.1 KiB
C#

using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using SharedLibrary;
using SharedLibrary.Interfaces;
using SharedLibrary.Network;
using SharedLibrary.Helpers;
namespace Plugin
{
public class FastRestartConfig : Serialize<FastRestartConfig>
{
public bool Enabled;
}
public class CEnableFastRestart : Command
{
public CEnableFastRestart() : base("frenable", "enable fast restarting at the end of a map. syntax: !fre", "fre", Player.Permission.SeniorAdmin, 0, false) { }
public override async Task ExecuteAsync(Event E)
{
FastRestartPlugin.ConfigManager.UpdateProperty(E.Owner, new KeyValuePair<string, object>("Enabled", true));
await E.Origin.Tell("Fast restarting is now enabled for this server");
}
}
public class CDisableFastRestart : Command
{
public CDisableFastRestart() : base("fredisable", "disable fast restarting at the end of a map. syntax: !frd", "frd", Player.Permission.SeniorAdmin, 0, false) { }
public override async Task ExecuteAsync(Event E)
{
FastRestartPlugin.ConfigManager.UpdateProperty(E.Owner, new KeyValuePair<string, object>("Enabled", false));
await E.Origin.Tell("Fast restarting is now disabled for this server");
}
}
public class FastRestartPlugin : IPlugin
{
bool MatchEnded;
DateTime MatchEndTime;
Dictionary<int, int> FastRestarts;
public static ConfigurationManager ConfigManager { get; private set; }
public string Name => "Fast Restarter";
public float Version => 1.0f;
public string Author => "RaidMax";
public async Task OnEventAsync(Event E, Server S)
{
if (E.Type == Event.GType.Start)
{
ConfigManager.AddConfiguration(S);
if (ConfigManager.GetConfiguration(S).Keys.Count == 0)
ConfigManager.AddProperty(S, new KeyValuePair<string, object>("Enabled", false));
FastRestarts.Add(S.GetHashCode(), 0);
try
{
await S.GetDvarAsync<int>("scr_intermission_time");
}
catch (SharedLibrary.Exceptions.DvarException)
{
await S.SetDvarAsync("scr_intermission_time", 20);
}
}
}
public async Task OnLoadAsync(IManager manager)
{
ConfigManager = new ConfigurationManager(typeof(FastRestartPlugin));
FastRestarts = new Dictionary<int, int>();
}
public async Task OnTickAsync(Server S)
{
if ((bool)ConfigManager.GetConfiguration(S)["Enabled"] == false)
return;
MatchEnded = (await S.GetDvarAsync<int>("scr_gameended")).Value == 1;
if (MatchEnded && MatchEndTime == DateTime.MinValue)
MatchEndTime = DateTime.Now;
int intermissionTime = 20;
try
{
var intermissionTimeDvar = await S.GetDvarAsync<int>("scr_intermission_time");
intermissionTime = intermissionTimeDvar.Value;
}
catch(SharedLibrary.Exceptions.DvarException)
{
await S.SetDvarAsync("scr_intermission_time", 20);
}
// I'm pretty sure the timelength from game ended to scoreboard popup is 2000ms
if (MatchEnded && (DateTime.Now - MatchEndTime).TotalSeconds >= intermissionTime - 2)
{
if (FastRestarts[S.GetHashCode()] >= 8)
{
await S.ExecuteCommandAsync("map_restart");
FastRestarts[S.GetHashCode()] = 0;
}
else
{
await S.ExecuteCommandAsync("fast_restart");
FastRestarts[S.GetHashCode()] = FastRestarts[S.GetHashCode()] + 1;
}
MatchEndTime = DateTime.MinValue;
}
}
public async Task OnUnloadAsync()
{
}
}
}