Zekrommaster110

[LUA | CC1.4.7] CC Remote Tool v2 - SERVER

Sep 18th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1. -- CC REMOTE TOOL v2 - SERVER
  2.  
  3. -- Copyright 2018 zekro Development (Ringo Hoffmann)
  4.  
  5. -- Permission is hereby granted, free of charge, to any person obtaining
  6. -- a copy of this software and associated documentation files (the "Software"),
  7. -- to deal in the Software without restriction, including without limitation
  8. -- the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. -- and/or sell copies of the Software, and to permit persons to whom the Software
  10. -- is furnished to do so, subject to the following conditions:
  11.  
  12. -- The above copyright notice and this permission notice shall be included in all
  13. -- copies or substantial portions of the Software.
  14.  
  15. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. -- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. ---------------------------------- CONSTS ----------------------------------
  23. SIDES       = {"top", "bottom", "front", "back", "left", "right"}
  24. CONFIG_FILE = "server.config"
  25. VERSION     = "S2.1.0"
  26. ----------------------------------------------------------------------------
  27.  
  28. STATUS = {
  29.     [0]   = {"DOWN", colors.red},
  30.     [1]   = {" UP ", colors.green},
  31.     [2]   = {"OFFL", colors.purple},
  32. }
  33.  
  34. statusMap = {}
  35. tileLocations = {}
  36. sizeX, sizeY = term.getSize()
  37.  
  38. function fatal(error)
  39.     print("[ERROR] ", error)
  40.     exit()
  41. end
  42.  
  43. function mountModem()
  44.     for k, v in pairs(SIDES) do
  45.         if pcall(rednet.open, v) then
  46.             return true
  47.         end
  48.     end
  49.     return false
  50. end
  51.  
  52. function writeFile(filename, table)
  53.     local file = fs.open(filename, "w")
  54.     file.write(textutils.serialize(table))
  55.     file.close()
  56. end
  57.  
  58. function readFile(filename)
  59.     local file = fs.open(filename, "r")
  60.     if file ~= nil then
  61.         local table = textutils.unserialize(file.readAll())
  62.         file.close()
  63.         return table
  64.     end
  65.     return nil
  66. end
  67.  
  68. function drawTile(text, bgColor, textColor)
  69.     term.setBackgroundColor(bgColor)
  70.     term.setTextColor(textColor)
  71.     term.write(text)
  72.     term.setBackgroundColor(colors.black)
  73.     term.setTextColor(colors.white)
  74. end
  75.  
  76. function itoa(i)
  77.     if i < 10 then
  78.         return "0" .. i
  79.     end
  80.     return tostring(i)
  81. end
  82.  
  83. function status(id)
  84.     return statusMap[id]
  85. end
  86.  
  87. function printStatus(id)
  88.     local _status = STATUS[status(id)][1]
  89.     local _color  = STATUS[status(id)][2]
  90.     drawTile("[" .. _status .. "]", _color, colors.black)
  91. end
  92.  
  93. function printTitle()
  94.     local title = ""
  95.     local text = "CC REMOTE v." .. VERSION .. " | (c) zekro 2018"
  96.     local space = (sizeX - string.len(text)) / 2
  97.     for i = 1, space, 1 do
  98.         title = title .. " "
  99.     end
  100.     title = title .. text
  101.     for i = 1, space+2, 1 do
  102.         title = title .. " "
  103.     end
  104.     drawTile(title, colors.lightGray, colors.black)
  105. end
  106.  
  107. function printGUI(config)
  108.     shell.run("clear")
  109.     printTitle()
  110.     local i = 3
  111.     for k, v in pairs(config) do
  112.         term.setCursorPos(2, i)
  113.         printStatus(k)
  114.         term.write(" ")
  115.         drawTile("[" .. itoa(k) .. "]", colors.cyan, colors.black)
  116.         term.write(" ")
  117.         drawTile(v, colors.orange, colors.black)
  118.         tileLocations[i] = k
  119.         i = i + 2
  120.     end
  121. end
  122.  
  123. function swap(n)
  124.     if n == 1 then return 0 end
  125.     return 1
  126. end
  127.  
  128. ----------------------------------- MAIN -----------------------------------
  129.  
  130. shell.run("clear")
  131.  
  132. if not mountModem() then
  133.     fatal("No modem found to mount")
  134. end
  135.  
  136. config = readFile(CONFIG_FILE)
  137. if config == nil then
  138.     writeFile(CONFIG_FILE, {
  139.         [1] = "EXAMPLE DISPLAY NAME",
  140.         [2] = "EXAMPLE DISPLAY NAME 2"
  141.     })
  142.     print("Config file was not existent yet and was created in the computers root directory as 'server.config'.\
  143.           \nOpen it and edit the variables. Then restart the server component.")
  144.     return
  145. end
  146.  
  147. for k, v in pairs(config) do
  148.     statusMap[k] = 2
  149.     local id, status = rednet.receive(10)
  150.     if id ~= nil then
  151.         statusMap[id] = status
  152.         print("Received status of " .. id .. ": " .. status)
  153.     else
  154.         print("Timed out.")
  155.     end
  156. end
  157.  
  158. while true do
  159.     printGUI(config)
  160.     event, side, x, y = os.pullEvent()
  161.     if event == "monitor_touch" then
  162.         local id = tileLocations[y]
  163.         if id ~= nil then
  164.             local _status = swap(statusMap[id])
  165.             statusMap[id] = _status
  166.             rednet.send(id, _status)
  167.         end
  168.     end
  169. end
  170.  
  171. ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment