Daniel_I_Am

Community Goals 2.0.2

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