全站通知:

模块:FishBySpot

来自星露谷物语维基
跳到导航 跳到搜索
[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
local items = require("Module:Items")

local p = {}

function getItemID(itemName)
	local result = items.getId(itemName)
    return result
end

-- 初始化数据
local function initData()
	local rawData = mw.loadData("Module:FishBySpot/data")
	local data = {}
	
	-- 深拷贝数据 - 使用 pairs 而不是 ipairs
	for location, fishes in pairs(rawData) do
		data[location] = {}
		-- 获取最大索引
		local maxIndex = 0
		for k, _ in pairs(fishes) do
			if type(k) == "number" and k > maxIndex then
				maxIndex = k
			end
		end
		-- 遍历所有索引,包括 nil 值
		for i = 1, maxIndex do
			data[location][i] = fishes[i]
		end
	end
	
	-- 1. 处理 BeachNightMarket - 添加 Pelican Beach 的数据
	if data["BeachNightMarket"] then
		local newBeachNightMarket = {}
		for i = 1, #data["BeachNightMarket"] + 100 do  -- 使用足够大的数字
			local fish = data["BeachNightMarket"][i]
			if fish == nil then
				-- 跳过 nil
			elseif fish ~= "LOCATION_FISH Beach BOBBER_X BOBBER_Y WATER_DEPTH" then
				table.insert(newBeachNightMarket, fish)
			end
		end
		-- 添加 Pelican Beach 的鱼
		if data["Pelican Beach"] then
			for i = 1, #data["Pelican Beach"] + 100 do
				local fish = data["Pelican Beach"][i]
				if fish ~= nil then
					table.insert(newBeachNightMarket, fish)
				end
			end
		end
		data["BeachNightMarket"] = newBeachNightMarket
	end
	
	-- 2. 处理 DesertFestival - 添加 Calico Desert 的数据
	if data["DesertFestival"] then
		local newDesertFestival = {}
		for i = 1, #data["DesertFestival"] + 100 do
			local fish = data["DesertFestival"][i]
			if fish == nil then
				-- 跳过 nil
			elseif fish ~= "LOCATION_FISH Desert BOBBER_X BOBBER_Y WATER_DEPTH" then
				table.insert(newDesertFestival, fish)
			end
		end
		-- 添加 Calico Desert 的鱼
		if data["Calico Desert"] then
			for i = 1, #data["Calico Desert"] + 100 do
				local fish = data["Calico Desert"][i]
				if fish ~= nil then
					table.insert(newDesertFestival, fish)
				end
			end
		end
		data["DesertFestival"] = newDesertFestival
	end
	
	-- 3. 去掉所有 nil 值 - 使用索引遍历
	for location, fishes in pairs(data) do
		local cleaned = {}
		-- 获取最大索引
		local maxIndex = 0
		for k, _ in pairs(fishes) do
			if type(k) == "number" and k > maxIndex then
				maxIndex = k
			end
		end
		-- 遍历所有可能的索引
		for i = 1, maxIndex do
			if fishes[i] ~= nil then
				table.insert(cleaned, fishes[i])
			end
		end
		data[location] = cleaned
	end
	
	return data
end

-- 地点名称映射表(英文 -> 中文)
local locationMap = {
	["Pelican Town"] = "鹈鹕镇",
	["Pelican Beach"] = "海滩",
	["BeachNightMarket"] = "夜市",
	["Mountains"] = "深山",
	["Cindersap Forest"] = "煤矿森林",
	["Sewer"] = "下水道",
	["BugLand"] = "突变虫穴",
	["Calico Desert"] = "沙漠",
	["Secret Woods"] = "秘密森林",
	["Railroad"] = "铁路",
	["WitchSwamp"] = "巫婆沼泽",
	["Backwoods"] = "边远森林",
	["Submarine"] = "钓鱼潜艇",
	["BoatTunnel"] = "威利的船",
	["IslandSouth"] = "姜岛南部",
	["IslandSouthEast"] = "姜岛东南部",
	["IslandSouthEastCave"] = "海盗湾",
	["IslandWest"] = "姜岛西部",
	["IslandNorth"] = "姜岛北部",
	["IslandFarmCave"] = "饕餮青蛙洞穴",
	["Caldera"] = "火山口",
	["DesertFestival"] = "沙漠节",
}

-- 黑名单(这些地点不会出现在结果中)
local blacklist = {
	["Backwoods"] = true,
	["DesertFestival"] = true,
}

-- 主函数:根据物品ID查找所有地点
function p.getLocations(frame)
	local para
	if frame.args == "table" then
		para = frame.args[1]
	else
		para = frame
	end
	if type(para) == "table" and para.args then
		para = para.args[1]
	end
	
	local itemId = getItemID(para) or para
	
	-- 去除空格
	itemId = mw.text.trim(itemId or "")
	
	if itemId == "" then
		return ""
	end
	
	local data = initData()
	local locations = {}
	local locationSet = {} -- 用于去重
	
	-- 遍历所有地点,查找包含该物品的地点
	for location, fishes in pairs(data) do
		-- 检查是否在黑名单中
		if not blacklist[location] then
			for _, fish in ipairs(fishes) do
				if fish == itemId then
					local chineseName = locationMap[location] or location
					if not locationSet[chineseName] then
						table.insert(locations, chineseName)
						locationSet[chineseName] = true
					end
					break
				end
			end
		end
	end
	
	-- 如果没有找到任何地点
	if #locations == 0 then
		return ""
	end
	
	-- 排序并用逗号连接
	table.sort(locations)
	return table.concat(locations, ",")
end

-- 调试函数:显示某个地点的所有鱼
function p.debug(frame)
	local location = frame.args[1] or ""
	local data = initData()
	
	if data[location] then
		return table.concat(data[location], ", ")
	else
		return "地点不存在"
	end
end

return p