<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.runerealm.org/index.php?action=history&amp;feed=atom&amp;title=Module%3AFetchItemStats</id>
	<title>Module:FetchItemStats - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.runerealm.org/index.php?action=history&amp;feed=atom&amp;title=Module%3AFetchItemStats"/>
	<link rel="alternate" type="text/html" href="https://wiki.runerealm.org/index.php?title=Module:FetchItemStats&amp;action=history"/>
	<updated>2026-04-30T09:20:47Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://wiki.runerealm.org/index.php?title=Module:FetchItemStats&amp;diff=620&amp;oldid=prev</id>
		<title>Eq: Created page with &quot;-- Note: This module is not meant to be invoked directly, and instead is intended to be used from other modules. local exchange = require(&#039;Module:Exchange&#039;) local yesno = require(&#039;Module:Yesno&#039;)  local p = {}  -- Obtain information about which version of an item is the default version of the item -- if no switch infoobox is present, the item name itself will be returned. -- The second return value is the actual pagename of the page that is retrieved in the SMW call, taki...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.runerealm.org/index.php?title=Module:FetchItemStats&amp;diff=620&amp;oldid=prev"/>
		<updated>2024-10-12T12:38:02Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;-- Note: This module is not meant to be invoked directly, and instead is intended to be used from other modules. local exchange = require(&amp;#039;Module:Exchange&amp;#039;) local yesno = require(&amp;#039;Module:Yesno&amp;#039;)  local p = {}  -- Obtain information about which version of an item is the default version of the item -- if no switch infoobox is present, the item name itself will be returned. -- The second return value is the actual pagename of the page that is retrieved in the SMW call, taki...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- Note: This module is not meant to be invoked directly, and instead is intended to be used from other modules.&lt;br /&gt;
local exchange = require(&amp;#039;Module:Exchange&amp;#039;)&lt;br /&gt;
local yesno = require(&amp;#039;Module:Yesno&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
-- Obtain information about which version of an item is the default version of the item&lt;br /&gt;
-- if no switch infoobox is present, the item name itself will be returned.&lt;br /&gt;
-- The second return value is the actual pagename of the page that is retrieved in the SMW call, taking redirects into account.&lt;br /&gt;
function p.defaultVersion(itemname)&lt;br /&gt;
	if not itemname or itemname == &amp;#039;&amp;#039; then&lt;br /&gt;
		return nil&lt;br /&gt;
	end&lt;br /&gt;
	local item = mw.smw.ask({&amp;quot;[[&amp;quot; .. itemname .. &amp;quot;]]&amp;quot;, &amp;quot;?Default version&amp;quot;})&lt;br /&gt;
	if not item then&lt;br /&gt;
		return nil&lt;br /&gt;
	end&lt;br /&gt;
	item = item[1]&lt;br /&gt;
	local defver = item[&amp;#039;Default version&amp;#039;]&lt;br /&gt;
	if defver then&lt;br /&gt;
		return itemname .. &amp;#039;#&amp;#039; .. defver, item[1]&lt;br /&gt;
	else&lt;br /&gt;
		return itemname, item[1]&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Get item value from its page based on name.&lt;br /&gt;
-- If a switch infobox is present on the entered page, the price for the item version that is shown by default is fetched.&lt;br /&gt;
function p.value(itemname)&lt;br /&gt;
	if not itemname or itemname == &amp;#039;&amp;#039; then&lt;br /&gt;
		return 0&lt;br /&gt;
	end&lt;br /&gt;
	local item = mw.smw.ask({&amp;quot;[[&amp;quot; .. itemname .. &amp;quot;]]&amp;quot;, &amp;quot;?Default version&amp;quot;, &amp;quot;?Value&amp;quot;})&lt;br /&gt;
	if not item then&lt;br /&gt;
		return -1&lt;br /&gt;
	end&lt;br /&gt;
	item = item[1]&lt;br /&gt;
	local value = 0&lt;br /&gt;
	if item[&amp;#039;Value&amp;#039;] == nil then&lt;br /&gt;
	    local itemversion = item[&amp;#039;Default version&amp;#039;]&lt;br /&gt;
	    if not itemversion then&lt;br /&gt;
	    	return 0&lt;br /&gt;
	    end&lt;br /&gt;
	    local default = mw.smw.ask({&amp;quot;[[&amp;quot; .. itemname .. &amp;quot;#&amp;quot; .. itemversion .. &amp;quot;]]&amp;quot;, &amp;quot;?Value&amp;quot;})[1]&lt;br /&gt;
	    if default[&amp;#039;Value&amp;#039;] ~= nil then&lt;br /&gt;
	        value = default[&amp;#039;Value&amp;#039;]&lt;br /&gt;
	    end&lt;br /&gt;
	else&lt;br /&gt;
	    value = item[&amp;#039;Value&amp;#039;]&lt;br /&gt;
	end&lt;br /&gt;
	return value&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function undoSort(pages, statslist)&lt;br /&gt;
	local lookup = {}&lt;br /&gt;
	for i, page in ipairs(pages) do&lt;br /&gt;
		lookup[string.lower(page)] = i&lt;br /&gt;
	end&lt;br /&gt;
	local ret = {}&lt;br /&gt;
	for _, stats in ipairs(statslist) do&lt;br /&gt;
		local i = lookup[string.lower(stats[1])]&lt;br /&gt;
		-- if i==nil then an item was referred to by their redirect page; cannot undo sorting&lt;br /&gt;
		assert(i ~= nil, string.format(&amp;quot;Cannot use `sort=no` for redirect pages. Ensure the item \&amp;quot;%s\&amp;quot; is a direct link to the page&amp;quot;, stats[1]))&lt;br /&gt;
		ret[i] = stats&lt;br /&gt;
	end&lt;br /&gt;
	return ret&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Fetches equipment stats, image, and weight for a list of pages. &lt;br /&gt;
-- First argument: a list of pages&lt;br /&gt;
-- Second argument: the sort keys for the returned data (array of keys); if equal to {&amp;#039;no&amp;#039;} the order used in the first parameter is used.&lt;br /&gt;
-- Third argument: the order in which these sort keys should be (asc / desc / reverse / random)&lt;br /&gt;
function p.equipmentStats(pages, keys, orders)&lt;br /&gt;
	-- mw.logObject(keys)&lt;br /&gt;
	if keys == nil then&lt;br /&gt;
		keys = {}&lt;br /&gt;
		orders = {}&lt;br /&gt;
	elseif orders == nil then&lt;br /&gt;
		orders = {}&lt;br /&gt;
	end&lt;br /&gt;
    local prop = {&lt;br /&gt;
        astab  = &amp;#039;Stab attack bonus&amp;#039;,&lt;br /&gt;
        aslash = &amp;#039;Slash attack bonus&amp;#039;,&lt;br /&gt;
        acrush = &amp;#039;Crush attack bonus&amp;#039;,&lt;br /&gt;
        amagic = &amp;#039;Magic attack bonus&amp;#039;,&lt;br /&gt;
        arange = &amp;#039;Range attack bonus&amp;#039;,&lt;br /&gt;
        dstab  = &amp;#039;Stab defence bonus&amp;#039;,&lt;br /&gt;
        dslash = &amp;#039;Slash defence bonus&amp;#039;,&lt;br /&gt;
        dcrush = &amp;#039;Crush defence bonus&amp;#039;,&lt;br /&gt;
        dmagic = &amp;#039;Magic defence bonus&amp;#039;,&lt;br /&gt;
        drange = &amp;#039;Range defence bonus&amp;#039;,&lt;br /&gt;
        str    = &amp;#039;Strength bonus&amp;#039;,&lt;br /&gt;
        mdmg   = &amp;#039;Magic Damage bonus&amp;#039;,&lt;br /&gt;
        rstr   = &amp;#039;Ranged Strength bonus&amp;#039;,&lt;br /&gt;
        prayer = &amp;#039;Prayer bonus&amp;#039;,&lt;br /&gt;
        weight = &amp;#039;Weight&amp;#039;,&lt;br /&gt;
        image  = &amp;#039;Image&amp;#039;,&lt;br /&gt;
        subobj = &amp;#039;Has subobject&amp;#039;,&lt;br /&gt;
        membs  = &amp;quot;Is members only&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    local query = {}&lt;br /&gt;
&lt;br /&gt;
    -- Build query&lt;br /&gt;
    table.insert(query, string.format(&amp;#039;[[%s]]&amp;#039;, table.concat(pages, &amp;#039; || &amp;#039;)))&lt;br /&gt;
    table.insert(query, &amp;#039;?=#-&amp;#039;)&lt;br /&gt;
    for k, p in pairs(prop) do&lt;br /&gt;
        table.insert(query, string.format(&amp;#039;?%s #- = %s&amp;#039;, p, k))&lt;br /&gt;
    end&lt;br /&gt;
    if #keys &amp;gt; 0 and yesno(keys[1], true) then -- do not sort if keys[1] is &amp;#039;no&amp;#039;&lt;br /&gt;
        local sprops = {}&lt;br /&gt;
        for _, k in ipairs(keys) do&lt;br /&gt;
            table.insert(sprops, prop[k])&lt;br /&gt;
        end&lt;br /&gt;
        query.sort = table.concat(sprops, &amp;#039;, &amp;#039;)&lt;br /&gt;
    end&lt;br /&gt;
    if #orders &amp;gt; 0 then&lt;br /&gt;
        query.order = table.concat(orders, &amp;#039;, &amp;#039;)&lt;br /&gt;
    end&lt;br /&gt;
    query.offset = 0&lt;br /&gt;
    query.limit = 500&lt;br /&gt;
&lt;br /&gt;
    -- Fetch the data&lt;br /&gt;
    -- mw.logObject(query)&lt;br /&gt;
    local t1 = os.clock()&lt;br /&gt;
    local smwdata = mw.smw.ask(query)&lt;br /&gt;
    local t2 = os.clock()&lt;br /&gt;
&lt;br /&gt;
    local smw_max_conditions = 16&lt;br /&gt;
&lt;br /&gt;
    if not smwdata or #smwdata == 0 then&lt;br /&gt;
        if #pages &amp;gt; smw_max_conditions then&lt;br /&gt;
            error(&amp;#039;smw query failed (your table probably have too many items)&amp;#039;)&lt;br /&gt;
        end&lt;br /&gt;
    &lt;br /&gt;
        smwdata = {}&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Post-process the data&lt;br /&gt;
    if keys[1] and not yesno(keys[1], true) then&lt;br /&gt;
    	smwdata = undoSort(pages, smwdata)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
    local data = {}&lt;br /&gt;
&lt;br /&gt;
    for _, i in ipairs(smwdata) do &lt;br /&gt;
        local dl = i&lt;br /&gt;
        dl.name = dl[1]&lt;br /&gt;
        dl[1] = nil&lt;br /&gt;
        if type(dl.image) == &amp;#039;table&amp;#039; then&lt;br /&gt;
            dl.image = dl.image[1]&lt;br /&gt;
        end&lt;br /&gt;
        table.insert(data, dl)&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- Statistics&lt;br /&gt;
    local smwstats = {&lt;br /&gt;
        offset = query.offset,&lt;br /&gt;
        limit  = query.limit,&lt;br /&gt;
        found  = #smwdata,&lt;br /&gt;
        time   = (t2 - t1) * 1000&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    --[[ mw.log(string.format(&amp;#039;SMW: Found %i, offset %i, limit %i, time elapsed %.3f ms.&amp;#039;, &lt;br /&gt;
        smwstats.found, smwstats.offset, smwstats.limit, smwstats.time))&lt;br /&gt;
        --]]&lt;br /&gt;
&lt;br /&gt;
    return data&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Eq</name></author>
	</entry>
</feed>