From 2e6253b532c7a0b2d1ecc7aa1f88d6c777ad8166 Mon Sep 17 00:00:00 2001
From: ezsh <ezsh.junk@gmail.com>
Date: Tue, 31 May 2022 16:12:25 +0200
Subject: [PATCH] RA: add conditional that does regular expression match

---
 js/rulesAssistant/conditionEditor.js                   | 10 ++++++----
 js/rulesAssistant/conditionEvaluation.js               |  4 ++++
 src/gui/Encyclopedia/encyclopediaRAActivationEditor.js |  2 ++
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/js/rulesAssistant/conditionEditor.js b/js/rulesAssistant/conditionEditor.js
index 88a6ac2ba08..e6f4ec6be00 100644
--- a/js/rulesAssistant/conditionEditor.js
+++ b/js/rulesAssistant/conditionEditor.js
@@ -533,9 +533,9 @@ App.RA.Activation.Editor = (function() {
 	}
 
 	/**
-	 * @typedef {"sub" | "div" | "eq" | "neq" | "gt" | "gte" | "lt" | "lte"| "substr"} RulePairComparators
+	 * @typedef {"sub" | "div" | "eq" | "neq" | "gt" | "gte" | "lt" | "lte"| "substr" | "match"} RulePairComparators
 	 * @typedef {object} RulePairComparatorDisplay
-	 * @property {string} key
+	 * @property {RulePairComparators} key
 	 * @property {string} name
 	 * @type {RulePairComparatorDisplay[]}
 	 */
@@ -549,6 +549,7 @@ App.RA.Activation.Editor = (function() {
 		{key: "sub", name: "-"},
 		{key: "div", name: "/"},
 		{key: "substr", name: "Contains"},
+		{key: "match", name: "Matches"},
 	];
 
 	class RulePair extends RuleContainer {
@@ -604,7 +605,7 @@ App.RA.Activation.Editor = (function() {
 					errorList.push("Both sides need to return the same type.");
 					return "error";
 				}
-			} else if (this.mode === "substr") {
+			} else if (this.mode === "substr" || this.mode === "match") {
 				if (this._child1.validate(errorList) === "string" && this._child2.validate(errorList) === "string") {
 					return "number";
 				} else {
@@ -1123,7 +1124,7 @@ App.RA.Activation.Editor = (function() {
 		}
 
 		/**
-		 * @param {"sub" | "div" | "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "substr"} mode
+		 * @param {"sub" | "div" | "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "substr" | "match"} mode
 		 */
 		function makePair(mode) {
 			const pair = new RulePair(mode);
@@ -1162,6 +1163,7 @@ App.RA.Activation.Editor = (function() {
 			["lt", () => makePair("lt")],
 			["lte", () => makePair("lte")],
 			["substr", () => makePair("substr")],
+			["match", () => makePair("match")],
 			["not", () => {
 				const negate = new RuleNegate();
 				negate.child = stack.popRulePart();
diff --git a/js/rulesAssistant/conditionEvaluation.js b/js/rulesAssistant/conditionEvaluation.js
index 66f423c03cc..e3c50573c75 100644
--- a/js/rulesAssistant/conditionEvaluation.js
+++ b/js/rulesAssistant/conditionEvaluation.js
@@ -763,6 +763,10 @@ App.RA.Activation.evaluate = function(slave, rule) {
 			const value = stack.popString();
 			stack.pushBoolean(stack.popString().includes(value));
 		}],
+		["match", () => {
+			const value = stack.popString();
+			stack.pushBoolean(stack.popString().match(value) !== null);
+		}],
 		["not", () => stack.pushBoolean(stack.popNumber() === 0)],
 		["ternarystr", () => {
 			const ifFalse = stack.popString();
diff --git a/src/gui/Encyclopedia/encyclopediaRAActivationEditor.js b/src/gui/Encyclopedia/encyclopediaRAActivationEditor.js
index 5fa62bc7cac..6347e8031d5 100644
--- a/src/gui/Encyclopedia/encyclopediaRAActivationEditor.js
+++ b/src/gui/Encyclopedia/encyclopediaRAActivationEditor.js
@@ -68,6 +68,8 @@ App.Encyclopedia.addArticle("RA Condition Editor", function() {
 		transformerRow(el, "/", "Divides the second value by the first value", "Number");
 		transformerRow(el, "Contains", "True, if the second value is somewhere in the first value",
 			"String");
+		transformerRow(el, "Matches", "True, if the first value matches the regular expression in the second value",
+			"String");
 		transformerRow(el, "Not …", "Negates the input value.", "Boolean");
 		transformerRow(el, "If … Then … Else …",
 			"If the first value is true, returns the second value, otherwise the third value. The second " +
-- 
GitLab