Daniel_I_Am

Community Goals 2.0.3

Dec 15th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.17 KB | None | 0 0
  1. transposer = {"dc3","26f","ded","3da"}
  2. redstoneFlush = "cd7"
  3. outputRedstoneIOs = {"bf6", "7b4", "27c", "7c7", "007"}
  4.  
  5. component = require("component")
  6. sides = require("sides")
  7. term = require("term")
  8. fs = require("filesystem")
  9. event = require("event")
  10. os = require("os")
  11.  
  12. gpu = component.gpu
  13. internet = component.internet
  14. keyboard = component.keyboard
  15.  
  16. transposer = {component.proxy(component.get(transposer[1])),component.proxy(component.get(transposer[2])),component.proxy(component.get(transposer[3])),component.proxy(component.get(transposer[4]))}
  17. outputRedstoneIOs = {component.proxy(component.get(outputRedstoneIOs[1])),component.proxy(component.get(outputRedstoneIOs[2])),component.proxy(component.get(outputRedstoneIOs[3])),component.proxy(component.get(outputRedstoneIOs[4])), component.proxy(component.get(outputRedstoneIOs[5]))}
  18. width = 100
  19. height = 30
  20. gpu.setResolution(width, height)
  21.  
  22. colorTable = {
  23.     uncommon = {
  24.         PrimaryCol = 0x36c95e,
  25.         SecondaryCol = 0x1b642f,
  26.     textCol = 0xffffff,
  27.     progressFilled = 0x00ff00,
  28.     progressEmpty = 0x000000
  29.     },
  30.     rare = {
  31.         SecondaryCol = 0x4283d8,
  32.         PrimaryCol = 0x21416c,
  33.     textCol = 0xffffff,
  34.     progressFilled = 0x0000ff,
  35.     progressEmpty = 0x000000
  36.     },
  37.     epic = {
  38.         SecondaryCol = 0x9c52c8,
  39.         PrimaryCol = 0x4e2964,
  40.     textCol = 0xffffff,
  41.     progressFilled = 0xcc66ff,
  42.     progressEmpty = 0x000000
  43.     },
  44.     legendary = {
  45.         SecondaryCol = 0xdd871c,
  46.         PrimaryCol = 0x6e430e,
  47.     textCol = 0xffffff,
  48.     progressFilled = 0xff9933,
  49.     progressEmpty = 0x000000
  50.     }
  51. }
  52.  
  53. --variables
  54. currentTier = 1
  55. subObjective = 1
  56. link = "http://www.endercompanies.com/time.php"
  57. dataFile = "data.txt"
  58. multiplier = 1
  59. redstoneFlushSide = sides.east
  60. redstoneOutputSide = sides.top
  61. goalCompletionTime = 7200
  62. inbetweenGoalTime = 7200
  63.  
  64. --completion at:
  65. completionTier1 = 6
  66. completionTier2 = 4
  67. completionTier3 = 2
  68. completionTier4 = 1
  69.  
  70. function addRect(x1,y1,x2,y2,color)
  71.     temp = gpu.getBackground()
  72.     gpu.setBackground(color)
  73.     gpu.fill(x1,y1,x2-x1+1,y2-y1+1," ")
  74.     gpu.setBackground(temp)
  75. end
  76.  
  77. function addProgressBar(x1,y1,x2,y2,sideWidth,sideHeight,borderColor,filledBarColor,emptyBarColor,textColor,headerText,filledPercentage,isSideways,isInverted)
  78.     isSideways = isSideways or false
  79.     isInverted = isInverted or false
  80.     if type(x1)~="number" then throwError("addProgressBar() argument 1 (x1) type int expected, "..type(x1).." found") end
  81.     if type(x2)~="number" then throwError("addProgressBar() argument 3 (x2) type int expected, "..type(x2).." found") end
  82.     if type(y1)~="number" then throwError("addProgressBar() argument 2 (y1) type int expected, "..type(y1).." found") end
  83.     if type(y2)~="number" then throwError("addProgressBar() argument 4 (y2) type int expected, "..type(y2).." found") end
  84.     if type(sideWidth)~="number" then throwError("addProgressBar() argument 5 (sideWidth) type int expected, "..type(sideWidth).." found") end
  85.     if type(sideHeight)~="number" then throwError("addProgressBar() argument 6 (sideHeight) type int expected, "..type(sideHeight).." found") end
  86.     if type(filledPercentage)~="number" then throwError("addProgressBar() argument 11 (filledPercentage) type float expected, "..type(filledPercentage).." found") end
  87.     if filledPercentage > 1 then filledPercentage = 1 end
  88.     if isSideways == false then
  89.         addRect(x1,y1,x2,y2,borderColor)
  90.         addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y2-sideHeight,emptyBarColor)
  91.         barSize = (y2-y1-2*sideHeight)
  92.         pixelsFilled = math.floor(filledPercentage*barSize+0.5)
  93.         if isInverted == false then
  94.             addRect(x1+sideWidth,y2-sideHeight-pixelsFilled,x2-sideWidth,y2-sideHeight,filledBarColor)
  95.         elseif isInverted == true then
  96.             addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y1+sideHeight+pixelsFilled,filledBarColor)
  97.         else
  98.             throwError("addProgressBar() argument 13 (isInverted) type boolean expected, "..type(isInverted).." found")
  99.         end
  100.     elseif isSideways == true then
  101.         addRect(x1,y1,x2,y2,borderColor)
  102.         addRect(x1+sideWidth,y1+sideHeight,x2-sideWidth,y2-sideHeight,emptyBarColor)
  103.         barSize = (x2-x1-2*sideWidth)
  104.         pixelsFilled = math.floor(filledPercentage*barSize+0.5)
  105.         if isInverted == false then
  106.             addRect(x1+sideWidth,y1+sideHeight,x1+sideWidth+pixelsFilled,y2-sideHeight,filledBarColor)
  107.         elseif isInverted == true then
  108.             addRect(x2-sideWidth-pixelsFilled,y1+sideHeight,x2-sideWidth,y2-sideHeight,filledBarColor)
  109.         else
  110.             throwError("addProgressBar() argument 13 (isInverted) type boolean expected, "..type(isInverted).." found")
  111.         end
  112.     else
  113.         throwError("addProgressBar() argument 12 (isSideways) type boolean expected, "..type(isSideways).." found")
  114.     end
  115.     if sideHeight>0 then
  116.         col = {f = gpu.getForeground(),b = gpu.getBackground()}
  117.         centerX = math.ceil((x1+x2)/2)
  118.         centerY = math.floor(y1+sideHeight/2)
  119.         gpu.setForeground(textColor)
  120.         gpu.setBackground(borderColor)
  121.         gpu.set(centerX-(string.len(tostring(headerText))/2),centerY,tostring(headerText))
  122.         gpu.setForeground(col.f)
  123.         gpu.setBackground(col.b)
  124.         centerX = nil
  125.         centerY = nil
  126.         col = nil
  127.     end
  128. end
  129.  
  130. function throwError(errorMessage)
  131.     errorMessage = errorMessage or "Unknown Error"
  132.     gpu.setResolution(gpu.maxResolution())
  133.     gpu.setForeground(0xff0000)
  134.     gpu.setBackground(0x000000)
  135.     term.clear()
  136.     print("The program ran into a serious error during execution.")
  137.     print(errorMessage)
  138.     gpu.setForeground(0xffffff)
  139.     os.exit()
  140. end
  141.  
  142. function listToPages(list,entriesPerPage)
  143.     pageList = {}
  144.     value = nil
  145.     tempList = {}
  146.    
  147.     for i = 1,#list,entriesPerPage do
  148.         tempList = {}
  149.         for j = 1,entriesPerPage do
  150.             value = table.remove(list, 1)
  151.             table.insert(tempList, value)
  152.         end
  153.         table.insert(pageList, tempList)
  154.     end
  155.     return pageList
  156. end
  157.  
  158. function completedObjective()
  159.     outputRedstoneIOs[currentTier].setOutput(redstoneOutputSide, 15)
  160.     os.sleep(1)
  161.     outputRedstoneIOs[currentTier].setOutput(redstoneOutputSide, 0)
  162.     if currentTier == 1 then
  163.         if subObjective >= completionTier1 then
  164.             subObjective = 1
  165.             currentTier = 2
  166.         else
  167.             subObjective = subObjective + 1
  168.         end
  169.     elseif currentTier == 2 then
  170.         if subObjective >= completionTier2 then
  171.             subObjective = 1
  172.             currentTier = 3
  173.         else
  174.             subObjective = subObjective + 1
  175.         end
  176.     elseif currentTier == 3 then
  177.         if subObjective >= completionTier3 then
  178.             subObjective = 1
  179.             currentTier = 4
  180.         else
  181.             subObjective = subObjective + 1
  182.         end
  183.     elseif currentTier == 4 then
  184.         if subObjective >= completionTier4 then
  185.             finishedObjective()
  186.         else
  187.             subObjective = subObjective + 1
  188.         end
  189.     end
  190.     multiplier = 2 ^ (currentTier - 1)
  191. end
  192.  
  193. function getTime()
  194.     page = internet.request(link)
  195.     page.read()
  196.     local current = page.read()
  197.     local formattedTime = {}
  198.     local currentDate = string.sub(current, 1,string.find(current, ", ")-1)
  199.     local currentTime = string.sub(current, string.find(current, ", ")+2)
  200.     local currentDay = string.sub(current, 1,string.find(currentDate, " ")-1)
  201.     if currentDay == "Mon" then currentDay = 1 elseif currentDay == "Tue" then currentDay = 2 elseif currentDay == "Wed" then currentDay = 3 elseif currentDay == "Thu" then currentDay = 4 elseif currentDay == "Fri" then currentDay = 5 elseif currentDay == "Sat" then currentDay = 6 elseif currentDay == "Sun" then currentDay = 7 end
  202.     local currentHour = string.sub(currentTime, string.find(currentTime, " ")+1,string.find(currentTime, ":")-1)
  203.     local currentMinute = string.sub(string.sub(string.sub(currentTime, string.find(currentTime, " ")+1),string.find(string.sub(currentTime, string.find(currentTime, " ")+1), ":")+1),1,string.find(string.sub(string.sub(currentTime, string.find(currentTime, " ")+1),string.find(string.sub(currentTime, string.find(currentTime, " ")+1), ":")+1)," ")-1)
  204.     return {tonumber(currentDay), tonumber(currentHour), tonumber(currentMinute)}
  205. end
  206.  
  207. function addTimeToTime(currentTime, deltaTime)
  208.     day = currentTime[1]
  209.     hour = currentTime[2]
  210.     minute = currentTime[3]
  211.     local minute = minute + deltaTime
  212.     local hour = hour + math.floor(minute/60)
  213.     local day = day + math.floor(hour/24)
  214.     local minute = minute%60
  215.     local hour = hour%24
  216.     return {day, hour, minute}
  217. end
  218.  
  219. function getItems()
  220.     local s = {sides.north, sides.east, sides.south, sides.west}
  221.     local items = {}
  222.     for t = 1, #transposer do
  223.         for i = 1, #s do
  224.         size, error = transposer[t].getInventorySize(s[i])
  225.             if error ~= "no inventory" then
  226.                 for j = 2,5 do
  227.                     item = transposer[t].getStackInSlot(s[i], j)
  228.                     if item ~= nil then
  229.                         while string.find(item.label, "§") do
  230.                             item.label = string.sub(item.label, 1, string.find(item.label, "§")-1) .. string.sub(item.label, string.find(item.label, "§")+3)
  231.                         end
  232.                         table.insert(items, item)
  233.                     else
  234.                         table.insert(items, {label = "Unknown"})
  235.                     end
  236.                 end
  237.             else
  238.                 error = nil
  239.             end
  240.         end
  241.     end
  242.     return items --{item1, item2, item3, item4, item5, [...] } if there is no itemx then it is 'Unknown'
  243. end
  244.  
  245. function fileRead(fileLocation)
  246.     local str = {}
  247.     if fs.exists("/home/"..fileLocation) then
  248.         f = io.open("/home/"..fileLocation, "r")
  249.         repeat
  250.             line = f:read("*line")
  251.             if line ~= nil then
  252.                 table.insert(str, line)
  253.             end
  254.         until line == nil
  255.     else
  256.         throwError("File /home/"..fileLocation.." not found!")
  257.     end
  258.     f:close()
  259.     f = nil
  260.     return str
  261. end
  262.  
  263. function pickNewObjective()
  264.     local n = math.random(#objectives)
  265.     outputRedstoneIOs[5].setOutput(redstoneOutputSide, 15)
  266.     os.sleep(1)
  267.     outputRedstoneIOs[5].setOutput(redstoneOutputSide, 0)
  268.     return objectives[n]
  269. end
  270.  
  271. function finishedObjective()
  272.     w = 20
  273.     h = 6
  274.     gpu.setResolution(w,h)
  275.     addRect(1,1,w,h,secondaryCol)
  276.     centerText("Completed Objective!", 1, w, 2, textCol, secondaryCol)
  277.     centerText("It will reset in:", 1, w, 3, textCol, secondaryCol)
  278.     timeLeft = inbetweenGoalTime
  279.     repeat
  280.         os.sleep(1)
  281.         timeLeft = timeLeft - 1
  282.         centerText(timeLeft .. " seconds", 1, w, 4, textCol, secondaryCol)
  283.     until timeLeft < 0
  284.     os.execute("reboot")
  285. end
  286.  
  287. function makeObjectives()
  288.     local objectiveItems = getItems()
  289.     local objectiveAmountsTemp = fileRead("data.txt")
  290.     objectiveAmountsTemp = objectiveAmountsTemp[1]
  291.     objectiveAmounts = {}
  292.     while string.find(objectiveAmountsTemp, ";") do
  293.         local a = string.find(objectiveAmountsTemp, ";")
  294.         table.insert(objectiveAmounts, string.sub(objectiveAmountsTemp, 1, a-1))
  295.         objectiveAmountsTemp = string.sub(objectiveAmountsTemp, a+1)
  296.     end
  297.     table.insert(objectiveAmounts, objectiveAmountsTemp)
  298.     objectives = {}
  299.     for i = 1, #objectiveItems do
  300.         if #objectiveAmounts<i then
  301.             table.insert(objectiveAmounts, 1)
  302.         end
  303.         table.insert(objectives, {objectiveItems[i].label, objectiveAmounts[i]})
  304.     end
  305.     local objectivesCopy = {}
  306.     for i=1,#objectives do table.insert(objectivesCopy, objectives[i]) end
  307.     local pagedObjectives = listToPages(objectivesCopy, 20)
  308.     for j = 1, #pagedObjectives do
  309.         for i = 1,#pagedObjectives[j] do
  310.             print(pagedObjectives[j][i][1] .." = "..pagedObjectives[j][i][2])
  311.         end
  312.         --print("please click the screen to continue")
  313.         --event.pull("touch")
  314.         print("please wait ... loading ...")
  315.         os.sleep(5)
  316.         term.clear()
  317.     end
  318. end
  319.  
  320. function resetStuff()
  321.     gpu.setForeground(0xffffff)
  322.     gpu.setBackground(0x000000)
  323.     term.clear()
  324. end
  325.  
  326. function init()
  327.     timeLeft = goalCompletionTime
  328.     resetStuff()
  329.     makeObjectives()
  330.     currentObjective = pickNewObjective()
  331.     findItem()
  332.     term.clear()
  333. end
  334.  
  335. function main()
  336.     update()
  337.     screen()
  338.     os.sleep(1)
  339. end
  340.  
  341. function findItem()
  342.     correctTransposer = nil
  343.     local s = {sides.north, sides.east, sides.south, sides.west}
  344.     for i = 1,#transposer do
  345.         for j = 1, #s do
  346.             size, error = transposer[i].getInventorySize(s[j])
  347.             if error ~= "no inventory" then
  348.                 for k = 2,5 do
  349.                     local itemLabel = transposer[i].getStackInSlot(s[j], k).label
  350.                     while string.find(itemLabel, "§") do
  351.                         itemLabel = string.sub(itemLabel, 1, string.find(itemLabel, "§")-1) .. string.sub(itemLabel, string.find(itemLabel, "§")+3)
  352.                     end
  353.                     if itemLabel == currentObjective[1] then
  354.                         correctDrawerSide = s[j]
  355.                         correctTransposer = transposer[i]
  356.                         correctSlot = k
  357.                         return
  358.                     end
  359.                 end
  360.             end
  361.         end
  362.     end
  363.     throwError("finditem() "..correctSlot)
  364. end
  365.  
  366. function flushDrawers()
  367.     component.proxy(component.get(redstoneFlush)).setOutput(redstoneFlushSide, 15)
  368.     repeat
  369.         os.sleep(1)
  370.     until correctTransposer.getStackInSlot(correctDrawerSide, correctSlot).size == 0
  371.     component.proxy(component.get(redstoneFlush)).setOutput(redstoneFlushSide, 0)
  372. end
  373.  
  374. function checkItem()
  375.     local transposerAmount = correctTransposer.getStackInSlot(correctDrawerSide, correctSlot).size
  376.     if transposerAmount >= currentObjective[2]*multiplier then
  377.         storedAmount = transposerAmount
  378.         filledPercentage = 1
  379.         screen()
  380.         flushDrawers()
  381.         newGoal()
  382.         return nil
  383.     end
  384.     return transposerAmount
  385. end
  386.  
  387. function newGoal()
  388.     completedObjective()
  389.     displaySleepTime(height - 11,timeLeft, textCol, secondaryCol, "Time left until next objective:", "")
  390.     currentObjective = pickNewObjective()
  391.     findItem()
  392.     timeLeft = goalCompletionTime
  393. end
  394.  
  395. function update()
  396.     storedAmount = checkItem()
  397.     if storedAmount ~= nil then
  398.         filledPercentage = storedAmount/(currentObjective[2]*multiplier)
  399.     else
  400.         filledPercentage = 0
  401.     end
  402.     timeLeft = timeLeft - 1
  403.     if timeLeft == -1 then
  404.         component.proxy(component.get(redstoneFlush)).setOutput(redstoneFlushSide, 15)
  405.         os.sleep(5)
  406.         component.proxy(component.get(redstoneFlush)).setOutput(redstoneFlushSide, 0)
  407.         os.execute("reboot")
  408.     end
  409. end
  410.  
  411. function centerText(str, x1, x2, y, FGCol, BGCol)
  412.     local w,_ = gpu.getResolution()
  413.     x1 = x1 or 1
  414.     x2 = x2 or w
  415.     y = y or 1
  416.     str = str or "text"
  417.     FGCol = FGCol or 0xffffff
  418.     BGCol = BGCol or 0x000000
  419.     local prevFG = gpu.getForeground()
  420.     local prevBG = gpu.getBackground()
  421.     local l = string.len(tostring(str))
  422.     local center = math.floor((x1+x2)/2)
  423.     local startPos = math.ceil(center - (l-1)/2)
  424.     gpu.setForeground(FGCol)
  425.     gpu.setBackground(BGCol)
  426.     gpu.set(startPos,y,str)
  427.     gpu.setForeground(prevFG)
  428.     gpu.setBackground(prevBG)
  429. end
  430.  
  431. function displaySleepTime(y,toSleep, txtcol, bgcol, textToShowPre, textToShowPost)
  432.     repeat
  433.         centerText("   " .. textToShowPre .. " " .. math.floor(toSleep/60) .. " minutes and " .. toSleep%60 .. " seconds" .. textToShowPost .. "   ", 1, w, y, txtcol, bgcol)
  434.         toSleep = toSleep - 1
  435.         os.sleep(1)
  436.     until toSleep == -1
  437. end
  438.  
  439. function screen()
  440.     if currentTier == 1 then
  441.         primaryCol = colorTable.uncommon.PrimaryCol
  442.         secondaryCol = colorTable.uncommon.SecondaryCol
  443.         textCol = colorTable.uncommon.textCol
  444.         progressFilled = colorTable.uncommon.progressFilled
  445.         progressEmpty = colorTable.uncommon.progressEmpty
  446.     end
  447.     if currentTier == 2 then
  448.         primaryCol = colorTable.rare.PrimaryCol
  449.         secondaryCol = colorTable.rare.SecondaryCol
  450.         textCol = colorTable.rare.textCol
  451.         progressFilled = colorTable.rare.progressFilled
  452.         progressEmpty = colorTable.rare.progressEmpty
  453.     end
  454.     if currentTier == 3 then
  455.         primaryCol = colorTable.epic.PrimaryCol
  456.         secondaryCol = colorTable.epic.SecondaryCol
  457.         textCol = colorTable.epic.textCol
  458.         progressFilled = colorTable.epic.progressFilled
  459.         progressEmpty = colorTable.epic.progressEmpty
  460.     end
  461.     if currentTier == 4 then
  462.         primaryCol = colorTable.legendary.PrimaryCol
  463.         secondaryCol = colorTable.legendary.SecondaryCol
  464.         textCol = colorTable.legendary.textCol
  465.         progressFilled = colorTable.legendary.progressFilled
  466.         progressEmpty = colorTable.legendary.progressEmpty
  467.     end
  468.     addRect(1,1,width,height,primaryCol)
  469.     addRect(8,4,width-8,height-4,secondaryCol)
  470.     gpu.setBackground(primaryCol)
  471.     gpu.setForeground(textCol)
  472.     addProgressBar(12,height - 10,width - 12,height - 6,2,1,primaryCol,progressFilled,progressEmpty,0x000000,"",filledPercentage,true,false)
  473.    
  474.     centerText("COMMUNITY CHALLENGE", 1, w, 5, textCol, secondaryCol)
  475.     centerText("Work together to complete this challenge", 1, w, 7, textCol, secondaryCol)
  476.     centerText("And everyone online will be rewarded!", 1, w, 9, textCol, secondaryCol)
  477.    
  478.     centerText("Deliver " .. currentObjective[2]*multiplier .. " of " .. currentObjective[1] .. " to our community chest!", 1, w, height - 12, textCol, secondaryCol)
  479.     centerText("Time remaining: " .. timeLeft, 1, w, height - 11, textCol, secondaryCol)
  480.     if storedAmount ~= nil then
  481.         centerText("Current objective is at: ".. storedAmount .." / ".. currentObjective[2]*multiplier, 1, w, height-5, textCol, secondaryCol)
  482.     end
  483.    
  484. end
  485.  
  486. init()
  487. repeat
  488.     main()
  489. until false
Advertisement
Add Comment
Please, Sign In to add comment