Advertisement
Shaka01

Pocket Computer Startup

Mar 2nd, 2023 (edited)
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.73 KB | None | 0 0
  1. --load dependencies
  2.  
  3. if fs.exists("stockPile") == false then
  4. shell.run("pastebin", "get", "bWaSkZ2v", "stockPile")
  5. end
  6. if fs.exists("listen") == false then
  7. shell.run("pastebin", "get", "eEbSAZZV", "listen")
  8. end
  9. if fs.exists("API") == false then
  10. shell.run("pastebin", "get", "EzkfU5ZM", "API")
  11. end
  12. if fs.exists("messenger") == false then
  13. shell.run("pastebin", "get", "cJa7YLVs", "messenger")
  14. end
  15. shaka = require("API")
  16. rednet.open("back")
  17. shell.run("set", "motd.enable", "false")
  18.  
  19. ---functions
  20.  
  21. local function receiveInfos()
  22. if fs.exists(".userData") then
  23.     local msg = nil
  24.     local file = fs.open(".userData", "r")
  25.     local userName = file.readAll()
  26.     file.close()
  27.     local end_index = string.len(userName)
  28.     local computerName = os.computerLabel()
  29.     local extracted_substring = ""
  30.     if computerName ~= nil then
  31.         extracted_substring = string.sub(computerName, 1, end_index)
  32.     end
  33.     if extracted_substring ~= userName then
  34.         os.setComputerLabel(userName.."'s Pocket Rocket")
  35.     end
  36.     rednet.broadcast(userName, "anyMessages?")
  37.     local sender, msg = rednet.receive("notificationReminder", 1)
  38.     if msg ~= nil then
  39.         if tonumber(msg) ~= 0 then
  40.             term.setCursorPos(1,1)
  41.             shaka.changeColors(colors.green, colors.black)
  42.             term.clearLine()
  43.             if tonumber(msg) > 1 then
  44.                 shaka.centerText("New messages: " ..msg)
  45.             else
  46.                 shaka.centerText("New message!")
  47.             end
  48.         end
  49.     else
  50.         shaka.changeColors(colors.red, colors.black)
  51.         term.clear()
  52.         shaka.centerText("Server not responding!", 7)
  53.         term.setCursorPos(8, 9)
  54.         textutils.slowPrint("Restarting..", 10)
  55.         sleep(2)
  56.         os.reboot()
  57.     end
  58. else
  59. shell.run("messenger")
  60. end
  61. end
  62.  
  63.  
  64. function showButtons()
  65.  
  66.   -- Define the buttons and their associated programs and colors
  67.   local buttons = {
  68.       {label="Messenger", program="messenger", color = colors.purple},
  69.       {label="View stockpile items", program="stockPile", color = colors.green},
  70.       {label="Listen to requests", program="listen", color = colors.gray},
  71.       -- {label="test", program="test.lua", color = colors.orange},
  72.       -- {label="Program 5", program="program3.lua", color = colors.red}
  73.   }
  74.  
  75.   -- Set the background color and padding size
  76.   local bgColor = colors.black
  77.   local padding = 2
  78.  
  79.   -- Calculate the size of the buttons and the total size of the interface
  80.   local buttonWidth = 24
  81.   local buttonHeight = 3
  82.   local interfaceWidth, interfaceHeight = term.getSize()
  83.   interfaceHeight = (buttonHeight + padding) * #buttons + padding
  84.  
  85.   -- Create a new canvas to draw the interface on
  86.   local canvas = {}
  87.   for y = 1, interfaceHeight do
  88.     canvas[y] = {}
  89.     for x = 1, interfaceWidth do
  90.       canvas[y][x] = {bg = bgColor, fg = colors.white, char = " "}
  91.     end
  92.   end
  93.  
  94.   -- Draw the background on the canvas
  95. term.setBackgroundColor(colors.black)
  96. term.clear()
  97.  
  98.   -- Draw the buttons on the canvas
  99.   for i, button in ipairs(buttons) do
  100.       local x = padding
  101.       local y = padding + (i-1) * (buttonHeight + padding)
  102.       for y2 = y, y + buttonHeight - 1 do
  103.         for x2 = x, x + buttonWidth - 1 do
  104.           canvas[y2][x2].bg = button.color
  105.         end
  106.       end
  107.       local label = string.sub(button.label, 1, buttonWidth)
  108.       local labelLength = #label
  109.       local labelX = x + math.floor((buttonWidth - labelLength) / 2)
  110.       local labelY = y + math.floor((buttonHeight - 1) / 2)
  111.       for j = 1, labelLength do
  112.         canvas[labelY][labelX+j-1] = {bg = button.color, fg = colors.black, char = string.sub(label, j, j)}
  113.       end
  114.   end
  115.  
  116.   -- Draw the canvas on the screen
  117.   term.setBackgroundColor(bgColor)
  118.   term.clear()
  119.   for y = 1, interfaceHeight do
  120.     for x = 1, interfaceWidth do
  121.       term.setCursorPos(x, y)
  122.       term.setTextColor(canvas[y][x].fg)
  123.       term.setBackgroundColor(canvas[y][x].bg)
  124.       term.write(canvas[y][x].char)
  125.     end
  126.   end
  127.    receiveInfos()
  128. -- Wait for a button to be clicked
  129.   -- while true do
  130.     local event, _, x, y = os.pullEvent("mouse_click")
  131.     for i, button in ipairs(buttons) do
  132.       local buttonX = padding
  133.       local buttonY = padding + (i-1) * (buttonHeight + padding)
  134.       if x >= buttonX and x < buttonX + buttonWidth and y >= buttonY and y < buttonY + buttonHeight then
  135.         shell.run(button.program)
  136.         return
  137.       end
  138.     end
  139.   -- end
  140. end
  141.  
  142. function receiveReminders()
  143.     msg = nil
  144.     local id, msg, protocol = rednet.receive("notificationReminder")
  145.     print(id, msg, protocol)
  146.     if msg ~= nil then
  147.         if tonumber(msg) ~= 0 then
  148.             term.setCursorPos(1,1)
  149.             shaka.changeColors(colors.green, colors.black)
  150.             term.clearLine()
  151.             if tonumber(msg) > 1 then
  152.                 shaka.centerText("New messages: " ..msg)
  153.             else
  154.                 shaka.centerText("New message!")
  155.             end
  156.         end
  157.     end
  158. end
  159.    
  160.  
  161.  
  162. while true do
  163.     showButtons()
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement