Module:Infobox Bonuses

-- -- Implements Template:Infobox Bonuses -- see also supporting modules Module:Infobox bonuses super, Module:Infobox Bonuses multi -- -- local p = {}

-- imports local format = require('Module:Format equipment stat').format local speedbar = require('Module:Attack speed bar').make_bar local edit = require('Module:Edit button') local onmain = require('Module:Mainonly').on_main

-- accepted names for slots local slots = { head = 'head', ammo = 'ammo', neck = 'neck', back = 'cape', cape = 'cape', weapon = 'weapon', ['2h'] = '2h', body = 'torso', torso = 'torso', shield = 'shield', sheild = 'shield', legs = 'legs', hands = 'gloves', gloves = 'gloves', feet = 'boots', boots = 'boots', ring = 'ring', set = 'e', none = 'e' }

-- info for slots using the defined names above, in {i=image, l=link (default i), c=category, t=slot table} format local slot_data = { head = { i = 'Head slot', c = 'Head slot items', t = 'Head slot table' }, ammo = { i = 'Ammo slot', l='Ammunition slot', c = 'Ammunition slot items', t = 'Ammunition slot table' }, neck = { i = 'Neck slot', c = 'Neck slot items', t = 'Neck slot table' }, cape = { i = 'Cape slot', c = 'Cape slot items', t = 'Cape slot table' }, weapon = { i = 'Weapon slot', c = 'Weapon slot items', t = 'Weapon slot table', _weapon = true }, ['2h'] = { i = '2h slot', l = 'Two-handed slot', c = 'Two-handed slot items', t = 'Two-handed slot table', _weapon = true }, torso = { i = 'Torso slot', l = 'Body slot', c = 'Body slot items', t = 'Body slot table' }, shield = { i = 'Shield slot', c = 'Shield slot items', t = 'Shield slot table' }, legs = { i = 'Legs slot', l = 'Leg slot', c = 'Leg slot items', t = 'Legs slot table' }, gloves = { i = 'Gloves slot', l = 'Hand slot', c = 'Hand slot items', t = 'Hand slot table' }, boots = { i = 'Boots slot', l = 'Feet slot', c = 'Feet slot items', t = 'Feet slot table' }, ring = { i = 'Ring slot', c = 'Ring slot items', t = 'Ring slot table' }, e = {}, }

local bonus_images = { stab = '', slash = '', crush = '', range = '', magic = '', str = '', rstr = '', mdmg = '', prayer = '', attack_bonuses = ' Attack bonuses', defence_bonuses = ' Defence bonuses', other_bonuses = ' Other bonuses' }

-- Returns resized file string, image width, image height function clean_image(file, caption) if not file or (file and (file:lower == 'no' or file == '')) then return nil end if file:lower == 'needed' then return " Please upload an image! " end

local default_width, default_height = 200, 250 local max_width, max_height = 300, 350 local result_width, result_height = default_width, default_height file = file:gsub('[Ff]ile:',''):gsub('|','|')

-- enforce max height and width file = mw.text.split(file, '|')

if #file == 1 then -- no extra parameters, just the file link local f = mw.title.makeTitle('File', file[1]).file local sizestr = '' if f.exists then if f.width and f.width > default_width then sizestr = sizestr..default_width end if f.height and f.height > default_height then sizestr = sizestr .. 'x' .. default_height end if sizestr ~= '' then sizestr = '|' .. sizestr .. 'px' end end file = string.format('%s%s',file[1], sizestr) else -- cap width at 300px, height at 350px -- first param will always be the filename local dimen = nil for i = 2, #file do			-- look for string ending with px			if mw.ustring.find( file[i], 'px$' ) then -- strip px				dimen = mw.ustring.gsub(file[i], 'px$', '') dimen = mw.text.split(dimen, 'x') if #dimen == 1 then -- width if tonumber(dimen[1]) > max_width then dimen[1] = max_width end -- set max height whilst we're here dimen[2] = max_height else -- width, height if tonumber(dimen[1]) > max_width then dimen[1] = max_width end if tonumber(dimen[2]) > max_height then dimen[2] = max_height end end result_width, result_height = dimen[1], dimen[2] file[i] = table.concat(dimen, 'x')..'px' end end

file = table.concat( file, '|' ) if dimen == nil then file = string.format('%s|%sx%spx',file, default_width, default_height) end if caption then file = string.format('%s|%s', file, caption) end end return string.format('', file), result_width, result_height end

-- template entry point -- formats params ready for making the infobox function p.main(frame) local _args = frame:getParent.args local args = {_raw_args_ = _args} args.style = _args.style args.slot = slots[string.lower(_args.slot or '')] args._slotdata = slot_data[args.slot] or {i=''} args._isweapon = args._slotdata._weapon or false for _,v in ipairs({'stab', 'slash', 'crush', 'magic', 'range'}) do		args['a'..v] = format(_args['a'..v], '?') args['d'..v] = format(_args['d'..v], '?') end args.prayer = format(_args.prayer, '?') args.str = format(_args.str, '?') args.rstr = format(_args.rstr, '?') args._raw_mdmg = format(_args.mdmg, '?') if args._raw_mdmg ~= '?' then args.mdmg = args._raw_mdmg .. '%'	end args.speed = _args.speed or _args.aspeed args.caption = _args.caption args.altcaption = _args.altcaption or args.caption2 args.image, args.image_width, args.image_height = clean_image(_args.image, args.caption) args.altimage, args.altimage_width, args.altimage_height = clean_image(_args.altimage or _args.image2, args.caption2) local r = _main(args) if onmain then r = tostring(r) .. tostring(_categories(args)) _smw(args) end return r end

-- makes the infobox layout function _main(args) local ret = mw.html.create('table') local tr, tr2, td	ret	:addClass('rsw-infobox infobox-bonuses') :tag('caption') :wikitext('Bonuses') :addClass('infobox-caption') :done tr = ret:tag('tr') tr	:tag('th') :attr('colspan', 5) :addClass('infobox-subheader infobox-bonuses-first-header') :wikitext(bonus_images.attack_bonuses) :done if args.image then local image_rowspan = 15 if args._isweapon and tostring(args.speed):lower ~= 'no' then image_rowspan = 19 end td = tr:tag('td') td	:addClass('infobox-bonuses-image') :attr('rowspan', image_rowspan) :css('width', args.image_width..'px') :wikitext(args.image) if args.caption then td	:tag('br') :done :tag('span') :addClass('infobox-bonuses-image-caption') :wikitext(args.caption) :done end if args.altimage then td = tr:tag('td') td	:addClass('infobox-bonuses-image') :attr('rowspan', image_rowspan) :css('width', args.altimage_width..'px') :wikitext(args.altimage) if args.altcaption then td	:tag('br'):done :tag('span') :addClass('infobox-bonuses-image-caption') :wikitext(args.altcaption) :done end end end

ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done tr = ret:tag('tr') tr2 = ret:tag('tr') for _,v in ipairs({'stab', 'slash', 'crush', 'magic', 'range'}) do		tr	:tag('th') :addClass('infobox-nested') :wikitext(bonus_images[v]) :done tr2	:tag('td') :addClass('infobox-nested') :wikitext(args['a'..v]) :done end ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done

ret	:tag('tr') :tag('th') :attr('colspan', 5) :addClass('infobox-subheader') :wikitext(bonus_images.defence_bonuses) :done :done

ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done tr = ret:tag('tr') tr2 = ret:tag('tr') for _,v in ipairs({'stab', 'slash', 'crush', 'magic', 'range'}) do		tr	:tag('th') :addClass('infobox-nested') :wikitext(bonus_images[v]) :done tr2	:tag('td') :addClass('infobox-nested') :wikitext(args['d'..v]) :done end ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done ret	:tag('tr') :tag('th') :attr('colspan', 4) :addClass('infobox-subheader') :wikitext(bonus_images.other_bonuses) :done :tag('th') :addClass('infobox-subheader') :wikitext('Slot') :done :done

ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done

