FANDOM


-- <nowiki>
--
-- Implements {{exchange}} and various other templates
--
-- Essentially a replacement for {{Exchange:<item>|view=<view>}}
--
 
local p = {}
 
local addcommas = require( 'Module:Addcommas' )._commas
local yesno = require( 'Module:Yesno' )
local timeago = require( 'Module:Timeago' )
 
--
-- Just a mw.loadData wrapper
--
local function loadData( item )
 
    local page = 'Module:Exchange/' .. item
    return mw.loadData( page )
 
end
 
--
-- Returns the icon of the item with a link to the item
--
-- If the icon doesn't exist, uses [[File:Coins 1000.png]] instead
--
local function icon( item )
 
     local icon = loadData( item ).icon
     local file = icon or 'Coins 1000.png'
 
     return '[[File:' .. file .. '|link=' .. item ..']]'
 
end
 
--
-- Returns difference between current price and previous price
--
-- If either don't exist then returns 0
--
local function diff( item )
 
    local data = loadData( item )
 
    if data.price and data.last then
        return tostring( data.price - data.last )
    end
 
    return '0'
 
end
 
--
-- Implements {{GEItem}}
--
local function table_( item )
 
    local data = loadData( item )
    local tag = mw.html.create( 'tr' )
 
    tag
        -- icon
        :tag( 'td' )
            :wikitext( icon( item ) )
            :done()
        -- link
        :tag( 'td' )
            :css( {
                ['width'] = '15%',
                ['text-align'] = 'left'
            } )
            :wikitext( '[[' .. item .. ']]' )
            :done()
        -- price
        :tag( 'td' )
            :wikitext( addCommas( data.price ) )
            :done()
 
    local text
 
    if data.last then
 
        -- calc change per day here
        local change -- absolute change
        link = 'http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=' .. data.item_id
        local arrow
 
        if abs( data['price'] - data['last'] ) > 0 then
 
            if data.price > data.last then
                arrow = '[[File:Up.png|20px|link=' .. link .. ']]'
            elseif data.price < data.last then
                arrow = '[[File:Down.png|20px|link=' .. link .. ']]'
            end
 
            if change > 0.04 then
                text = arrow .. arrow .. arrow
            elseif change > 0.02 then
                text = arrow .. arrow
            else
                text = arrow
            end
 
        else
            text = '[[File:Unchg.png|40px|link=' .. link .. ']]'
        end
 
        tag
            :tag( 'td' )
                :tag( 'div' )
                    :wikitext( text )
                    :done()
                :done()
 
    end
 
    if data.alchable ~= nil and not yesno( data.alchable ) then
        tag
            :tag( 'td' )
                :attr( 'colspan', '2' )
                :tag( 'i' )
                    :wikitext( 'Cannot be alchemised' )
                    :done()
                :done()
    else
        tag
            :tag( 'td' )
                :wikitext( data.value and addcommas( floor( data.value * 0.4 ) ) or '\'\'Unknown\'\'' )
                :done()
            :tag( 'td' )
                :wikitext( data.value and addcommas( floor( data.value * 0.6 ) ) or '\'\'Unknown\'\'' )
                :done()
    end
 
    tag
        :tag( 'td' )
            :wikitext( data.limit and addcommas( data.limit ) or '\'\'Unknown\'\'' )
            :done()
        :tag( 'td' )
            :wikitext(
                data.members == nil and '\'\'Unknown\'\'' or
                yesno( data.members ) and '[[File:P2P icon.png|30px|link=Pay-to-play]]' or
                '[[File:F2P icon.png|30px|link=Free-to-play]]'
            )
            :done()
        :tag( 'td' )
            :css( 'white-space', 'nowrap' )
            -- @todo remove graph link when data is moved to module ns
            :wikitext( '[[:Exchange:' .. item .. '|view]] &bull; [[:Exchange:' .. item .. '/Data|graph]]' )
        :tag( 'td' )
            :css( 'font-size', '85%' )
            :wikitext( timeago( data['date'] ) )
            :done()
 
    return tostring( tag )
 
