Monster Train Wiki
Advertisement

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

--<nowiki>
local p = {}
local all_cards = require("Module:Cards/Data").cards

---------------------------------------- Infobox
local infobox_template = [=[
{{Card
| desc = %s
| image = %s 
| essence = %s
| clan = %s
| type = %s
| rarity = %s
| cost = %s
| capacity = %s
| attack = %s
| health = %s
%s}}]=]

function p.Infobox(frame)
  local card_name = frame.args[1] ~= "" and frame.args[1] or mw.title.getCurrentTitle().text
  local card = all_cards[card_name]
  return frame:preprocess(infobox_template:format(
    card.Desc and Description(card.Desc, 20) or "",
    card.Image or "",
    card.Essence and Description(card.Essence, 20) or "", ---I have no idea what I did here, but hopefully it'll do...
    card.Clan or "",
    card.Type or "",
    card.Rarity or "",
    card.Cost or "",
    card.Capacity or "",
    card.Attack or "",
    card.Health or "",
    Tags(card)
  ))
end

---------------------------------------- Table
local table_head = [=[
{| class="wikitable sortable floatheader" style="width:99%"
! style="width:20%" | Card
! class="unsortable" | Description
! Clan
! Type
! Rarity
! data-sort-type="number" | [[File:Ember.png]]
! data-sort-type="number" | [[File:Capacity.png]]
! data-sort-type="number" | [[File:Attack.png]]
! data-sort-type="number" | [[File:Health.png]]
]=]

-- [[File:%s.png|center|64px|link=%s]]
local table_row = [=[
|-
| style="text-align:center" data-sort-value="%s" | %s[[%s]]
| style="text-align:center" | %s
| data-sort-value="%s" | %s
| data-sort-value="%s" | %s
| data-sort-value="%s" | %s
| style="text-align:center;border-right:none" | %s
| style="text-align:center;border-left:none;border-right:none" | %s
| style="text-align:center;border-left:none;border-right:none" | %s
| style="text-align:center;border-left:none" | %s
]=]

function p.Table(frame)
  local table = table_head
  local clans = { Hellhorned = 1, Awoken = 2, ["Stygian Guard"] = 3, Umbra = 4, ["Melting Remnant"] = 5, Wurmkin = 6, Clanless = 7 }
  local rarities = { Champion = 1, Common = 2, Uncommon = 3, Rare = 4 }
  
  for i, card in ipairs(Sort(all_cards)) do
    local image = (card.Type == "Spell" or card.Type == "Scourge" or card.Type == "Blight") and "" or "[[File:" .. card.Name .. ".png|center|x64px|link=" .. card.Name .. "]]"
    local type = card.Type == "Spell" and "~" or card.Type == "Scourge" and "~~" or card.Type == "Blight" and "~~~" or card.Type == "Champion" and "!" or card.Type or ""
    
    table = table .. table_row:format(
      card.Name, image, card.Name,
      card.Desc and Description(card.Desc, 24) or "-",
      clans[card.Clan], card.Clan or "",
      type, card.Type or "",
      card.Rarity and rarities[card.Rarity] or 5, card.Rarity or "-",
      card.Cost or "-",
      card.Capacity or "",
      card.Attack or "",
      card.Health or ""
    )
  end
  return frame:preprocess(table .. "|}")
end

---------------------------------------- Essence Table
local essence_table_head = [=[
{| class="wikitable sortable floatheader" style="width:99%"
! style="width:20%" | Unit
! Clan
! Rarity
! [[File:Hell's Banners.png|36px|link=Banner Unit]]
! class="unsortable" style="width:50%" | Essence
]=]

-- [[File:%s.png|center|64px|link=%s]]
local essence_table_row = [=[
|-
| style="text-align:center" data-sort-value="%s" | %s[[%s]]
| data-sort-value="%s" | %s
| data-sort-value="%s" | %s
| data-sort-value="%s" style="text-align:center;border-left:none;border-right:none" | %s
| style="text-align:center" | %s
]=]

function p.EssenceTable(frame)
  local table = essence_table_head
  local clans = { Hellhorned = 1, Awoken = 2, ["Stygian Guard"] = 3, Umbra = 4, ["Melting Remnant"] = 5, Wurmkin = 6, Clanless = 7 }
  local rarities = { Champion = 1, Common = 2, Uncommon = 3, Rare = 4 }
  
  for i, card in ipairs(Sort(all_cards)) do
  	if card.Type ~= "Spell" and card.Type ~= "Scourge" and card.Type ~= "Blight" and card.Essence then
    	local image = "[[File:" .. card.Name .. ".png|center|x64px|link=" .. card.Name .. "]]"
    	
    	table = table .. essence_table_row:format(
    		card.Name, image, card.Name,
    		clans[card.Clan], card.Clan or "",
    		card.Rarity and rarities[card.Rarity] or 5, card.Rarity or "-",
    		card.IsBanner and card.IsBanner == "true" and 0 or 1, card.IsBanner and "Yes" or "",
    		card.Essence and Description(card.Essence, 24) or "-"
    	)
    end
  end
  return frame:preprocess(table .. "|}")
end

---------------------------------------- Unit Table
local unit_table_head = [=[
{| class="wikitable sortable floatheader" style="width:99%"
! style="width:20%" | Unit
! Clan
! Rarity
! class="unsortable" | Description
! class="unsortable" | Essence<br>[[The Last Divinity (DLC)|(DLC)]]
! [[File:Hell's Banners.png|36px|link=Banner Unit]]
! data-sort-type="number" | [[File:Ember.png]]
! data-sort-type="number" | [[File:Capacity.png]]
! data-sort-type="number" | [[File:Attack.png]]
! data-sort-type="number" | [[File:Health.png]]
! Type
]=]

-- [[File:%s.png|center|64px|link=%s]]
local unit_table_row = [=[
|-
| style="text-align:center" data-sort-value="%s" | %s[[%s]]
| data-sort-value="%s" | %s
| data-sort-value="%s" | %s
| style="text-align:center" | %s
| style="text-align:center" | %s
| data-sort-value="%s" style="text-align:center;border-right:none" | %s
| style="text-align:center;border-left:none;border-right:none" | %s
| style="text-align:center;border-left:none;border-right:none" | %s
| style="text-align:center;border-left:none;border-right:none" | %s
| style="text-align:center;border-left:none" | %s
| data-sort-value="%s" | %s
]=]

function p.UnitTable(frame)
  local table = unit_table_head
  local clans = { Hellhorned = 1, Awoken = 2, ["Stygian Guard"] = 3, Umbra = 4, ["Melting Remnant"] = 5, Wurmkin = 6, Clanless = 7 }
  local rarities = { Champion = 1, Common = 2, Uncommon = 3, Rare = 4 }
  
  for i, card in ipairs(Sort(all_cards)) do
  	if card.Type ~= "Spell" and card.Type ~= "Scourge" and card.Type ~= "Blight" then
    	local image = "[[File:" .. card.Name .. ".png|center|x64px|link=" .. card.Name .. "]]"
    	local type = card.Type == "Spell" and "~" or card.Type == "Scourge" and "~~" or card.Type == "Blight" and "~~~" or card.Type == "Champion" and "!" or card.Type or ""
    
    	table = table .. unit_table_row:format(
    		card.Name, image, card.Name,
    		clans[card.Clan], card.Clan or "",
    		card.Rarity and rarities[card.Rarity] or 5, card.Rarity or "-",
    		card.Desc and Description(card.Desc, 24) or "-",
    		card.Essence and Description(card.Essence, 24) or "-",
    		card.IsBanner and card.IsBanner == "true" and 0 or 1, card.IsBanner and "Yes" or "",
    		card.Cost or "-",
    		card.Capacity or "",
    		card.Attack or "",
    		card.Health or "",
    		type, card.Type or ""
    	)
    end
  end
  return frame:preprocess(table .. "|}")
end
---------------------------------------- Spell Table
local spell_table_head = [=[
{| class="wikitable sortable floatheader" style="width:99%"
! style="width:20%" | Spell
! Clan
! Rarity
! data-sort-type="number" | [[File:Ember.png]]
! class="unsortable" style="width:50%" | Description
]=]

-- [[File:%s.png|center|64px|link=%s]]
local spell_table_row = [=[
|-
| style="text-align:center" data-sort-value="%s" | %s[[%s]]
| data-sort-value="%s" | %s
| data-sort-value="%s" | %s
| data-sort-value="%s" style="text-align:center;border-right:none;border-left:none" | %s
| style="text-align:center" | %s
]=]

function p.SpellTable(frame)
  local table = spell_table_head
  local clans = { Hellhorned = 1, Awoken = 2, ["Stygian Guard"] = 3, Umbra = 4, ["Melting Remnant"] = 5, Wurmkin = 6, Clanless = 7 }
  local rarities = { Common = 1, Uncommon = 2, Rare = 3 }
  
  for i, card in ipairs(Sort(all_cards)) do
  	if card.Type == "Spell" then
    	local image = "[[File:" .. card.Name .. ".png|center|x128px|link=" .. card.Name .. "]]"
    	
    	table = table .. spell_table_row:format(
    		card.Name, image, card.Name,
    		clans[card.Clan], card.Clan or "",
    		card.Rarity and rarities[card.Rarity] or 4, card.Rarity or "-",
    		card.Cost or -1, card.Cost or "-",
    		card.Desc and Description(card.Desc, 24) or "-"
    	)
    end
  end
  return frame:preprocess(table .. "|}")
end

---------------------------------------- Description()
local link_patterns = {}
link_patterns["Sting"] = { "Sting" }
link_patterns["Antumbra Morsel"] = { "Antumbra Morsel" }
link_patterns["Magma Morsel"] = { "Magma Morsel" }
link_patterns["Morsel Miner"] = { "Morsel Miner" }
link_patterns["Excavated Ember"] = { "Excavated Ember" }
link_patterns["Draff"] = { "Draff" }
link_patterns["Bog Fly"] = { "Bog Fly" }
link_patterns["Kinhost Pupa"] = { "Kinhost Pupa" }
link_patterns["Bog Wurmling"] = { "Bog Wurmling" }
link_patterns["Dante's Candle"] = { "Dante's Candle" }
link_patterns[":Category:Blight Cards"] = { "Blight" }
--link_patterns["Pyre"] = { "Pyre" }
--link_patterns[":Category:Imp Cards"] = { "[Ii]mp" }
--link_patterns[":Category:Morsel Cards"] = { "[Mm]orsel" }

function Description(desc, size)
  desc = desc:gsub("{{Value|", "{{Value|size=" .. size .. "|"):gsub("%[(.-)%]", "[[File:%1.png|" .. size .. "px|link=|%1]]")

  for link, patterns in pairs(link_patterns) do
    for i, v in ipairs(patterns) do
      local a, b = desc:find(v)
      if a then
        desc = desc:sub(1, a - 1) .. "[[" .. link .. "|" .. desc:sub(a, b) .. "]]" .. desc:sub(b + 1, desc:len())
        break
      end
    end
  end
  return desc
end

---------------------------------------- Tags()
function Tags(card)
  local cats = {}
  local tags = card.Tags or {}
  
  for i, tag in ipairs(tags) do
    table.insert(cats, "[[Category:" .. tag .. "]]")
  end
  
  if card.Capacity then table.insert(tags, 1, "Unit") end
  if card.Rarity then table.insert(tags, 1, card.Rarity) end
  if card.Type then table.insert(tags, 1, card.Type) end
  if card.Clan then table.insert(tags, 1, card.Clan) end
  
  if card.Cost then
  	if card.Cost == "-" then
  		--nothing happens
  	elseif card.Cost ~= "X" and card.Cost >= 4 then
  		table.insert(tags, 1, "4+ Cost") 
  	else
  		table.insert(tags, 1, card.Cost .. " Cost") 
  	end
  end
  
  for i, tag in ipairs(tags) do
    table.insert(cats, "[[Category:" .. tag .. " Cards]]")
  end
  return table.concat(cats)
end

---------------------------------------- Nav
local nav_head = [=[
{| class="navbox mw-collapsible mw-collapsed"
|-
! [[Cards]]
]=]

local nav_row = [=[
|-
| %s
|-
| %s
]=]

function p.Nav(frame)
  local nav = nav_head
  local cards = { Hellhorned = {}, Awoken = {}, ["Stygian Guard"] = {}, Umbra = {}, ["Melting Remnant"] = {}, Wurmkin = {}, Clanless = {} }
  
  for name, card in pairs(all_cards) do
    table.insert(cards[card.Clan], name)
  end
  for clan in pairs(cards) do
    table.sort(cards[clan])
  end
  
  for i, clan in ipairs({ "Hellhorned", "Awoken", "Stygian Guard", "Umbra", "Melting Remnant", "Wurmkin", "Clanless" }) do
    local cardsByRarity = { Champion = {}, Common = {}, Uncommon = {}, Rare = {}, Other = {} }
    local stringTable = {}
    for j, name in ipairs(cards[clan]) do
    	local rarity = all_cards[name].Rarity
    	if rarity == nil or rarity == "Blight" or rarity == "Scourge" then
    		rarity = "Other"
    	end
    	table.insert(cardsByRarity[rarity], "[[" .. name .. "]]")
    end
    for i, rarity in ipairs({ "Champion", "Common", "Uncommon", "Rare", "Other" }) do
    	if #cardsByRarity[rarity] > 0 then
    		table.insert(stringTable, table.concat(cardsByRarity[rarity], " • "))
    	end
    end
    nav = nav .. nav_row:format(
      "[[:Category:" .. clan .. " Cards|" .. clan .. "]]",
      table.concat(stringTable, " <hr> ")
      --table.concat(cards[clan], " • ")
    )
  end
  return nav .. "|}"
end

---------------------------------------- Count
function p.Count()
  local c = 0
  for i in pairs(all_cards) do
    c = c + 1
  end
  return c
end

---------------------------------------- Sort()
function Sort(list)
  local cards = {}
  for name, card in pairs(list) do
    card.Name = name
    table.insert(cards, card)
  end
  local clans = { Hellhorned = 1, Awoken = 2, ["Stygian Guard"] = 3, Umbra = 4, ["Melting Remnant"] = 5, Wurmkin = 6, Clanless = 7 }
  table.sort(cards, function(a, b)
    local atype = a.Type == "Spell" and "~" or a.Type == "Scourge" and "~~" or a.Type == "Blight" and "~~~" or a.Type == "Champion" and "!" or a.Type or ""
    local btype = b.Type == "Spell" and "~" or b.Type == "Scourge" and "~~" or b.Type == "Blight" and "~~~" or b.Type == "Champion" and "!" or b.Type or ""
    return a.Clan == b.Clan and a.Type == b.Type and a.Name < b.Name or a.Clan == b.Clan and atype < btype or clans[a.Clan] < clans[b.Clan]
  end)
  return cards
end

return p
--</nowiki>
Advertisement