Advertisement
Reavik

RobsonModem

Jan 16th, 2023 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | Gaming | 0 0
  1. --by: Player_rs, Reavik, zResckBR
  2. local botName = "Robson"
  3. local color = 32
  4.  
  5. local modem = peripheral.find("modem")
  6. modem.open(100, 200)
  7.  
  8. local monitor = peripheral.wrap("left")
  9. local pos = 1
  10. print("Positivo e Operando")
  11.  
  12. monitor.clear()
  13. --editado
  14. function ler()
  15.     if not fs.exists(botName.."/log.lua") then
  16.         monitor.setTextScale(0.5)
  17.         monitor.setTextColour(color)
  18.         monitor.setCursorPos(1,pos+1)
  19.         monitor.write("Arquivo de log não encontrado, criando log.lua")
  20.  
  21.         fs.makeDir(botName.."/log.lua")
  22.         local f = fs.open(botName.."/log.lua", "w")
  23.         f.write("{\"Positivo e operante\"}")
  24.         f.close()
  25.     end
  26.     -----------------------------------------
  27. local f = fs.open(botName.."/log.lua", "r")
  28. local data = textutils.unserialize(f.readAll())
  29.         for i = 1, #data do
  30.             monitor.setBackgroundColour(32768)
  31.             monitor.setTextScale(0.5)
  32.             monitor.setTextColour(color)
  33.             monitor.setCursorPos(1,pos+1)
  34.             pos = pos+1
  35.             monitor.write(data[i])
  36.         end
  37.     f.close()
  38. end
  39. ler()
  40. function escrever(resposta)
  41.     local time = os.date('*t')
  42.     local f = fs.open(botName.."/log.lua", "r")
  43.     local data = textutils.unserialize(f.readAll())
  44.     f.close()
  45.     local a = fs.open(botName.."/log.lua", "w")
  46.  
  47.     for _, v in pairs({"day", "month", "year", "hour", "min", "sec"}) do
  48.         if time[v] < 10 then
  49.             time[v] = "0"..time[v]
  50.         end
  51.     end
  52.     if #data >= 79 then
  53.         table.remove(data, 1)
  54.         table.insert(data, string.format("[%s/%s/%s-%s:%s:%s]: %s", time.day, time.month, time.year, time.hour, time.min, time.sec, message))
  55.         a.write(textutils.serialize(data))
  56.     else
  57.         table.insert(data, string.format("[%s/%s/%s-%s:%s:%s]: %s", time.day, time.month, time.year, time.hour, time.min, time.sec, resposta))
  58.         a.write(textutils.serialize(data))
  59.     end
  60.     a.close()
  61. end
  62.  
  63.     while true do
  64.         local e, p,channel, replyChannel, message, distance = os.pullEvent("modem_message")
  65.         print(message)
  66.         if message == "mon" then
  67.             monitor.clear()
  68.             local f = fs.open(botName.."/log.lua", "w")
  69.             f.write("{}")
  70.             f.close()
  71.             monitor.write("["..botName.."] Monitor e log limpos com sucesso.")
  72.             os.reboot()
  73.         end
  74.         if message ~= new then
  75.         escrever(message)
  76.         local time = os.date('*t')
  77.         for _, v in pairs({"day", "month", "year", "hour", "min", "sec"}) do
  78.             if time[v] < 10 then
  79.                 time[v] = "0"..time[v]
  80.             end
  81.         end
  82.         monitor.setBackgroundColour(32768)
  83.         monitor.setTextScale(0.5)
  84.         monitor.setTextColour(color)
  85.         monitor.setCursorPos(1,pos+1)
  86.         pos = pos+1
  87.         monitor.write(string.format("[%s/%s/%s-%s:%s:%s]: %s", time.day, time.month, time.year, time.hour, time.min, time.sec, message))
  88.         if pos >= 79 then
  89.             monitor.scroll(1)
  90.             pos = 78
  91.         end
  92.     end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement