Module:ItemEffectsDebug

From Fallen London Wiki

Documentation for this module may be created at Module:ItemEffectsDebug/doc

local p = {}

function ask_minmax(quality, slot, pageID, minmax, extra)
	local limit = 0
	local ask = mw.smw.ask{
		'[[Modifies quality::' .. quality .. ']][[-Has effect::<q>[[Has effect::' .. quality .. ']][[Equips in slot::Category:' .. slot .. ']]' .. extra .. '[[Page ID::!' .. pageID .. ']]</q>]]',
		'?Modifies by=',
		sort = 'Modifies by',order = minmax,limit = 1,searchlabel = '',mainlabel = '-'}
	if ask ~= nil then limit = ask[1][1] end
	return limit
end

function ask_minmax_bdr(slot, mimax, extra)
	local limit = 0
	local ask = mw.smw.ask{
		'[[Equips in slot::Category:' .. slot .. ']]' .. extra .. '[[Page ID::!' .. pageID .. ']]',
		'?Total BDR=',
		sort = 'Total BDR',order = minmax,limit = 1,searchlabel = '',mainlabel = '-'}
	if ask ~= nil then limit = ask[1][1] end
	return limit
end

function msg_logic(value, limit_f, limit_nf, control, mood_count)
	local out = ''
	
	if value > 0 then -- BiS
		if value > limit_nf and value ~= limit_f then
			out = out .. 'Strict'
		elseif value == limit_nf or (value == limit_f and limit_nf <= limit_f) then
			out = out .. 'Shared'
		end
		
		if out ~= '' then
			out = out .. ' Best-in-Slot'
			if value < limit_f and string.sub(control,1,1) == '-' then
				out = out .. ' non-Fate'
			end
		end
	elseif value < 0 then -- WiS
		if value < limit_nf and value ~= limit_f then
			out = out .. 'Strict'
		elseif value == limit_nf or (value == limit_f and limit_nf <= limit_f) then
			out = out .. 'Shared'
		end
		
		if out ~= '' then
			out = out .. ' Worst-in-Slot'
			if value > limit_f and string.sub(control,1,1) == '-' then
				out = out .. ' non-Fate'
			end
		end
	end
	
	if out ~= '' and string.sub(control,2,2) == '-' and mood_count > 0 and value > 0 then
		out = out .. ' non-[[Mood]]'
	end
	
	return out
end

function bis_msg(value, quality, slot, pageID, minmax, control, state)
	if state ~= "Normal" then
		return ''	
	end
	local attributes = {
        ["Watchful"] = true,
        ["Shadowy"] = true,
        ["Persuasive"] = true,
        ["Dangerous"] = true,
        ["Bizarre"] = true,
        ["Dreaded"] = true,
        ["Respectable"] = true,
        ["A Player of Chess"] = true,
        ["Artisan of the Red Science"] = true,
        ["Glasswork"] = true,
        ["Kataleptic Toxicology"] = true,
        ["Mithridacy"] = true,
        ["Monstrous Anatomy"] = true,
        ["Shapeling Arts"] = true,
        ["Neathproofed"] = true,
        ["Zeefaring"] = true,
    }
    
    local item_slot = {
        ["Burden"] = true,   	
        ["Hat"] = true,
        ["Clothing"] = true,
        ["Gloves"] = true,
        ["Weapon"] = true,
        ["Boots"] = true,
        ["Companion"] = true,
        ["Treasure"] = true,        
        ["Destiny"] = true,
        ["Affiliation"] = true,
        ["Transport"] = true,
        ["Home Comfort"] = true,
        ["Ship"] = true,
        ["Spouse"] = true,
        ["Club"] = true,
        ["Tools of the Trade"] = true
    }
    
    if (quality == nil) or (not item_slot[slot]) then
    	return '[[Category:Items with Malformed Effects Field]]'
    end

	if not attributes[quality] then
		return ''
	end
	
	local out_bis = ''
	local mood_count = mw.smw.ask{
		'[[Has effect::' .. quality .. ';?]][[Equipment status::Mood]][[Equips in slot::Category:' .. slot .. ']][[Page ID::!' .. pageID .. ']]',
		format = 'count'}
	
	local limit_f = 0
	local limit_nf = 0
	
	-- Hey Lua, where's my switch?
	if control == '--' then -- Non-Fate non-Mood
		limit_f = ask_minmax(quality, slot, pageID, minmax, '[[Equipment status::Normal]][[Is Fate::true]]')
		limit_nf = ask_minmax(quality, slot, pageID, minmax, '[[Equipment status::Normal]][[Is Fate::false]]')
	elseif control == '-m' then -- Non-Fate Mood
		limit_f = ask_minmax(quality, slot, pageID, minmax, '[[Equipment status::Mood]][[Is Fate::true]]')
		limit_nf = ask_minmax(quality, slot, pageID, minmax, '[[Equipment status::Mood]][[Is Fate::false]]')
	elseif control == 'f-' then -- Fate non-Mood
		limit_nf = ask_minmax(quality, slot, pageID, minmax, '[[Equipment status::Normal]]')
	elseif control == 'fm' then -- Fate Mood (please no)
		limit_nf = ask_minmax(quality, slot, pageID, minmax, '[[Equipment status::Mood]]')
	end
	
	out_bis = msg_logic(value, limit_f, limit_nf, control, mood_count)
	
	if out_bis ~= '' then
		out_bis = out_bis .. ' item'
	end
	
	return out_bis
end

function bis_msg_bdr(total_bdr, slot, pageID, control, state)
	if state ~= "Normal" then
		return ''	
	end
	
	local out_bdr = ''
	if total_bdr > 0 then minmax = 'desc'
	elseif total_bdr < 0 then minmax = 'asc'
  	else return '' end
	local limit_f = 0
	local limit_nf = 0
	
	if string.sub(control,1,1) == 'f' then
		limit_nf = ask_minmax_bdr(slot, minmax, '')
	elseif string.sub(control,1,1) == '-' then
		limit_f = ask_minmax_bdr(slot, minmax, '[[Is Fate::true]]')
		limit_nf = ask_minmax_bdr(slot, minmax, '[[Is Fate::false]]')
	end
	
	out_bdr = msg_logic(total_bdr, limit_f, limit_nf, control, 0)
	
	if out_bdr ~= '' then
		out_bdr = out_bdr .. ' [[BDR]] item'
	end
	
	return out_bdr
end

-- Original 'smw_set' function by [[User:Asarta]]
function smw_set(quality, value, bis, menace)
	if quality and value then
		local propertyname = "Has effect"
		if menace then propertyname = "Has gain effect" end
		mw.smw.set({[propertyname] = quality .. ";" .. value})
		if bis ~= '' then
			local bis = bis.gsub(bis, "%[%[Mood%]%]", "Mood")
			mw.smw.set({["Is Best in Slot"] = quality .. ";" .. bis})
		end
	end
end

-- Original 'quality_from_effect' function and multiple effect sorting by [[User:Adnoam]]
function quality_from_effect(effect)
    local quality, sign, value = string.match(effect, "%[%[File:.*link=([^%|]*)%|alt=%]%] %[%[%1%]%]%s+([%+-]?)%D*(%d+)")
    value = tonumber(value)
    local menace = string.match(effect, "^Reduces") or string.match(effect, "^Increases")
    if sign == '-' or menace == 'Reduces' then
        value = value * -1
    end
    return quality, value, menace
end

function p.effects(frame)
	targs = frame:getParent().args
	list = {}
	slot = frame.args.slot
	out = ''
	ask = nil
	control = ''
	total_bdr = 0
	has_bis = false
	if frame.args.fate ~= 'no' then control = control .. 'f' else control = control .. '-'  end
	if frame.args.state == 'Mood' then control = control .. 'm' else control = control .. '-' end
	
	pageID = 0
	title = mw.title.getCurrentTitle()
	ask = mw.smw.ask{'[[' .. title.prefixedText .. ']]','?Page ID=',mainlabel = '-'}
	if ask ~= nil then pageID = ask[1][1] end
	
	for k, v in pairs(targs) do
        num = string.match(k, "^Effects(%d+)$")
        if (num) then
            table.insert(list, tonumber(num))
        end
	end
	
	bdr = {
		["Bizarre"] = true,
		["Dreaded"] = true,
       	["Respectable"] = true
    }
	
	for _, num in ipairs(list) do
    	local effect = targs["Effects" .. num]
  	    local quality, value, menace = quality_from_effect(effect)
  	    
  	    if (bdr[quality]) then
            total_bdr = total_bdr + value
        end
  	    
  	    minmax = ''
  	    if value > 0 then minmax = 'desc'
  	    elseif value < 0 then minmax = 'asc' end
		
		if menace then
			value = value .. '% gain'
		end
		
		out = out .. '*' .. effect
		out_bis = ''
		-- don't bother with BiS for menaces, there's only one positive and one negative value
		if not menace then
			out_bis = bis_msg(value, quality, slot, pageID, minmax, control, frame.args.state)
			if out_bis ~= '' then
				has_bis = true
				out = out .. ' (' .. out_bis .. ')'
			end
		end
		
		smw_set(quality, value, out_bis, menace)
		
		out = out .. '[[Category:' .. quality .. ' Items]]\n'
	end
	
	if total_bdr ~= 0 then
		out_bdr = bis_msg_bdr(total_bdr, slot, pageID, control, frame.args.state)
		if out_bdr ~= '' then
			out = out .. ':* '
			if has_bis then out = out .. 'Also: ' end
			out = out .. out_bdr
			out = out .. '\n'
		end
	end
	
	return out
end

return p