模块:螺旋/记忆

来自魔法纪录中文Wiki
跳转至: 导航 搜索

{{#invoke:螺旋/sandbox2|fn|}} Game:记忆结晶


function analyzeTxt(txtList)
    local result = {}
    for i,txt in ipairs(txtList) do
        local obj = {}
        string.gsub(txt, '[^\n]+', function(line)
            -- print('> "'..line..'"')
            local splitIndex = string.find(line, '=')
            local key   = string.sub(line, 0, splitIndex-1 )
            local value = string.sub(line, splitIndex+1 )
            -- 删空格
            key   = string.gsub(key, "^%s*(.-)%s*$", "%1")
            value = string.gsub(value, "^%s*(.-)%s*$", "%1")
            -- print('> "'..key..'" = "'..value..'"')
            obj[key] = value
        end)
        table.insert(result, obj)
    end
    -- mw.logObject(result , '【result】')
    return result;
end

-- mw.logObject(package.preload , '【package.preload】' )
	
local memoriaData = nil
-- if( type(package.preload['_memoriaData']) == 'table' ) then
-- 	memoriaData = package.preload['_memoriaData']
-- else
	local data = require('模块:螺旋/记忆数据')
	-- mw.log('do require')
	local d = analyzeTxt(data.d)
	memoriaData = {
		update = data.update,
		list = d,
	}
	-- mw.logObject(package.loaders , '【package.loaders】' )
-- end

function toStringOr(value, def)
    if value == nil then return def end
	if (type(value) == 'string') then
		return string.gsub(value, "^%s*(.-)%s*$", "%1") -- 删空格
	end
    return def
end
function toNumberOr(value, def)
	local num = def
	if (type(value) == "string") then num = tonumber(value) end
	if (type(value) == 'number') then num = value end
	if num == nil then num = def end
    return num
end

function memoriaProcess(frame, memoria)
	memoria['效果'] = frame:preprocess( memoria['效果'] )
	memoria['满破效果'] = frame:preprocess( memoria['满破效果'] )
	if(memoria['可装备text'] ~= nil ) then
		memoria['可装备text'] = frame:preprocess( memoria['可装备text'] )
	end
    return memoria
end

local p = {}
p.list = function (frame)
	-- mw.logObject(frame , '【frame】')
	mw.logObject(frame.args , '【frame-args】')
  
    local template = toStringOr(frame.args[1], '记忆详细数据表')
	mw.logObject({['in']=frame.args[1],['out']=template} , '【template(模板)】')
    local startIndex = toNumberOr(frame.args[2], 0)
	mw.logObject({['in']=frame.args[2],['out']=startIndex} , '【startIndex(开始位置,倒序)】')
    local num = toNumberOr(frame.args[3], 1)
	mw.logObject({['in']=frame.args[3],['out']=num} , '【num(显示数量)】') -- 传入`0`显示`startIndex`到最后
	
	local memoriaList = memoriaData.list
	local txt = ''
	for i, memoria in pairs( memoriaList ) do
		if ( i >= startIndex ) then 
			memoria = memoriaProcess(frame, memoria)
			txt = txt  .. '\n\n' .. frame:expandTemplate{title = template, args = memoria }
		end
		if ( num > 0 and i >= (startIndex-1+num) ) then return txt end
	end
	if( '记忆详细数据表'==template ) then
		txt = '更新时间:' .. memoriaData.update .. txt
	end
	return txt
end
-- 别名
p['列表']=p.list


p.memoria = function (frame)
	-- mw.logObject(frame , '【frame】')
	mw.logObject(frame.args , '【frame-args】')
  
    local template = toStringOr(frame.args[1], '记忆详细数据表')
	mw.logObject({['in']=frame.args[1],['out']=template} , '【template(模板)】')
	
	local nameJp = nil
	local mId = toNumberOr(frame.args[2])
	mw.logObject({['in']=frame.args[2],['out']=mId} , '【mId(记忆Id)】')
	if( mId ~= nil ) then
		mId = ''..mId -- 转回字符串...
	else
	    nameJp = toStringOr(frame.args[2], '未填写记忆日文名')
		mw.logObject({['in']=frame.args[2],['out']=nameJp} , '【nameJp(记忆日文名)】')
	end
	
    local field = toStringOr(frame.args[3], nil)
	mw.logObject( { ['in']=frame.args[3], ['out']=field } , '【field(选取字段)】')
	
	local memoriaList = memoriaData.list
	for i, memoria in pairs( memoriaList ) do
		if ( 
			( mId ~= nil and mId == memoria['编号'] )
			or ( nameJp ~= nil and nameJp == memoria['日文名'] )
		) then 
		
			if( field ~= nil ) then 
				if( memoria[field] ~= nil ) then 
					return frame:preprocess( memoria[field] )
				else
					return '找不到字段:'..field
				end
			end
		
			memoria = memoriaProcess(frame, memoria)
			
			for k, v in frame:argumentPairs() do
				if( type(k)=='string' ) then
					if( memoria[k] ~= nil ) then mw.log( '覆盖更新[' .. k .. ']=' .. v ) end
					memoria[k] = v
				end
			end
			
			return frame:expandTemplate{title = template, args = memoria }
		end
	end
	if( mId ~= nil ) then
		return frame:preprocess('{{Ombox|text=找不到Id:'..mId..'}}')
	else
		return frame:preprocess('{{Ombox|text=找不到:'..nameJp..'}}')
	end
end
-- 别名
p['数据']=p.memoria
p['记忆']=p.memoria

return p