Difference between revisions of "Module:HalidomMap"

From Dragalia Lost Wiki
Jump to navigation Jump to search
imported>Elaeagnifolia
>Mornsta-gpuser
 
(121 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
 +
--explode-like split function taken from LUA documentation
 +
function split(d,p)
 +
  local t, ll
 +
  t={}
 +
  ll=0
 +
  if(#p == 1) then
 +
      return {p}
 +
  end
 +
  while true do
 +
      l = string.find(p, d, ll, true) -- find the next d in the string
 +
      if l ~= nil then -- if "not not" found then..
 +
        table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
 +
        ll = l + 1 -- save just after where we found it for searching next time.
 +
      else
 +
        table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
 +
        break -- Break at end, as it should be, according to the lua manual.
 +
      end
 +
  end
 +
  return t
 +
end
 +
 +
--renders the table of different cell colors based on given arguments
 
p._halidomMap = function (args)
 
p._halidomMap = function (args)
   local tbl = mw.html.create('table'):css('text-align', 'center')
+
  -- Create the table
 +
   local tbl = mw.html.create('table'):css('text-align', 'center'):css('table-layout','fixed'):css('border-collapse', 'collapse')
  
 +
  -- Loop through all the template arguments ("rows of the halidom map")
 
   for idx, row in ipairs(args) do
 
   for idx, row in ipairs(args) do
 +
    -- Create the table row
 
     local tr = tbl:tag('tr')
 
     local tr = tbl:tag('tr')
     local cellValues = split(row, ",")
+
 
 +
    -- Split the cell values
 +
     local cellValues = split(",", row)
 +
 
 +
    -- Loop through all cell values in the row
 
     for idx2, cellValue in ipairs(cellValues) do
 
     for idx2, cellValue in ipairs(cellValues) do
 +
      -- Create the table cell with the background color based on the cell value
 
       local td = tr:tag('td')
 
       local td = tr:tag('td')
         :css('width', '15px')
+
         :css('width', '7px')
         :css('height', '15px')
+
         :css('height', '7px')
 +
        --:css('border', '1px solid #c0c0c0')
 
         :css('background-color', getCellColor(cellValue))
 
         :css('background-color', getCellColor(cellValue))
 
     end
 
     end
     local td = tr:tag('td'):wikitext(row)
+
     --local td = tr:tag('td'):wikitext(row)
 
   end
 
   end
  
Line 19: Line 50:
 
end
 
end
  
local function split(str,pat)
+
--- Function that converts a given value to a color
  local tbl = {}
+
-- @param value
  str:gsub(pat, function(x) tbl[#tbl+1]=x end)
+
-- @return Hex color
  return tbl
 
end
 
 
 
--- Function that converts months from text to numbers
 
-- @param n
 
-- @return The numerical value of the month provided
 
 
function getCellColor(value)
 
function getCellColor(value)
 
   local halidomValueToColor = {
 
   local halidomValueToColor = {
     ["255"] = "#fff",
+
     ["255"] = "#466999",
 
     ["0"] = "#000",
 
     ["0"] = "#000",
 
     ["1"] = "red",
 
     ["1"] = "red",
     ["2"] = "orange",
+
     ["2"] = "#f38500",
     ["3"] = "yellow",
+
     ["3"] = "#e6e600",
     ["4"] = "greenyellow",
+
     ["4"] = "lime",
 
     ["5"] = "green",
 
     ["5"] = "green",
 
     ["6"] = "turquoise",
 
     ["6"] = "turquoise",
Line 42: Line 67:
 
     ["9"] = "brown",
 
     ["9"] = "brown",
 
     ["10"] = "#808080",
 
     ["10"] = "#808080",
     ["11"] = "#808080",
+
     ["11"] = "#696969",
 
     ["101"] = "lightpink",
 
     ["101"] = "lightpink",
     ["102"] = "lightsalmon",
+
     ["102"] = "#ffd29b",
     ["103"] = "lightyellow",
+
     ["103"] = "#ffff8e",
 
     ["104"] = "#e0ffb3",
 
     ["104"] = "#e0ffb3",
 
     ["105"] = "lightgreen",
 
     ["105"] = "lightgreen",
Line 53: Line 78:
 
     ["109"] = "burlywood",
 
     ["109"] = "burlywood",
 
     ["110"] = "#d3d3d3",
 
     ["110"] = "#d3d3d3",
     ["111"] = "#d3d3d3"
+
     ["111"] = "#C0C0C0"
 
   }
 
   }
 
   if halidomValueToColor[value] ~= nil then
 
   if halidomValueToColor[value] ~= nil then

Latest revision as of 14:00, 24 January 2019

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

local p = {}

--explode-like split function taken from LUA documentation
function split(d,p)
   local t, ll
   t={}
   ll=0
   if(#p == 1) then
      return {p}
   end
   while true do
      l = string.find(p, d, ll, true) -- find the next d in the string
      if l ~= nil then -- if "not not" found then..
         table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
         ll = l + 1 -- save just after where we found it for searching next time.
      else
         table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
         break -- Break at end, as it should be, according to the lua manual.
      end
   end
   return t
end

--renders the table of different cell colors based on given arguments
p._halidomMap = function (args)
  -- Create the table
  local tbl = mw.html.create('table'):css('text-align', 'center'):css('table-layout','fixed'):css('border-collapse', 'collapse')

  -- Loop through all the template arguments ("rows of the halidom map")
  for idx, row in ipairs(args) do
    -- Create the table row
    local tr = tbl:tag('tr')

    -- Split the cell values
    local cellValues = split(",", row)

    -- Loop through all cell values in the row
    for idx2, cellValue in ipairs(cellValues) do
      -- Create the table cell with the background color based on the cell value
      local td = tr:tag('td')
        :css('width', '7px')
        :css('height', '7px')
        --:css('border', '1px solid #c0c0c0')
        :css('background-color', getCellColor(cellValue))
    end
    --local td = tr:tag('td'):wikitext(row)
  end

  return tostring(tbl)
end

--- Function that converts a given value to a color
-- @param value
-- @return Hex color
function getCellColor(value)
  local halidomValueToColor = {
    ["255"] = "#466999",
    ["0"] = "#000",
    ["1"] = "red",
    ["2"] = "#f38500",
    ["3"] = "#e6e600",
    ["4"] = "lime",
    ["5"] = "green",
    ["6"] = "turquoise",
    ["7"] = "blue",
    ["8"] = "purple",
    ["9"] = "brown",
    ["10"] = "#808080",
    ["11"] = "#696969",
    ["101"] = "lightpink",
    ["102"] = "#ffd29b",
    ["103"] = "#ffff8e",
    ["104"] = "#e0ffb3",
    ["105"] = "lightgreen",
    ["106"] = "#ccfeff",
    ["107"] = "lightblue",
    ["108"] = "#ffb3ff",
    ["109"] = "burlywood",
    ["110"] = "#d3d3d3",
    ["111"] = "#C0C0C0"
  }
  if halidomValueToColor[value] ~= nil then
    return halidomValueToColor[value]
  end
  return value
end

p.halidomMap = function (frame)
  local getArgs = require 'Module:Arguments'.getArgs
  return p._halidomMap(getArgs(frame, {wrappers = 'Template:HalidomMap'}))
end

return p