bugfix250107.1
全站通知:

模块:提取商店礼包数据

来自恋与深空WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:提取商店礼包数据/doc创建

local p = {}
local itemCache = {}
local randomItemCache = {}

function p.getData()
	
	local jsonText = mw.title.new("data:GiftPack.json"):getContent()
	
	if not jsonText then 
		return "找不到json文件内容"
	end 
	
	-- 解析 json 内容  
	local data = mw.text.jsonDecode(jsonText)
	if not data then 
		return "无法解析json内容"
	end 
	
	local function toTimestamp(text)
		if not text or text == "" then return 0 end
		
		local day, month, year, time = text:match("(%d+)%s*(%d+)月%s*(%d+)%s*(%d+:%d+:%d+)")
		if day and month and year and time then 
			month = tonumber(month)
			day = tonumber(day)
			local newText = string.format("%04d-%02d-%02d %s", year, month, day, time)
			return tonumber(mw.getContentLanguage():formatDate("U", newText)) or 0
		end
    end

	-- 整理数据
	table.sort(data, function(a,b)
		local tA, tB = toTimestamp(a["礼包结束时间"]), toTimestamp(b["礼包结束时间"])
		if tA ~= tB then 
			return tA > tB
		end
		
		if a["礼包单抽价排序"] ~= b["礼包单抽价排序"] then 
			return a["礼包单抽价排序"] < b["礼包单抽价排序"]
		end
		
		if a["礼包售价"] ~= b["礼包售价"] then 
			return a["礼包售价"] < b["礼包售价"]
		end
	end) 
	
	return data 
end

-- 用于在模板中调用
function p.show(frame)
	local data = p.getData()
	if type(data) ~= "table" then 
		return data 
	end 

	local root = mw.html.create()
	
	for _, code in ipairs(data) do
		local items = code["道具"] or {}
		local random_items = code["礼包随机内容"] or {}
		
		-- 道具内容整理以及缓存
		local item_text = ""
	    for name, quantity in pairs(items) do
	    	if not itemCache[name] then 
	    		itemCache[name] = frame:expandTemplate{
	    			title = "Item",
	    			args = {name, quantity}
	    		}
	    	end 
	    	
	    	local html = itemCache[name]
			html = html:gsub("item%-rightBottom\">.-</div>", 'item-rightBottom">' .. quantity .. '</div>')
			item_text = item_text .. html
	    end
	    
	    -- 礼包随机内容整理以及缓存
	    local random_text = ""
	    for _, entry in ipairs(random_items) do 
	    	local name = entry["名称"]
	    	local qty = entry["数量"]
	    	local chance = entry["概率"]
	    	
	    	local cacheKey = name .. "|" .. qty .. "|" .. chance
	    	
	    	if not randomItemCache[cacheKey] then 
	    		randomItemCache[cacheKey] = frame:expandTemplate{
	    			title = "Item",
	    			args = {
	    				name,
	    				qty,
	    				top = tostring(chance * 100) .. "%"
	    			}
	    		}
	    	end 
        
            local html = randomItemCache[cacheKey]
            random_text = random_text .. html
        end
	
        local args = {
	        ["礼包类型"] = code["礼包类型"],
	        ["礼包卡池"] = code["礼包卡池"],
	        ["礼包档位"] = code["礼包档位"],
	        ["礼包"] = code["礼包"],
	        ["礼包单抽价"] = code["礼包单抽价"],
	        ["礼包抽数"] = code["礼包抽数"],
	        ["礼包特殊售价"] = code["礼包特殊售价"],
	        ["礼包售价"] = code["礼包售价"],
	        ["礼包限购"] = code["礼包限购"],
	        ["礼包描述"] = code["礼包描述"],
	        ["礼包内容"] = item_text,
	        ["礼包随机内容"] = random_text,
	        }
	
	    local html = frame:expandTemplate{ title = "礼包/行", args = args }
	    root:wikitext(html)	
	                
    end
	
	return tostring(root)
end

return p