end
 
-- for use on exchange pages
local function page( item )
 
end
 
-- for accessing exchange data
local function data( item )
 
    local data = loadData( item )
    local excg_data = loadData( item .. '/data' )
    local frame = mw.getCurrentFrame()
    local ns = frame:preprocess( '{{NAMESPACE}}' )
    local pagename = frame:preprocess( '{{PAGENAME}}' )
 
    if mw.ustring.sub( pagename, -5 ) == '/Data' and ns == 'Exchange' then
        mw.html.create( 'div' )
            :attr( 'id', 'contentSub' )
            :css( {
                ['line-height'] = '0.8em',
                ['margin'] = '-0.75em 0 1.5em 1em'
            } )
            :tag( 'span' )
                :addClass( 'subpages' )
                -- strips '/Data'
                :wikitext( frame:preprocess( '&lt; [[{{NAMESPACE}}:{{#sub:{{PAGENAME}}|0|-5}}]]' )
                :done()
            :done()
 
    if size ~= 'data' then
        local tag = mw.html.create( 'div' )
            :addClass( 'GEChartBox' )
 
        if moreItems then
            tag
                :tag( 'div' )
                    :addClass( 'addGEChartItems' )
                    :css( 'display', 'none' )
                    :wikitext( moreItems )
                    :done()
        end
 
        -- checks we're not on an exchange ns subpage
        if not excg_subpage then
            tag
                :tag( 'div' )
                    :css( {
                        border = '1px solid #666',
                        ['padding-left'] = '1px'
                    } )
                    :addClass( 'GEdatachart' )
                    :cssText( size == 'small' and 'margin:5px; float:left; width:258px; height:200px;' or 'margin:5px 0 25px 0; min-width:600px; height:500px;' )
                    :addClass( size == 'small' and 'smallChart' or '' )
                    :tag( 'div' )
                        :css( {
                            ['text-align'] = 'center',
                            ['font-size'] = '11px',
                            color = '#666'
                        } )
                        :wikitext( 'Loading...' )
                        :done()
                    :done()
        end
    end
 
    mw.html.create( 'p' )
        :addClass( 'GEdataprices' )
        :attr( 'data-item', name )
        :css( 'display', 'none' )
        :wikitext( args[1] )
    if name then
        -- http://runescape.wikia.com/wiki/Category:GEMW_data see indexes
        if name not contains 'Index' then
            :attr( {
                ['data-value'] =  loadData( item ).value,
                ['data-limit'] =  loadData( item ).limit
            } )
        end
    end
 
 
{{#ifeq:{{{size|}}}|data||</div>}}<!--
-->{{#ifeq:{{#sub:{{PAGENAME}}|-5}}|/Data|<!--
-->{{#ifeq:{{NAMESPACE}}|Exchange|[[Category:GEMW data|{{{name|}}}]]}}<!--
-->{{#if:{{#pos:{{{name|}}}|Index}}|[[Category:GEMW data|*]]}}<!--
-->{{Excgonly|{{#ifeq:{{#replace:{{{name}}}|'|&#39;}}|{{#sub:{{PAGENAME}}|0|-5}}||{{#ifeq:{{#replace:{{{name}}}|&|&#38;}}|{{#sub:{{PAGENAME}}|0|-5}}||[[Category:Exchange names that needs checking]]}}}}}}<!--
-->}}
 
 
end
 
function p.exchange( frame )
 
    -- for when a template defaults to a certain view
    local frame_args = frame.args
    -- for {{exchange}}
    local args = frame:getParent().args
 
    local view = frame_args['View'] or frame_args['view'] or args['View'] or args['view'] or 'price'
    local item = args[1]
 
    -- these views do more than just returning a simple value
    local funcs = {
        ['icon'] = icon,
        ['diff'] = diff,
        ['table'] = table_,
        ['page'] = page
    }
 
    view = mw.ustring.lower( view )
 
    if not funcs[view] then
        return loadData( item )[view]
    end
 
    return funcs[view]( item )
 
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.