c1cc1291-376d-4080-b4e7-38c602d88f62.js 3.94 KB
"use strict";
cc._RF.push(module, 'c1cc1KRN21AgLTnOMYC2I9i', 'PlotUtils');
// script/avg/utils/PlotUtils.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTextWithVariables = exports.evalConditionExpr = void 0;
const ConditionModel_1 = require("../model/ConditionModel");
const GameRecord_1 = require("../game-data/GameRecord");
const simba_utils_1 = require("simba-utils");
const ConfigManager_1 = require("../../common/gameplay/managers/ConfigManager");
function evalConditionItem(item) {
    let op1 = GameRecord_1.GameRecord.getVariableValue(item.target);
    let op2;
    if (item.oprand.type === ConditionModel_1.ConditionOprandType.Const) {
        op2 = item.target;
    }
    else {
        op2 = GameRecord_1.GameRecord.getVariableValue(item.target);
    }
    switch (item.operator) {
        case ConditionModel_1.ConditionOperator.Equal:
            return op1 == op2;
        case ConditionModel_1.ConditionOperator.Greater:
            return op1 > op2;
        case ConditionModel_1.ConditionOperator.GreaterOrEqual:
            return op1 >= op2;
        case ConditionModel_1.ConditionOperator.Less:
            return op1 < op2;
        case ConditionModel_1.ConditionOperator.LessOrEqual:
            return op1 <= op2;
        case ConditionModel_1.ConditionOperator.NotEqual:
            return op1 != op2;
    }
}
function evalConditionExpr(condition) {
    let ret = condition.relation === ConditionModel_1.ConditionRelation.Or ? false : true;
    for (let group of condition.groups) {
        for (let item of group.items) {
            ret = evalConditionItem(item);
            if ((ret && group.relation === ConditionModel_1.ConditionRelation.Or) || (!ret && group.relation === ConditionModel_1.ConditionRelation.And)) {
                break;
            }
        }
        if ((ret && condition.relation === ConditionModel_1.ConditionRelation.Or) || (!ret && condition.relation === ConditionModel_1.ConditionRelation.And)) {
            break;
        }
    }
    return ret;
}
exports.evalConditionExpr = evalConditionExpr;
function parseTextWithVariables(text) {
    if (text.charCodeAt(0) !== 1)
        return { parsedText: text, variables: [] };
    text = text.substr(1);
    let parsedText = text;
    let matches = parsedText.match(/\${(.*?)}/g);
    if (!matches)
        return { parsedText: text, variables: [] };
    let matchVars = simba_utils_1.uniqArray(matches);
    let variables = matchVars.map(v => v.substring(2, v.length - 1));
    for (let i = 0; i < matchVars.length; i++) {
        let varName = variables[i];
        if (/^[gr]\./.test(varName)) {
            parsedText = parsedText.replace(new RegExp(matchVars[i].replace(/\./gm, "\\.").replace(/\$/gm, "\\$"), "gm"), GameRecord_1.GameRecord.getVariableValue(varName).toString());
        }
        else { // value from table
            let index = varName.indexOf(".");
            let index1 = varName.indexOf("[");
            if (index >= 0) {
                let tableName = varName.substring(0, index1);
                let valueId = parseInt(varName.substring(index1 + 1, index - 1));
                let fieldName = varName.substring(index + 1);
                try {
                    let text = ConfigManager_1.ConfigManager.getConfig({ name: tableName }, valueId)[fieldName];
                    if (typeof text === "number") {
                        text = text.toString();
                    }
                    else {
                        text = parseTextWithVariables(text);
                    }
                    parsedText = parsedText.replace(new RegExp(matchVars[i].replace(/\./gm, "\\.").replace(/\$/gm, "\\$"), "gm"), text);
                }
                catch (e) {
                    console.error(e);
                }
            }
            else {
                console.error("Invalid variable");
            }
        }
    }
    return { parsedText, variables };
}
exports.parseTextWithVariables = parseTextWithVariables;

cc._RF.pop();