2017-06-05 22:49:26 -04:00
using System ;
using System.Threading.Tasks ;
2017-06-12 20:24:12 -04:00
using System.Collections.Generic ;
2017-06-05 22:49:26 -04:00
using SharedLibrary ;
using SharedLibrary.Interfaces ;
using SharedLibrary.Network ;
2017-06-12 17:47:31 -04:00
using SharedLibrary.Helpers ;
2017-06-05 22:49:26 -04:00
namespace Plugin
{
2017-06-06 23:45:21 -04:00
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 )
{
2017-06-12 20:24:12 -04:00
FastRestartPlugin . ConfigManager . UpdateProperty ( E . Owner , new KeyValuePair < string , object > ( "Enabled" , true ) ) ;
await E . Origin . Tell ( "Fast restarting is now enabled for this server" ) ;
2017-06-06 23:45:21 -04:00
}
}
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 )
{
2017-06-12 20:24:12 -04:00
FastRestartPlugin . ConfigManager . UpdateProperty ( E . Owner , new KeyValuePair < string , object > ( "Enabled" , false ) ) ;
await E . Origin . Tell ( "Fast restarting is now disabled for this server" ) ;
2017-06-06 23:45:21 -04:00
}
}
2017-06-05 22:49:26 -04:00
public class FastRestartPlugin : IPlugin
{
bool MatchEnded ;
DateTime MatchEndTime ;
2017-10-11 17:49:02 -04:00
Dictionary < int , int > FastRestarts ;
2017-06-05 22:49:26 -04:00
2017-06-12 20:24:12 -04:00
public static ConfigurationManager ConfigManager { get ; private set ; }
2017-11-02 12:49:45 -04:00
public string Name = > "Fast Restarter" ;
2017-06-05 22:49:26 -04:00
2017-11-02 12:49:45 -04:00
public float Version = > 1.0f ;
2017-06-05 22:49:26 -04:00
2017-11-02 12:49:45 -04:00
public string Author = > "RaidMax" ;
2017-06-05 22:49:26 -04:00
public async Task OnEventAsync ( Event E , Server S )
{
if ( E . Type = = Event . GType . Start )
{
2017-06-13 18:33:47 -04:00
ConfigManager . AddConfiguration ( S ) ;
if ( ConfigManager . GetConfiguration ( S ) . Keys . Count = = 0 )
ConfigManager . AddProperty ( S , new KeyValuePair < string , object > ( "Enabled" , false ) ) ;
2017-10-11 17:49:02 -04:00
FastRestarts . Add ( S . GetHashCode ( ) , 0 ) ;
2017-06-05 22:49:26 -04:00
try
{
await S . GetDvarAsync < int > ( "scr_intermission_time" ) ;
}
catch ( SharedLibrary . Exceptions . DvarException )
{
2017-11-02 12:49:45 -04:00
await S . SetDvarAsync ( "scr_intermission_time" , 20 ) ;
2017-06-06 23:45:21 -04:00
}
2017-06-05 22:49:26 -04:00
}
}
2017-10-16 23:47:41 -04:00
public async Task OnLoadAsync ( IManager manager )
2017-06-05 22:49:26 -04:00
{
2017-10-11 17:49:02 -04:00
ConfigManager = new ConfigurationManager ( typeof ( FastRestartPlugin ) ) ;
FastRestarts = new Dictionary < int , int > ( ) ;
2017-06-05 22:49:26 -04:00
}
public async Task OnTickAsync ( Server S )
{
2017-06-12 20:24:12 -04:00
if ( ( bool ) ConfigManager . GetConfiguration ( S ) [ "Enabled" ] = = false )
2017-06-06 23:45:21 -04:00
return ;
2017-06-05 22:49:26 -04:00
MatchEnded = ( await S . GetDvarAsync < int > ( "scr_gameended" ) ) . Value = = 1 ;
if ( MatchEnded & & MatchEndTime = = DateTime . MinValue )
MatchEndTime = DateTime . Now ;
2017-10-11 17:49:02 -04:00
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 ) ;
}
2017-06-06 23:45:21 -04:00
// I'm pretty sure the timelength from game ended to scoreboard popup is 2000ms
2017-10-11 17:49:02 -04:00
if ( MatchEnded & & ( DateTime . Now - MatchEndTime ) . TotalSeconds > = intermissionTime - 2 )
2017-06-05 22:49:26 -04:00
{
2017-10-11 17:49:02 -04:00
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 ;
}
2017-06-05 22:49:26 -04:00
MatchEndTime = DateTime . MinValue ;
}
}
2017-10-03 19:17:35 -04:00
public async Task OnUnloadAsync ( )
2017-06-05 22:49:26 -04:00
{
2017-10-03 19:17:35 -04:00
2017-06-05 22:49:26 -04:00
}
}
}