Module:Role Advancement Table: Difference between revisions

From Drifter's Wiki TEST
Jump to navigation Jump to search
(Add alignment style to level column)
mNo edit summary
 
(8 intermediate revisions by the same user not shown)
Line 2: Line 2:


local headers = '!Level\
local headers = '!Level\
![[Health]]</br>Total (increment)\
![[Health]]<br />Total (increment)\
![[Armor Weight|A.W.]]</br>Total (increment)\
![[Armor Weight|A.W.]]<br />Total (increment)\
![[Skills]]\
![[Skills]]\
'
'


local tableBegin = '{| class="wikitable"\
local tableBegin = '{| class="wikitable" style="text-align: center;"\
|+ {{{name|{{PAGENAME}} Level Advancement}}}\
'
 
local tableCaption = '|+ {{ROOTPAGENAME}} Level Advancement\
'
'


Line 20: Line 22:
function p.makeHealthOrWeight( value, multiplier )
function p.makeHealthOrWeight( value, multiplier )
   if value == nil then return "" end
   if value == nil then return "" end
   local result, increment
   local result, increment
   if multiplier == 0 then
   if multiplier == 0 then
Line 30: Line 33:


   return tostring( math.floor( result )) .. " (+" .. increment .. ")"
   return tostring( math.floor( result )) .. " (+" .. increment .. ")"
end
function p.makeIncrement( incremental, multiplier)
end
end


function p.makeRows( text, start, hp, aw )
function p.makeRows( text, start, hp, aw )
local buffer, count, row, rows
local buffer, count, row, skills
   local health = { values }
   local health = { values }
   local weight = {}
   local weight = {}


rows = p.splitTextWithNulls( text )
skills = p.splitTextWithNulls( text )


   if start == nil then
   if start == nil then
       if #rows > 6 then
       if #skills > 6 then
         row = 6
         row = 6
       else
       else
Line 52: Line 52:
   end
   end


   if hp == nil then
   if hp == nil or #hp == 0 then
       health = nil
       health = nil
   else
   else
Line 65: Line 65:
         health['min'] = 0
         health['min'] = 0
       end
       end
       health['inc'] = health['max'] / (#rows - 1)
       health['inc'] = health['max'] / (#skills - 1)
   end
   end


   if aw == nil then
   if aw == nil or #aw == 0 then
       weight = nil
       weight = nil
   else
   else
Line 81: Line 81:
         weight['min'] = 0
         weight['min'] = 0
       end
       end
       weight['inc'] = weight['max'] / (#rows - 1)
       weight['inc'] = weight['max'] / (#skills - 1)
     end
     end


   buffer = ""
   buffer = ""
   count = 0
   count = 0
   for _, v in ipairs(rows) do
   for _, v in ipairs(skills) do
         buffer = buffer .. '|-' .. "\n" .. '| style="text-align:right;" |' .. tostring(row) .. "\n"
         buffer = buffer .. '|-' .. "\n" .. '| style="text-align:right;" |' .. tostring(row) .. "\n"
         buffer = buffer .. '|' .. p.makeHealthOrWeight( health, count ) .. "\n"
         buffer = buffer .. '|' .. p.makeHealthOrWeight( health, count ) .. "\n"
         buffer = buffer .. '|' .. p.makeHealthOrWeight( weight, count ) .. "\n"
         buffer = buffer .. '|' .. p.makeHealthOrWeight( weight, count ) .. "\n"
         buffer = buffer .. '|' .. p.makeSkills(v) .. "\n"
         buffer = buffer .. '| style="text-align:left;" |' .. p.makeSkills(v) .. "\n"


         count = count + 1
         count = count + 1
Line 104: Line 104:


   for _, v in ipairs(skills) do
   for _, v in ipairs(skills) do
       buffer = buffer .. "[[" .. v .. "]]</br>\n"
       buffer = buffer .. "[[" .. v .. "]]<br />\n"
   end
   end


   return string.sub(buffer, 1, -7)
   return string.sub(buffer, 1, -8)
end
end


function p.makeTable( frame )
function p.makeTable( frame )
   local buffer = tableBegin
   if frame.args['skills'] == nil or frame.args['skills'] == '' then
  local start
      return ''
  else
      local start = p.processStart( frame )
      local caption = p.processCaption( frame )
      local headers = p.processHeaders( frame )
      local rows = p.makeRows( frame.args['skills'], start, frame.args['hp'], frame.args['aw'] )


      return tableBegin .. caption .. headers .. rows .. tableEnd
  end
end
function p.processCaption( frame )
  local buffer
  if frame.args['name'] == nil then
      buffer = tableCaption
  else
      buffer = '|+ ' .. frame.args['name'] .. "'s Level Advancement\
"
  end
  return buffer
end
function p.processHeaders( frame )
  local buffer
  if frame['args']['headers'] == nil then
        buffer = headers
      else
        buffer = makeHeaders( frame['args']['headers'] )
  end
  return buffer
end
function p.processStart( frame )
  local start = nil
   if frame.args['start'] == nil then
   if frame.args['start'] == nil then
      start = nil
       if ( frame.args['type'] ~= nil ) and ( frame.args['type'] ~= '' ) then
       if ( frame.args['type'] ~= nil ) and ( frame.args['type'] ~= '' ) then
         if string.lower(frame.args['type']) == 'base' then
         if string.lower(frame.args['type']) == 'base' then
Line 127: Line 160:
   end
   end


   if frame['args']['headers'] == nil then
   return start
        buffer = buffer .. headers
      else
        buffer = buffer .. makeHeaders( frame['args']['headers'] )
  end
 
  buffer = buffer .. p.makeRows( frame.args['rows'], start, frame.args['health'], frame.args['weight'] ) .. tableEnd
 
  return buffer
end
end



Latest revision as of 02:26, 10 November 2022

Documentation for this module may be created at Module:Role Advancement Table/doc

p = {} -- p stands for package

local headers = '!Level\
![[Health]]<br />Total (increment)\
![[Armor Weight|A.W.]]<br />Total (increment)\
![[Skills]]\
'

local tableBegin = '{| class="wikitable" style="text-align: center;"\
'

local tableCaption = '|+ {{ROOTPAGENAME}} Level Advancement\
'

local tableEnd = '|}\
'

function p.makeHeaders( frame )
    return "Not Implemented Yet!!"
end

function p.makeHealthOrWeight( value, multiplier )
   if value == nil then return "" end

   local result, increment
   if multiplier == 0 then
      result = 0
      increment = "0"
   else
      result = value['inc'] * multiplier
      increment = tostring( value['inc'] )
   end

   return tostring( math.floor( result )) .. " (+" .. increment .. ")"
end

function p.makeRows( text, start, hp, aw )
	local buffer, count, row, skills
   local health = { values }
   local weight = {}

	skills = p.splitTextWithNulls( text )

   if start == nil then
      if #skills > 6 then
         row = 6
      else
         row = 1
      end
   else
	   row = start
   end

   if hp == nil or #hp == 0 then
      health = nil
   else
      for min, max in hp:gmatch "(%d+)-%s*(%d+)" do
         health['min'] = tonumber( min )
         health['max'] = tonumber( max )
         --print("min: " .. min .. ", max: " .. max)
      end

      if health['min'] > 0 then
         health['max'] = health['max'] - health['min']
         health['min'] = 0
      end
      health['inc'] = health['max'] / (#skills - 1)
   end

   if aw == nil or #aw == 0 then
      weight = nil
   else
      for min, max in aw:gmatch "(%d+)-%s*(%d+)" do
         weight['min'] = tonumber( min )
         weight['max'] = tonumber( max )
         --print("min: " .. min .. ", max: " .. max)
      end

      if weight['min'] > 0 then
         weight['max'] = weight['max'] - weight['min']
         weight['min'] = 0
      end
      weight['inc'] = weight['max'] / (#skills - 1)
    end

   buffer = ""
   count = 0
   for _, v in ipairs(skills) do
         buffer = buffer .. '|-' .. "\n" .. '| style="text-align:right;" |' .. tostring(row) .. "\n"
         buffer = buffer .. '|' .. p.makeHealthOrWeight( health, count ) .. "\n"
         buffer = buffer .. '|' .. p.makeHealthOrWeight( weight, count ) .. "\n"
         buffer = buffer .. '| style="text-align:left;" |' .. p.makeSkills(v) .. "\n"

         count = count + 1
         row = row + 1
	end

	return buffer
end

function p.makeSkills( text )
   local buffer = ""
   local skills = p.splitTextWithNulls( text, "," )

   for _, v in ipairs(skills) do
      buffer = buffer .. "[[" .. v .. "]]<br />\n"
   end

   return string.sub(buffer, 1, -8)
end

function p.makeTable( frame )
   if frame.args['skills'] == nil or frame.args['skills'] == '' then
      return ''
   else
      local start = p.processStart( frame )
      local caption = p.processCaption( frame )
      local headers = p.processHeaders( frame )
      local rows = p.makeRows( frame.args['skills'], start, frame.args['hp'], frame.args['aw'] )

      return tableBegin .. caption .. headers .. rows .. tableEnd
   end
end

function p.processCaption( frame )
   local buffer
   if frame.args['name'] == nil then
      buffer = tableCaption
   else
      buffer = '|+ ' .. frame.args['name'] .. "'s Level Advancement\
"
   end

   return buffer
end

function p.processHeaders( frame )
   local buffer
   if frame['args']['headers'] == nil then
         buffer = headers
      else
         buffer = makeHeaders( frame['args']['headers'] )
   end

   return buffer
end

function p.processStart( frame )
   local start = nil
   if frame.args['start'] == nil then
      if ( frame.args['type'] ~= nil ) and ( frame.args['type'] ~= '' ) then
         if string.lower(frame.args['type']) == 'base' then
            start = 1
         elseif string.lower(frame.args['type']) == 'spec' then
            start = 6
         end
      end
   else
      start = tonumber( frame.args['start'] )
   end

   return start
end

function p.splitTextWithNulls( text, seperator )
   local buffer = ''
   local length = string.len(text)
   local pos = 1
   local results = {}

   if length > 0 then
      if seperator == nil then
         seperator = "|"
      end

      while pos <= length do
         local char = string.sub(text, pos, pos)

         if (char ~= seperator) then
            buffer = buffer .. char
         else
            table.insert(results, buffer)
            buffer = ""
         end

         if (pos == length) then
            table.insert(results, buffer)
         end

         pos = pos + 1
      end
   end

   return results
end

return p