Skip to content
Permalink
Browse files

Add /autorankcheck command which outputs autorank status for a user.

  • Loading branch information...
UnknownShadow200 committed Jul 26, 2017
1 parent a4fcf9b commit fef4e253b21f25760a2ae4a632166b362417c417
@@ -16,9 +16,7 @@ static class AutoRankManager {
public const string TagName = "fCraftAutoRankConfig";

// Whether any criteria are defined.
static bool HasCriteria {
get { return Criteria.Count > 0; }
}
static bool HasCriteria { get { return Criteria.Count > 0; } }


// Adds a new criterion to the list. Throws an ArgumentException on duplicates.
@@ -36,16 +34,28 @@ static class AutoRankManager {
public static Rank Check( [NotNull] PlayerInfo info ) {
if( info == null ) throw new ArgumentNullException( "info" );
for( int i = 0; i < Criteria.Count; i++ ) {
if( Criteria[i].FromRank == info.Rank &&
!info.IsBanned &&
Criteria[i].Condition.Eval( info ) ) {

if( Criteria[i].FromRank == info.Rank && !info.IsBanned && Criteria[i].Condition.Eval( info ) ) {
return Criteria[i].ToRank;
}
}
return null;
}


public static void OutputDetails( [NotNull] Player player, [NotNull] PlayerInfo info ) {
if( player == null ) throw new ArgumentNullException( "player" );
if( info == null ) throw new ArgumentNullException( "info" );
for( int i = 0; i < Criteria.Count; i++ ) {
if( Criteria[i].FromRank == info.Rank && !info.IsBanned ) {
player.Message("Criteria for {0} &Sto be ranked to {1}:",
info.ClassyName, Criteria[i].ToRank.ClassyName);
Criteria[i].Condition.OutputDetails( "", player, info );
return;
}
}
player.Message("No autorank criteria matches curent rank of {0}.", info.ClassyName);
}


internal static void TaskCallback( SchedulerTask schedulerTask ) {
if( !ConfigKey.AutoRankEnabled.Enabled() ) return;
@@ -10,6 +10,12 @@ namespace fCraft.AutoRank {
/// <summary> Base class for all AutoRank conditions. </summary>
abstract class Condition {
public abstract bool Eval( PlayerInfo info );

protected string DetailsColor( PlayerInfo info ) {
return Eval( info ) ? Color.Green : Color.Red;
}

public abstract void OutputDetails( string indent, Player player, PlayerInfo info );

public static Condition Parse( XElement el ) {
switch( el.Name.ToString() ) {
@@ -33,6 +39,9 @@ abstract class Condition {
}

public abstract XElement Serialize();

protected static string[] comparisonOps = { "=", "=/=", ">", ">=", "<", "<=" };
protected const string IndentLevel = " ";
}


@@ -127,6 +136,16 @@ public ConditionIntRange( [NotNull] XElement el )
throw new ArgumentOutOfRangeException();
}
}

public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
string color = DetailsColor( info );
if( (Comparison == ComparisonOp.Gte || Comparison == ComparisonOp.Gt) && !Eval( info ) ) {
color = Color.Silver;
}

player.Message( indent + color + "{0} is {1} {2}",
Field, comparisonOps[(int)Comparison], Value );
}

public override XElement Serialize() {
XElement el = new XElement( "ConditionIntRange" );
@@ -162,6 +181,11 @@ sealed class ConditionRankChangeType : Condition {
if( info == null ) throw new ArgumentNullException( "info" );
return (info.RankChangeType == Type);
}

public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
player.Message( indent + DetailsColor( info ) + "{0} {1}",
"Rank change type of", Type );
}

public override XElement Serialize() {
XElement el = new XElement( "ConditionRankChangeType" );
@@ -211,6 +235,11 @@ sealed class ConditionPreviousRank : Condition {
throw new ArgumentOutOfRangeException();
}
}

public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
player.Message( indent + DetailsColor( info ) + "Rank was previously {0} {1}",
comparisonOps[(int)Comparison], Rank.ClassyName );
}

public override XElement Serialize() {
XElement el = new XElement( "ConditionPreviousRank" );
@@ -247,6 +276,12 @@ protected ConditionSet( [NotNull] XContainer el )
public override bool Eval( PlayerInfo info ) {
throw new NotImplementedException();
}

public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
foreach( Condition cond in Conditions ) {
cond.OutputDetails( indent + IndentLevel, player, info );
}
}

public void Add( [NotNull] Condition condition ) {
if( condition == null ) throw new ArgumentNullException( "condition" );
@@ -268,7 +303,11 @@ sealed class ConditionAND : ConditionSet {
public override bool Eval( PlayerInfo info ) {
return Conditions == null || Conditions.All( t => t.Eval( info ) );
}


public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
player.Message( indent + "All the following conditions are met:" );
base.OutputDetails( indent, player, info );
}

public override XElement Serialize() {
XElement el = new XElement( "AND" );
@@ -280,7 +319,7 @@ sealed class ConditionAND : ConditionSet {
}


/// <summary> Logical AND - true if NOT ALL of the conditions are true. </summary>
/// <summary> Logical NAND - true if NOT ALL of the conditions are true. </summary>
sealed class ConditionNAND : ConditionSet {
public ConditionNAND() { }
public ConditionNAND( IEnumerable<Condition> conditions ) : base( conditions ) { }
@@ -290,7 +329,11 @@ sealed class ConditionNAND : ConditionSet {
if( info == null ) throw new ArgumentNullException( "info" );
return Conditions == null || Conditions.Any( t => !t.Eval( info ) );
}


public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
player.Message( indent + "Any the following conditions are NOT met" );
base.OutputDetails( indent, player, info );
}

public override XElement Serialize() {
XElement el = new XElement( "NAND" );
@@ -302,7 +345,7 @@ sealed class ConditionNAND : ConditionSet {
}


/// <summary> Logical AND - true if ANY of the conditions are true. </summary>
/// <summary> Logical OR - true if ANY of the conditions are true. </summary>
sealed class ConditionOR : ConditionSet {
public ConditionOR() { }
public ConditionOR( IEnumerable<Condition> conditions ) : base( conditions ) { }
@@ -312,7 +355,11 @@ sealed class ConditionOR : ConditionSet {
if( info == null ) throw new ArgumentNullException( "info" );
return Conditions == null || Conditions.Any( t => t.Eval( info ) );
}


public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
player.Message( indent + "Any the following conditions are met:" );
base.OutputDetails( indent, player, info );
}

public override XElement Serialize() {
XElement el = new XElement( "OR" );
@@ -324,7 +371,7 @@ sealed class ConditionOR : ConditionSet {
}


/// <summary> Logical AND - true if NONE of the conditions are true. </summary>
/// <summary> Logical NOR - true if NONE of the conditions are true. </summary>
sealed class ConditionNOR : ConditionSet {
public ConditionNOR() { }
public ConditionNOR( IEnumerable<Condition> conditions ) : base( conditions ) { }
@@ -334,7 +381,11 @@ sealed class ConditionNOR : ConditionSet {
if( info == null ) throw new ArgumentNullException( "info" );
return Conditions == null || Conditions.All( t => !t.Eval( info ) );
}


public override void OutputDetails( string indent, Player player, PlayerInfo info ) {
player.Message( indent + "All the following conditions are NOT met:" );
base.OutputDetails( indent, player, info );
}

public override XElement Serialize() {
XElement el = new XElement( "NOR" );
@@ -25,7 +25,7 @@ static class MaintenanceCommands {
CommandManager.RegisterCommand( CdInfoSwap );
CommandManager.RegisterCommand(CdSave);
CommandManager.RegisterCommand(CdCheckUpdate);

CommandManager.RegisterCommand( CdAutoRankCheck );
}
#region DumpStats

@@ -1374,5 +1374,30 @@ static void SaveHandler(Player player, CommandReader cmd)
player.Message("Download updated Zip here: &9http://123DMWM.tk/ProCraft/Builds/Latest.zip");
}
#endregion
#region AutoRankCheck

static readonly CommandDescriptor CdAutoRankCheck = new CommandDescriptor {
Name = "AutoRankCheck",
Aliases = new string[] { "arc" },
Category = CommandCategory.Maintenance | CommandCategory.Moderation,
IsConsoleSafe = true,
Permissions = new[] { Permission.EditPlayerDB },
Help = "Allows checking whether a player has met all autorank condition(s).",
Usage = "/AutoRankCheck [Player]",
Handler = AutoRankCheckHandler
};

static void AutoRankCheckHandler( Player player, CommandReader cmd ) {
string name = cmd.Next();
PlayerInfo info = PlayerDB.FindPlayerInfoOrPrintMatches( player, name, SearchOptions.IncludeSelf );
if( info == null ) return;

if( !ConfigKey.AutoRankEnabled.Enabled() ) {
player.Message( "AutoRank is not enabled in config." ); return;
}
AutoRankManager.OutputDetails( player, info );
}

#endregion
}
}
@@ -188,9 +188,7 @@ public sealed partial class PlayerInfo : IClassy {
public BanStatus BanStatus;

/// <summary> Returns whether player is name-banned or not. </summary>
public bool IsBanned {
get { return BanStatus == BanStatus.Banned; }
}
public bool IsBanned { get { return BanStatus == BanStatus.Banned; } }

/// <summary> Date of most recent ban, UTC. May be DateTime.MinValue if player was never banned. </summary>
public DateTime BanDate;

0 comments on commit fef4e25

Please sign in to comment.
You can’t perform that action at this time.