Difference between revisions of "Module:Apt"

From CrawlWiki
Jump to: navigation, search
m (Undo revision 40918 by Medar (talk))
(Use Module:Table of species. Misc other small changes)
Line 1: Line 1:
 
local p = {}
 
local p = {}
 +
 +
local data = mw.loadData('Module:Table of species')
  
 
local function name_arg(frame)
 
local function name_arg(frame)
Line 7: Line 9:
 
   end
 
   end
 
   return name
 
   return name
 +
end
 +
 +
local function is_colored_draconian(species)
 +
  return species:match(" Draconian$")
 +
end
 +
 +
local function species_sorted()
 +
  local species = {}
 +
  for sp in pairs(data) do
 +
    if not is_colored_draconian(sp) then
 +
      table.insert(species, sp)
 +
    end
 +
  end
 +
  table.sort(species)
 +
  return species
 
end
 
end
 
   
 
   
 
function p.aptitude(frame)
 
function p.aptitude(frame)
  local data = mw.loadData('Module:Table of aptitudes')
 
 
   local sp = frame.args[1]
 
   local sp = frame.args[1]
 
   local sk = frame.args[2]
 
   local sk = frame.args[2]
Line 16: Line 32:
 
     return ""
 
     return ""
 
   end
 
   end
   return data[sp][sk]
+
   return data[sp].Aptitudes[sk] or "N/A"
 
end
 
end
  
 
function p.skill_table(frame)
 
function p.skill_table(frame)
  local data = mw.loadData('Module:Table of aptitudes')
 
 
   local skill = name_arg(frame)
 
   local skill = name_arg(frame)
   local species = {'Human',  'Centaur', 'Demigod', 'Demonspawn',
+
  local headers = {}
  'Draconian', 'Deep Dwarf', 'Deep Elf', 'High Elf',
+
   local species = species_sorted()
  'Felid', 'Formicid', 'Gargoyle', 'Ghoul', 'Halfling', 'Kobold', 'Merfolk',  
+
  for _, sp in ipairs(species) do
  'Minotaur', 'Mummy', 'Naga', 'Octopode', 'Ogre', 'Hill Orc',
+
    local header = ("'''[[%s|%s]]'''"):format(sp, data[sp].Abbrev)
  'Spriggan', 'Tengu', 'Troll', 'Vampire', 'Vine Stalker'}
