Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. -- サンプリング周期
  2. samplingPeriod = 10
  3.  
  4. -- samplingPeriod毎に期間を区切った、通ったアイテムの数を格納したDictのリスト
  5. samples = {}
  6.  
  7. function table.unique (tbl)
  8. local check = {}
  9. local res = {}
  10.  
  11. -- 整数型だけユニーク化
  12. for i, v in ipairs(tbl) do
  13. if not(check[v]) then
  14. check[v] = true
  15. res[1+#res] = v
  16. end
  17. end
  18.  
  19. -- キータイプはそのまま残す
  20. for k, v in pairs (tbl) do
  21. -- 整数以外
  22. if not (type(k)=="number" and k%1==0) then
  23. res[k] = v
  24. end
  25. end
  26. return res
  27. end
  28.  
  29. function array.tostring(ary)
  30. result = "["
  31. for elem in ary do
  32. result = result .. elem
  33. end
  34. return string.sub(result, 0, string.len(result) - 1) .. "]"
  35. end
  36.  
  37. function sleepConsideringDelay(func, delaySecond)
  38. local startTime = os.clock()
  39. func()
  40. local endTime = os.clock()
  41. local delta = endTime - startTime
  42. sleep(delaySecond - delta)
  43. end
  44.  
  45. function sampling()
  46. currentSample = {}
  47. for i=1, 16 do
  48. itemDetail = turtle.getItemDetail(i)
  49. if itemDetail then
  50. currentItemNum = currentSample[itemDetail['name']]
  51. if currentItemNum == nil then
  52. currentItemNum = 0
  53. end
  54. currentSample[itemDetail['name']] = currentItemNum + itemDetail['count']
  55. end
  56.  
  57. turtle.select(i)
  58. turtle.dropDown()
  59. end
  60. table.insert(samples, currentSample)
  61. end
  62.  
  63. function aggregate(samples)
  64. aggregatedItems = {}
  65.  
  66. -- アイテム一覧をまずは出す
  67. items = {}
  68. for sample in samples do
  69. for k, v in pairs(sample) do
  70. table.insert(items, k)
  71. end
  72. end
  73. items = table.unique(items)
  74.  
  75. -- アイテム毎に数を出す
  76. -- Key: ItemName
  77. -- Value: Array[Int]
  78. aggregatedItem = {}
  79. for item in items do
  80. for sample in samples:
  81. itemNum = sample[item]
  82. if not itemNum == nil then
  83. table.insert(aggregatedItem, itemNum)
  84. else
  85. table.insert(aggregatedItem, 0)
  86. end
  87. end
  88. table.insert(aggregatedItems, aggregatedItem)
  89. end
  90.  
  91. for k,v in pairs(aggregatedItems) do
  92. print(string.format("%s:\n%s", k, array.tostring(v)))
  93. end
  94. end
  95.  
  96. function startSampling()
  97. while true do
  98. sleepConsideringDelay(sampling, samplingPeriod)
  99. aggregatedItems = aggregate()
  100. end
  101. end
  102.  
  103.  
  104. startSampling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement