bugfix1001.2
全站通知:

模块:沙盒3

来自火星求生WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:沙盒3/doc创建

-- 定义一个模块
local p = {}

-- 创建词汇表和图片模板映射
local wordToImageMap = {
    ["火焚耐性"] = "火焚耐性",
    ["雷蛰耐性"] = "雷蛰耐性",
    ["寒冻耐性"] = "寒冻耐性",
    ["毒伤耐性"] = "毒伤耐性",
    ["火焚攻击"] = "火焚",  -- 特殊情况
    ["雷蛰攻击"] = "雷蛰",  -- 特殊情况
    ["寒冻攻击"] = "寒冻",  -- 特殊情况
    ["毒伤攻击"] = "毒伤",  -- 特殊情况
    ["移动速度"] = "移动速度",
    ["气力恢复"] = "气力恢复",
    ["伤害减免"] = "伤害减免",
    ["伤害加成"] = "伤害加成",
    ["法术"] = "法术",
    ["暴击伤害"] = "暴击伤害",
    ["防御"] = "防御",
    ["攻击"] = "攻击",
    ["气力"] = "气力",
    ["法力"] = "法力",
    ["生命"] = "生命",
    ["暴击率"] = "暴击率"
}

-- 需要排除的部分词汇列表
local excludeWords = {
    "火焚攻击",
    "雷蛰攻击",
    "寒冻攻击",
    "毒伤攻击",
    "跳跃攻击"  -- 跳跃攻击没有对应图片
}

-- 实现 table.contains 函数
function table.contains(t, value)
    for _, v in pairs(t) do
        if v == value then
            return true
        end
    end
    return false
end

-- 创建模式匹配规则(注意:先匹配较长的复合词)
local patterns = {}
for word, imgTemplate in pairs(wordToImageMap) do
    if not table.contains(excludeWords, word) then
        -- 对正则表达式的特殊字符进行转义
        local escapedWord = mw.ustring.gsub(word, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
        table.insert(patterns, {pattern = escapedWord, template = imgTemplate})
    end
end

-- 确保复合词优先匹配
for _, word in ipairs(excludeWords) do
    local imgTemplate = wordToImageMap[word]
    if imgTemplate then
        local escapedWord = mw.ustring.gsub(word, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
        table.insert(patterns, {pattern = escapedWord, template = imgTemplate})
    end
end

-- 按长度降序排列模式,确保长词优先匹配
table.sort(patterns, function(a, b) return #a.pattern > #b.pattern end)

-- 辅助函数:处理单个字符串
local function processSingleString(input)
    if type(input) ~= 'string' or input:match("^%s*$") then
        return ''
    end

    local output = input

    -- 遍历并应用模式,确保每个匹配只替换一次
    for _, entry in ipairs(patterns) do
        local pattern, template = entry.pattern, entry.template
        output = mw.ustring.gsub(output, pattern, function(matched)
            if template then
                return "{{i|" .. template .. "}}" .. matched
            else
                return matched -- 对于没有对应图片的词汇,保持原样
            end
        end, 1) -- 只替换第一次出现的匹配
    end

    return output
end

-- 函数设计:处理输入字符串,支持分号分隔的多个短语
function p.processString(frame)
    -- 从模板调用中获取输入参数
    local input = frame.args[1] or ''

    -- 确保输入是字符串并且不为空白
    if type(input) ~= 'string' or input:match("^%s*$") then
        return ''
    end

    -- 分割输入字符串为多个短语,并去除首尾空白
    local phrases = mw.text.split(mw.text.trim(input), "%s*;%s*")

    -- 处理每个短语并重新组合
    local processedPhrases = {}
    for _, phrase in ipairs(phrases) do
        local result = processSingleString(phrase)
        if result ~= '' then
            table.insert(processedPhrases, result)
        end
    end

    -- 将处理过的短语用分号连接起来
    local finalResult = table.concat(processedPhrases, "; ")
    
    -- 如果最终结果为空,则返回原始输入
    if finalResult == '' then
        return input
    end
    
    return finalResult
end

-- 返回模块
return p