+
    table.insert(headers, header)
   local result = [=[
+
  end
{| class="wikitable" border="1"
+
   local result = [[{| class="wikitable" border="1"
|-
 
|'''[[Human|Hu]]'''
 
|'''[[Centaur|Ce]]'''
 
|'''[[Demigod|Dg]]'''
 
|'''[[Demonspawn|Ds]]'''
 
|'''[[Draconian|Dr]]'''
 
|'''[[Deep Dwarf|DD]]
 
|'''[[Deep Elf|DE]]'''
 
|'''[[High Elf|HE]]'''
 
|'''[[Felid|Fe]]'''
 
|'''[[Formicid|Fo]]'''
 
|'''[[Gargoyle|Gr]]'''
 
|'''[[Ghoul|Gh]]'''
 
|'''[[Halfling|Ha]]'''
 
|'''[[Kobold|Ko]]'''
 
|'''[[Merfolk|Mf]]'''
 
|'''[[Minotaur|Mi]]'''
 
|'''[[Mummy|Mu]]'''
 
|'''[[Naga|Na]]'''
 
|'''[[Octopode|Op]]'''
 
|'''[[Ogre|Og]]'''
 
|'''[[Hill Orc|HO]]'''
 
|'''[[Spriggan|Sp]]'''
 
|'''[[Tengu|Te]]'''
 
|'''[[Troll|Tr]]'''
 
|'''[[Vampire|Vp]]'''
 
|'''[[Vine Stalker|VS]]'''
 
 
|-
 
|-
]=]
+
|]]
 +
  result = result .. table.concat(headers, "\n|")
 +
  result = result .. "\n|-\n"
 
   for _, sp in ipairs(species) do
 
   for _, sp in ipairs(species) do
     result = result .. "| " .. data[sp][skill] .. "\n"
+
     result = result .. "| " .. (data[sp].Aptitudes[skill] or "N/A") .. "\n"
 
   end
 
   end
 
   result = result .. "|}"
 
   result = result .. "|}"
Line 66: Line 56:
  
 
function p.skill_draconian_exceptions(frame)
 
function p.skill_draconian_exceptions(frame)
  local data = mw.loadData('Module:Table of aptitudes')
 
 
   local skill = name_arg(frame)
 
   local skill = name_arg(frame)
  local colours = {'Red', 'White', 'Green', 'Yellow', 'Grey', 'Black', 'Purple', 'Mottled', 'Pale'}
+
   local normal = data['Draconian'].Aptitudes[skill]
   local normal = data['Draconian'][skill]
 
 
   local result = ''
 
   local result = ''
   for _, col in ipairs(colours) do
+
   for name, species in pairs(data) do
     if data[col .. ' Draconian'][skill] ~= normal then
+
     if is_colored_draconian(name) and species.Aptitudes[skill] ~= normal then
       result = result .. '* [[Draconian#' .. col .. '|' .. col .. ' draconian]]s get ' .. data[col .. ' Draconian'][skill] .. '.\n'
+
      local col = name:match("^%a*")
 +
       result = result .. '* [[Draconian#' .. col .. '|' .. col .. ' draconian]]s get ' .. species.Aptitudes[skill] .. '.\n'
 
     end
 
     end
 
   end
 
   end
Line 80: Line 69:
  
 
function p.skill_average(frame)
 
function p.skill_average(frame)
  local data = mw.loadData('Module:Table of aptitudes')
 
 
   local skill = name_arg(frame)
 
   local skill = name_arg(frame)
  local species = {'Human',  'Centaur', 'Demigod', 'Demonspawn',
 
  'Draconian', 'Deep Dwarf', 'Deep Elf', 'High Elf',
 
  'Felid', 'Formicid', 'Gargoyle', 'Ghoul', 'Halfling', 'Kobold', 'Merfolk',
 
  'Minotaur', 'Mummy', 'Naga', 'Octopode', 'Ogre', 'Hill Orc',
 
  'Spriggan', 'Tengu', 'Troll', 'Vampire', 'Vine Stalker'}
 
 
   local count = 0
 
   local count = 0
 
   local sum = 0
 
   local sum = 0
   for _, sp in ipairs(species) do
+
   for _, sp in pairs(data) do
     if data[sp][skill] ~= "N/A" then
+
     if sp.Aptitude[skill] then
 
       count = count + 1
 
       count = count + 1
       sum = sum + data[sp][skill]
+
       sum = sum + sp.Aptitude[skill]
 
     end
 
     end
 
   end
 
   end
Line 98: Line 81:
 
end
 
end
  
function p.aptitude_chart(skills, header)
+
local function aptitude_chart(skills, header)
  local data = mw.loadData('Module:Table of aptitudes')
 
  local species = {'Human',  'Centaur', 'Demigod', 'Demonspawn',
 
  'Draconian', 'Deep Dwarf', 'Deep Elf', 'High Elf',
 
  'Felid', 'Formicid', 'Gargoyle', 'Ghoul', 'Halfling', 'Kobold', 'Merfolk',
 
  'Minotaur', 'Mummy', 'Naga', 'Octopode', 'Ogre', 'Hill Orc',
 
  'Spriggan', 'Tengu', 'Troll', 'Vampire', 'Vine Stalker'}
 
 
   local result = header
 
   local result = header
  
Line 112: Line 89:
 
   local aa_ord = string.byte('A')
 
   local aa_ord = string.byte('A')
 
   -- generates the table keys
 
   -- generates the table keys
   function sort_key(i)
+
   local function sort_key(i)
 
     local result
 
     local result
 
     if i > z_ord - a_ord then
 
     if i > z_ord - a_ord then
Line 124: Line 101:
  
 
   local i = 0
 
   local i = 0
   for _, sp in ipairs(species) do
+
   for _, sp in ipairs(species_sorted()) do
 
     result = result .. '|<span style="display:none;">' .. sort_key(i) .. '</span>' .. sp .. "\n"
 
     result = result .. '|<span style="display:none;">' .. sort_key(i) .. '</span>' .. sp .. "\n"
     for _,sk in ipairs(skills) do
+
     for _, sk in ipairs(skills) do
       result = result .. "||" .. data[sp][sk] .. "\n"
+
       result = result .. "||" .. (data[sp].Aptitudes[sk] or "N/A") .. "\n"
 
     end
 
     end
 
     result = result .. "|-\n"
 
     result = result .. "|-\n"
Line 153: Line 130:
 
|-
 
|-
 
]=]
 
]=]
   return p.aptitude_chart(skills, header)
+
   return aptitude_chart(skills, header)
 
end
 
end
  
Line 176: Line 153:
 
|-
 
|-
 
]=]
 
]=]
   return p.aptitude_chart(skills, header)
+
   return aptitude_chart(skills, header)
 
end
 
end
  
Line 201: Line 178:
 
|-
 
|-
 
]=]
 
]=]
   return p.aptitude_chart(skills, header)
+
   return aptitude_chart(skills, header)
 
end
 
end
  
 
return p
 
return p

Revision as of 00:40, 6 May 2016

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

local p = {}

local data = mw.loadData('Module:Table of species')

local function name_arg(frame)
  local name = frame.args[1]
  if not name or name == "" then
    name = mw.title.getCurrentTitle().text
  end
  return name
end

local function is_colored_draconian(species)
  return species:match(" Draconian$")
end

local function species_sorted()
  local species = {}
  for sp in pairs(data) do
    if not is_colored_draconian(sp) then
      table.insert(species, sp)
    end
  end
  table.sort(species)
  return species
end
 
function p.aptitude(frame)
  local sp = frame.args[1]
  local sk = frame.args[2]
  if not sp or not sk then
    return ""
  end
  return data[sp].Aptitudes[sk] or "N/A"
end

function p.skill_table(frame)
  local skill = name_arg(frame)
  local headers = {}
  local species = species_sorted()
  for _, sp in ipairs(species) do
    local header = ("'''[[%s|%s]]'''"):format(sp, data[sp].Abbrev)
    table.insert(headers, header)
  end
  local result = [[{| class="wikitable" border="1"
|-
|]]
  result = result .. table.concat(headers, "\n|")
  result = result .. "\n|-\n"
  for _, sp in ipairs(species) do
    result = result .. "| " .. (data[sp].Aptitudes[skill] or "N/A") .. "\n"
  end
  result = result .. "|}"
  return result
end

function p.skill_draconian_exceptions(frame)
  local skill = name_arg(frame)
  local normal = data['Draconian'].Aptitudes[skill]
  local result = ''
  for name, species in pairs(data) do
    if is_colored_draconian(name) and species.Aptitudes[skill] ~= normal then
      local col = name:match("^%a*")
      result = result .. '* [[Draconian#' .. col .. '|' .. col .. ' draconian]]s get ' .. species.Aptitudes[skill] .. '.\n'
    end
  end
  return result
end

function p.skill_average(frame)
  local skill = name_arg(frame)
  local count = 0
  local sum = 0
  for _, sp in pairs(data) do
    if sp.Aptitude[skill] then
      count = count + 1
      sum = sum + sp.Aptitude[skill]
    end
  end
  return string.format("%.3f", sum / count)
end

local function aptitude_chart(skills, header)
  local result = header

  -- constants used by sort_key
  local a_ord = string.byte('a')
  local z_ord = string.byte('z')
  local aa_ord = string.byte('A')
  -- generates the table keys
  local function sort_key(i)
    local result
    if i > z_ord - a_ord then
      i = i - (z_ord - a_ord) - 1
      result = '!' .. string.char(aa_ord + i)
    else
      result = '"' .. string.char(a_ord + i)
    end
    return result
  end

  local i = 0
  for _, sp in ipairs(species_sorted()) do
    result = result .. '|<span style="display:none;">' .. sort_key(i) .. '</span>' .. sp .. "\n"
    for _, sk in ipairs(skills) do
      result = result .. "||" .. (data[sp].Aptitudes[sk] or "N/A") .. "\n"
    end
    result = result .. "|-\n"
    i = i + 1
  end
  result = result .. "|}"
  return result
end

function p.aptitude_chart_general(frame)
  local skills = {'Armour', 'Dodging', 'Stealth',
   'Shields', 'Invocations', 'Evocations', 'Hit Points', 'Magic Points', 'Experience'}
  local header = [=[
{| class="wikitable sortable" style="font-size:75%" border=1
! scope="col" | [[Species]]
! scope="col" | [[Armour (skill)|Arm]]
! scope="col" | [[Dodging|Ddg]]
! scope="col" | [[Stealth|Sth]]
! scope="col" | [[Shields (skill)|Shd]]
! scope="col" | [[Invocations|Inv]]
! scope="col" | [[Evocations|Evo]]
! scope="col" | [[HP]]
! scope="col" | [[MP]]
! scope="col" | [[Exp]]
|-
]=]
  return aptitude_chart(skills, header)
end

function p.aptitude_chart_combat(frame)
  local skills = {'Fighting', 'Short Blades', 'Long Blades', 'Maces & Flails',
   'Axes', 'Polearms', 'Staves', 'Unarmed Combat', 'Throwing', 'Slings', 'Bows', 'Crossbows'}
  local header = [=[
{| class="wikitable sortable" style="font-size:75%" border=1
! scope="col" | [[Species]]
! scope="col" | [[Fighting|Fgt]]
! scope="col" | [[Short Blades|SBl]]
! scope="col" | [[Long Blades|LBl]]
! scope="col" | [[Maces &amp; Flails|M&amp;F]]
! scope="col" | [[Axes|Axs]]
! scope="col" | [[Polearms|Pla]]
! scope="col" | [[Staves|Stv]]
! scope="col" | [[Unarmed Combat|UC]]
! scope="col" | [[Throwing|Thr]]
! scope="col" | [[Slings|Slg]]
! scope="col" | [[Bows|Bws]]
! scope="col" | [[Crossbows|Crb]]
|-
]=]
  return aptitude_chart(skills, header)
end

function p.aptitude_chart_magic(frame)
  local skills = {'Spellcasting', 'Conjurations', 'Hexes', 'Charms',
   'Summonings', 'Necromancy', 'Translocations', 'Transmutations', 
   'Fire Magic', 'Ice Magic', 'Air Magic', 'Earth Magic', 'Poison Magic'}
  local header = [=[
{| class="wikitable sortable" style="font-size:75%" border=1
! scope="col" | [[Species]]
! scope="col" | [[Spellcasting|Spc]]
! scope="col" | [[Conjurations|Coj]]
! scope="col" | [[Hexes|Hex]]
! scope="col" | [[Charms|Cha]]
! scope="col" | [[Summonings|Sum]]
! scope="col" | [[Necromancy|Nec]]
! scope="col" | [[Translocations|Trl]]
! scope="col" | [[Transmutations|Trm]]
! scope="col" | [[Fire Magic|Fir]]
! scope="col" | [[Ice Magic|Ice]]
! scope="col" | [[Air Magic|Air]]
! scope="col" | [[Earth Magic|Ear]]
! scope="col" | [[Poison Magic|Poi]]
|-
]=]
  return aptitude_chart(skills, header)
end

return p