Module:Sandbox/Sevto/test

Revision as of 21:23, 4 November 2021 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Sevto/test/doc

-- <pre>
-- {{questdetails}}
--

local p = {}
local yesno = require( 'Module:Yesno' )
local onmain = require('Module:Mainonly').on_main()


function p.details( frame )
    local _args = frame:getParent().args
    local tbl = mw.html.create( 'table' )
        :addClass( 'questdetails plainlinks' )
        -- @todo replace with CSS
        :attr( 'cellspacing',  '3' )
    local title = mw.uri.fullUrl( mw.title.getCurrentTitle().fullText, { action = 'edit' } )
    local unknown = 'Unknown <small>[' .. tostring( title ) .. ' edit]</small>'
    local args = {}
    
    for k, v in pairs(_args) do
        if v ~= '' then
            args[k] = v
        end
    end
        
    local icon = args.icon and '[[File:' .. args.icon .. '|50px|right]]' or ''
    local start = args.start or unknown
    local members = yesno( args.members ) and '[[File:P2P icon.png|30px|link=]] Members only' or
        -- check it's actually set to something false-y
        yesno( args.members ) == false and '[[File:F2P icon.png|30px|link=]] Free to play' or unknown
    local reqs = args.requirements or '* None'
    local items = args.items or '* None'
    local kills = args.kills or '* None'
    local checklist = items
    if items ~= '* None' then
    	checklist = '<div class="lighttable checklist">\n'..items..'</div>' --frame:expandTemplate{ title = 'Checklist', args = { items } }
    end
        
    tbl
        :tag( 'tr' )
            :tag( 'th' )
                :addClass( 'questdetails-header' )
                :wikitext( 'Location' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :wikitext( '[[File:Quest map icon.png|17px|link=]] ' .. icon .. start )
                :done()
            :done()
        :tag( 'tr' )
            :tag( 'th' )
                :addClass( 'questdetails-header' )
                :wikitext( 'Member requirement' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :wikitext( members )
                :done()
            :done()
        :tag( 'tr' )
            :tag( 'th' )
                :addClass( 'questdetails-header' )
                :wikitext( 'Requirements' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :addClass( 'qc-active' )
                :tag( 'i' )
                    :wikitext( 'Skill requirements are not boostable unless marked with a [B] for boostable.' )
                    :done()
                :newline():newline()
                :wikitext( reqs )
                :done()
            :done()
        :tag( 'tr' )
            :tag( 'th' )
                :addClass( 'questdetails-header' )
                :wikitext( 'Items required' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :tag( 'i' )
                    :wikitext( 'Items from the [[tool belt]] are not listed unless they do not work or are not automatically added.' )
                    :done()
                :newline():newline()
                :wikitext( checklist )
                :done()
            :done()
    
    if args.recommended ~= nil and yesno( args.recommended ) ~= false then
        tbl
            :tag( 'tr' )
                :tag( 'th' )
                    :addClass( 'questdetails-header' )
                    :wikitext( 'Recommended' )
                    :done()
                :tag( 'td' )
                    :addClass( 'questdetails-info' )
                    :newline():newline()
                    :wikitext( '<div class="lighttable checklist">\n'..args.recommended..'</div>') -- frame:expandTemplate{ title = 'Checklist', args = { args.recommended } } )
                    :done()
                :done()
    end
    
    tbl
        :tag( 'tr' )
            :tag( 'th' )
                :addClass( 'questdetails-header' )
                :wikitext( 'Enemies to defeat' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :newline():newline()
                :wikitext( kills )
                :done()
            :done()

    return tostring( tbl ) .. smw(frame,_args)
end

function smw(frame,args)
	if not onmain then
		return ''
	end
	-- more to come
	local err = mw.html.create('div')
	err:addClass('hidden'):css('display','none')
	local haserr = false
	local info = ''
	local smwJSON = {
		name = mw.title.getCurrentTitle().text,
		start = args.start or nil,
		requirements = args.requirements or 'None',
		items = args.items or 'None',
		kills = args.kills or 'None',
		members = yesno(args.members,false)
	}
	local jsongood, encsmwJSON = pcall(mw.text.jsonEncode, smwJSON)
	
	if jsongood then
		-- for easier debug, please do not remove
		encsmwJSON = mw.text.nowiki(encsmwJSON)
		local div = mw.html.create('div')
		div	:addClass('hidden')
			:css('display', 'none')
			:wikitext('[[Quest JSON::'..encsmwJSON..']]')
		info = frame:preprocess(tostring(div))
	else
		haserr = true
		err:wikitext('Error setting SMW JSON, string: ' .. mw.dumpObject(smwJSON))
	end
	if not haserr then
		err = ''
	end
	return tostring(info) .. tostring(err)
end

return p