Aleksey2093

Miner System

Aug 22nd, 2025 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.67 KB | Gaming | 0 0
  1. local chanelIdSend = 63321
  2.  
  3. local dataDir = "data/miner_system"
  4. local dataFile = dataDir .. '/' .. 'miner_system.data'
  5.  
  6. local modem = peripheral.find("modem")
  7. local monitor = peripheral.find("monitor")
  8. local speaker = peripheral.find("speaker")
  9. if modem then
  10.     -- модем на месте все окей
  11. else
  12.     print("I need modem to connect wifi")
  13. end
  14. modem.open(chanelIdSend)
  15. print("I listen to channel", chanelIdSend)
  16.  
  17.  
  18. local turtleData = {}
  19. local buttonsTop = {}
  20. local stopButton = {}  -- Для хранения координат кнопки Stop
  21. local buttonsTopSelect = false
  22.  
  23. local lastInfoLineN = 4
  24.  
  25. local function monitorWriteInfo()
  26.     if monitor then
  27.  
  28.     else
  29.         return
  30.     end
  31.  
  32.     if buttonsTopSelect then
  33.  
  34.     else
  35.         return
  36.     end
  37.  
  38.     monitor.setBackgroundColor(colors.black)
  39.  
  40.     for i=4,lastInfoLineN do
  41.         monitor.setCursorPos(2,i)
  42.         monitor.setBackgroundColor(colors.black)
  43.         monitor.clearLine()
  44.     end
  45.  
  46.     local data = turtleData[buttonsTopSelect]
  47.     if data then
  48.  
  49.     else
  50.         return
  51.     end
  52.  
  53.     local i = 4
  54.     if data['time'] then
  55.         monitor.setCursorPos(2,i)
  56.         i = i + 1
  57.         monitor.write('time: ')
  58.         monitor.write(data['time'])
  59.     end
  60.  
  61.     if data['time'] then
  62.         monitor.setCursorPos(2,i)
  63.         i = i + 1
  64.         monitor.write('id: ')
  65.         monitor.write(data['id'])
  66.     end
  67.  
  68.     if data['label'] then
  69.         monitor.setCursorPos(2,i)
  70.         i = i + 1
  71.         monitor.write('label: ')
  72.         monitor.write(data['label'])
  73.     end
  74.  
  75.     if data['position'] then
  76.         if data['position']['local'] then
  77.             monitor.setCursorPos(2,i)
  78.             i = i + 1
  79.             monitor.write('position local: ')
  80.             monitor.write(data['position']['local']['x'])
  81.             monitor.write(', ')
  82.             monitor.write(data['position']['local']['y'])
  83.             monitor.write(', ')
  84.             monitor.write(data['position']['local']['z'])
  85.         end
  86.         if data['position']['global'] then
  87.             monitor.setCursorPos(2,i)
  88.             i = i + 1
  89.             monitor.write('position global: ')
  90.             monitor.write(data['position']['global']['x'])
  91.             monitor.write(', ')
  92.             monitor.write(data['position']['global']['y'])
  93.             monitor.write(', ')
  94.             monitor.write(data['position']['global']['z'])
  95.         end
  96.     end
  97.     if data['fuelLevel'] then
  98.         monitor.setCursorPos(2,i)
  99.         i = i + 1
  100.         monitor.write('fuel level:')
  101.         monitor.write(data['fuelLevel'])
  102.     end
  103.  
  104.     if data['message'] then
  105.         monitor.setCursorPos(2,i)
  106.         i = i + 1
  107.         monitor.write('last message: ')
  108.         monitor.write(data['message'])
  109.     end
  110.  
  111.     if data['alarm'] then
  112.         monitor.setCursorPos(2,i)
  113.         i = i + 1
  114.         monitor.write('alarm: ')
  115.         monitor.setBackgroundColor(colors.red)
  116.         monitor.write(data['alarm'])
  117.         monitor.setBackgroundColor(colors.black)
  118.     end
  119.  
  120.     --print('log dropData: ' .. tostring(data['dropData']))
  121.     if data['dropData'] then
  122.         local w, h = monitor.getSize()
  123.         h = h - 1
  124.         monitor.setCursorPos(2,i)
  125.         i = i + 1
  126.         monitor.write('dropData: ')
  127.         for k,v in pairs(data['dropData']) do
  128.             monitor.setCursorPos(4,i)
  129.             i = i + 1
  130.             monitor.write(v['itemName'])
  131.             monitor.write(' - ')
  132.             monitor.write(v['count'])
  133.             if i == h then
  134.                 monitor.write(' ...')
  135.                 break
  136.             end
  137.         end
  138.     end
  139.  
  140.     lastInfoLineN = i - 1
  141. end
  142.  
  143. local function buttonTopRecreate()
  144.     if monitor then
  145.     else
  146.         return
  147.     end
  148.  
  149.     monitor.setCursorPos(1,2)
  150.     monitor.setBackgroundColor(colors.black)
  151.     monitor.clearLine()
  152.  
  153.     local i = 1
  154.  
  155.     for k,v in pairs(turtleData) do
  156.         if buttonsTopSelect == false then
  157.             buttonsTopSelect = k
  158.         end
  159.  
  160.         monitor.setBackgroundColor(colors.black)
  161.         monitor.write(' ')
  162.         i = i + 1
  163.  
  164.         if buttonsTopSelect == k then
  165.             monitor.setBackgroundColor(colors.green)
  166.         else
  167.             monitor.setBackgroundColor(colors.blue)
  168.             if v['alarm'] then
  169.                 monitor.setBackgroundColor(colors.red)
  170.             end
  171.         end
  172.  
  173.         local labelText = tostring(v['label']) .. '(' .. tostring(v['id']) .. ')'
  174.         if v['alarm'] then
  175.             labelText = labelText .. '*'
  176.         end
  177.  
  178.         monitor.write(labelText)
  179.         monitor.setBackgroundColor(colors.black)
  180.  
  181.         local n = string.len(labelText)
  182.         local i1 = i
  183.         i = i + n
  184.         local i2 = i
  185.  
  186.         buttonsTop[k] = { i1, i2 }
  187.     end
  188.  
  189.     -- Добавляем кнопку Stop только если есть выбранная черепашка
  190.     if buttonsTopSelect then
  191.         monitor.write(' ')
  192.         i = i + 1
  193.        
  194.         monitor.setBackgroundColor(colors.orange)
  195.         local stopText = "Stop"
  196.         local stop_i1 = i
  197.         monitor.write(stopText)
  198.         local stop_i2 = i + string.len(stopText)
  199.         stopButton = {stop_i1, stop_i2}
  200.        
  201.         monitor.setBackgroundColor(colors.black)
  202.     else
  203.         stopButton = {}  -- Сбрасываем кнопку, если нет выбранной черепашки
  204.     end
  205. end
  206.  
  207. local function sendStopCommand()
  208.     if modem and buttonsTopSelect then
  209.         local message = {
  210.             command = 'stop',
  211.             sender = 'miner_system',
  212.             target = buttonsTopSelect  -- Указываем ID целевой черепашки
  213.         }
  214.         modem.transmit(chanelIdSend, buttonsTopSelect, message)  -- Отправляем конкретной черепашке
  215.         print("Stop command sent to turtle ID: " .. buttonsTopSelect)
  216.  
  217.         if speaker then
  218.             speaker.playNote("bass")
  219.         end
  220.     else
  221.         print("No turtle selected or modem not available")
  222.     end
  223. end
  224.  
  225. local function writeLogData2(file, data, prefix)
  226.     if type(data) == 'table' then
  227.         for k,v in pairs(data) do
  228.             if string.len(prefix) > 0 then
  229.                 writeLogData2(file, v, prefix .. ',' .. k)
  230.             else
  231.                 writeLogData2(file, v, k)
  232.             end
  233.         end
  234.     else
  235.         data = tostring(data)
  236.         file.writeLine(prefix .. '=' .. data)
  237.     end
  238. end
  239.  
  240. local function writeLogData()
  241.     if fs.exists(dataFile) then
  242.         fs.delete(dataFile)
  243.     end
  244.     if fs.exists(dataDir) == false then
  245.         fs.makeDir(dataDir)
  246.     end
  247.     local file = fs.open(dataFile,'w')
  248.     writeLogData2(file, turtleData, 'turtleData')
  249.     file.close()
  250. end
  251.  
  252. local function isNumber(str)
  253.     str = tostring(str)
  254.     if string.len(str) == 0 then
  255.         return false
  256.     end
  257.     for i=1, string.len(str) do
  258.         local ch = str.sub(str,i,i)
  259.         if ch == '1' then
  260.         elseif ch == '2' then
  261.         elseif ch == '3' then
  262.         elseif ch == '4' then
  263.         elseif ch == '5' then
  264.         elseif ch == '6' then
  265.         elseif ch == '7' then
  266.         elseif ch == '8' then
  267.         elseif ch == '9' then
  268.         elseif ch == '0' then
  269.         else
  270.             return false
  271.         end
  272.     end
  273.     return true
  274. end
  275.  
  276. local function isBoolean(str)
  277.     if str == 'true' then
  278.         return true
  279.     end
  280.     if str == 'false' then
  281.         return true
  282.     end
  283.     return false
  284. end
  285.  
  286. local function toBoolean(str)
  287.     if str == 'true' then
  288.         return true
  289.     end
  290.     if str == 'false' then
  291.         return false
  292.     end
  293. end
  294.  
  295. local function readLogData()
  296.     if fs.exists(dataFile) == false then
  297.         return
  298.     end
  299.     local file = fs.open(dataFile,'r')
  300.     while true do
  301.         local line = file.readLine()
  302.         if line then
  303.             if string.len(line) > 0 then
  304.                 local v = string.find(line, '=')
  305.                 if v then
  306.                     v = string.sub(line, v + 1)
  307.                 end
  308.                 local i = string.find(line,'=')
  309.                 line = string.sub(line, 1, i - 1)
  310.                 print(line)
  311.                 i = string.find(line,',')
  312.                 local field = string.sub(line, 1, i - 1)
  313.                 line = string.sub(line, i + 1)
  314.                 if field == 'turtleData' then
  315.                     local t2 = turtleData
  316.                     while true do
  317.                         i = string.find(line,',')
  318.                         if i then
  319.                             field = string.sub(line, 1, i - 1)
  320.                             if isNumber(field) then
  321.                                 field = tonumber(field)
  322.                             end
  323.                             line = string.sub(line, i + 1)
  324.                             if t2[field] then
  325.  
  326.                             else
  327.                                 t2[field] = {}
  328.                             end
  329.                             t2 = t2[field]
  330.                         else
  331.                             field = line
  332.                             if isNumber(field) then
  333.                                 field = tonumber(field)
  334.                             end
  335.                             if isNumber(v) then
  336.                                 v = tonumber(v)
  337.                             end
  338.                             if isBoolean(v) then
  339.                                 v = toBoolean(v)
  340.                             end
  341.                             t2[field] = v
  342.                             break
  343.                         end
  344.                     end
  345.                 end
  346.             end
  347.         else
  348.             break
  349.         end
  350.     end
  351.     file.close()
  352. end
  353.  
  354. readLogData()
  355.  
  356. local function modemEvent(event)
  357.     --local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  358.  
  359.     -- local modemSide = event[2]
  360.     -- local senderChannel = event[3]
  361.     -- local replyChannel = event[4]
  362.     local message = event[5]
  363.     local senderDistance = event[6]
  364.  
  365.     senderDistance = senderDistance - (senderDistance % 1)
  366.  
  367.     local time_str = textutils.formatTime(os.time(), false)
  368.  
  369.     local formatMessage = time_str .. ': '
  370.  
  371.     local first = true
  372.     for k, v in pairs(message) do
  373.         if first then
  374.             first = false
  375.         else
  376.             formatMessage = formatMessage .. ', '
  377.         end
  378.         formatMessage = formatMessage .. k .. ': ' .. tostring(v)
  379.     end
  380.     print(formatMessage)
  381.  
  382.     message['time'] = time_str
  383.     message['time_ms'] = os.clock()
  384.  
  385.     if message['sender'] and message['sender'] == 'miner_system' then
  386.         return
  387.     end
  388.  
  389.     if turtleData[message['id']] then
  390.  
  391.         local oldData = turtleData[message['id']]
  392.  
  393.         if message['type'] == 'movement' then
  394.             if oldData['message'] and math.abs(os.clock() - oldData['time_ms']) < 30 then
  395.                 message['message'] = oldData['message']
  396.             end
  397.         end
  398.         if message['dropData'] then
  399.             -- есть данные
  400.         else
  401.             if oldData['dropData'] then
  402.                 message['dropData'] = oldData['dropData']
  403.             end
  404.         end
  405.  
  406.         turtleData[message['id']] = message
  407.     else
  408.         turtleData[message['id']] = message
  409.     end
  410.  
  411.     buttonTopRecreate()
  412.  
  413.     if buttonsTopSelect == message['id'] then
  414.         monitorWriteInfo()
  415.     end
  416.  
  417.     if speaker and message['alarm'] then
  418.         speaker.playNote("hat")
  419.     end
  420.  
  421.     writeLogData()
  422. end
  423.  
  424. local function monitorClickButton(pos_x, pos_y)
  425.     for k,v in pairs(buttonsTop) do
  426.         if pos_x > v[1] and pos_x < v[2] then
  427.             if buttonsTopSelect == k then
  428.                 break
  429.             end
  430.             buttonsTopSelect = k
  431.             buttonTopRecreate()
  432.             monitorWriteInfo()
  433.             break
  434.         end
  435.     end
  436.     -- Проверяем кнопку Stop (только если она существует)
  437.     if stopButton[1] and pos_x >= stopButton[1] and pos_x <= stopButton[2] then
  438.         sendStopCommand()
  439.         if monitor then
  440.             -- Мигание кнопки при нажатии
  441.             monitor.setCursorPos(stopButton[1], 2)
  442.             monitor.setBackgroundColor(colors.red)
  443.             monitor.write("Stop")
  444.             os.sleep(3)
  445.             buttonTopRecreate()
  446.         end
  447.     end
  448. end
  449.  
  450. local function monitorClick(event)
  451.     local pos_x = event[3]
  452.     local pos_y = event[4]
  453.  
  454.     if pos_y == 2 then
  455.         monitorClickButton(pos_x, pos_y)
  456.     end
  457. end
  458.  
  459. if monitor then
  460.     monitor.setBackgroundColor(colors.black)
  461.     monitor.clear()
  462.     monitor.setTextScale(0.5)
  463. end
  464.  
  465. local timerCurrentTimeInfo = false
  466. local timerReMonitorInfoWrite = false
  467. if monitor then
  468.     timerCurrentTimeInfo = os.startTimer(1)
  469.     timerReMonitorInfoWrite = os.startTimer(10)
  470. end
  471.  
  472. local function timerEventWait(event)
  473.     if timerCurrentTimeInfo == event[2] then
  474.         if monitor then
  475.             local w, h = monitor.getSize()
  476.             monitor.setCursorPos(2, h - 1)
  477.             monitor.clearLine()
  478.             monitor.write('current time: ')
  479.             monitor.write(textutils.formatTime(os.time(), false))
  480.         end
  481.         timerCurrentTimeInfo = os.startTimer(1)
  482.     elseif timerReMonitorInfoWrite == event[2] then
  483.         buttonTopRecreate()
  484.         monitorWriteInfo()
  485.         timerReMonitorInfoWrite = os.startTimer(10)
  486.     end
  487. end
  488.  
  489. local function iter()
  490.     local e = { os.pullEvent() }
  491.     if e[1] == 'modem_message' then
  492.         modemEvent(e)
  493.     elseif e[1] == 'monitor_touch' then
  494.         monitorClick(e)
  495.     elseif e[1] == 'timer' then
  496.         timerEventWait(e)
  497.     end
  498. end
  499.  
  500. buttonTopRecreate()
  501. monitorWriteInfo()
  502.  
  503. while true do
  504.     iter()
  505. end
Tags: minecraft
Advertisement
Add Comment
Please, Sign In to add comment