Daniel_I_Am

Community Goals 2.0

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