Daniel_I_Am

Community Goals 2.0.4

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