Module:QuestDetails

From WIDEVERSE Wiki
Revision as of 21:22, 4 November 2021 by Admin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

-- <pre>
-- {{Quest details}}
--

local p = {}
local yesno = require( 'Module:Yesno' )
local onmain = require('Module:Mainonly').on_main()
    
local difficulties = {
    none = 'None',
    tutorial = '[[File:Novice.svg|7px|Tutorial|link=]] Tutorial',
    novice = '[[File:Novice.svg|7px|Novice|link=]] Novice',
    intermediate = '[[File:Intermediate.svg|7px|Intermediate|link=]] Intermediate',
    experienced = '[[File:Experienced.svg|7px|Experienced|link=]] Experienced',
    master = '[[File:Master.svg|7px|Master|link=]] Master',
    grandmaster = '[[File:Grandmaster.svg|7px|Grandmaster|link=]] Grandmaster',
    special = '[[File:Special.svg|7px|Special|link=]] Special'
}
    
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 difficulty = difficulties[mw.ustring.lower(args.difficulty or '')] or unknown
    local length = args.length 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( 'Start point' )
                :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( 'Official difficulty' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :wikitext( difficulty )
                :done()
            :done()
        :tag( 'tr' )
            :tag( 'th' )
                :addClass( 'questdetails-header' )
                :wikitext( 'Official length' )
                :done()
            :tag( 'td' )
                :addClass( 'questdetails-info' )
                :wikitext( length )
                :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' )
                	:addClass( 'qc-active' )
                    :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,
		difficulty = difficulties[mw.ustring.lower(args.difficulty or '')],
		start = args.start or nil,
		length = args.length 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(mw.text.killMarkers(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
	
	-- Set other smw values
	local smw = {
		['Official difficulty'] = args.difficulty or nil,
		['Is members only'] = yesno(args.members,false),
		['Official length'] = args.length or nil,
	}
	local smwstat = mw.smw.set(smw)
	if not smwstat == true then
		err = err .. ' ' .. smwstat.error
	end
	
	return tostring(info) .. tostring(err)
end

return p