Module:Decks

local utils = require("Module:TemplateUtils") local p = {} local totalNumberOfCards = 5 local totalNumberOfOtherCards = 1

local setNames = {} setNames["XLN"]="Ixalan"

function p.SingleCard(name) local cards = utils.RecreateTable(mw.loadData("Module:Data/Cards")) for i = 1, totalNumberOfCards do   	if cards[i].Name == name then return cards[i] end end end local function PT(card) if ((card.Power == nil) or (card.Toughness == nil)) then if card.Loyalty ~= nil then return "("..card.Loyalty..")" else return "" end else return "("..card.Power.."/"..card.Toughness..")" end end

local function ExpansionSymbol(card) return "" end

local function TableContains(t,item) if(not t) or (not item) then return false end for _,v in pairs(t) do       if v == item then return true end end return false end

local function splitString(inputstr, sep) if sep == nil then sep = "$" end local t={} ; i=1 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do               t[i] = str i = i + 1 end return t end

local function ConcatTables(target,source) if not source then return end for _,v in pairs(source) do       table.insert(target,v) end end

function p.GetCardCategories(card) local categories = {} table.insert(categories,"Cards") table.insert(categories,setNames[card.SetCode]) ConcatTables(categories,card.Colors) table.insert(categories,card.Rarity) ConcatTables(categories,card.SuperTypes) ConcatTables(categories,card.Types) ConcatTables(categories,card.SubTypes) if card.Watermark then table.insert(categories,card.Watermark) end local s = "" for _,v in pairs(categories) do        s = s .. ""   end return s end

local Land = {} local Creature = {} local Artifact = {} local Enchantment = {} local Instant = {} local Sorcery = {} local Planeswalker = {} local errors = {} local decklistTemplate = [=[   %s

%s]=]

local function SortListIntoTypes(list) for _,t in pairs(list) do       local split = mw.text.split(t," ") local num, name = table.remove(split,1), table.concat(split," ") if tonumber(num) == nil then num = 0 end local card = p.SingleCardNonSensitive(name) if card then if TableContains(card.Types,"Land") then table.insert(Land,{num,card}) elseif TableContains(card.Types,"Creature") then table.insert(Creature,{num,card}) elseif TableContains(card.Types,"Artifact") then table.insert(Artifact,{num,card}) elseif TableContains(card.Types,"Enchantment") then table.insert(Enchantment,{num,card}) elseif TableContains(card.Types,"Instant") then table.insert(Instant,{num,card}) elseif TableContains(card.Types,"Sorcery") then table.insert(Sorcery,{num,card}) elseif TableContains(card.Types,"Planeswalker") then table.insert(Planeswalker,{num,card}) else table.insert(errors,{num,{Name=name}}) mw.log(num.." "..name) end else table.insert(errors,{num,{Name=name}}) mw.log(num.." "..name) end end end

local function LogTypes mw.log("Land : "..#Land) mw.log("Creature : "..#Creature) mw.log("Artifact : "..#Artifact) mw.log("Enchantment : "..#Enchantment) mw.log("Instant : "..#Instant) mw.log("Sorcery : "..#Sorcery) mw.log("Planeswalker : "..#Planeswalker) mw.log("errors : "..#errors) end

function p.SingleCardNonSensitive(name) name = string.lower(name) local cards = utils.RecreateTable(mw.loadData("Module:Data/Cards")) for i = 1, totalNumberOfCards do   	if string.lower(cards[i].Name) == name then local foundCard = utils.MakeTableWriteable(cards[i]) foundCard.Playable = true return foundCard end end local otherCards = utils.RecreateTable(mw.loadData("Module:Data/OtherCards")) for i = 1, totalNumberOfOtherCards do   	if string.lower(otherCards[i].Name) == name then local foundOtherCard = utils.MakeTableWriteable(otherCards[i]); foundOtherCard.Playable = false return foundOtherCard end end end

local buffer = ""

local function Write(s) buffer = buffer..s end

local function WriteLine(s) buffer = buffer..s.."\n" end

local function WriteCardsFromType(typeCards,typeName) if typeCards[1] and typeCards[1][2].cmc then table.sort(typeCards,function(a,b) return (a[2].cmc < b[2].cmc) or ((a[2].cmc == b[2].cmc) and (a[2].Name < b[2].Name)) end) end local numType = 0 for i = 1, #typeCards do       numType = numType + typeCards[i][1] end if #typeCards > 0 then WriteLine(" "..numType.." "..typeName.."  ") for i = 1, #typeCards do           if (typeCards[i][2].Playable) then WriteLine(typeCards[i][1].." ") else WriteLine(typeCards[i][1].." ") end end end end

local function WriteOtherCards(typeCards) local numType = 0 for i = 1, #typeCards do       numType = numType + typeCards[i][1] end if #typeCards > 0 then WriteLine(" "..numType.." Others  ") for i = 1, #typeCards do           WriteLine(typeCards[i][1].."  ") end end end

local function WriteTypeLists

WriteCardsFromType(Land,"Lands ") WriteCardsFromType(Creature,"Creatures ") WriteCardsFromType(Artifact,"Artifacts ") WriteCardsFromType(Enchantment,"Enchantments ") WriteCardsFromType(Instant,"Instants ") WriteCardsFromType(Sorcery,"Sorceries ") WriteCardsFromType(Planeswalker,"Planeswalkers ") WriteOtherCards(errors) end

local function GenerateDeckFromList(name,list) SortListIntoTypes(list) WriteTypeLists return string.format(decklistTemplate,name,buffer) end

function p.TestGenerateDeckFromList(name,inputList) local list = mw.text.split( inputList, "\n" ) return (GenerateDeckFromList(name,list)) end

function p.GenerateDeckFromList(frame) local args = utils.RecreateTable(frame:getParent.args) local list = mw.text.split( args.Deck, "\n" ) return frame:preprocess(GenerateDeckFromList(args.Name,list)) end

return p