using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; namespace DominionBase.Players.AI.Bots { public enum BotRuleConditionOperator { EqualsTo, NotEqualTo, LessThan, LessThanOrEqualTo, GreaterThan, GreaterThanOrEqualTo } public class BotRuleCondition { public BotRuleConditionOperand LeftOperand { get; set; } public BotRuleConditionOperator Operator { get; set; } public BotRuleConditionOperand RightOperand { get; set; } public object Value { get; private set; } public BotRuleCondition() { Value = null; } public BotRuleCondition(XmlNode node) { if (node?.Attributes == null) return; LeftOperand = new BotRuleConditionOperand(node.SelectSingleNode("left")); var operatorNode = node.SelectSingleNode("operator"); RightOperand = new BotRuleConditionOperand(node.SelectSingleNode("right"), node.SelectSingleNode("extra_operation")); switch (operatorNode.Attributes["type"].Value) { case "equalTo": Operator = BotRuleConditionOperator.EqualsTo; break; case "smallerThan": Operator = BotRuleConditionOperator.LessThan; break; case "smallerOrEqualThan": Operator = BotRuleConditionOperator.LessThanOrEqualTo; break; case "greaterThan": Operator = BotRuleConditionOperator.GreaterThan; break; case "greaterOrEqualThan": Operator = BotRuleConditionOperator.GreaterThanOrEqualTo; break; default: throw new Exception(); } } public object Evaluate(IPlayer player) { LeftOperand.Evaluate(player); RightOperand.Evaluate(player); decimal lov = LeftOperand.Value; decimal rov = RightOperand.Value; switch (Operator) { case BotRuleConditionOperator.EqualsTo: Value = LeftOperand.Value == RightOperand.Value; break; case BotRuleConditionOperator.NotEqualTo: Value = LeftOperand.Value != RightOperand.Value; break; case BotRuleConditionOperator.LessThan: Value = lov < rov; break; case BotRuleConditionOperator.LessThanOrEqualTo: Value = lov <= rov; break; case BotRuleConditionOperator.GreaterThan: Value = lov > rov; break; case BotRuleConditionOperator.GreaterThanOrEqualTo: Value = lov >= rov; break; } return Value; } public bool IsTrue => Value as bool? == true; public override string ToString() { string op; switch (Operator) { case BotRuleConditionOperator.EqualsTo: op = "="; break; case BotRuleConditionOperator.NotEqualTo: op = "<>"; break; case BotRuleConditionOperator.LessThan: op = "<"; break; case BotRuleConditionOperator.LessThanOrEqualTo: op = "<="; break; case BotRuleConditionOperator.GreaterThan: op = ">"; break; case BotRuleConditionOperator.GreaterThanOrEqualTo: op = ">="; break; default: op = ""; break; } return $"{LeftOperand} {op} {RightOperand}"; } } }