全站通知:

模块:ExcludeChecker

来自星露谷物语维基
跳到导航 跳到搜索
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
星露谷物语 Objects.json 处理脚本 - 简化版
从Objects.json中提取具有排除属性的物品ID,按类型分组导出为简化的Lua table格式
"""

import json
import os


def load_objects_json(file_path):
    """加载Objects.json文件"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        print(f"错误:找不到文件 {file_path}")
        return None
    except json.JSONDecodeError as e:
        print(f"错误:JSON解析失败 - {e}")
        return None


def extract_excluded_ids(objects_data):
    """
    提取具有排除属性的物品ID
    返回按排除类型分组的字典
    """
    excluded_groups = {
        'ExcludeFromFishingCollection': [],
        'ExcludeFromShippingCollection': [],
        'ExcludeFromRandomSale': []
    }
    
    for object_id, object_data in objects_data.items():
        # 检查每个排除属性
        for exclude_type in excluded_groups.keys():
            if object_data.get(exclude_type, False):
                excluded_groups[exclude_type].append(object_id)
    
    return excluded_groups


def format_simple_lua_table(excluded_groups):
    """将ID分组数据格式化为简化的Lua table字符串"""
    lua_lines = ['-- 星露谷物语排除物品ID表 - 简化版', '-- 生成时间: ' + 
                 __import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '']
    
    lua_lines.append('local excluded_object_ids = {')
    
    for group_name, ids in excluded_groups.items():
        if ids:  # 只处理非空的分组
            lua_lines.append(f'    {group_name} = {{')
            
            # 将ID分组,每行最多10个
            for i in range(0, len(ids), 10):
                chunk = ids[i:i+10]
                id_list = ', '.join(f'"{id_}"' for id_ in chunk)
                lua_lines.append(f'        {id_list},')
            
            lua_lines.append('    },')
            lua_lines.append('')
    
    lua_lines.append('}')
    lua_lines.append('')
    lua_lines.append('return excluded_object_ids')
    
    return '\n'.join(lua_lines)


def save_lua_file(lua_content, output_path):
    """保存Lua文件"""
    try:
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(lua_content)
        print(f"成功导出Lua文件: {output_path}")
        return True
    except Exception as e:
        print(f"错误:保存文件失败 - {e}")
        return False


def print_statistics(excluded_groups):
    """打印统计信息"""
    print("\n=== 统计信息 ===")
    total_items = 0
    
    for group_name, ids in excluded_groups.items():
        count = len(ids)
        total_items += count
        print(f"{group_name}: {count} 个物品")
        
        if ids:
            print("  ID列表:")
            # 显示所有ID
            for i in range(0, len(ids), 10):
                chunk = ids[i:i+10]
                print(f"    {', '.join(chunk)}")
        print()
    
    print(f"总计: {total_items} 个物品")


def main():
    """主函数"""
    # 文件路径
    objects_json_path = "Objects.json"
    output_lua_path = "excluded_object_ids.lua"
    
    # 检查文件是否存在
    if not os.path.exists(objects_json_path):
        print(f"错误:找不到 {objects_json_path} 文件")
        print("请确保脚本在包含Objects.json的目录中运行")
        return
    
    print("正在加载Objects.json...")
    objects_data = load_objects_json(objects_json_path)
    
    if objects_data is None:
        return
    
    print(f"成功加载 {len(objects_data)} 个物品数据")
    
    print("正在提取排除物品ID...")
    excluded_groups = extract_excluded_ids(objects_data)
    
    # 打印统计信息
    print_statistics(excluded_groups)
    
    print("正在生成简化Lua代码...")
    lua_content = format_simple_lua_table(excluded_groups)
    
    # 保存文件
    if save_lua_file(lua_content, output_lua_path):
        print(f"\n处理完成!输出文件: {output_lua_path}")
    else:
        print("\n处理失败!")


if __name__ == "__main__":
    main()
[ 查看 | 编辑 | 历史 | 刷新 ]上述文档的内容来自模块:ExcludeChecker/doc
local excluded_object_ids = {
    ExcludeFromFishingCollection = {
        "167", "168", "169", "170", "171", "172", "898", "899", "900", "901",
        "902",
    },

    ExcludeFromShippingCollection = {
        "434", "889", "928",
    },

    ExcludeFromRandomSale = {
        "79", "158", "159", "160", "161", "162", "163", "261", "277", "279",
        "305", "308", "326", "341", "413", "417", "437", "439", "447", "454",
        "460", "499", "645", "680", "681", "682", "688", "689", "690", "774",
        "775", "797", "798", "799", "800", "801", "802", "803", "807", "812",
        "292", "289", "73", "69", "91", "FarAwayStone",
    },

}

local p = {}

-- 定义排除列表
local excluded_object_ids = {
    ExcludeFromFishingCollection = {
        "167", "168", "169", "170", "171", "172", "898", "899", "900", "901",
        "902",
    },

    ExcludeFromShippingCollection = {
        "434", "889", "928",
    },

    ExcludeFromRandomSale = {
        "79", "158", "159", "160", "161", "162", "163", "261", "277", "279",
        "305", "308", "326", "341", "413", "417", "437", "439", "447", "454",
        "460", "499", "645", "680", "681", "682", "688", "689", "690", "774",
        "775", "797", "798", "799", "800", "801", "802", "803", "807", "812",
        "292", "289", "73", "69", "91", "FarAwayStone",
    },
}

-- 将数组转换为哈希表以提高查找效率
local function arrayToSet(array)
    local set = {}
    for _, value in ipairs(array) do
        set[value] = true
    end
    return set
end

-- 预处理排除列表,转换为哈希表
local exclude_sets = {}
for category, ids in pairs(excluded_object_ids) do
    exclude_sets[category] = arrayToSet(ids)
end

-- 去除ID开头的括号内容
local function removePrefix(id)
    if not id then
        return ""
    end
    
    -- 去除开头的括号及括号内的内容,例如 "(O)666" -> "666"
    local cleaned_id = string.gsub(id, "^%b()", "")
    return cleaned_id
end

-- 检查ID是否在指定的排除列表中
function p.checkExclusion(frame)
    local args = frame.args
    local id = args[1] or args.id or ""
    local category = args[2] or args.category or "ExcludeFromFishingCollection"
    
    -- 去除ID的前缀
    local cleaned_id = removePrefix(id)
    
    -- 检查指定的排除类别是否存在
    if not exclude_sets[category] then
        -- 如果指定的类别不存在,返回true(不排除)
        return "true"
    end
    
    -- 检查清理后的ID是否在排除列表中
    if exclude_sets[category][cleaned_id] then
        return "false"  -- 在排除列表中,返回false
    else
        return "true"   -- 不在排除列表中,返回true
    end
end

return p


    -- {{#invoke:ExcludeChecker|checkExclusion|(O)167}} → 返回 "false"(167在ExcludeFromFishingCollection中)
    -- {{#invoke:ExcludeChecker|checkExclusion|(O)123}} → 返回 "true"(123不在ExcludeFromFishingCollection中)
    -- {{#invoke:ExcludeChecker|checkExclusion|434|ExcludeFromShippingCollection}} → 返回 "false"(434在ExcludeFromShippingCollection中)