Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
77
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:
  32. result = result + elem
  33. end
  34. return string.sub(result, 0, string.len(result) - 1) + "]"
  35.  
  36. function sleepConsideringDelay(func, delaySecond)
  37. local startTime = os.clock()
  38. func()
  39. local endTime = os.clock()
  40. local delta = endTime - startTime
  41. sleep(delaySecond - delta)
  42. end
  43.  
  44. function sampling()
  45. currentSample = {}
  46. for i=1, 16 do
  47. itemDetail = turtle.getItemDetail(i)
  48. if itemDetail then
  49. currentItemNum = currentSample[itemDetail['name']]
  50. if currentItemNum == nil then
  51. currentItemNum = 0
  52. end
  53. currentSample[itemDetail['name']] = currentItemNum + itemDetail['count']
  54. end
  55.  
  56. turtle.select(i)
  57. turtle.dropDown()
  58. end
  59. table.insert(samples, currentSample)
  60. end
  61.  
  62. function aggregate(samples)
  63. aggregatedItems = {}
  64.  
  65. -- アイテム一覧をまずは出す
  66. items = {}
  67. for sample in samples do
  68. for k, v in pairs(sample) do
  69. table.insert(items, k)
  70. end
  71. end
  72. items = table.unique(items)
  73.  
  74. -- アイテム毎に数を出す
  75. -- Key: ItemName
  76. -- Value: Array[Int]
  77. aggregatedItem = {}
  78. for item in items do
  79. for sample in samples:
  80. itemNum = sample[item]
  81. if not itemNum == nil then
  82. table.insert(aggregatedItem, itemNum)
  83. else
  84. table.insert(aggregatedItem, 0)
  85. end
  86. end
  87. table.insert(aggregatedItems, aggregatedItem)
  88. end
  89.  
  90. for k,v in pairs(aggregatedItems) do
  91. print(string.format("%s:\n%s", k, array.tostring(v)))
  92. end
  93. end
  94.  
  95. function startSampling()
  96. while true do
  97. sleepConsideringDelay(sampling, samplingPeriod)
  98. aggregatedItems = aggregate()
  99. end
  100. end
  101.  
  102.  
  103. startSampling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement