Module:Base stats

-- local p   = {} local lib      = require('Module:Feature') local json     = require('Module:JSON') local userError = require('Dev:User error') local IL       = require('Module:ImageLink') local wikidata = mw.loadData('Module:ChampionData/data')

function p.tftcompare(frame) local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end local s="" local jsondata = jsontolua("Template:Set/TFT.json") local luadata = require('Module:TFTUnitData/data') local championtable={} local stattable={} local resulttable={} local settable = {} for set,setdata in pairs(jsondata["sets"]) do           set = tonumber(set) for _,championdata in pairs(setdata.champions) do           for stat,statdata in pairs(championdata.stats) do                if stat ~= "critChance" and stat ~= "critMultiplier" then if stat == "armor" then stat = "arm" end if stat == "attackSpeed" then stat = "as" statdata = round(statdata) end if stat == "damage" then stat = "ad" end if stat == "initialMana" then stat = "startmana" end if stat == "magicResist" then stat = "mr" end local championname = championdata.name local wikistatdata if luadata[championname] == nil or luadata[championname][set] == nil or luadata[championname][set][stat] == nil then wikistatdata = nil else wikistatdata = luadata[championname][set][stat] end if tonumber(statdata)~=tonumber(wikistatdata or "") then if resulttable[championname]==nil then resulttable[championname]={} stattable[championname]={} settable[championname]={} table.insert(championtable,championname) end if resulttable[championname][set]==nil then resulttable[championname][set]={} stattable[championname][set]={} table.insert(settable[championname],set) end table.insert(stattable[championname][set],stat) resulttable[championname][set][stat]=statdata end end end end end table.sort(championtable) for _,championname in pairs(championtable) do       table.sort(settable[championname]) for _,set in pairs (settable[championname]) do           table.sort(stattable[championname][set]) s=s..ci(championname).." Set " .. set .. "\n" for _,stat in pairs(stattable[championname][set]) do               s=s.."*"..stat.."= "..resulttable[championname][set][stat].."\n" end end end if s == "" then return "Everything is updated!" else return s   end end

function p.compare(frame) local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end local s="" local jsondata = jsontolua("Template:Set/champion.json") local championtable={} local stattable={} local resulttable={} for _,championdata in pairs(jsondata["data"]) do       local championname = championdata.name for stat,statdata in pairs(championdata.stats) do           if stat ~= "crit" and stat ~= "critperlevel" then if stat == "hp" then stat = "hp_base" end if stat == "hpperlevel" then stat = "hp_lvl" end if stat == "mp" then stat = "mp_base" end if stat == "mpperlevel" then stat = "mp_lvl" end if stat == "movespeed" then stat = "ms" end if stat == "armor" then stat = "arm_base" end if stat == "armorperlevel" then stat = "arm_lvl" end if stat == "spellblock" then stat = "mr_base" end if stat == "spellblockperlevel" then stat = "mr_lvl" end if stat == "attackrange" then stat = "range" end if stat == "hpregen" then stat = "hp5_base" end if stat == "hpregenperlevel" then stat = "hp5_lvl" end if stat == "mpregen" then stat = "mp5_base" end if stat == "mpregenperlevel" then stat = "mp5_lvl" end if stat == "attackdamage" then stat = "dam_base" end if stat == "attackdamageperlevel" then stat = "dam_lvl" end if stat == "attackspeedperlevel" then stat = "as_lvl" end if stat == "attackspeed" then stat = "as_base" end if tonumber(statdata) ~= tonumber(wikidata[championname]["stats"][stat]) then if resulttable[championname]==nil then resulttable[championname]={} stattable[championname]={} table.insert(championtable,championname) end table.insert(stattable[championname],stat) resulttable[championname][stat]=statdata end end end for stat,statdata in pairs(championdata.tags) do           if stat == 1 then stat = "herotype" end if stat == 2 then stat = "alttype" end if statdata ~= wikidata[championname][stat] then if resulttable[championname]==nil then resulttable[championname]={} stattable[championname]={} table.insert(championtable,championname) end table.insert(stattable[championname],stat) resulttable[championname][stat]=statdata end end for stat,statdata in pairs(championdata) do           if stat ~= "version" and stat ~= "name" and stat ~= "blurb" then if type(statdata) ~= "table" then if stat == "id" then stat = "apiname" end if stat == "key" then stat = "id" end if stat == "partype" then stat = "resource" end if (tonumber(statdata) or statdata) ~= wikidata[championname][stat] then if resulttable[championname]==nil then resulttable[championname]={} stattable[championname]={} table.insert(championtable,championname) end table.insert(stattable[championname],stat) resulttable[championname][stat]=statdata end end end end end table.sort(championtable) for _,championname in pairs(championtable) do       table.sort(stattable[championname]) s=s..ci(championname).."\n" for _,stat in pairs(stattable[championname]) do           s=s.."*"..stat.."= "..resulttable[championname][stat].."\n" end end return "[https://leagueoflegends.fandom.com/wiki/Module:ChampionData/data?action=edit Patch Data: " .. jsondata["version"] .."] "..s end

function p.get(frame) local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end local stat3      = string.lower(args[1] or "as") local rangetype  = args["rangetype"] local sortby     = args["sortby"] local lvl        = args["lvl"] local show       = tonumber(args["show"] or 1) local get = args["get"] if rangetype~=nil then rangetype=string.lower(rangetype) end if sortby~=nil then sortby=string.lower(sortby) end local size       = tonumber(args["size"] or 5) local stat,stat2="","" if stat3=="ad" or stat3=="dam" or stat3=="attack damage" then stat="dam" stat2="AD" else if stat3=="attack speed" or stat3=="as" then stat="as" stat2="AS" else if stat3=="health" or stat3=="hp" then stat="hp" stat2="health" else if stat3=="health regeneration" or stat3=="hp5" then stat="hp5" stat2="hp5" else if stat3=="mana" or stat3=="mp" then stat="mp" stat2="mana" else if stat3=="mana regeneration" or stat3=="mp5" then stat="mp5" stat2="mp5" else if stat3=="armor" or stat3=="armor" then stat="arm" stat2="armor" else if stat3=="magic resistance" or stat3=="mr" then stat="mr" stat2="MR" else stat=stat3 stat2=stat3 end end end end end end end end if lvl~=nil and size==1 and sortby~=nil then return getstats(lvl,stat,stat2,frame,rangetype,sortby,size,show,get) else local s          = '{| class="wikitable"\n|+' if rangetype~=nil then s=s..mw.ustring.gsub(rangetype, "^.", mw.ustring.upper).." c"       else s=s.."C" end s=s..'hampions with the ' if sortby~=nil then if sortby=="top" then s=s.."highest " else if sortby=="bot" then s=s.."lowest " else end end else s=s.."lowest or highest " end s=s..stat3..' before items, runes, or abilities\n! width="100pt" | Champion Level' s=s..'\n! colspan="2" width="200pt" | ' if sortby~=nil then if sortby=="top" then s=s..'Top' end if sortby=="bot" then s=s..'Bottom' end else s=s..'Top' if size~=1 then s=s.." "..size..' champions' else s=s..' champion' end s=s..'\n! colspan="2" width="200pt" | Bottom' end if size~=1 then s=s.." "..size..' champions' else s=s..' champion' end s=s..'\n|-\n! rowspan="'..size..'" | Level ' if lvl~=nil then s=s..lvl s=s..getstats(lvl,stat,stat2,frame,rangetype,sortby,size) else s=s.."1" s=s..getstats(1,stat,stat2,frame,rangetype,sortby,size) s=s..'\n|-\n! colspan="5" height="5pt" |\n|-\n! rowspan="'..size..'" | Level 18' s=s..getstats(18,stat,stat2,frame,rangetype,sortby,size) end return s.."\n|}" end end

-- HELPER FUNCTIONS --

function getstats(lvl,stat,stat2,frame,rangetype,sortby,size,show,get) local stats={} local s="" for champname in pairs(wikidata) do       if (stat=="mp" or stat=="mp5") and wikidata[champname]["resource"]~="" and wikidata[champname]["resource"]~=nil and string.lower(wikidata[champname]["resource"])~="mana" then else if rangetype==nil or string.lower(wikidata[champname]["rangetype"])==string.lower(rangetype) then base=tonumber(wikidata[champname]["stats"][stat.."_base"]) or tonumber(wikidata[champname]["stats"][stat]) xlvl=tonumber(wikidata[champname]["stats"][stat.."_lvl"]) or 0 if stat=="as" then local as_ratio = round(tonumber(wikidata[champname]["stats"]["as_ratio"])) if as_ratio == nil or as_ratio == 0 then as_ratio = tonumber(wikidata[champname]["stats"]["as_base"]) end table.insert(stats,{champname, base+as_ratio*xlvl/100*(lvl-1)*(0.7025+0.0175*(lvl-1))}) else table.insert(stats,{champname, base+xlvl*(lvl-1)*(0.7025+0.0175*(lvl-1))}) end end end end table.sort(stats, function(a, b) return a[2]<b[2] end) if lvl~=nil and size==1 and sortby~=nil then if get == nil then if sortby=="top" then s = s .. fd(round(stats[#stats-show+1][2])) else s = s .. fd(round(stats[show][2])) end else if get == "stat" then if sortby == "top" then s = s .. round(stats[#stats-show+1][2]) else s = s .. round(stats[show][2]) end end end if get == nil then s = s .. " ("       end        if get ~= "stat" then            if sortby=="top" then                s = s .. ci(stats[#stats-show+1][1],frame)            else                s = s .. ci(stats[show][1],frame)            end        end        if get == nil then            s = s .. ")" end return s   else local bot={} local top={} j=#stats k=1 while j>=1 and sortby~="bot" do           if j==#stats then top[k]="\n| "..k..". "..ci(stats[j][1],frame) else if stats[j][2]==stats[j+1][2] then top[k]=top[k].." "..k..". "..ci(stats[j][1],frame) else top[k]=top[k].."\n| "..fd(round(stats[j+1][2])).." "..stat2 k=k+1 if k>size then break end top[k]="\n| "..k..". "..ci(stats[j][1],frame) if j==1 then top[k]=top[k].."\n| "..fd(round(stats[j][2])).." "..stat2 end end end j=j-1 end j=1 k=1 while j<=#stats and sortby~="top" do           if j==1 then bot[k]="\n| "..k..". "..ci(stats[j][1],frame) else if stats[j][2]==stats[j-1][2] then bot[k]=bot[k].." "..k..". "..ci(stats[j][1],frame) else bot[k]=bot[k].."\n| "..fd(round(stats[j-1][2])).." "..stat2 k=k+1 if k>size then break end bot[k]="\n| "..k..". "..ci(stats[j][1],frame) if j==#stats then bot[k]=bot[k].."\n| "..fd(round(stats[j][2])).." "..stat2 end end end j=j+1 end k=1 while k<=size do           if sortby~=nil then if sortby=="top" then if top[k]~=nil then s=s..top[k] else s=s.."\n|\n|" end else if sortby=="bot" then if bot[k]~=nil then s=s..bot[k] else s=s.."\n|\n|" end end end else if top[k]~=nil then s=s..top[k] else s=s.."\n|\n|" end if bot[k]~=nil then s=s..bot[k] else s=s.."\n|\n|" end end s=s.."\n|-" k=k+1 end return s   end end

function jsontolua(pagename) local titleobject = mw.title.new(pagename) local o           = json.decode(titleobject:getContent) return o end

function exists(page) local sPage = page if sPage then local _, val = pcall(package.loaders[2], sPage) if type(val) == "function" or type(val) == "string" then return true end end return false end

function ci(champion) return tostring(IL.champion{           ["champion"] = champion,        }) end

function fd(val) val=tostring(val) if string.find(val,"[0-9]") == nil or string.find(val,"%.") == nil then return val end local a,b = val:match"([0-9]*).([0-9]*)" return a .. ". " .. b .. " " end

function round(val) if val == nil or val == "" then return val end val = tonumber(val) local mult = 10^3 local rounding=math.floor(val * mult + 0.5) / mult return rounding end return p --