RenKylo

SmartHome #2

Jun 19th, 2019 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local computer = require("computer")
  3. local gui = require("gui")
  4. local unicode=require("unicode")
  5. local gpu = component.gpu
  6. local event = require('event')
  7. local chat = component.chat_box
  8. local redstone = component.redstone
  9.  
  10. local level, file, file_text, message_orig
  11. local array
  12. local saved = {}
  13. local rep_players = {}
  14.  
  15. --SETTINGS
  16. local RIGHTS = {["guard"]=99,["light"]=1,["music"]=1,["hello"]=1,["repeat"]=7}
  17. local NAME = "§6Алекса§7"
  18. local DISTANCE = 150
  19.  
  20. local OUTPUTS = {["guard"]=1,["light"]=2,["music"]=3}
  21.  
  22. updateRes(56, 29)
  23.  
  24. -- Переменную с окнами должна быть глобальной!!!
  25. Home = {['windows'] = {
  26.                   [1] = {
  27.                     ['head'] = {['width'] = "parentX - 4", ['max-width'] = 120, ['height'] = "parentY - 2", ['visible'] = true},
  28.                     ['body'] = {
  29.                       {['type'] = 'rect', ['style'] = {1, 1, "full", "full", 0x171717, false}},
  30.                       {['type'] = 'rect', ['style'] = {"center", "center", "parent", "parent", 0xeeeeee, false}},
  31.                       {['type'] = 'text', ['style'] = {"center", 3, 0x000}, ['text'] = '&0Умный дом '},
  32.                       {['type'] = 'line', ['style'] = {"center", 4, "parent", 0x000}},
  33.                       {['type'] = 'text', ['style'] = {4, 3, 0x000}, ['text'] = '&0© &9RenKylo'},
  34.                     }
  35.                   },
  36.                 }
  37.               }
  38.  
  39. local elem_count = #Home['windows'][1]['body']
  40.  
  41. local function touch(_, _, x, y)
  42.   for i = 1, #buttons do
  43.     if x >= buttons[i]['x'] and x <= buttons[i]['x'] + buttons[i]['width'] and y >= buttons[i]['y'] and y <= buttons[i]['y'] + buttons[i]['height'] then
  44.       -- Анимация кнопки при клике
  45.       click(buttons[i])
  46.       -- Выполнение функции кнопки
  47.       buttons[i]['action'](player)
  48.       break
  49.     end
  50.   end
  51. end
  52.  
  53. local e = {event.listen('touch', touch)} -- Ждём клика на экран
  54.  
  55. --Получение списка Мемберов
  56. local MEMBERS = {}
  57. local file = io.open("users", "r")
  58. file_text = file:read("*a")
  59. file:close()
  60. for i in string.gmatch(file_text, "[^,]+") do --Делим текст
  61.     array = {}
  62.     for a in string.gmatch(i, "[^ ]+") do
  63.         array[#array+1] = a
  64.     end
  65.     MEMBERS[array[1]] = array[2]
  66. end
  67. --Загрузка сохранений дома
  68. file = io.open("config", "r")
  69. file_text = file:read("*a")
  70. file:close()
  71. for i in string.gmatch(file_text, "[^,]+") do --Делим текст
  72.     saved[#saved+1] = tonumber(i)
  73. end
  74. local LIGHT = saved[1]
  75. local MUSIC = saved[2]
  76. local GUARD = saved[3]
  77. --SWITCH VARIABLES
  78. --Pre Actions
  79. os.execute("clear")
  80. chat.setName(NAME)
  81. chat.setDistance(DISTANCE)
  82. if LIGHT == 1 then redstone.setOutput(OUTPUTS["light"], 16) else redstone.setOutput(OUTPUTS["light"], 0) end --Свет
  83. if GUARD == 1 then redstone.setOutput(OUTPUTS["guard"], 16) else redstone.setOutput(OUTPUTS["guard"], 0) end --Защита
  84. if MUSIC == 1 then redstone.setOutput(OUTPUTS["music"], 16) else redstone.setOutput(OUTPUTS["music"], 0) end --Музыка
  85. --Actions
  86. function save()
  87.     file = io.open("config", "w")
  88.     file:write(LIGHT..","..MUSIC..","..GUARD)
  89.     file:close()
  90. end
  91.  
  92. function updateData()
  93.   local g, m, l, r = "&cВыключено", "&cВыключено", "&cВыключено"
  94.   if LIGHT == 1 then l = "&2Включено" end
  95.   if MUSIC == 1 then m = "&2Включено" end
  96.   if GUARD == 1 then g = "&2Включено" end
  97.   Home['windows'][1]['body'][elem_count + 1] = {
  98.     ['type'] = 'text', ['style'] = {4, 6, 0x000}, ['text'] = "&8Защита: "..g
  99.   }
  100.   Home['windows'][1]['body'][elem_count + 2] = {
  101.     ['type'] = 'text', ['style'] = {4, 8, 0x000}, ['text'] = "&8Освещение: "..l
  102.   }
  103.   Home['windows'][1]['body'][elem_count + 3] = {
  104.     ['type'] = 'text', ['style'] = {4, 10, 0x000}, ['text'] = "&8Музыка: "..m
  105.   }
  106.   local players = ""
  107.   for i = 1, #rep_players do
  108.     if i ~= 1 then
  109.       players =  players..", "
  110.     end
  111.     players =  players..rep_players[i]
  112.     i = i + 1
  113.   end
  114.   if players == "" then
  115.     players = "&0Нет игроков для повтора"
  116.   end
  117.   Home['windows'][1]['body'][elem_count + 4] = {
  118.     ['type'] = 'text', ['style'] = {4, 12, 0x000}, ['text'] = "&8Повторение: &9"..players
  119.   }
  120.   draw(Home) -- Обновляем интерфейс
  121. end
  122.  
  123. function findRepPlayer(player)
  124.   local index = 0
  125.   for i = 1, #rep_players do
  126.     if player == rep_players[i] then
  127.       index = i
  128.     end
  129.     i = i + 1
  130.   end
  131.   return index
  132. end
  133. updateData()
  134. while true do
  135.     local _,_,player,msg = event.pull("chat_message")
  136.     message_orig = msg
  137.     msg = unicode.lower(msg)
  138.     if MEMBERS[string.lower(player)] then
  139.         level = tonumber(MEMBERS[string.lower(player)])
  140.         if msg == "-защита" and level >= RIGHTS["guard"] then
  141.             chat.say("§eЗащита §a§lактивирована")
  142.             redstone.setOutput(OUTPUTS["guard"], 16)
  143.             GUARD = 1
  144.         elseif msg == "-стоп" and level >= RIGHTS["guard"] then
  145.             chat.say("§eЗащита §c§lдеактивирована")
  146.             redstone.setOutput(OUTPUTS["guard"], 0)
  147.             GUARD = 0
  148.         elseif msg == "-свет" and level >= RIGHTS["light"] then
  149.             if LIGHT == 0 then
  150.                 chat.say("§eСвет §a§lOn")
  151.                 redstone.setOutput(OUTPUTS["light"], 16)
  152.                 LIGHT = 1
  153.             else
  154.                 chat.say("§eСвет §c§lOff")
  155.                 redstone.setOutput(OUTPUTS["light"], 0)
  156.                 LIGHT = 0
  157.             end
  158.         elseif msg == "-музыка" and level >= RIGHTS["music"] then
  159.             if MUSIC == 0 then
  160.                 chat.say("§eМузыка §a§lOn")
  161.                 MUSIC = 1
  162.             else
  163.                 chat.say("§eМузыка §c§lOff")
  164.                 MUSIC = 0
  165.             end
  166.             redstone.setOutput(OUTPUTS["music"], 16)
  167.             redstone.setOutput(OUTPUTS["music"], 0)
  168.         elseif msg == "-привет" and level >= RIGHTS["hello"] then
  169.             chat.say("§fПриветствую, §c"..player)
  170.         elseif msg == "-повтор" and level >= RIGHTS["repeat"] then
  171.           local index = findRepPlayer(player)
  172.           if index ~= 0 then
  173.             rep_players[index] = nil
  174.             chat.say("§eПовтор чата для §9"..player.." §c§lOff")
  175.           else
  176.             rep_players[#rep_players+1] = player
  177.             chat.say("§eПовтор чата для §9"..player.." §a§lOn")
  178.           end
  179.         elseif msg == "-взорви всё нахуй" and level >= RIGHTS["guard"] then
  180.             chat.say("§c"..player.." §fзапустил отсчёт")
  181.             for i = 5, 1, -1 do
  182.                 chat.say("§fДо взрыва §c"..i)
  183.                 os.sleep(1)
  184.             end
  185.             redstone.setOutput(OUTPUTS["light"], 0)
  186.             LIGHT = 0
  187.             chat.say("§fЯ...§fв-выключаюсь...§f. §fПрощайте, §c"..player)
  188.             os.execute("clear")
  189.             os.sleep(10)
  190.         else
  191.           if findRepPlayer(player) ~= 0 then
  192.             chat.setName("§aАнтиМут§7] [§6"..level.."§7")
  193.             chat.say("§c"..player.."§f: "..message_orig)
  194.             chat.setName(NAME)
  195.           end
  196.         end
  197.         updateData()
  198.         save() --Сохраняем все
  199.     end
  200. end
Add Comment
Please, Sign In to add comment