using EasyTalk.Controller;
using EasyTalk.Nodes.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace EasyTalk.Nodes.Logic
{
///
/// A conditional node which compares two string values, outputs the result of the comparison, and continues down the dialogue flow path attributed to the result.
///
[Serializable]
public class CompareStringsNode : Node, DialogueFlowNode, ConditionalNode, FunctionalNode
{
///
/// The type of string comparison to perform.
///
[SerializeField]
private StringComparisonType comparisonType;
///
/// The first value to use in the comparison.
///
[SerializeField]
private string valueA;
///
/// The second value to use in the comparison.
///
[SerializeField]
private string valueB;
///
/// Creates a new CompareStringNode.
///
public CompareStringsNode()
{
this.name = "COMPARE STRINGS";
this.nodeType = NodeType.STRING_COMPARE;
}
///
/// Gets or sets the type of comparison this node should perform (from a string equivalent to a StringComparisonType toString() value).
///
public string ComparisonTypeString
{
get { return comparisonType.ToString(); }
set { Enum.TryParse(value, out comparisonType); }
}
///
/// Gets or sets the string comparison type to perform.
///
public StringComparisonType ComparisonType
{
get { return comparisonType; }
set { comparisonType = value; }
}
///
/// Gets or sets the first value to use in the comparison.
///
public string ValueA
{
get { return this.valueA; }
set { this.valueA = value; }
}
///
/// Gets or sets the second value to use in the comparison.
///
public string ValueB
{
get { return this.valueB; }
set { this.valueB = value; }
}
///
public bool DetermineAndStoreValue(NodeHandler nodeHandler, Dictionary nodeValues, GameObject convoOwner = null)
{
string stringA = "";
string stringB = "";
if (Inputs[1].AttachedIDs.Count > 0)
{
stringA = nodeValues[Inputs[1].AttachedIDs[0]].ToString();
}
else if(valueA != null)
{
stringA = nodeHandler.ReplaceVariablesInString(valueA);
}
if (Inputs[2].AttachedIDs.Count > 0)
{
stringB = nodeValues[Inputs[2].AttachedIDs[0]].ToString();
}
else if(valueB != null)
{
stringB = nodeHandler.ReplaceVariablesInString(valueB);
}
NodeConnection boolOutput = FindOutputOfType(InputOutputType.BOOL);
switch(comparisonType)
{
case StringComparisonType.BEFORE:
nodeValues.TryAdd(boolOutput.ID, stringA.CompareTo(stringB) < 0);
nodeValues.TryAdd(this.ID, stringA.CompareTo(stringB) < 0);
break;
case StringComparisonType.AFTER:
nodeValues.TryAdd(boolOutput.ID, stringA.CompareTo(stringB) > 0);
nodeValues.TryAdd(this.ID, stringA.CompareTo(stringB) > 0);
break;
case StringComparisonType.EQUAL:
nodeValues.TryAdd(boolOutput.ID, stringA.Equals(stringB));
nodeValues.TryAdd(this.ID, stringA.Equals(stringB));
break;
case StringComparisonType.NOT_EQUAL:
nodeValues.TryAdd(boolOutput.ID, !stringA.Equals(stringB));
nodeValues.TryAdd(this.ID, !stringA.Equals(stringB));
break;
}
return true;
}
///
public List GetDependencyOutputIDs()
{
return FindDependencyOutputIDs();
}
///
public NodeConnection GetFalseOutput()
{
return FindOutputOfType(InputOutputType.DIALOGUE_FALSE_FLOW);
}
///
public NodeConnection GetFlowInput()
{
return FindFlowInputs()[0];
}
///
public NodeConnection GetFlowOutput()
{
NodeConnection conn = GetTrueOutput();
if(conn.AttachedIDs.Count > 0)
{
return conn;
}
return GetFalseOutput();
}
///
public NodeConnection GetOutput(bool value)
{
if(value) { return GetTrueOutput(); }
return GetFalseOutput();
}
///
public NodeConnection GetTrueOutput()
{
return FindOutputOfType(InputOutputType.DIALOGUE_TRUE_FLOW);
}
///
public bool HasDependencies()
{
return FindDependencyOutputIDs().Count > 0;
}
}
///
/// An enumeration which defines types of string comparisons which can be performed by a 'compare strings' node.
///
public enum StringComparisonType { EQUAL, NOT_EQUAL, AFTER, BEFORE }
}