tr = ret:tag('tr') tr2 = ret:tag('tr') for _,v in ipairs({'str', 'rstr', 'mdmg', 'prayer'}) do		tr	:tag('th') :addClass('infobox-nested') :wikitext(bonus_images[v]) :done tr2	:tag('td') :addClass('infobox-nested') :wikitext(args[v]) :done end local slotimg = '?' if args.slot == 'e' then slotimg = '' elseif args.slot and args._slotdata.i then slotimg = string.format('', args._slotdata.i, args._slotdata.l or args._slotdata.i)	end td = tr:tag('th') td	:addClass('infobox-nested') :wikitext(slotimg) td2 = tr2:tag('td') td2	:addClass('infobox-nested') if args._slotdata.t then td2	:wikitext(string.format('List',args._slotdata.t)) end if args._isweapon and tostring(args.speed):lower ~= 'no' then ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done

ret	:tag('tr') :tag('th') :attr('colspan', 5) :addClass('infobox-subheader') :wikitext('Attack speed') :done :done

ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done local spd = tostring(speedbar(args.speed)) if spd == '' then spd = 'Unknown attack speed '..edit end ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-full-width-content') :wikitext(spd) :done :done

end

ret	:tag('tr') :tag('td') :attr('colspan', 5) :addClass('infobox-padding') :done :done return ret end

-- attaches categories function _categories(args) local cats = {'Equipment'} if args._slotdata.c then table.insert(cats, args._slotdata.c)	end if args._isweapon then if not args.speed then table.insert(cats, 'Missing attack speed') end end if args.slot == nil then table.insert(cats, 'Incomplete bonuses') end for i,v in ipairs({ 'astab', 'aslash', 'acrush', 'arange', 'amagic', 'dstab', 'dslash', 'dcrush', 'drange', 'dmagic', 'str', 'rstr', 'mdmg', 'prayer' }) do		if args[v] == '?' then table.insert(cats, 'Incomplete bonuses') break end end for i,v in ipairs(cats) do		cats[i] = string.format('',v) end return table.concat(cats,'') end

local smwJsonToNiceName = { requirements = 'Equipment requirements', slot = 'Equipment slot', astab = 'Stab attack bonus', aslash = 'Slash attack bonus', acrush = 'Crush attack bonus', arange = 'Range attack bonus', amagic = 'Magic attack bonus', dstab = 'Stab defence bonus', dslash = 'Slash defence bonus', dcrush = 'Crush defence bonus', drange = 'Range defence bonus', dmagic = 'Magic defence bonus', str = 'Strength bonus', rstr = 'Ranged Strength bonus', mdmg = 'Magic Damage bonus', prayer = 'Prayer bonus', speed = 'Weapon attack speed', }

-- attaches semantic mediawiki data function _smw(args) local smw = {} local ret_cont = mw.html.create('div') ret_cont:css('display','none'):addClass('infobox-semantics-data hidden') local ret = ret_cont:tag('div') ret:css({			['white-space'] = 'pre-wrap',			['background-color'] = '#f7f7f7',			border = '1px solid #ddd',			['border-radius'] = '2px',			['line-height'] = '14px',			overflow = 'auto',			padding = '12px',			['word-wrap'] = 'normal',			display = 'block',			['font-family'] = 'monospace',		}) for i,v in ipairs({ 'astab', 'aslash', 'acrush', 'arange', 'amagic', 'dstab', 'dslash', 'dcrush', 'drange', 'dmagic', 'str', 'rstr', 'prayer' }) do		if args[v] ~= '?' then smw[v] = args[v] end end if args._raw_mdmg ~= '?' then smw.mdmg = args._raw_mdmg end if args.slot ~= nil and args.slot ~= 'e' then smw.slot = args.slot end if args._isweapon then if args.speed then smw.speed = args.speed end end local smwDone = {} -- make properties for v,k in pairs(smw) do		smwDone[smwJsonToNiceName[v]] = k	end -- version only in json if args._raw_args_.version then smw.version = args._raw_args_.version end local smwJsonGood, smwJsonEncoded = pcall(mw.text.jsonEncode,smw) if smwJsonGood then smwDone['Equipment JSON'] = smwJsonEncoded end mw.smw.set(smwDone) end

return p --