Module:Decks

local utils = require("Module:TemplateUtils") local json = require("Dev:Json") local p = {} local totalNumberOfCards = 485 local totalNumberOfOtherCards = 4

local setNames = {} setNames["XLN"]="Ixalan" setNames["RIX"]="Rivals of 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 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 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 ParseCardEntry(entry) local pos, _ = string.find(entry, "%(")	if pos ~= nil and pos > 2 then		entry = string.sub(entry, 1, pos - 2)	end   local intNumber, cardName    pos, _ = string.find(entry, " ")    if pos ~= nil and pos > 1 then        local strNumber = string.sub(entry, 1, pos - 1)        intNumber = tonumber(strNumber)        if intNumber ~= nil then            cardName = string.sub(entry, pos + 1)        else            intNumber = 0            cardName = entry        end    else        intNumber = 0        cardName = entry    end	return intNumber, cardName end local function SortListIntoTypes(list)    for _,t in pairs(list) do        local num, name = ParseCardEntry(t)        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}}) end else table.insert(errors,{num,{Name=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 GetAdditionalData(cardList) local arenaExport = "" local alternatives = "" local cardlist = {} for _,cardEntry in pairs(cardList) do        local number, name = ParseCardEntry(cardEntry) local card = p.SingleCardNonSensitive(name) if card then local carddata = { num=number; colors=card.Colors; cmc=card.cmc; types=card.Types } table.insert(cardlist, carddata) arenaExport = arenaExport .. number .. " " ..                card.Name .. " (" .. card.SetCode .. ") " .. string.match(card.CardNumber, "%d+") .. "\n" if card.Sets ~= nil then for _,set in pairs(card.Sets) do                   alternatives = alternatives .. card.Name .. " (" .. set.Set .. ") " .. set.CardNumber .. "\n" end end end end local cardJson = json.encode(cardlist) return arenaExport, alternatives, cardJson end

local function CardJsonDataSection(cardJson) return "\n \n" end

local function ArenaExportSection(exportText, id) if exportText == "" then return "" else return "\n" .. exportText .. " \n" end end

local function GenerateDeckFromList(name,list) SortListIntoTypes(list) WriteTypeLists local arenaExport, altExport, cardJson = GetAdditionalData(list) return string.format(decklistTemplate, name, buffer) .. CardJsonDataSection(cardJson) .. ArenaExportSection(arenaExport, "mdw-arena-export-src") .. ArenaExportSection(altExport, "mdw-arena-export-src-altenative") 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