csmit195

MasterServer

Sep 19th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.24 KB | None | 0 0
  1. -- Modem
  2. os.loadAPI("json")
  3.  
  4. local modem = peripheral.wrap('front')
  5. local glass = peripheral.wrap('bottom')
  6. local power = peripheral.wrap('left')
  7.  
  8. -- Glasses
  9. local text = 'POWER: 0.00'
  10. local startX = 20
  11. local startY = 5
  12.  
  13. local statusID = 'AWWayofTime:bloodMagicBaseItems'
  14. local powerID = 'IC2:fluidUuMatter'
  15.  
  16. local c = 0
  17. local spawnerChannel = 3101
  18. local thisChannel = 3000
  19.  
  20. local coreState
  21. local viewerCount = 0
  22.  
  23. -- Accepted Values for Commands
  24. local booleanValues = {}
  25. booleanValues['true'] = true
  26. booleanValues['yes'] = true
  27. booleanValues['y'] = true
  28. booleanValues['on'] = true
  29. booleanValues['false'] = false
  30. booleanValues['no'] = false
  31. booleanValues['n'] = false
  32. booleanValues['off'] = false
  33.  
  34. local spawnerStates = {}
  35. spawnerStates.cow = false
  36. spawnerStates.spider = false
  37. spawnerStates.basalz = false
  38. spawnerStates.blitz = false
  39.  
  40. function init()
  41.   if ( checkErrors() ) then
  42.     error('error found')
  43.   end
  44.  
  45.   modem.transmit(spawnerChannel, thisChannel, {'setState', 'all', false})
  46.  
  47.   parallel.waitForAll(glassesLoop, eventLoop, updateTwitchViewerCount)
  48. end
  49.  
  50. -- Loops
  51. function spawnerLoop()
  52.   while true do
  53.     local items = peripheral.call('right', 'getAvailableItems')
  54.     if ( #items > 0 ) then
  55.       for slot, item in pairs(items) do
  56.         local itemId = item.fingerprint.id
  57.         if ( itemId == 'minecraft:leather' ) then
  58.           modem.transmit(spawnerChannel, thisChannel, {'setState', 'cow', (item.size <= 640)})
  59.         end
  60.       end
  61.     end
  62.     sleep(10)
  63.   end
  64. end
  65.  
  66. function glassesLoop()
  67.   while true do
  68.     glass.clear()
  69.  
  70.     -- Power
  71.     local powerUsed = power.getEnergyStored()
  72.     local powerMax = power.getMaxEnergyStored()
  73.     local powerPercentage = ( ( powerUsed / powerMax ) * 100 )
  74.     local text = 'POWER: ' .. round(powerPercentage, 2)
  75.  
  76.     addChargeProgress(startX+1, startY+1, powerID, nil, powerUsed, powerMax, 0.75, 0x7f7f7f, 0.5, 0x4B0082, 1, 0.25, 0.5, 3, text)
  77.  
  78.     -- Core Status
  79.     local coreText = glass.addText(startX+5,startY+25, 'Core Status: ', 0xFFFF00)
  80.     coreText.setScale(0.5)
  81.         glass.addIcon(startX+5+getStringWidth('Core Status: '),startY+21, statusID, ( coreState and 22 or 12 ))
  82.  
  83.         -- Spawner Status
  84.     local spawnerStatus = 'Cow: ' .. (spawnerStates['cow'] and 'y' or 'n') .. ', Spider: ' .. (spawnerStates['spider'] and 'y' or 'n') .. ', Blitz: ' .. (spawnerStates['blitz'] and 'y' or 'n') .. ', Basalz: ' .. (spawnerStates['basalz'] and 'y' or 'n')
  85.     glass.addText(startX+5,startY+40, spawnerStatus, 0xFF6900)
  86.  
  87.  
  88.     glass.addText(startX+5,startY+55, 'Twitch Viewers: ' .. viewerCount, 0x00FF00)
  89.  
  90.     glass.sync()
  91.     sleep(1)
  92.   end
  93. end
  94.  
  95. function updateTwitchViewerCount()
  96.   local headers = {
  97.     ['Client-ID'] = '2uiwi7gks15yxct7abpic82z66itg1'
  98.   }
  99.   while true do
  100.     local response = http.get("https://api.twitch.tv/helix/streams?user_id=82541781", headers)
  101.     if ( response ~= nil ) then
  102.         local responseContent = response.readAll()
  103.         local obj = json.decode(responseContent)
  104.         if ( obj.data ~= nil and obj.data[1] ~= nil and obj.data[1].viewer_count ~= nil ) then
  105.           viewerCount = obj.data[1].viewer_count
  106.         end
  107.         sleep(30)
  108.       else
  109.         print(textutils.serialise(response), response)
  110.     end
  111.   end
  112. end
  113.  
  114.  
  115. function eventLoop()
  116.   while true do
  117.     event,action,user,uuid,message = os.pullEvent()
  118.     if event == 'glasses_chat_command' then
  119.       if ( message:len() > 0 ) then
  120.         local cmd = split(message, '%s+')
  121.         if ( cmd[1] == 'shutdown' ) then
  122.           if ( #cmd == 2 ) then
  123.             if ( cmd[2] == 'core' ) then
  124.               setCoreState(true)
  125.             end
  126.           else
  127.             print('Expected 1 arguments... got ', #cmd-1)
  128.           end
  129.         elseif ( cmd[1] == 'start' ) then
  130.           if ( #cmd == 2 ) then
  131.             if ( cmd[2] == 'core' ) then
  132.               setCoreState(false)
  133.             end
  134.           else
  135.             print('Expected 1 arguments... got ', #cmd-1)
  136.           end
  137.         elseif ( cmd[1] == 'spawner' ) then
  138.           if ( #cmd == 3 ) then
  139.             if ( #cmd[2] > 1 and #cmd[3] > 1 ) then
  140.               if ( booleanValues[cmd[3]] ~= nil ) then
  141.                 modem.transmit(spawnerChannel, thisChannel, {'setState', cmd[2], booleanValues[cmd[3]]})
  142.                 if ( cmd[2] == 'all' ) then
  143.                   for spawner, state in pairs(spawnerStates) do
  144.                     spawnerStates[spawner] = booleanValues[cmd[3]]
  145.                   end
  146.                 else
  147.                   spawnerStates[cmd[2]] = booleanValues[cmd[3]]
  148.                 end
  149.               else
  150.                 print('2nd argument not recognised got,', cmd[3])
  151.               end
  152.             else
  153.               print('Values need to be over 1 character long, got ', cmd[2], ' and ', cmd[3])
  154.             end
  155.           else
  156.             print('Expected 2 arguments... got', #cmd-1)
  157.           end
  158.         elseif ( cmd[1] == 'update' ) then
  159.           shell.run('update')
  160.         end
  161.       end
  162.     elseif event == 'timer' then
  163.       -- Alarm
  164.       if ( alarmTimer and action == alarmTimer ) then
  165.         redstone.setOutput('top', false)
  166.       end
  167.     end
  168.   end
  169. end
  170.  
  171. function checkErrors()
  172.   return false
  173. end
  174.  
  175. function setCoreState(state)
  176.   redstone.setOutput('back', state)
  177.   redstone.setOutput('top', state)
  178.   coreState = state
  179.   alarmTimer = os.startTimer(5)
  180. end
  181.  
  182. -- Utility Functions
  183.  
  184. function addLiquidProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, name, text)
  185.   local w = getStringWidth(text) + 21
  186.   local h = 18
  187.   glass.addLiquid(x + 1, y + 1, w - 2, h - 2, name)
  188.   glass.addBox(x, y, w, 1, s_rgb, s_a)
  189.   glass.addBox(x, y + h - 1, w, 1, s_rgb, s_a)
  190.   glass.addBox(x, y + 1, 1, h - 2, s_rgb, s_a)
  191.   glass.addBox(x + w - 1, y + 1, 1, h - 2, s_rgb, s_a)
  192.   local amt_prog = math.ceil(amt / max_amt * w - 2)
  193.   glass.addBox(x + 1 + amt_prog, y + 1, w - 2 - amt_prog, h - 2, 0x000000, n_a)
  194.   glass.addIcon(x + 1, y + 1, id, meta)
  195.   glass.addText(x + 18, y + 5, text)
  196. end
  197.  
  198. function addChargeProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, rgb, a, tb_a, e_a, e_int, text)
  199.     local w = getStringWidth(text) + 34
  200.   local h = 18
  201.   glass.addBox(x + 1, y + 1, w - 2, h - 2, rgb, a)
  202.   local i = x + 1 + e_a
  203.   while i <= x + w - 2 do
  204.       glass.addBox(i, y + 1, 1, h - 2, 0x000000, e_a)
  205.       i = i + e_int
  206.   end
  207.   glass.addBox(x + 1, y + 1, w - 2, 3, 0x000000, tb_a)
  208.   glass.addBox(x + 1, y + h - 4, w - 2, 3, 0x000000, tb_a)
  209.   glass.addBox(x, y, w, 1, s_rgb, s_a)
  210.   glass.addBox(x, y + h - 1, w, 1, s_rgb, s_a)
  211.   glass.addBox(x, y + 1, 1, h - 2, s_rgb, s_a)
  212.   glass.addBox(x + w - 1, y + 1, 1, h - 2, s_rgb, s_a)
  213.   local amt_prog = math.ceil(amt / max_amt * w - 2)
  214.   glass.addBox(x + 1 + amt_prog, y + 1, w - 2 - amt_prog, h - 2, 0x000000, n_a)
  215.   glass.addIcon(x + 1, y + 1, id, meta)
  216.   glass.addText(x + 22, y + 5, text)
  217. end
  218.  
  219. function gridLiquidProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, name, text)
  220.   x = x - 1
  221.   y = y - 1
  222.   addLiquidProgress(x * 19 + 1, y * 19 + 1, id, meta, amt, max_amt, n_a, s_rgb, s_a, name, text)
  223.   return math.ceil((getStringWidth(text) + 20) / 19), 1
  224. end
  225.  
  226. function getStringWidth(text)
  227.   local w = 0
  228.   local def = 5
  229.   local w1 = "i!,.:;|"
  230.   local w2 = "'l"
  231.   local w3 = " t"
  232.   local w4 = "*{}()\"<>f"
  233.   local w6 = "~"
  234.   for i = 1, string.len(text) do
  235.       local c = string.sub(text, i, i)
  236.       if string.find(w1, c) ~= nil then
  237.           w = w + 1
  238.       elseif string.find(w2, c) ~= nil then
  239.           w = w + 2
  240.       elseif string.find(w3, c) ~= nil then
  241.           w = w + 3
  242.       elseif string.find(w4, c) ~= nil then
  243.           w = w + 4
  244.       elseif string.find(w6, c) ~= nil then
  245.           w = w + 6
  246.       else
  247.           w = w + 5
  248.       end
  249.       if i < string.len(text) - 1 then
  250.           w = w + 1
  251.       end
  252.   end
  253.   return w
  254. end
  255.  
  256. function esc(x)
  257.     return (x:gsub('%%', '%%%%')
  258.             :gsub('^%^', '%%^')
  259.             :gsub('%$$', '%%$')
  260.             :gsub('%(', '%%(')
  261.             :gsub('%)', '%%)')
  262.             :gsub('%.', '%%.')
  263.             :gsub('%[', '%%[')
  264.             :gsub('%]', '%%]')
  265.             :gsub('%*', '%%*')
  266.             :gsub('%+', '%%+')
  267.             :gsub('%-', '%%-')
  268.             :gsub('%?', '%%?'))
  269. end
  270.  
  271. function split(str, pat)
  272.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  273.     local fpat = '(.-)' .. pat
  274.     local last_end = 1
  275.     local s, e, cap = str:find(fpat, 1)
  276.     while s do
  277.       if s ~= 1 or cap ~= '' then
  278.          table.insert(t,cap)
  279.       end
  280.       last_end = e+1
  281.       s, e, cap = str:find(fpat, last_end)
  282.     end
  283.     if last_end <= #str then
  284.       cap = str:sub(last_end)
  285.       table.insert(t, cap)
  286.     end
  287.     return t
  288. end
  289.  
  290. function ReadableNumber(num, places)
  291.   local ret
  292.   local placeValue = ('%%.%df'):format(places or 0)
  293.   if not num then
  294.     return 0
  295.   elseif num >= 1000000000000 then
  296.     ret = placeValue:format(num / 1000000000000) .. 'T' -- trillion
  297.   elseif num >= 1000000000 then
  298.     ret = placeValue:format(num / 1000000000) .. 'B' -- billion
  299.   elseif num >= 1000000 then
  300.     ret = placeValue:format(num / 1000000) .. 'M' -- million
  301.   elseif num >= 1000 then
  302.     ret = placeValue:format(num / 1000) .. 'K' -- thousand
  303.   else
  304.     ret = num -- hundreds
  305.   end
  306.   return ret
  307. end
  308.  
  309. function round(num, numDecimalPlaces)
  310.   local mult = 10^(numDecimalPlaces or 0)
  311.   return math.floor(num * mult + 0.5) / mult
  312. end
  313.  
  314. init()
Advertisement
Add Comment
Please, Sign In to add comment