User:Fewfre/Module:Sandbox

-- -- Used for displaying weapon stats. used by Template:Infobox_weapon --

local p = {}

local paramtest = require('Module:Paramtest') local default_to = paramtest.default_to local yesno = require('Module:Yesno') local format_num = require('Module:Number').format_num local round = require('Module:Number').round local mainonly = require('Module:Mainonly')._main

local TARGET_PRECISION = 82; -- A chosen percent to represent a reasonable range of accuracy a skilled player could hit within every shot.

function p.stats(frame) local args = frame:getParent.args local get = args['get'] local weapontype = args['weapontype'] local name = args['name'] local aka = args['aka'] local image = args['image'] local cost = args['cost'] local ammo = args['ammo'] local pellets = tonumber(default_to(args['pellets'], "1")) local automatic = yesno(args['automatic']) local damage = args['damage'] local firerate = args['firerate'] local precision = args['precision'] local reload = args['reload'] local scope = yesno(args['scope']) local releasedate = args['releasedate'] if not get or get=='' or get=='table' then return p._getInfobox(weapontype, name, aka, image, cost, ammo, pellets, automatic, damage, firerate, precision, reload, scope, releasedate) elseif get=='row' then return p._getRow(weapontype, name, aka, image, cost, ammo, pellets, automatic, damage, firerate, precision, reload, scope, releasedate) elseif get=='weapontype'	then return weapontype elseif get=='name'			then return name elseif get=='image'			then return image elseif get=='cost'			then return cost elseif get=='ammo'			then return ammo elseif get=='pellets'		then return pellets elseif get=='automatic'		then return automatic elseif get=='damage'		then return damage elseif get=='firerate'		then return firerate elseif get=='precision'		then return precision elseif get=='reload'		then return reload elseif get=='scope'			then return scope elseif get=='releasedate'	then return releasedate else						    return "Invalid retrieval type." end end

function p._getRow(pWeapontype, pName, pAka, pImage, pCost, pAmmo, pPellets, pAutomatic, pDamage, pFirerate, pPrecision, pReload, pScope, pReleasedate) local name = pName or "???" local image = pName and  or  local cost = pCost or "?" if cost == '0' then cost = 'data-sort-value="0"|Free' end local ammo = pAmmo or "?" local automatic = p._yesnoIcon(pAutomatic) local damage = pDamage or "?" local firerate = pFirerate or "?" local precision = pPrecision or "?" local reload = pReload or "?" local scope = p._yesnoIcon(pScope) local releasedate = pReleasedate or "Unknown" return '\n|-'.. '\n| style="text-align:right;" |'..image.. '\n|'..name.. '\n| style="text-align:center;" '..cost.. '\n| style="text-align:center;" |'..ammo.. '\n| style="text-align:center;" |'..automatic.. '\n| style="text-align:center;" |'..damage.. '\n| style="text-align:center;" |'..firerate..' '.. '\n| style="text-align:center;" |'..precision..'%'.. '\n| style="text-align:center;" |'..reload.. '\n| style="text-align:center;" |'..scope.. '\n|'..releasedate end

