Advertisement
massacring

startup

Jun 18th, 2024 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. local Screen = require('Screen')
  2. local MassaPeripheralLib = require('MassaPeripheralLib')
  3. local MassaMainLib = require('MassaMainLib')
  4.  
  5. local modem = MassaPeripheralLib.getOnePeripheral("modem", function(name, peripheral)
  6.     return peripheral.isWireless()
  7. end)
  8. local channel = 4243
  9. modem.open(channel)
  10.  
  11. local _monitor = MassaPeripheralLib.getOnePeripheral("monitor", function(name, peripheral)
  12.     return peripheral.isColor()
  13. end)
  14.  
  15. local mainScreen = Screen:new(nil, "main", _monitor, colors.gray)
  16. local reactorScreen = Screen:new(nil, "storage", _monitor, colors.gray)
  17. local currentScreen = mainScreen
  18.  
  19. local function loadReactorScreen()
  20.     local homeButton = reactorScreen:createButton("Home", function (button)
  21.         currentScreen = mainScreen
  22.         mainScreen:loadScreen()
  23.     end, 2, reactorScreen.height - 5, 1, 1, colors.lightBlue, colors.yellow, colors.blue, colors.orange, colors.white, colors.lightGray, false)
  24.  
  25.     local refreshButton = reactorScreen:createButton("Refresh", function (button)
  26.         reactorScreen:loadScreen()
  27.     end, 11, reactorScreen.height - 5, 1, 1, colors.lightBlue, colors.yellow, colors.blue, colors.orange, colors.white, colors.lightGray, false)
  28.  
  29.     function reactorScreen:loadScreen()
  30.         self:clearWindow(self.backgroundColor)
  31.         self:placeButtons()
  32.     end
  33. end
  34. loadReactorScreen()
  35.  
  36. local function loadMainScreen()
  37.     local reactor = mainScreen:createButton("Reactor", function (button)
  38.         currentScreen = reactorScreen
  39.         reactorScreen:loadScreen()
  40.     end, 0, 0, 1, 1, colors.lightBlue, colors.yellow, colors.blue, colors.orange, colors.white, colors.lightGray, true)
  41.  
  42.     local rainbow = mainScreen:createButton("Rainbow", function (button)
  43.         mainScreen:loadRainbow()
  44.         mainScreen:loadScreen()
  45.     end, mainScreen.width - 9, mainScreen.height - 3, 1, 1, nil, nil, nil, nil, nil, nil, false)
  46. end
  47. loadMainScreen()
  48.  
  49. currentScreen:loadScreen()
  50.  
  51. while true do
  52.     local eventData = {os.pullEvent()}
  53.     local event = eventData[1]
  54.  
  55.     if event == 'monitor_touch' then
  56.         local x, y = eventData[3], eventData[4]
  57.         for index, button in pairs(currentScreen.buttonsRegistry) do
  58.             if not button.isActive then goto continue end
  59.             if ((x >= button.x) and (x < (button.x + button.width))) and ((y >= button.y) and (y < (button.y + button.height))) then
  60.                 button.clickEvent(button)
  61.                 break
  62.             end
  63.             ::continue::
  64.         end
  65.     elseif event == 'modem_message' then
  66.         local senderChannel, replyChannel, message = eventData[3], eventData[4], eventData[5]
  67.         local result = MassaMainLib.split(message, "-")
  68.         message = result[1]
  69.         local value = tonumber(result[2])
  70.         if senderChannel == channel then
  71.             print("Received a message:", tostring(message))
  72.  
  73.             local switch = function (argument)
  74.                 argument = argument and tonumber(argument) or argument
  75.  
  76.                 local case =
  77.                 {
  78.                     reboot = function ()
  79.                         modem.transmit(replyChannel, senderChannel, "Rebooting...")
  80.                         os.reboot()
  81.                     end,
  82.                     rainbow = function ()
  83.                         modem.transmit(replyChannel, senderChannel, "How tasteful!")
  84.                         currentScreen:loadRainbow(value)
  85.                         currentScreen:loadScreen()
  86.                     end,
  87.                     default = function ()
  88.                         print("Invalid command")
  89.                     end
  90.                 }
  91.  
  92.                 if case[argument] then
  93.                     case[argument]()
  94.                 else
  95.                     case["default"]()
  96.                 end
  97.             end
  98.  
  99.             switch(message)
  100.         end
  101.     end
  102. end
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement