Advertisement
Guest User

bars

a guest
Apr 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.89 KB | None | 0 0
  1. colors    = require("colors")
  2. term      = require("term")
  3. component = require("component")
  4. gpu       = component.gpu
  5.  
  6. local viewX, viewY = gpu.getViewport()
  7.  
  8. local barsLeft  = 3
  9. local barWidth  = 5
  10. local barHeight = 10
  11. local colourHeight = barHeight - 2
  12. local barSpace  = 5
  13. local barsTop   = viewY - barHeight
  14.  
  15. local barOutline, barHighlight = 0xFFFFFF, 0x8888FF
  16.  
  17. local itemList = {}
  18. local bars = {}
  19. local lastUsed = nil
  20.  
  21. -- List
  22.  
  23. ListItem = { ItemContent = nil,
  24.              ListAssociations = nil,
  25.              Completed = false,
  26.              Value = 1 }
  27.  
  28. function ListItem:new (itemContent, listAssociations, completed, value)
  29.   local self = setmetatable({}, self)
  30.   self.__index = self
  31.   self.ItemContent = itemContent
  32.   self.ListAssociations = listAssociations
  33.   self.Completed = completed
  34.   self.Value = value
  35.   return self
  36. end
  37.  
  38. -- Bars
  39.  
  40. Bar = { Left = barsLeft,
  41.         Level = 1,
  42.         Max = 8,
  43.         Name = null,
  44.         Number = 1 }
  45.  
  46. function Bar:new (left, level, max, name, number)
  47.   local self = setmetatable({}, self)
  48.   self.__index = self
  49.   self.Left = left
  50.   self.Level = level
  51.   self.Max = max
  52.   self.Name = name
  53.   self.Number = number
  54.   return self
  55. end
  56.  
  57. -- Setters and Getters
  58.  
  59. function getBarName(bar)
  60.   if type(bar) == "table" then return bar.Name end
  61. end
  62.  
  63. function setBarName(bar, newName)
  64.   if type(bar) == "table" and type(newName) == "string" then
  65.     bar.Name = newName
  66.   end
  67. end
  68.  
  69. function getBarLevel(bar)
  70.   if type(bar) == "table" then
  71.     return bar.Level
  72.   end
  73. end
  74.  
  75. function setBarLevel(bar, newLevel)
  76.   if type(bar) == "table" and type(newLevel) == "number" then
  77.     if newLevel >= 1 and newLevel <= bar.Max then
  78.       bar.Level = newLevel
  79.     end
  80.   end
  81.   drawBars()
  82. end
  83.  
  84. function getBarMax(bar)
  85.   return bar.Max
  86. end
  87.  
  88. function setBarMax(bar, newMax)
  89.   if type(bar) == "table" and newMax < 1000 then
  90.     bar.Max = newMax
  91.   end
  92. end
  93.  
  94. function getBarFill(bar)
  95.   current, max = getBarLevel(bar), getBarMax(bar)
  96.   level = math.floor((current / max) * colourHeight)
  97.   if level < 1 then return 1 end
  98.   if level > colourHeight then return colourHeight end
  99.   return level  
  100. end
  101.  
  102. function completeItem(item, bar)
  103.   item.Completed = "true"
  104.   --setBarLevel(bar, bar.Level + item.Value)
  105.   for i=1,8 do        -- TODO: UNHARDCODE 8
  106.     associationCheck = tonumber(string.sub(item.ListAssociations, i, i))
  107.     print(associationCheck) -- THIS IS WHERE YOU ARE NOW got getitem done yay :3
  108.   end
  109. end
  110.  
  111. function addListItem(content, associatedBar)
  112.   local currentListItem = nil
  113.   if type(content) == "string" then
  114.     currentListItem = ListItem:new(content, "00000000", "false", 1)
  115.   else
  116.     return nil
  117.   end
  118.   local listAssociationNumber = associatedBar.Number
  119.   currentListItem.ListAssociations = string.sub(currentListItem.ListAssociations, 1, listAssociationNumber - 1) .. 1 .. string.sub(currentListItem.ListAssociations, listAssociationNumber + 1, string.len(currentListItem.ListAssociations))
  120.   print(currentListItem.ItemContent .. " " .. currentListItem.ListAssociations)
  121.   table.insert(itemList, currentListItem)
  122. end
  123.  
  124. function printItemList()
  125.   for k,v in pairs(itemList) do
  126.     if v.Completed == "false" then
  127.       print(k .. ": " .. v.ItemContent .. ", " .. v.ListAssociations .. ", " .. v.Completed .. ", " .. v.Value)
  128.     end
  129.   end
  130. end
  131.  
  132. function clearSaveFile(filename)
  133.   local saveFile = io.open(filename, "w")
  134.   saveFile:write("")
  135.   saveFile:close()
  136. end
  137.  
  138. function loadDate()
  139.   local readFile = io.open("./data/lastRun", "r")
  140.   for line in io.lines("./data/lastRun") do
  141.     print(line)
  142.   end
  143. end
  144.  
  145. function loadItems()
  146.   local readFile = io.open("./data/items", "r")
  147.   lines = {}
  148.   loadedItems = {}
  149.  
  150.   if readFile == nil then return end
  151.  
  152.   for line in io.lines("./data/items") do
  153.     table.insert(lines, line)
  154.   end
  155.  
  156.   local lineIndex = 0
  157.  
  158.   local listAssociationString = '%[%"ListAssociations%"%] %= '
  159.   local itemContentString = '%[%"ItemContent%"%] %= '
  160.   local completedString = '%[%"Completed%"%] %= '
  161.   local valueString = '%[%"Value%"%] %= '
  162.  
  163.   local loadAssociation, loadContent, loadCompleted, loadValue = nil, nil, nil, nil
  164.  
  165.   for k,v in pairs(lines) do
  166.     local lineString = '%[' .. lineIndex + 1 .. '%] %= %{'
  167.     local stringEnd, _ = string.find(v, ",")
  168.  
  169.     if string.find(v, lineString) then
  170.       lineIndex = lineIndex + 1
  171.     end
  172.  
  173.     if string.find(v, listAssociationString) then
  174.       _, loadAssociation = string.find(v, listAssociationString)
  175.       loadAssociation = string.sub(v, loadAssociation + 2, stringEnd - 2)
  176.       --print("Ding! loadAssociation = '" .. loadAssociation .. "'")
  177.     elseif string.find(v, itemContentString) then
  178.       _, loadContent = string.find(v, itemContentString)
  179.       loadContent = string.sub(v, loadContent + 2, stringEnd - 2)
  180.       --print("Ding! loadContent = '" .. loadContent .. "'")
  181.     elseif string.find(v, completedString) then
  182.       _, loadCompleted = string.find(v, completedString)
  183.       loadCompleted = string.sub(v, loadCompleted + 2, stringEnd - 2)
  184.     elseif string.find(v, valueString) then
  185.       _, loadValue = string.find(v, valueString)
  186.       loadValue = tonumber(string.sub(v, loadValue + 1, stringEnd - 1))
  187.     end
  188.    
  189.     if loadAssociation ~= nil and loadContent ~= nil and loadCompleted ~= nil and loadValue ~= nil then
  190.       table.insert(itemList, ListItem:new(loadContent, loadAssociation, loadCompleted, loadValue))
  191.       loadAssociation, loadContent, loadCompleted, loadValue = nil, nil, nil, nil
  192.     end
  193.   end
  194. end
  195.  
  196. function loadBars()
  197.   local readFile = io.open("./data/barData", "r")
  198.   lines = {}
  199.   loadedBars = {}
  200.  
  201.   if readFile == nil then
  202.     --Load default bars
  203.     for i=1, 8 do          -- TODO : replace '8'
  204.       if i==1 then
  205.         currentLeft = barsLeft
  206.       else
  207.         currentLeft = currentLeft + barWidth + barSpace
  208.       end
  209.       currentBar = Bar:new(currentLeft, i, 8, "Bar " .. i, i)
  210.       table.insert(loadedBars, currentBar)
  211.     end
  212.   else
  213.   --Load saved bars
  214.     barIndex = 0
  215.  
  216.     for line in io.lines("./data/barData") do
  217.       table.insert(lines, line)
  218.     end
  219.  
  220.     local numString = '%[%"Number%"%] %= '
  221.     local levString = '%[%"Level%"%] %= '
  222.     local maxString = '%[%"Max%"%] %= '
  223.     local namString = '%[%"Name%"%] %= '
  224.     local lefString = '%[%"Left%"%] %= '
  225.  
  226.     local loadNum, loadLev, loadMax, loadNam, loadLef = nil, nil, nil, nil, nil
  227.  
  228.     for k,v in pairs(lines) do
  229.  
  230.       local barString = '%[' .. barIndex + 1 .. '%] %= %{'
  231.       local stringEnd, _ = string.find(v, ",")
  232.  
  233.       if string.find(v, barString) then
  234.         barIndex = barIndex + 1
  235.       end
  236.  
  237.       if string.find(v, numString) then
  238.         _, loadNum = string.find(v, numString)
  239.         loadNum = tonumber(string.sub(v, loadNum + 1, stringEnd - 1))
  240.       elseif string.find(v, levString) then
  241.         _, loadLev = string.find(v, levString)
  242.         loadLev = tonumber(string.sub(v, loadLev + 1, stringEnd - 1))
  243.       elseif string.find(v, maxString) then
  244.         _, loadMax = string.find(v, maxString)
  245.         loadMax = tonumber(string.sub(v, loadMax + 1, stringEnd - 1))
  246.       elseif string.find(v, namString) then
  247.         _, loadNam = string.find(v, namString)
  248.         loadNam = string.sub(v, loadNam + 1, stringEnd - 1)
  249.         loadNam = string.sub(loadNam, 2, string.len(loadNam)-1)        
  250.       elseif string.find(v, lefString) then
  251.         _, loadLef = string.find(v, lefString)
  252.         loadLef = tonumber(string.sub(v, loadLef + 1, stringEnd - 1))
  253.       end
  254.  
  255.       if loadNum ~= nil and loadLev ~= nil and loadMax ~= nil and loadNam ~= nil and loadLef ~= nil then
  256.         table.insert(loadedBars, Bar:new(loadLef, loadLev, loadMax, loadNam, loadNum))
  257.         loadNum, loadLev, loadMax, loadNam, loadLef = nil, nil, nil, nil
  258.       end
  259.    
  260.     --os.sleep(0.05)
  261.     end
  262.   end
  263.   if readFile ~= nil then readFile:close() end
  264.   bars = loadedBars
  265. end
  266.  
  267. function loadAll()
  268.   loadDate()
  269.   loadBars()
  270.   loadItems()
  271. end
  272.  
  273. function saveBars(b)
  274.   barDataFileName = "./data/barData"
  275.   clearSaveFile(barDataFileName)
  276.   local saveFile = io.open(barDataFileName, "a")
  277.   writeTable(b, saveFile)
  278.   saveFile:close(saveFile)
  279. end
  280.  
  281. function saveDate()
  282.   dateFileName = "./data/date"
  283.   tempFileName = "./data/tmp"
  284.   clearSaveFile(dateFileName)
  285.   clearSaveFile(tempFileName)
  286.   local saveFile = io.open(dateFileName, "a")
  287.   --local tempFile = io.open(tempFileName, "r")
  288.   --os.date("%d") something something
  289.   saveFile:close(saveFile)
  290. end
  291.  
  292. function saveList(il)
  293.   itemListFileName = "./data/items"
  294.   clearSaveFile(itemListFileName)
  295.   local saveFile = io.open(itemListFileName, "a")
  296.   writeTable(il, saveFile)
  297.   saveFile:close(saveFile)
  298. end
  299.  
  300. function writeTable (o, f)
  301.   if type(o) == "number" then
  302.     f:write(o)
  303.   elseif type(o) == "string" then
  304.     f:write(string.format("%q", o))
  305.   elseif type(o) == "table" then
  306.     f:write("{\n")
  307.     for k,v in pairs(o) do
  308.       if (k ~= "__index") then
  309.         f:write("  [")
  310.         writeTable(k, f)
  311.         f:write("] = ")
  312.         writeTable(v, f)
  313.         f:write(",\n")
  314.       end
  315.     end
  316.     f:write("  }\n")
  317.       else
  318.     error("cannot serialize a " .. type(o))
  319.   end
  320. end
  321.  
  322. local barColors = { 0xFF0000,
  323.                     0xDD2200,
  324.                     0xBB4400,
  325.                     0x996600,
  326.                     0x669900,
  327.                     0x44BB00,
  328.                     0x22DD00,
  329.                     0x00FF00 }
  330.  
  331. function drawRectangle(left, top, width, height, colorFG, colorBG)          
  332.   gpu.setForeground(colorFG)
  333.   gpu.setBackground(colorBG)
  334.   gpu.fill(left, top, width, height, " ")
  335. end
  336.  
  337. function drawBar(bar, highlightColor)      -- BARMAX UPDATES REQUIRED
  338.   --Box, then black, then bar
  339.   if highlightColor == nil then
  340.     drawRectangle(bar.Left, barsTop, barWidth, barHeight, 0x000000, barOutline)
  341.   else
  342.     drawRectangle(bar.Left, barsTop, barWidth, barHeight, 0x000000, highlightColor)
  343.   end
  344.   drawRectangle(bar.Left+1, barsTop+1, barWidth-2, barHeight-2, 0x000000, 0x000000)
  345.   if bar.Level >= 1 and getBarFill(bar) <= 8 then
  346.     drawRectangle(bar.Left+1, barsTop+1+(colourHeight - getBarFill(bar)), barWidth-2, getBarFill(bar), 0x000000, barColors[getBarFill(bar)])
  347.   end
  348. end
  349.  
  350. function highlightBar(bar)
  351.   drawBar(bar, barHighlight)
  352. end
  353.  
  354. function drawBars()
  355.   for i = 1, 8 do     -- TODO: Update 8 here
  356.     drawBar(bars[i])
  357.   end
  358.   setDefaultColours()
  359. end
  360.  
  361. function drawAll()
  362.   drawBars()
  363.   drawRectangle(1, barsTop-2, viewX, 1, 0x000000, 0xFFFFFF)
  364. end
  365.  
  366. function grooveBars()           -- USE GETTER/SETTER INSTEAD OF MANUAL SET
  367.   for i = 1, 100 do
  368.     local randBar =  math.random(8)
  369.     local randSign = math.random(2)
  370.  
  371.     local curBar = bars[randBar]
  372.  
  373.     curBar.Level = curBar.Level + (randSign == 1 and -1 or 1)
  374.     if curBar.Level == -1 then curBar.Level = 0 end
  375.     if curBar.Level == 9 then curBar.Level = 8 end
  376.     drawBars()
  377.     os.sleep(0.005)
  378.   end
  379. end
  380.  
  381. function clearAll(draw)
  382.   draw = draw or ""
  383.   io.flush()
  384.   gpu.setBackground(0x000000)
  385.   gpu.setForeground(0xFFFFFF)
  386.   term.clear()
  387.   if draw ~= "clear" then drawAll() end
  388.   setDefaultColours()
  389.  
  390. end
  391.  
  392. function setDefaultColours()
  393.   gpu.setBackground(0x000000)
  394.   gpu.setForeground(0xFFFFFF)
  395. end
  396.  
  397. function waitSelectBar()
  398.   local input
  399.   local continueLoop = true
  400.   local help = false
  401.   local inputReturnValue = nil
  402.   clearAll()
  403.   while(true) do
  404.     print("Select a bar, or try 'help' or 'exit'")  
  405.   if inputReturnValue ~= nil then print(inputReturnValue) end
  406.   inputReturnValue = parseInput(string.lower(io.read()))
  407.   if type(inputReturnValue) == "table" then return inputReturnValue end
  408.   end
  409.   return nil
  410. end
  411.  
  412. function parseInput(input)
  413.   clearAll()
  414.  
  415.   for i, v in ipairs(bars) do
  416.       if tonumber(input) == v.Number or input == string.lower(v.Name) then
  417.         return v
  418.       end
  419.     end
  420.  
  421.   if input == "exit" then
  422.       clearAll("clear")
  423.       exitBars()
  424.     elseif input == "help" then
  425.       printHelp()
  426.     elseif input == "g r o o v e" then
  427.       grooveBars()
  428.     elseif input == "print list" then
  429.       printItemList()
  430.     elseif input == "raise all" then
  431.       raiseAllBars(1)
  432.     elseif input == "lower all" then
  433.       lowerAllBars(1)
  434.     elseif input == "bars" then
  435.       printBars()
  436.     elseif input == "debugmax" then
  437.       getBarFill(getBar(1))
  438.  
  439.     elseif string.match(string.sub(input, 1, 4), "add ") then
  440.       parseAddListItem(parseMultipleWords(input))
  441.     elseif string.match(string.sub(input, 1, 4), "try ") then
  442.       print(parseMultipleWords(input))
  443.     elseif string.match(string.sub(input, 1, 7), "rename ") then
  444.       local _, barToRename, newName, _ = parseMultipleWords(input)
  445.       barToRename = getBar(barToRename)
  446.       setBarName(barToRename, newName)
  447.  
  448.     elseif string.match(string.sub(input, 1, 9), "complete ") then
  449.  
  450.       local _, relevantBar, relevantItem, _ = parseMultipleWords(input)
  451.       relevantBar = getBar(relevantBar)
  452.       relevantItem = getItem(tonumber(relevantItem), relevantBar)
  453.       print(relevantItem.ItemContent .. "marked as complete!")
  454.      
  455.       os.sleep(3)
  456.     else
  457.       return input .. " is not a valid input."
  458.     end
  459. end
  460.  
  461. function getItem(itemNumber, bar)
  462.   local index = 0
  463.   for i, v in ipairs(itemList) do
  464.     associated = tonumber(string.sub(v.ListAssociations, bar.Number, bar.Number))
  465.     if (associated == 1) then
  466.       index = index + 1
  467.       if (index == itemNumber) then
  468.       return v end
  469.     end
  470.   end
  471. end
  472.  
  473. function getBar(barName)
  474.   for i, v in ipairs(bars) do
  475.     if tonumber(barName) == v.Number or barName == string.lower(v.Name) then
  476.       return v
  477.     end
  478.   end
  479. end
  480.  
  481. function parseMultipleWords(input)
  482.   local remainingInput = input
  483.   local firstSpace = string.find(remainingInput, " ")
  484.   if firstSpace == nil then return input end
  485.   local firstWord = string.sub(remainingInput, 1, firstSpace - 1)
  486.  
  487.   remainingInput = string.sub(remainingInput, string.len(firstWord) + 2, string.len(remainingInput))
  488.   local secondWord = nil
  489.   if string.sub(remainingInput, 1, 1) ~= '"' then
  490.     local secondSpace = string.find(remainingInput, " ")
  491.     if secondSpace == nil then return firstWord, remainingInput end
  492.     secondWord = string.sub(remainingInput, 1, secondSpace - 1)
  493.     remainingInput = string.sub(remainingInput, string.len(secondWord) + 2, string.len(remainingInput))
  494.   else
  495.     local firstQuote = 1
  496.     local secondQuote = string.find(string.sub(remainingInput, 2, string.len(remainingInput)), '"') + 1
  497.     secondWord = string.sub(remainingInput, firstQuote + 1, secondQuote - 1)
  498.     remainingInput = string.sub(remainingInput, string.len(secondWord) + 4, string.len(remainingInput))
  499.   end
  500.  
  501.   local thirdWord = nil
  502.   if string.sub(remainingInput, 1, 1) ~= '"' then
  503.     local thirdSpace = string.find(remainingInput, " ")
  504.     if thirdSpace == nil then return firstWord, secondWord, remainingInput end
  505.     thirdWord = string.sub(remainingInput, 1, thirdSpace - 1)
  506.     remainingInput = string.sub(remainingInput, string.len(secondWord) + 2, string.len(remainingInput))
  507.   else
  508.     local firstQuote = 1
  509.     local secondQuote = string.find(string.sub(remainingInput, 2, string.len(remainingInput)), '"') + 1
  510.     thirdWord = string.sub(remainingInput, firstQuote + 1, secondQuote - 1)
  511.     remainingInput = string.sub(remainingInput, string.len(thirdWord) + 4, string.len(remainingInput))
  512.   end
  513.  
  514.   local fourthWord = string.sub(remainingInput, 1, string.len(remainingInput))
  515.  
  516.   return firstWord, secondWord, thirdWord, fourthWord
  517. end
  518.  
  519. function parseAddListItem(_, content, addBar)
  520.   local correctUsage = "Correct usage: 'add [text] [bar name/number]'"
  521.   local currentBar = nil
  522.  
  523.   if content == nil or addBar == nil or type(content) ~= "string" then
  524.     print(correctUsage)
  525.     return nil
  526.   end
  527.  
  528.   for i,v in ipairs(bars) do
  529.     if tonumber(addBar) == v.Number or addBar == string.lower(v.Name) then
  530.       currentBar = v
  531.     end
  532.     if currentBar ~= nil then break end
  533.   end
  534.  
  535.   if currentBar == nil then
  536.     print(correctUsage)
  537.   else
  538.     addListItem(content, currentBar)
  539.   end
  540. end
  541.  
  542. function lowerAllBars(amount)
  543.   for k,v in pairs(bars) do
  544.     currentLevel = v.Level
  545.     setBarLevel(v, currentLevel - amount)
  546.   end
  547. end
  548.  
  549. function raiseAllBars(amount)
  550.   for k,v in pairs(bars) do
  551.     currentLevel = v.Level
  552.     setBarLevel(v, currentLevel + amount)
  553.   end
  554. end
  555.  
  556. function printTasksByBar(bar)
  557.   gpu.setForeground(0x00FF00)
  558.   print("  " .. bar.Name)
  559.   gpu.setForeground(0xFFFFFF)
  560.   print(string.rep("=", string.len(bar.Name) + 4))
  561.   for k,v in pairs(itemList) do
  562.     if tonumber(string.sub(v.ListAssociations, bar.Number, bar.Number)) == 1 then
  563.       print(v.ItemContent)
  564.       os.sleep(0.1)
  565.     end
  566.   end
  567. end
  568.  
  569. function printBars()
  570.   for i, v in ipairs(bars) do
  571.     print(i .. ": [" .. v.Name .. "]")
  572.     print(i .. ": [" .. v.Name .. "] - " .. v.Level .. " " .. v.Max)
  573.   end
  574. end
  575.  
  576. function printHelp()
  577.     local otherCommands = "'bars' - Lists all bars\n'print list' - Prints list of tasks\n'add [task] [bar name/number]' - Adds a task to task list\n'rename [bar] [newname]' - Rename a bar\n'raise all / lower all' - debug\n"
  578.     local helpMessage = "Select a bar by entering its name or number\n"
  579.   print(otherCommands)
  580.   print(helpMessage)
  581. end
  582.  
  583. function exitBars()
  584.   print("Exited")
  585.   saveBars(bars)
  586.   saveList(itemList)
  587.   os.exit()
  588. end
  589.  
  590. -- ===== Main =====
  591.  
  592. loadAll()
  593. drawAll()
  594. while(true) do
  595.   local selectedBar = waitSelectBar()
  596.   printTasksByBar(selectedBar)
  597.   highlightBar(selectedBar)
  598.   os.sleep(1)
  599.   drawBar(selectedBar)
  600. end
  601. saveBars(bars)
  602. saveList(itemList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement