Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initial variable and function declarations
- local tArgs = {...}
- local cardsMain = {}
- local cardsMainKeys = {}
- local currentCardData = {}
- local eventParameters = {}
- local currentCardIndex = 1
- local wrappedLoreCount = 0
- -- Used for loading a card file, localCardsMain is the table of all the currently loaded cards and path is the path of the card file to load. Returns localCardsMain with the cards from the card file added.
- local function registerCardFile(localCardsMain,path)
- local handle = io.open(path,"r")
- for line in handle:lines() do
- local cardData = textutils.unserialize(line)
- localCardsMain[cardData["id"]] = cardData
- end
- handle:close()
- return localCardsMain
- end
- -- Saves localCardsMain as a card file to path
- local function saveCardFile(localCardsMain,path)
- local handle = io.open(path,"w")
- for k,v in pairs(localCardsMain) do
- handle:write(textutils.serialize(v).."\n")
- end
- handle:close()
- end
- -- Returns a table containing a list of keys from localCardsMain
- local function generateKeyList(localTable)
- local keys = {}
- for k,v in pairs(localTable) do
- table.insert(keys,k)
- end
- table.sort(keys)
- return keys
- end
- -- Returns a 6-character string representing the level of a monster from the integer level
- local function getLevelString(level)
- local levelString = string.rep(":",math.max(0,level-6)) .. string.rep(".",math.min(level,12-level))
- return string.rep(" ",math.ceil((6-string.len(levelString))/2)) .. levelString .. string.rep(" ",math.floor((6-string.len(levelString))/2))
- end
- -- Returns a table containing a wrapped version of the card lore
- local function wrapLore(lore)
- currentCardData["wrappedLore"] = {}
- for i = 1, #lore, 31 do
- table.insert(currentCardData["wrappedLore"],string.sub(lore,i,i+30))
- end
- return currentCardData, #currentCardData["wrappedLore"]
- end
- -- Call when the card index changes
- local function updateIndex()
- -- Localise the current card's data
- currentCardData = cardsMain[cardsMainKeys[currentCardIndex]]
- -- Add a temporary field in currentCardData with a wrapped version of the lore text
- currentCardData, wrappedLoreCount = wrapLore(currentCardData["lore"])
- end
- -- Create a new card entry
- local function createNew()
- cardsMain[1] = {
- ["cardType"]="Monster",
- ["name"]="New Card",
- ["image"]={[1]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[2]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[3]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[4]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[5]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},},
- ["lore"]="This is a new card.",
- ["id"]=1,
- ["level"]=1,
- ["attribute"]="DARK",
- ["type"]="Aqua",
- ["atk"]=0,
- ["def"]=0
- }
- cardsMainKeys = generateKeyList(cardsMain)
- currentCardIndex = 1
- updateIndex()
- end
- -- Splits the card type into its components, i.e. "Monster/Synchro/Effect" to {"Monster","Synchro","Effect"}
- local function parseCardType(cardType)
- local parsedType = {}
- for word in string.gmatch(cardType,"[^/]+") do
- table.insert(parsedType,word)
- end
- return parsedType
- end
- -- Draws the screen
- local function draw(cardsMain,currentCardIndex)
- term.clear() -- Clear screen, declare supporting tables and functions
- local parsedType = parseCardType(currentCardData["cardType"])
- -- Set the text and background colour
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- -- Colour choosing based on the cardType, WIP
- -- Use a metatable for normal monsters, i.e. nothing written next to the type
- local metatable = {
- __index = function(t,k)
- return {colors.yellow, colors.yellow}
- end
- }
- local monsterCardColours = {
- ["Effect"] = {colors.orange, colors.yellow},
- ["Ritual"] = {colors.lightBlue, colors.lightBlue},
- ["Fusion"] = {colors.purple, colors.magenta},
- ["Xyz"] = {colors.gray, colors.lightGray},
- ["Synchro"] = {colors.white, colors.white}
- }
- local metaMonsterCardColours = setmetatable(monsterCardColours,metatable)
- local cardColours = {
- ["Monster"] = metaMonsterCardColours[parsedType[2]],
- ["Spell"] = {colors.cyan,colors.lightBlue},
- ["Trap"] = {colors.magenta,colors.pink}
- }
- -- Draw the backing of the card
- for i = 2, 11 do
- paintutils.drawLine(2,i,11,i,cardColours[parsedType[1]][1])
- end
- -- Draw the backing of the image
- for i = 3, 9 do
- paintutils.drawLine(3,i,10,i,colors.gray)
- end
- -- Draw the image itself
- paintutils.drawImage(currentCardData["image"],4,4)
- -- Draw the level stars / spell/trap type
- term.setBackgroundColor(cardColours[parsedType[1]][2])
- term.setCursorPos(4,10)
- if parsedType[1] == "Monster" then
- term.setTextColor(colors.red)
- write(getLevelString(currentCardData["level"]))
- term.setTextColor(colors.white)
- else
- local substring = string.sub(parsedType[2] or "",1,6)
- write(substring..string.rep(" ",6-#substring))
- end
- term.setBackgroundColor(colors.black)
- -- Draw the navigation arrows
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(3,13)
- write("<")
- term.setCursorPos(5,13)
- write(">")
- -- Draw the 'New' button
- term.setCursorPos(8,13)
- write("New")
- -- Draw the 'Import' button
- term.setCursorPos(4,15)
- write("Import")
- -- Draw the 'Save' button
- term.setCursorPos(5,16)
- write("Save")
- term.setBackgroundColor(colors.black)
- -- Draw card values
- term.setCursorPos(14,2)
- write("@Card Type: " .. currentCardData["cardType"])
- term.setCursorPos(14,3)
- write("@Name: " .. currentCardData["name"])
- term.setCursorPos(14,4)
- write("@Lore: ")
- for k,v in pairs(currentCardData["wrappedLore"]) do
- write(v)
- term.setCursorPos(21,4+k)
- end
- term.setCursorPos(14,4+wrappedLoreCount)
- write("@ID: " .. currentCardData["id"])
- if parsedType[1] == "Monster" then
- term.setCursorPos(14,5+wrappedLoreCount)
- write("@Level: " .. currentCardData["level"])
- term.setCursorPos(14,6+wrappedLoreCount)
- write("@Attribute: " .. currentCardData["attribute"])
- term.setCursorPos(14,7+wrappedLoreCount)
- write("@Type: " .. currentCardData["type"])
- term.setCursorPos(14,8+wrappedLoreCount)
- write("@ATK: " .. currentCardData["atk"])
- term.setCursorPos(14,9+wrappedLoreCount)
- write("@DEF: " .. currentCardData["def"])
- end
- end
- -- Functions to be called when certain events occur
- local eventResponseTable = {
- ["mouse_click"]=function(args)
- local coordinateEvents = {
- [{3,3,13,13}]=function() if currentCardIndex > 1 then currentCardIndex = currentCardIndex - 1 updateIndex() end end,
- [{5,5,13,13}]=function() if currentCardIndex < #cardsMainKeys then currentCardIndex = currentCardIndex + 1 updateIndex() end end,
- [{8,10,13,13}]=function() term.setCursorPos(2,18) write("Create a new entry? (Y/[N]): ") if string.upper(read()) == "Y" then createNew() end end,
- [{4,11,15,15}]=function() term.setCursorPos(2,18) write("Image Path (Absolute): ") currentCardData["image"] = paintutils.loadImage(read()) end,
- [{5,8,16,16}]=function() term.setCursorPos(2,18) write("Are you sure you want to save? (Y/[N]): ") if string.upper(read()) == "Y" then saveCardFile(cardsMain,shell.resolve(tArgs[1])) end end,
- [{14,14,2,2}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["cardType"] = read() end,
- [{14,14,3,3}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["name"] = read() end,
- [{14,14,4,4}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["lore"] = read() currentCardData, wrappedLoreCount = wrapLore(currentCardData["lore"]) end,
- [{14,14,4+wrappedLoreCount,4+wrappedLoreCount}]=function()
- term.setCursorPos(2,18)
- write("New Value: ")
- local oldID = currentCardData["id"]
- currentCardData["id"] = tonumber(read())
- cardsMain[currentCardData["id"]] = currentCardData
- cardsMain[oldID] = nil
- cardsMainKeys = generateKeyList(cardsMain)
- end,
- [{14,14,5+wrappedLoreCount,5+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["level"] = tonumber(read()) end,
- [{14,14,6+wrappedLoreCount,6+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["attribute"] = read() end,
- [{14,14,7+wrappedLoreCount,7+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["type"] = read() end,
- [{14,14,8+wrappedLoreCount,8+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["atk"] = tonumber(read()) end,
- [{14,14,9+wrappedLoreCount,9+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["def"] = tonumber(read()) end
- }
- if args[2] == 1 then
- for k,v in pairs(coordinateEvents) do
- if args[3] >= k[1] and args[3] <= k[2] and args[4] >= k[3] and args[4] <= k[4] then
- v()
- break
- end
- end
- end
- end
- }
- -- Argument checking
- if #tArgs == 0 or fs.isDir(shell.resolve(tArgs[1])) then
- print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <path>")
- return
- end
- -- Startup
- print("Checking card file...")
- if not fs.exists(shell.resolve(tArgs[1])) then
- print("File does not exist! Creating...")
- io.open(shell.resolve(tArgs[1]),"w"):close()
- end
- local handle = io.open(shell.resolve(tArgs[1]),"r")
- local totalContents = handle:read("*a")
- handle:close()
- if totalContents == "" then
- print("File empty! A blank entry will be created.")
- createNew()
- else
- print("Card file good!")
- print("Registering card file...")
- cardsMain = registerCardFile(cardsMain,shell.resolve(tArgs[1]))
- end
- print("Generating key table...")
- cardsMainKeys = generateKeyList(cardsMain)
- print("Updating card index...")
- updateIndex()
- -- Event capturing
- while true do
- draw(cardsMain,currentCardIndex)
- eventParameters = {os.pullEvent()}
- -- Local variables are much faster than global variables and tables
- -- Replace eventResponseTable[eventParameters[1]] with a local variable during this section
- if eventResponseTable[eventParameters[1]] then
- eventResponseTable[eventParameters[1]](eventParameters)
- end
- end
- -- Type: Monster = 1 Spell = 2 Trap = 3
- -- Name:
- -- Attribute: Dark = 1 Divine = 2 Earth = 3 Fire = 4 Light = 5 Water = 6 Wind = 7
- -- If the card is a Monster card: Level
- -- If the card is a Trap or Spell card: Normal = 1 Continuous = 2 (Trap:Counter)(Spell:Equip) = 3 (Spell:Quick-Play) = 4 (Spell:Field) = 5 (Spell:Ritual) = 6
- -- Image path
- -- Old card format:
- -- 1111111111
- -- 1777777771
- -- 17 71
- -- 17 71
- -- 17 71
- -- 17 71
- -- 17 71
- -- 1777777771
- -- 1 4444 1
- -- 1111111111
- -- If the card is a Monster card: Aqua 1 Beast 2 Beast-Warrior 3 Creator God 4 Dinosaur 5 Divine-Beast 6 Dragon 7 Fairy 8 Fiend 9 Fish 10 Insect 11 Machine 12 Plant 13 Psychic 14 Pyro 15 Reptile 16 Rock 17 Sea Serpent 18 Spellcaster 19 Thunder 20 Warrior 21 Winged Beast 22 Zombie 23
- -- Description
- -- ATK and DEF
- -- Number on bottom left is the card's ID (or limitation text)
Add Comment
Please, Sign In to add comment