Module:ExchangeDefault

-- -- Implements -- -- @todo everything

local p = {}

-- enables pulling exchange data from module ns local luaExcg = false

-- imports local excgdata = require( 'Module:ExchangeData' )._main local round = require( 'Module:Number' )._round local timeago = require( 'Module:TimeAgo' )._ago local yesno = require( 'Module:Yesno' )

-- temp fixes for exchange data being in strings local function tonum( x ) x = tostring( x ) x = x:gsub( ',', '' ) return tonumber( x ) end

function p.main( frame ) local args = frame:getParent.args local lang = mw.language.getContentLanguage local title = mw.title.getCurrentTitle

local item = args.item or args.Item local data = {}

if type( item ) == 'string' then item = mw.text.trim( item ) end

if item == '' or item == nil then error( '"item" argument is not specified', 0 ) end

-- load data from module ns if enabled if luaExcg then item = mw.text.split( item, '' ) item[1] = mw.ustring.upper( item[1] ) item = table.concat( item, '' ) data = mw.loadData( 'Module:Exchange/' .. item ) else -- map arg names to lower case for k, v in pairs( args ) do           if v ~= '' then k = mw.text.split( k, '' ) k[1] = mw.ustring.lower( k[1] ) k = table.concat( k, '' ) data[k] = v           end end

data.price = data.price and tonum( data.price ) data.last = data.last and tonum( data.last ) data.volume = data.volume and tonum( data.volume ) data.limit = data.limit and tonum( data.limit ) data.value = data.value and tonum( data.value ) end

-- set variables here if possible to keep table building easier to follow local rowspan = 8 local volDate = data.volumeDate or lang:formatDate( 'c' ) local dateDiffZ = lang:formatDate( 'z' ) - lang:formatDate( 'z', volDate ) local dateDiffY = lang:formatDate( 'Y' ) - lang:formatDate( 'Y', volDate ) local price = data.price and lang:formatNum( data.price ) or Unknown local date = Unknown local lowAlch = Not alchable local highAlch = Not alchable local memsIcon = '' local usage = ''

if data.volume and ( dateDiffZ + 365 * dateDiffY ) <= 7 then rowspan = 10 end

if data.date then date = lang:formatDate( 'j F Y, H:i "(UTC)"', data.date ) end

if data.value and ( data.alchable == nil or yesno( data.alchable ) ) then lowAlch = lang:formatNum( math.floor( data.value * 0.4 ) ) highAlch = lang:formatNum( math.floor( data.value * 0.6 ) ) end

if data.members ~= nil then if data.members then memsIcon = '' else memsIcon = '' end end

if luaExcg then -- workaround so we can use `table.concat` usage = {}

for k, v in data.usage do          usage[k] = v        end

usage = '\n* ' .. table.concat( usage, '\n* ' ) .. '' end

-- build table local tbl = mw.html.create( 'table' ) :addClass( 'wikitable' ) :css( {           ['border'] = '0',            ['border-collapse'] = 'separate',            ['margin-top'] = '-1em'        } ) :tag( 'tr' ) :tag( 'th' ) :attr( 'colspan', '2' ) :css( {                   border = '0',                    padding = '0'                } ) :tag( 'table' ) :css( {                       width = '100%',                        ['border-collapse'] = 'collapse'                    } ) :tag( 'tr' ) :tag( 'td' ) :css( {                               padding = '8px 16px',                                ['border-right'] = '0'                            } ) -- @todo implement p._icon here (from Module:Exchange) :wikitext( '' ) :done :tag( 'td' ) :css( {                               width = '100%',                                ['font-size'] = '135%',                                ['text-align'] = 'left',                                ['white-space'] = 'normal',                                padding = '8px 0',                                ['border-left'] = '0',                                ['border-right'] = '0'                            } ) -- this used to default to Grand Exchange Market Watch if item wasn't supplied -- but that only happened on 6 user pages, an orphanage and a preload :wikitext(  .. item ..  ) :tag( 'br' ) :done :tag( 'span' ) :css( {                                   ['font-weight'] = 'normal',                                    ['font-size'] = '75%'                                } ) :wikitext( data.examine or '' ) :done :done :tag( 'td' ) :css( {                               padding = '8px 16px',                                ['border-left'] = '0'                            } ) :wikitext( memsIcon ) :done :done :done :done :done :tag( 'tr' ) :tag( 'th' ) :css( {                   ['text-align'] = 'left',                    ['padding-left'] = '10px'                } ) :wikitext( 'Price data' ) :done :tag( 'td' ) :attr( 'rowspan', rowspan ) :css( 'vertical-align', 'top' ) :tag( 'div' ) :addClass( 'GEPage' ) :css( {                       padding = '10px 5px 0 5px',                        float = 'right'                    } ) :tag( 'div' ) :addClass( 'GEdatachart' ) :css( {                           margin = '5px 0 25px 0',                            ['min-width'] = '700px',                            ['height'] = '500px'                        } ) -- GE Chart goes here :done :done :done :done :tag( 'tr' ) :tag( 'td' ) :css( {                   ['vertical-align'] = 'top',                    ['padding-bottom'] = '5px'                } ) :tag( 'ul' ) :tag( 'li' ) :wikitext( 'Price: ' ) :tag( 'span' ) :attr( 'id', 'GEPrice' ) :wikitext( price ) :done :done :tag( 'li' ) :wikitext( 'Last updated: ' .. timeago{data.date, purge='yes'} ) :tag( 'br' ) :done :tag( 'span' ) :attr( 'id', 'GEDate' ) :wikitext( date ) :done :done :done :done :done if data.volume and ( dateDiffZ + 365 * dateDiffY ) <= 7 then tbl :tag( 'tr' ) :tag( 'th' ) :css( {                       ['text-align'] = 'left',                        ['padding-left'] = '10px'                    } ) :wikitext( 'Volume data' ) :done :done :tag( 'tr' ) :tag( 'td' ) :attr( 'id', 'volumeData' ) :css ( {                       ['vertical-align'] = 'top',                        ['padding-bottom'] = '5px'                    } ) :tag( 'ul' ) :tag( 'li' ) :wikitext( '7-day total: ' ) :tag( 'span' ) :attr( 'id', 'GEVolume' ) :wikitext( lang:formatNum( 1000000 * data.volume ) ) :done :done :tag( 'li' ) :wikitext( '1-day average ' .. lang:formatNum( round( 1000000 / 7 * data.volume, 3 ) ) ) :done :tag( 'li' ) :wikitext( 'Last updated: ' .. timeago{data.volumeDate, purge='yes'} ) :tag( 'br' ) :done :tag( 'span' ) :attr( 'id', 'GEVolumeDate' ) :wikitext( lang:formatDate( 'j F Y, H:i "(UTC)"', data.volumeDate ) ) :done :done :done :done end tbl :tag( 'tr' ) :tag( 'th' ) :css( {                   ['text-align'] = 'left',                    ['padding-left'] = '10px'                } ) :wikitext( 'Other details' ) :done :done :tag( 'tr' ) :tag( 'td' ) :css( {                   ['vertical-align'] = 'top',                    ['padding-bottom'] = '5px'                } ) :tag( 'ul' ) :tag( 'li' ) :wikitext( 'Exchange ID: ' ) :tag( 'span' ) :attr( 'id', 'GEDID' ) :wikitext( data.itemId or Unknown ) :done :done :tag( 'li' ) :wikitext( 'Low Alchemy: ' .. lowAlch ) :done :tag( 'li' ) :wikitext( 'High Alchemy: ' .. highAlch ) :done :tag( 'li' ) :wikitext( 'Value: ' .. ( data.value and lang:formatNum( data.value ) or Unknown ) ) :done :tag( 'li' ) :wikitext( 'Exchange limit: ' .. ( data.limit and lang:formatNum( data.limit ) or Unknown ) ) :done :tag( 'li' ) :wikitext( 'GED Category: ' .. data.category or Unknown ) :done :done :done :done :tag( 'tr' ) :tag( 'th' ) :css( {                   ['text-align'] = 'left',                    ['padding-left'] = '10px'                } ) :wikitext( 'External links' ) :done :done :tag( 'tr' ) :tag( 'td' ) :css( {                   ['vertical-align'] = 'top',                    ['padding-bottom'] = '5px'                } ) :tag( 'ul' ) :tag( 'li' ) :wikitext( '.. data.itemId .. ' Lookup current price' ) :done :tag( 'li' ) :wikitext( '.. mw.uri.encode( data.item, 'QUERY' ) .. ' Search for relevant prices' ) :done :done :done :done :tag( 'tr' ) :tag( 'th' ) :css( {                   ['text-align'] = 'left',                    ['padding-left'] = '10px'                } ) :wikitext( 'Usage' ) :done :done :tag( 'tr' ) :tag( 'td' ) :css( {                   ['vertical-align'] = 'top',                    ['padding-left'] = '5px',                    ['padding-bottom'] = '5px'                } ) :wikitext( 'This item is used in:' .. usage ) :done :done

tbl = tostring( tbl )

if not luaExcg then -- removes  so usage appears in table tbl = mw.ustring.sub( tbl, 1, -19 ) end

-- categories local cats = '' local natPrice

if title.nsText == 'Exchange' then cats = cats ..                    ..                     .. ''

if data.item ~= title.text then cats = cats .. ''       end

if data.item then -- have to preprocess this for it to work cats = cats .. frame:preprocess( '' ) end

if not data.itemId then cats = cats .. ''       end

if not data.icon then cats = cats .. ''       end

if data.price and data.value and ( data.alchable == nil or yesno( data.alchable ) ) then if luaExcg then -- @todo test this natPrice = load( 'Nature rune' ).price elseif mw.ustring.lower( item ) == 'nature rune' then natPrice = data.price else -- @todo If Exchange:Nature rune uses is causes an error somewhere --      No idea why, but ideally it should be figured out soon^TM natPrice = tonumber( frame:preprocess( '' ) ) end

if ( ( data.value * 0.6 ) - data.price - natPrice ) > 0 then cats = cats .. ''           end

if ( ( data.value * 0.4 ) - data.price - natPrice ) > 0 then cats = cats .. ''           end

if ( ( data.value * 0.3 ) - data.price ) > 0 then cats = cats .. ''           end

else cats = cats .. ''       end

-- this doesn't allow for the update time to be in the previous day but less than 24 hours -- if that's the case, then the page will end up in the 7 days category -- the same will happen for the other 2 categories too -- but it's not so noticeable given the already elapsed time local lastUpdate = ( lang:formatDate( 'z' ) - lang:formatDate( 'z', data.date ) + 365 * ( lang:formatDate( 'Y' ) - lang:formatDate( 'Y', data.date ) ) + 3 ) / 7 lastUpdate = round( lastUpdate, 0 ) if lastUpdate == 1 then cats = cats .. ''       elseif lastUpdate >= 2 and lastUpdate <= 4 then cats = cats .. ''       elseif lastUpdate > 4 then cats = cats .. ''       end

if not data.limit then cats = cats .. ''       end

if not data.category then cats = cats .. ''       end end

-- help header local help = mw.html.create( 'div' ) :attr( 'id', 'gemw_guide' ) :css( {           padding = '5px 10px',            color = '#000',            border = '1px solid #aaa',            background = '#f2f2f2'        } ) :wikitext( 'If you need assistance on editing this page, or trying to understand how these Exchange pages work, please add a comment to Talk:Grand Exchange Market Watch.' )

help = tostring( help )

-- load data if lua exchange is enabled local data = ''

if luaExcg then local excgdata = require( 'Module:ExchangeData' )._main data = excgdata( { name = item } ) end return help .. ' ' .. tbl .. data .. cats end

return p