Advertisement
PandaDoddo72Rus

client

Jul 13th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. local component = require('component')
  2. local tunnel = component.tunnel
  3. local event = require('event')
  4. local serialization = require('serialization')
  5.  
  6. local data_file = 'file.txt'
  7.  
  8. --[[ Сериализация таблиц в файл ]]--
  9. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  10. local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
  11.  
  12. --[[ Функция формирования запроса и получения ответа ]]--
  13.  
  14. --[[ Пояснения запросов ]]--
  15. -- {command='getAllStorage'} -- получить список всех вещей на складе G
  16. --[[====================]]--
  17. local function sendCommand(arr)
  18. local timeout = 3
  19. local result = {status=false}
  20. tunnel.send(serialization.serialize(arr))
  21. for i=1,3 do
  22. answear = {event.pull(timeout,'modem_message')}
  23. if answear ~= nil then
  24. result = serialization.serialize(answear)
  25. break
  26. end
  27. end
  28. return result
  29. end
  30.  
  31. --[[ Главный код (передача команд и обработка ответа) ]]--
  32.  
  33. function printItems(arr)
  34. for val,val2 in pairs(arr) do
  35. print(val,val2)
  36. end
  37. end
  38.  
  39. sendCommand({command='getAllStorage'})
  40. while true do
  41. local evnt = {event.pull(0)}
  42. if evnt[1] == 'key_up' then
  43. if tonumber(event[4]) == 34 then
  44. answear = sendCommand({command='getAllStorage'})
  45. printItems(answear)
  46. end
  47. elseif evnt[1] == 'modem_message' then
  48. local message = serialization.unserialize(evnt[6])
  49. printItems(message)
  50. end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement