Module:Items

From Portals of Phereon Wiki

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

local p = {};
local cargo = mw.ext.cargo;
local utils = require( "Module:Utils" );

function p.items(frame)
    local tf_r = {
    	['0'] = ' data-sort-value="0" |Common',
    	['1'] = ' data-sort-value="1" |Rare',
    	['2'] = ' data-sort-value="2" |Legendary',
    	['3'] = ' data-sort-value="3" |Special',
    	['4'] = ' data-sort-value="4" |Mythical'
    };
    setmetatable(tf_r,{__index = function(tab, index) return ' data-sort-value="5" |Unknown' end});
    local tf_at = {
    	['0'] = 'None',
    	['1'] = 'Basic',
    	['2'] = 'Cleave',
    	['3'] = 'Swing',
    	['4'] = 'Water',
    	['5'] = 'Unable',
    	['6'] = 'Ranged'
    };
    local unknw = function(tab, index) return 'Unknown' end;
    setmetatable(tf_at,{__index = unknw});
    local tf_dt = {
    	['0'] = 'None',
    	['1'] = 'Basic',
    	['2'] = 'Slashing',
    	['3'] = 'Fire',
    	['4'] = 'Water',
    	['5'] = 'Ice',
    	['6'] = 'Electric',
    	['7'] = 'Poison',
    	['8'] = 'Bleeding',
    	['9'] = 'True',
    	['10'] = 'Light',
    	['11'] = 'Shadow'
    };
    setmetatable(tf_dt,{__index = unknw});
    local tf_t = {
    	['1'] = 'Weapon',
    	['2'] = 'Armor',
    	['3'] = 'Misc',
    	['4'] = 'Consumable'
    };
    setmetatable(tf_t,{__index = unknw});
    local tf_use = {
    	['111'] = 'Everywhere',
    	['110'] = 'Overworld and Town',
    	['101'] = 'Overworld and Battle',
    	['100'] = 'Overworld Only',
    	['011'] = 'Town and Battle',
    	['010'] = 'Town Only',
    	['001'] = 'Battle Only',
    	['000'] = 'Nowhere'
    };
    setmetatable(tf_use,{__index = unknw});
    local temp_res = {};
	local fstr = utils.toNumberedTable(frame.args[2]);
    local results = cargo.query(frame.args[1], utils.trimBrackets(frame.args[3]), utils.toTable(utils.trimBrackets(frame.args[4])));
    for _, result in ipairs(results) do
    	local temp_item = {'|-\n'};
        for r, fv in ipairs( fstr ) do
		    -- cargo through scribunto seems to not allow query using integer as alias (can be done in SQL, but cast or convert are not understood by cargo)
		    -- the idea behind the integer as alias was to use args[2] as a way to reorganised the queried table
		    -- since using integer does not work, I arbitrarily decide to add a 'i' (for index) in front of the said index
		    local v = result['i' .. tostring(r)];
		    temp_item[#temp_item+1] = '|';
		    if (v ~= nil) then
		    if fv == 'name' then
		    	temp_item[#temp_item+1] = ' id="' .. v .. '" |' .. v;
		    elseif fv == 'text' then
		    	temp_item[#temp_item+1] = v;
		    elseif fv == 'rarity' then
		    	temp_item[#temp_item+1] = tf_r[v];
		    elseif fv == 'atkType' then
		    	temp_item[#temp_item+1] = tf_at[v];
		    elseif fv == 'damageType' then
		    	temp_item[#temp_item+1] = tf_dt[v];
		    elseif fv == 'mod' then	
	     		local modif = utils.toNumberedTable(v);
	     		if modif[1] ~= '0' then
	     			temp_item[#temp_item+1] = 'HP:&nbsp;' .. modif[1] .. '<br>';
	     		end if modif[2] ~= '0' then
	     			temp_item[#temp_item+1] = 'Mana:&nbsp;' .. modif[2] .. '<br>';
	     		end if modif[3] ~= '0' then
	     			temp_item[#temp_item+1] = 'Speed:&nbsp;' .. modif[3] .. '<br>';
	     		end if modif[4] ~= '0' then
	     			temp_item[#temp_item+1] = 'Strength:&nbsp;' .. modif[4] .. '<br>';
	     		end if modif[5] ~= '0' then
	     			temp_item[#temp_item+1] = 'Armor:&nbsp;' .. modif[5] .. '<br>';
	     		end if modif[6] ~= '0' then
	     			temp_item[#temp_item+1] = 'Lust damage:&nbsp;' .. modif[6] .. '<br>';
	     		end if modif[7] ~= '0' then
	     			temp_item[#temp_item+1] = 'Lust resistance:&nbsp;' .. modif[7] .. '<br>';
	     		end if modif[8] ~= '0' then
 					temp_item[#temp_item+1] = 'Magic strength:&nbsp;' .. modif[8] .. '<br>';
	     		end
		    elseif fv == 'aSkill' then
     	 		local aSkill = utils.toNumberedTable(v);
     	 		if aSkill[1] ~= '' then
	     			temp_item[#temp_item+1] = aSkill[1] .. '<br>';
	     		end if aSkill[2] ~= '' then
	     			temp_item[#temp_item+1] = 'Range:&nbsp;' .. aSkill[2] .. ' ';
	     		end if aSkill[3] ~= '' then
	     			temp_item[#temp_item+1] = 'Mana cost:&nbsp;' .. aSkill[3] .. ' ';
	     		end if aSkill[4] ~= '' then
	     			temp_item[#temp_item+1] = 'Ap cost:&nbsp;' .. aSkill[4] .. ' ';
	     		end if aSkill[5] ~= '' then
	     			temp_item[#temp_item+1] = 'Uses:&nbsp;' .. aSkill[5] .. ' ';
	     		end if aSkill[6] ~= '' then
	     			temp_item[#temp_item+1] = string.gsub(aSkill[6],"&lt;br&gt;",'<br>');
	     		end
	     	elseif fv == 'material' then
     	 		local materials = utils.toNumberedTable(v);
     	 		if materials[1] ~= '' and materials[2] ~= '' then
     	 			local material = utils.toNumberedTable(materials[1],',');
     	 			local count = utils.toNumberedTable(materials[2],',');
     	 			local prev_material = nil;
     	 			for i=1,#material do 
     	 				if (material[i] ~= nil and count[i] ~= nil and prev_material ~= material[i]) then
     	 					prev_material = material[i];
     	 					temp_item[#temp_item+1] = 'x'..count[i]..' '..material[i]..' ';
     	 				end
     	 			end
     	 		end
		    elseif fv == 'use' then
		    	temp_item[#temp_item+1] = tf_use[v];
		    elseif fv == 'type' then
		    	temp_item[#temp_item+1] = tf_t[v];
			else
				temp_item[#temp_item+1] = v;
		    end
		    end
			temp_item[#temp_item+1] = '\n';
        end
        temp_res[#temp_res+1] = table.concat(temp_item,"");
    end
    local restbl = table.concat(temp_res,"");
    return restbl;
end

return p