function p._getInfobox(pWeapontype, pName, pAka, pImage, pCost, pAmmo, pPellets, pAutomatic, pDamage, pFirerate, pPrecision, pReload, pScope, pReleasedate) local weapontype = default_to(pWeapontype, "?") local weapontype_title = "Unknown / Invalid weapon type" if weapontype=="primary" then weapontype="W1" weapontype_title="Primary Weapon (Weapon 1)" elseif weapontype=="secondary" then weapontype="W2" weapontype_title="Secondary Weapon (Weapon 2)" end local name = pName or "???" local aka = pAka and " ("..pAka..")" or '' local image = default_to(pImage, ''); local cost_text = pCost or "?" if cost_text == '0' then cost_text = 'Free' end local ammo = pAmmo or "?" local ammo_num = tonumber(default_to(pAmmo, "0")) local automatic = pAutomatic==nil and "Unknown" or p._yesnoIcon(pAutomatic) local damage_num = tonumber(default_to(pDamage, "0")) local firerate_num = tonumber(default_to(pFirerate, "0")) local precision_num = tonumber(default_to(pPrecision, "0")) local reload = pReload or "?" local scope = pScope==nil and "Unknown" or p._yesnoIcon(pScope) local releasedate = default_to(pReleasedate, "Unknown") -- 1 is "can hit every time" local hit_chance = precision_num == 0 and 1 or Math.min(1 - TARGET_PRECISION / precision_num, 1) local thCss = { ["padding"]="0 8px" }; local tdCss = { ["padding"]="0 8px", ["color"]="#D5D4D4", ["background"]="#222", ["border-radius"]="3px", ["border"]="1px solid #777", ["text-align"]="center" }; return mw.html.create('table') :attr({["cellspacing"]="5", ["cellpadding"]="5"}) :css({["border"]="3px double #5F6165", ["background"]="#4E5549", ["width"]="100%", ["margin-bottom"]="7px"}) :tag('tr') :tag('td'):attr({["colspan"]="2"}) :css({ ["padding"]="0 8px", ["color"]="#D5D4D4", ["background"]="#222", ["border"]="1px solid #777" }) :wikitext(name..aka.." ") :tag("span"):addClass("color1") :css({["font-size"]="75%", ["padding"]="3px 5px 2px", ["border-radius"]="8px"}) :attr('title', weapontype_title) :wikitext(" ( "..weapontype.." ) ") :done :done :tag('th'):addClass("color1"):css(thCss):wikitext("Cost"):done :tag('td'):css(tdCss):wikitext(cost_text.." "):done :tag('th'):addClass("color1"):css(thCss):wikitext("Release Date"):done :tag('td'):css(tdCss):wikitext(releasedate):done :tag('td'):attr({["colspan"]="4", ["rowspan"]="2"}):css({["text-align"]="right"}):wikitext(image):done :done :tag('tr') :tag('th'):addClass("color1"):css(thCss):wikitext("Ammo"):done :tag('td'):css(tdCss):wikitext(ammo..(pPellets==1 and "" or " x"..pPellets.." ").." "):done :tag('th'):addClass("color1"):css(thCss):wikitext("Damage"):done :tag('td'):css(tdCss):wikitext(pDamage or "?"):done :tag('th'):addClass("color1"):css(thCss):wikitext("Reload"):done :tag('td'):css(tdCss):wikitext(reload.." seconds"):done :done :tag('tr') :tag('th'):addClass("color1"):css(thCss):wikitext("Fire Rate"):done :tag('td'):css(tdCss):wikitext((pFirerate or "?").." "):done :tag('th'):addClass("color1"):css(thCss):wikitext("Precision"):done :tag('td'):css(tdCss):wikitext((pPrecision or "?").."%"):done :tag('th'):addClass("color1"):css(thCss):wikitext("DPS (short)"):done :tag('td'):css(tdCss):wikitext( format_num(damage_num*firerate_num*pPellets, 2).." dmg/sec" ):done :tag('th'):addClass("color1"):css(thCss):wikitext("Automatic"):done :tag('td'):css(tdCss):wikitext(automatic):done :tag('th'):addClass("color1"):css(thCss):wikitext("Scope"):done :tag('td'):css(tdCss):wikitext(scope):done :done :tag('tr') :tag('th'):addClass("color1"):css(thCss):wikitext("DPC"):attr("title", "Damage Per Clip"):done :tag('td'):css(tdCss):wikitext(round(damage_num * ammo_num, 1)):done :tag('th'):addClass("color1"):css(thCss):wikitext("40 Damage"):attr("title", "Time it takes to deal 40 damage"):done :tag('td'):css(tdCss):wikitext("zzz"):done :done :wikitext(mainonly("")) :done end

function p._yesnoIcon(pVal) if pVal then return '' else return '' end end

return p