Advertisement
melzneni

monitor_database

Apr 24th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. os.loadAPI("vislib")
  2.  
  3. function getTableFromSaveText(source)
  4. local tableSources = splitText(source, ";")
  5. local tables = {}
  6. for i = 1, #tableSources do
  7. table.insert(tables, {})
  8. end
  9. for i, v in ipairs(tableSources) do
  10. local tbl = tables[i]
  11. getTableFromSaveTextInd(tbl, v, tables)
  12. end
  13.  
  14. return tables[1]
  15. end
  16.  
  17. function getTableFromSaveTextInd(tbl, tblSource, tables)
  18. if string.sub(tblSource, 1, 1) ~= "{" or string.sub(tblSource, #tblSource) ~= "}" then error("invalid source (braces arround table): " .. tblSource) end
  19. local pts = splitText(string.sub(tblSource, 2, #tblSource - 1), ",")
  20. for i, pt in ipairs(pts) do
  21. local i = string.find(pt, "=")
  22. local vKey = getValueFromSaveText(string.sub(pt, 1, i - 1), tables)
  23. local vValue = getValueFromSaveText(string.sub(pt, i + 1), tables)
  24. tbl[vKey] = vValue
  25. end
  26. end
  27.  
  28. function getValueFromSaveText(source, tables)
  29. if string.sub(source, 1, 1) == "\"" and string.sub(source, #source) == "\"" then
  30. return string.sub(source, 2, #source - 1)
  31. elseif tonumber(source) ~= nil then
  32. return tonumber(source)
  33. elseif string.sub(source, 1, 4) == "tbl:" then
  34. return tables[tonumber(string.sub(source, 5))]
  35. elseif source == "false" then return false
  36. elseif source == "true" then return true
  37. else error("invalid source (getValueAnalysis): " .. source)
  38. end
  39. end
  40.  
  41. function getTableSaveText(sourceTable)
  42. local alreadyUsed = {}
  43. local sources = {}
  44. getTableSaveTextInd(sourceTable, alreadyUsed, sources)
  45. local txt = ""
  46. for i, v in pairs(sources) do
  47. txt = txt .. v .. ";"
  48. end
  49. return txt
  50. end
  51.  
  52. function getTableSaveTextInd(sourceTable, alreadyUsed, sources)
  53. local index
  54. for i, v in ipairs(alreadyUsed) do
  55. if v == sourceTable then index = i break end
  56. end
  57.  
  58. if index ~= nil then
  59. return "tbl:" .. index
  60. else
  61. table.insert(alreadyUsed, sourceTable)
  62. local sourceString = ""
  63. local ind = #alreadyUsed
  64. for k, v in pairs(sourceTable) do
  65. sourceString = sourceString .. getSaveValueString(k, alreadyUsed, sources) .. "=" .. getSaveValueString(v, alreadyUsed, sources) .. ","
  66. end
  67. sources[ind] = "{" .. sourceString .. "}"
  68. return "tbl:" .. ind
  69. end
  70. end
  71.  
  72. function getSaveValueString(obj, alreadyUsed, sources)
  73. local t = type(obj)
  74. if t == "string" then
  75. return "\"" .. obj .. "\""
  76. elseif t == "number" then
  77. return obj
  78. elseif t == "table" then
  79. return getTableSaveTextInd(obj, alreadyUsed, sources)
  80. elseif t == "boolean" then
  81. if obj then return "true"
  82. else return "false"
  83. end
  84. else error("unknown type: " .. t)
  85. end
  86. end
  87.  
  88. function writeToFile(fileName, data)
  89. local f = fs.open(fileName, "w")
  90. f.write(data)
  91. f.close()
  92. end
  93.  
  94. function readFileText(file)
  95. local f = fs.open(file, "r")
  96. local text = f.readAll()
  97. f.close()
  98. return text
  99. end
  100.  
  101.  
  102. local data = { counters = {} }
  103.  
  104. function save()
  105. writeToFile("data.data",getTableSaveText(data))
  106. end
  107.  
  108. if fs.exists("data.data") then
  109. data = getTableFromSaveText(readFileText("data.data"))
  110. end
  111.  
  112. rednet.open("top")
  113.  
  114. while true do
  115. local id, msg = vislib.receiveRednet()
  116. local tag, pts = vislib.getMsgData(msg)
  117. if tag == "@database" then
  118. if pts[1] == "add" then
  119. if data.counters[pts[2]] ~= nil then
  120. data.counters[pts[2]] = data.counters[pts[2]] + tonumber(pts[3])
  121. else data.counters[pts[2]] = tonumber(pts[3])
  122. end
  123. print(pts[2],": ",data.counters[pts[2]])
  124. else print("unknown command: " .. msg)
  125. end
  126. end
  127. end
  128.  
  129. --pastebin run 0YB9PsQV startup={files={vislib=<pb:GYvMFCY5>,database=<pb:gJXdA9Nx>},cmds={{'database'}}} label=<input:'Label'> reboot=true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement